2

I want to compute the correlation between the two arrays a and b; the shape of a and b is (10, 2). I expect a correlation matrix with shape (10, 10) with values in the range [-1, 1]; a correlation value for every pair.

>>> a
array([[-1.22674504,  0.08136256],
       [ 1.95456381, -1.31209914],
       [ 0.29199221,  0.00573356],
       [ 0.66700798, -0.68239164],
       [-1.03723395, -0.15456914],
       [-0.52541249, -0.21180142],
       [-0.94584861, -0.81954194],
       [ 1.11044632,  2.02689438],
       [-0.12003807,  0.00595059],
       [-0.16873215,  1.06046219]])

>>> b
array([[-0.06960341,  0.01320213],
       [ 0.1108986 , -0.21290515],
       [ 0.01656714,  0.00093034],
       [ 0.03784489, -0.11072692],
       [-0.05885088, -0.02508085],
       [-0.029811  , -0.03436753],
       [-0.05366583, -0.13298134],
       [ 0.06300482,  0.32888998],
       [-0.00681075,  0.00096556],
       [-0.00957357,  0.17207378]])

I use numpy.corrcoef(a, b) and get a (20, 20) matrix, instead of (10, 10).

>>> numpy.corrcoef(a, b)
array([[ 1., -1., -1., -1.,  1.,  1.,  1.,  1.,  1.,  1.,  1., -1., -1.,
        -1.,  1., -1., -1.,  1.,  1.,  1.],
       [-1.,  1.,  1.,  1., -1., -1., -1., -1., -1., -1., -1.,  1.,  1.,
         1., -1.,  1.,  1., -1., -1., -1.],
       [-1.,  1.,  1.,  1., -1., -1., -1., -1., -1., -1., -1.,  1.,  1.,
         1., -1.,  1.,  1., -1., -1., -1.],
       [-1.,  1.,  1.,  1., -1., -1., -1., -1., -1., -1., -1.,  1.,  1.,
         1., -1.,  1.,  1., -1., -1., -1.],
       [ 1., -1., -1., -1.,  1.,  1.,  1.,  1.,  1.,  1.,  1., -1., -1.,
        -1.,  1., -1., -1.,  1.,  1.,  1.],
       [ 1., -1., -1., -1.,  1.,  1.,  1.,  1.,  1.,  1.,  1., -1., -1.,
        -1.,  1., -1., -1.,  1.,  1.,  1.],
       [ 1., -1., -1., -1.,  1.,  1.,  1.,  1.,  1.,  1.,  1., -1., -1.,
        -1.,  1., -1., -1.,  1.,  1.,  1.],
       [ 1., -1., -1., -1.,  1.,  1.,  1.,  1.,  1.,  1.,  1., -1., -1.,
        -1.,  1., -1., -1.,  1.,  1.,  1.],
       [ 1., -1., -1., -1.,  1.,  1.,  1.,  1.,  1.,  1.,  1., -1., -1.,
        -1.,  1., -1., -1.,  1.,  1.,  1.],
       [ 1., -1., -1., -1.,  1.,  1.,  1.,  1.,  1.,  1.,  1., -1., -1.,
        -1.,  1., -1., -1.,  1.,  1.,  1.],
       [ 1., -1., -1., -1.,  1.,  1.,  1.,  1.,  1.,  1.,  1., -1., -1.,
        -1.,  1., -1., -1.,  1.,  1.,  1.],
       [-1.,  1.,  1.,  1., -1., -1., -1., -1., -1., -1., -1.,  1.,  1.,
         1., -1.,  1.,  1., -1., -1., -1.],
       [-1.,  1.,  1.,  1., -1., -1., -1., -1., -1., -1., -1.,  1.,  1.,
         1., -1.,  1.,  1., -1., -1., -1.],
       [-1.,  1.,  1.,  1., -1., -1., -1., -1., -1., -1., -1.,  1.,  1.,
         1., -1.,  1.,  1., -1., -1., -1.],
       [ 1., -1., -1., -1.,  1.,  1.,  1.,  1.,  1.,  1.,  1., -1., -1.,
        -1.,  1., -1., -1.,  1.,  1.,  1.],
       [-1.,  1.,  1.,  1., -1., -1., -1., -1., -1., -1., -1.,  1.,  1.,
         1., -1.,  1.,  1., -1., -1., -1.],
       [-1.,  1.,  1.,  1., -1., -1., -1., -1., -1., -1., -1.,  1.,  1.,
         1., -1.,  1.,  1., -1., -1., -1.],
       [ 1., -1., -1., -1.,  1.,  1.,  1.,  1.,  1.,  1.,  1., -1., -1.,
        -1.,  1., -1., -1.,  1.,  1.,  1.],
       [ 1., -1., -1., -1.,  1.,  1.,  1.,  1.,  1.,  1.,  1., -1., -1.,
        -1.,  1., -1., -1.,  1.,  1.,  1.],
       [ 1., -1., -1., -1.,  1.,  1.,  1.,  1.,  1.,  1.,  1., -1., -1.,
        -1.,  1., -1., -1.,  1.,  1.,  1.]]) 

One row is one observation with two values, how can I tell Python that it is 2-dimensional to compute the correlation? Why is every value exactly -1 or 1?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
  • Have you read [the `corrcoef` documentation](http://docs.scipy.org/doc/numpy/reference/generated/numpy.corrcoef.html)? – jonrsharpe Apr 29 '14 at 09:49
  • What do you really want to do? `corrcoef` computes the coefficients of among the `rows`. So `np.all(np.corrcoef(a, b) == np.corrcoef(np.vstack((a, b)))) == True` – emesday Apr 29 '14 at 09:55
  • I want to compute the correlation for each pair of a and b. Get a 10x10 matrix, the entry_11 stands for element#1 of a (first row) and element#1 of b (first row). How this is possible? I tried the *scipy.signal.correlated2d(a,b)* but the result isn't as expected. –  Apr 29 '14 at 10:46

1 Answers1

1

You may need this code for xcorr2 in Matlab, right? This is 2-D cross-correlation

import scipy.signal
print scipy.signal.correlate2d(a, b)

See : http://www.mathworks.co.kr/kr/help/signal/ref/xcorr2.html

Also take look at this post: Computing cross-correlation function?

Community
  • 1
  • 1
emesday
  • 6,078
  • 3
  • 29
  • 46