且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在python中计算列表的方差?

更新时间:2022-12-09 18:26:16

您可以使用numpy的内置函数

You can use numpy's built-in function var:

import numpy as np

results = [-14.82381293, -0.29423447, -13.56067979, -1.6288903, -0.31632439,
          0.53459687, -1.34069996, -1.61042692, -4.03220519, -0.24332097]

print(np.var(results))

这给您28.822364260579157

如果-由于某种原因-您不能使用numpy和/或您不想为其使用内置函数,则还可以使用例如手动"计算. 列表理解:

If - for whatever reason - you cannot use numpy and/or you don't want to use a built-in function for it, you can also calculate it "by hand" using e.g. a list comprehension:

# calculate mean
m = sum(results) / len(results)

# calculate variance using a list comprehension
var_res = sum((xi - m) ** 2 for xi in results) / len(results)

这将为您提供相同的结果.

which gives you the identical result.

如果您对标准偏差感兴趣,则可以使用

If you are interested in the standard deviation, you can use numpy.std:

print(np.std(results))
5.36864640860051

@Serge Ballesta很好地解释了方差nn-1之间的区别.在numpy中,您可以使用选项ddof轻松设置此参数.它的默认值为0,因此对于n-1情况,您可以轻松执行以下操作:

@Serge Ballesta explained very well the difference between variance n and n-1. In numpy you can easily set this parameter using the option ddof; its default is 0, so for the n-1 case you can simply do:

np.var(results, ddof=1)

手动"解决方案在 @Serge Ballesta的答案中给出.

The "by hand" solution is given in @Serge Ballesta's answer.

两种方法都产生32.024849178421285.

您也可以为std设置参数:

np.std(results, ddof=1)
5.659050201086865