计算一串数组的均值、中位数、标准差


#!/usr/bin/env python
#-*- coding:utf-8 -*-
'''
@author  : FIGTHING
@file    : DataMining.py
@function: 
@software: Pycharm
@time    : 2019/06/13/15:40
'''
import numpy as np

age = [23,23,27,27,39,41,47,49,50,52,54,54,56,57,58,58,61]
fat = [9.5,26.5,7.8,17.8,31.4,25.9,27.4,27.2,31.2,34.6,42.5,28.8,33.4,30.2,34.1,23.9,35.7]

#求均值
age_mean = np.mean(age)
print("age平均值为:%f" % age_mean)
fat_mean = np.mean(fat)
print("fat平均值为:%f" % fat_mean)

#求中位数
age.sort()
mid = int(len(age) / 2)
if len(age) % 2 == 0:
    median = (age[mid-1] + age[mid]) / 2.0
else:
    median = age[mid]
print("age中位数为:%f" % median)

fat.sort()
mid = int(len(fat) / 2)
if len(fat) % 2 == 0:
    median = (fat[mid-1] + fat[mid]) / 2.0
else:
    median = fat[mid]

print("fat中位数为:%f" % median)

#求标准差
age_std = np.std(age,ddof=1)
print("age标准差为:%f" % age_std)

fat_std = np.std(fat,ddof=1)
print("fat标准差为:%f" % fat_std)

"We often turn out to be our own worst enemy."--《 Wonder Wheel》

Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐