我想知道是否有人能给我一些关于如何在Python中计算协方差的提示;我不想使用numpy中的任何东西。我只想学习如何手动执行此操作,并练习for循环。在
基本上,我想计算的协方差:
X = [1,2]
Y = [1,2,3]
P = [[0.25,0.25,0.0], [0.0, 0.25, 0.25]]
Mean of X: 1.5
Mean of Y: 2
这些值取自:https://onlinecourses.science.psu.edu/stat414/node/109
结果应该是0.25。在
我已经在嵌套的for循环中循环X、Y和p,但不知道可以使用其他方法来组合它。在
我基本上想做这样的计算:
^{pr2}$
Python在} 函数也有帮助,它可以与}所需的索引,如下所示:
itertools
中的^{enumerate
结合,返回{给出结果:
^{pr2}$要计算协方差,您需要类似下面这样的内容,它有一个嵌套循环,遍历每个列表,并使用协方差公式累积协方差。在
相关问题 更多 >
编程相关推荐