Md. Zakir Hossan
Md. Zakir Hossan

Reputation: 537

Correlation coefficient between a 2D and a 3D array - NumPy/Python

Let

import numpy as np
A = np.ones([n,m])
B = np.ones([o,n,m])

Is there any way to compute correlation coefficient witout looping such that

C = corr(A,B) = array([1,o])

Where m, n and o are used to express dimension.

Loopy Example:

from scipy.stats.stats import pearsonr

A = np.random.random([5,5])
B = np.random.random([3,5,5])
C = []
for i in B:
    C.append(pearsonr(A.flatten(), i.flatten())[0])

C = np.array(C)

Upvotes: 1

Views: 4423

Answers (1)

Divakar
Divakar

Reputation: 221504

We could use corr2_coeff from this post after reshaping the inputs to 2D versions, such that the first input is reshaped to a one-column array and the second one would have number of columns same as the combined length of its last two axes, like so -

corr2_coeff(A.reshape(1,-1),B.reshape(B.shape[0],-1)).ravel()

Sample run -

In [143]: from scipy.stats.stats import pearsonr
     ...: 
     ...: A = np.random.random([5,5])
     ...: B = np.random.random([3,5,5])
     ...: C = []
     ...: for i in B:
     ...:     C.append(pearsonr(A.flatten(), i.flatten())[0])
     ...: 
     ...: C = np.array(C)
     ...: 

In [144]: C
Out[144]: array([ 0.05637413, -0.26749579, -0.08957621])

In [145]: corr2_coeff(A.reshape(1,-1),B.reshape(B.shape[0],-1)).ravel()
Out[145]: array([ 0.05637413, -0.26749579, -0.08957621])

For really huge arrays, we might need to resort to one-loop, like so -

[corr2_coeff(A.reshape(1,-1), i.reshape(1,-1)) for i in B]

Upvotes: 1

Related Questions