加载中......

python笔记——均值、方差、中位数计算

from __future__ import print_function

# 均值计算
data = [3.53, 3.47, 3.51, 3.72, 3.43]
average = float(sum(data))/len(data)
print(average)


#方差计算
total = 0
for value in data:
    total += (value - average) ** 2

stddev = math.sqrt(total/len(data))
print(stddev)


# 中位数计算
data.sort()
mid = len(data) / 2
if len(data) % 2 == 0:
    median = (data[mid - 1] + data[mid]) / 2.0
else:
    median = data[mid]

print(median)

 

posted @ 2019-01-02 13:46  青蛙快飞  阅读(11344)  评论(0编辑  收藏  举报