Fast numpy covariance for arrays of different shape

Gabriel

I need to obtain the covariance between an array a of shape (M1, M2, N) and another array b of shape (N,).

What I currently do is use a for block:

import numpy as np

M1, M2, N = 23, 50, 117
a = np.random.uniform(-2., 2., (M1, M2, N))
b = np.random.uniform(-1., 1., N)

c = []
for i in range(M1):
    for j in range(M2):
        c.append(np.cov(a[i][j], b)[0, 1])

but it gets a bit slow for large (M1, M2). Is there a way to speed this up?

Paul Panzer

You can always calculate the cov by hand. Here are two suggestions using dot and einsum respectively.

import numpy as np

M1, M2, N = 23, 50, 117
a = np.random.uniform(-2., 2., (M1, M2, N))
b = np.random.uniform(-1., 1., N)

c = []
for i in range(M1):
    for j in range(M2):
        c.append(np.cov(a[i][j], b)[0, 1])

c1 = np.reshape(c, (M1, M2))

ac = a - a.mean(axis=-1, keepdims=True)
bc = (b - b.mean()) / (N-1)

c2 = np.dot(ac, bc)
c3 = np.einsum('ijk,k->ij', ac, bc)
print(np.allclose(c1, c2))
print(np.allclose(c1, c3))

Prints

True
True

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Numpy function to get shape of added arrays

分類Dev

Append numpy arrays with different dimensions

分類Dev

python - numpy - summation of numpy arrays of different dimension

分類Dev

Fast way to do consecutive one-to-all calculations on Numpy arrays without a for-loop?

分類Dev

Numpy reshape acts different on copied vs. uncopied arrays

分類Dev

Combined mean and standard deviation from a collection of NumPy arrays of different shapes

分類Dev

Numpy.shape関数

分類Dev

Fastest way to get all unique numbers between two different dimension numpy arrays

分類Dev

Are Dot product and Multiplying matrices are same when coming to arrays of two different dimensions in numpy

分類Dev

NumPyのx.shape [0]とx [0] .shape

分類Dev

Numpy rank 1 arrays

分類Dev

How to combine arrays in numpy?

分類Dev

Defining numpy indexing arrays

分類Dev

Unpacking a List of Numpy Arrays

分類Dev

Unnesting Numpy Arrays

分類Dev

numpy fill an array with arrays

分類Dev

Inserting different arrays in a ArrayList

分類Dev

Detect number of dimensions of numpy array (not shape)

分類Dev

Numpy: Subtract 2 numpy arrays row wise

分類Dev

How to insert a numpy array to a numpy array of arrays?

分類Dev

Array of arrays in C, where the arrays are of different length

分類Dev

Streaming multiple numpy arrays to a file

分類Dev

Matrix multiplication with multiple numpy arrays

分類Dev

Calculating MSE between numpy arrays

分類Dev

Filtering Numpy's array of arrays

分類Dev

Concatenate unique numpy arrays with counters

分類Dev

Modifying a chunk of arrays in Numpy Python

分類Dev

Different Kinds of passing objects to Arrays

分類Dev

Two arrays with different length in a loop

Related 関連記事

ホットタグ

アーカイブ