Ray234
Ray234

Reputation: 173

Is there a way to correlate n arrays?

I need to correlate n arrays that I have combined into a list which looks like:

array_corr = [array([a1]), array([a2]), array([a3]), .... array([an])]

corr_mat = np.corrcoef([array_corr])

But I am getting the error:

operands could not be broadcast together.

All arrays are the same size. I don't understand the source of error.

I expect the output to be an nxn matrix since there are n arrays.

Upvotes: 0

Views: 246

Answers (1)

vurmux
vurmux

Reputation: 10020

Just make array_corr a pure numpy 2d-array and send it to np.corrcoef without brackets (you are creating a list with only one matrix element this way):

array_corr = np.array([
    [1,2,3,4,5],
    [1,6,3,3,5],
    [1,2,9,4,3],
    [2,1,3,8,5],
    [6,6,2,6,5],
])

corr_mat = np.corrcoef(array_corr)

corr_mat

returns:

array([[ 1.        ,  0.40555355,  0.30460385,  0.74074375, -0.18257419],
       [ 0.40555355,  1.        , -0.05764881, -0.11092108,  0.07404361],
       [ 0.30460385, -0.05764881,  1.        ,  0.16777868, -0.92688   ],
       [ 0.74074375, -0.11092108,  0.16777868,  1.        ,  0.1040313 ],
       [-0.18257419,  0.07404361, -0.92688   ,  0.1040313 ,  1.        ]])

Upvotes: 1

Related Questions