用Python求均值与方差,可以自己写,也可以借助于numpy,不过到底哪个快一点呢? 我做了个实验,首先生成9百万个样本: nlist=range(0,9000000) nlist=[float(i)/1000000 for i in nlist] N=len(nlist) 第二行是为了让样本小一点,否则从1加到9百万会溢出的. 自己实现,遍历数组来求均值方差: sum1=0.0 sum2=0.0 for i in range(N): sum1+=nlist[i] sum2+=nlist[i]
__author__ = 'dell' import Pmf import matplotlib.pyplot as pyplot pmf = Pmf.MakePmfFromList([1, 2, 2, 3, 5]) print 'Mean by Pmf ', pmf.Mean() print 'Var by Pmf ', pmf.Var() def PmfMean(pmf): t = [x * v for x, v in pmf.Items()] res = sum(t) return res