用Python计算协方差

用Python计算协方差,python,statistics,covariance,Python,Statistics,Covariance,我想知道是否有人能给我一些关于如何在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 这些值取自: 其结果应为0.25 我一直在嵌套for循环中循环X、Y和p,但不知道可以使用其他方法来组合这些循环 我基本上想做这个

我想知道是否有人能给我一些关于如何在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
这些值取自:

其结果应为0.25

我一直在嵌套for循环中循环X、Y和p,但不知道可以使用其他方法来组合这些循环

我基本上想做这个计算:

(1-1.5)(1-2)(0.25) + (1-1.5)(2-2)(0.25) +  ..... + (2-1.5)(3-2)(0.25)

要计算协方差,您需要下面这样的东西,它有一个嵌套循环,遍历每个列表,并使用协方差公式累积协方差

# let's get the mean of `X` (add all the vals in `X` and divide by
# the length
x_mean = float(sum(X)) / len(X)

# now, let's get the mean for `Y`
y_mean = float(sum(Y)) / len(Y)

# initialize the covariance to 0 so we can add it up
cov = 0

# we'll use a nested loop structure -- the outer loop can be through `Y`
# or `X`, it doesn't matter in this case
# we'll use python's `enumerate`, which lets us iterate through the `list`
# using a `tuple` that contains (the_current_index, the_current_element),
# or in `C`/`Java` terms, `(i, arr[i])`
for y_idx,y in enumerate(Y):
    for x_idx,x in enumerate(X):

        # the covariance is defined by the following equation
        # you don't need to loop through `P` -- the outer list
        # contains 2 elements, which is the size of `X`, and
        # the inner list contains 3 elements, which is the size of `Y`
        cov += (x - x_mean) * (y - y_mean) * P[x_idx][y_idx]

print cov # => 0.25

要计算协方差,您需要下面这样的东西,它有一个嵌套循环,遍历每个列表,并使用协方差公式累积协方差

# let's get the mean of `X` (add all the vals in `X` and divide by
# the length
x_mean = float(sum(X)) / len(X)

# now, let's get the mean for `Y`
y_mean = float(sum(Y)) / len(Y)

# initialize the covariance to 0 so we can add it up
cov = 0

# we'll use a nested loop structure -- the outer loop can be through `Y`
# or `X`, it doesn't matter in this case
# we'll use python's `enumerate`, which lets us iterate through the `list`
# using a `tuple` that contains (the_current_index, the_current_element),
# or in `C`/`Java` terms, `(i, arr[i])`
for y_idx,y in enumerate(Y):
    for x_idx,x in enumerate(X):

        # the covariance is defined by the following equation
        # you don't need to loop through `P` -- the outer list
        # contains 2 elements, which is the size of `X`, and
        # the inner list contains 3 elements, which is the size of `Y`
        cov += (x - x_mean) * (y - y_mean) * P[x_idx][y_idx]

print cov # => 0.25
在这里,
itertools
中的Python函数也有帮助,它可以与
enumerate
结合使用,以返回
p
所需的索引,如下所示:

from itertools import product

X = [1, 2]
Y = [1, 2, 3]
P = [[0.25,0.25,0.0], [0.0, 0.25, 0.25]]

mean_x = float(sum(X) / len(X))
mean_y = float(sum(Y) / len(Y))

print sum((x[1] - mean_x) * (y[1] - mean_y) * P[x[0]][y[0]] for x, y in product(enumerate(X), enumerate(Y)))
给出结果:

0.25
在这里,
itertools
中的Python函数也有帮助,它可以与
enumerate
结合使用,以返回
p
所需的索引,如下所示:

from itertools import product

X = [1, 2]
Y = [1, 2, 3]
P = [[0.25,0.25,0.0], [0.0, 0.25, 0.25]]

mean_x = float(sum(X) / len(X))
mean_y = float(sum(Y) / len(Y))

print sum((x[1] - mean_x) * (y[1] - mean_y) * P[x[0]][y[0]] for x, y in product(enumerate(X), enumerate(Y)))
给出结果:

0.25