Home > Software engineering >  Numpy - How do the following loop for covariance
Numpy - How do the following loop for covariance

Time:09-30

I am asking a similar question to this Numpy - Covariance between row of two matrix, but in this case I am not interested about the most efficient method, but just rewrite the for loop with some numpy functions. Is it possible convert the following code, without for loop, using numpy? Thanks in advance

Code:

m1 = np.array([[1,2,3],[2,2,2]])
m2 = np.array([[2.56, 2.89, 3.76],[1,2,3.95]])

output = []
for a,b in zip(m1,m2):
    cov = np.cov(a, b)
    output.append(cov[0][1])
print(output)

CodePudding user response:

Try this out.

mycov = np.cov(m1, m2)

output = mycov.diagonal(offset=m1.shape[0])

CodePudding user response:

You can avoid computing any extra elements by taking the relevant sum directly:

print(((m1 - m1.mean(1, keepdims=True)) * (m2 - m2.mean(1, keepdims=True))).sum(1) / (m1.shape[1] - 1))

It's just a product of the corresponding de-meaned vectors multiplied together, summed, and normalized by n-1.

  • Related