入门级计算
1、算数平均值
#样本:
S = [s1, s2, s3, …, sn]
#算术平均值:
m = (s1 + s2 + s3 + … + sn)/n
Numpy中的写法
m = numpy.mean(样本数组)
2、加权平均值
#样本:
S = [s1, s2, s3, …, sn]
#权重:
W = [w1, w2, w3, …, wn]
#加权平均值:
a = (s1w1 + s2w2 + s3w3 + … + snwn)/(w1 + w2 + w3 + … + wn)
3、Numpy中的格式
首先是数据源:需要求加权平均值的数据列表和对应的权值列表
elements = []
weights = []
使用numpy直接求:
import numpy as np
np.average(elements, weights=weights)
附纯python写法:
# 不使用numpy写法1
round(sum([elements[i]*weights[i] for i in range(n)])/sum(weights), 1)
# 不使用numpy写法2
round(sum([j[0]*j[1] for j in zip(elements, weights)])/sum(weights), 1)
定义函数计算一个序列的平均值的方法
def average(seq, total=0.0):
num = 0
for item in seq:
total += item
num += 1
return total / num
如果序列是数组或者元祖可以简单使用下面的代码
def average(seq):
return float(sum(seq)) / len(seq)
3、最大值与最小值
1、最大值、最小值
max:获取一个数组中最大元素
min:获取一个数组中最小元素
2、比较出最值数组
maximumÿ