# [SciPy-dev] Computing correlations with SciPy

John Hunter jdhunter at ace.bsd.uchicago.edu
Thu Mar 16 09:56:00 CST 2006

```The following message is a courtesy copy of an article
that has been posted to comp.lang.python as well.

>>>>> "tkpmep" == tkpmep  <tkpmep at hotmail.com> writes:

tkpmep> I want to compute the correlation between two sequences X
tkpmep> and Y, and tried using SciPy to do so without success.l
tkpmep> Here's what I have, how can I correct it?

>>>> X = [1, 2, 3, 4, 5] Y = [5, 4, 3, 2, 1] import scipy
>>>> scipy.corrcoef(X,Y)
tkpmep> Traceback (most recent call last): File "<interactive
tkpmep> input>", line 1, in ?  File
tkpmep> "C:\Python24\Lib\site-packages\numpy\lib\function_base.py",
tkpmep> line 671, in corrcoef d = diag(c) File
tkpmep> "C:\Python24\Lib\site-packages\numpy\lib\twodim_base.py",
tkpmep> line 80, in diag raise ValueError, "Input must be 1- or
tkpmep> 2-d."  ValueError: Input must be 1- or 2-d.
>>>>

Hmm, this may be a bug in scipy.  matplotlib also defines a corrcoef
function, which you may want to use until this problem gets sorted out

In [9]: matplotlib.mlab.corrcoef(X,Y)

In [10]: X = [1, 2, 3, 4, 5]

In [11]: Y = [5, 4, 3, 2, 1]

In [12]: matplotlib.mlab.corrcoef(X,Y)
Out[12]:
array([[ 1., -1.],
[-1.,  1.]])

```