99

In matlab I use

a=[1,4,6]
b=[1,2,3]
corr(a,b)

which returns .9934. I've tried numpy.correlate but it returns something completely different. What is the simplest way to get the correlation of two vectors?

Hooked
  • 84,485
  • 43
  • 192
  • 261
Luke Makk
  • 1,157
  • 3
  • 13
  • 13

1 Answers1

191

The docs indicate that numpy.correlate is not what you are looking for:

numpy.correlate(a, v, mode='valid', old_behavior=False)[source]
  Cross-correlation of two 1-dimensional sequences.
  This function computes the correlation as generally defined in signal processing texts:
     z[k] = sum_n a[n] * conj(v[n+k])
  with a and v sequences being zero-padded where necessary and conj being the conjugate.

Instead, as the other comments suggested, you are looking for a Pearson correlation coefficient. To do this with scipy try:

from scipy.stats.stats import pearsonr   
a = [1,4,6]
b = [1,2,3]   
print(pearsonr(a,b))

This gives

(0.99339926779878274, 0.073186395040328034)

You can also use numpy.corrcoef:

import numpy
print(numpy.corrcoef(a,b))

This gives:

[[ 1.          0.99339927]
 [ 0.99339927  1.        ]]
Guillaume Jacquenot
  • 11,217
  • 6
  • 43
  • 49
Hooked
  • 84,485
  • 43
  • 192
  • 261
  • 3
    what is the second value in the tuple printed by "pearsonr(a,b)"? – Muhammad Haseeb Khan Nov 11 '19 at 08:09
  • 2
    @MuhammadHaseebKhan According to the [docs](https://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.stats.pearsonr.html) the values it returns are (Pearson’s correlation coefficient, 2-tailed p-value) – Hooked Nov 12 '19 at 14:42
  • Hi @Hooked: I have 2 vectors in 2 columns. I want their corr coef in another column. How can I do that? Thanks! – Roy May 09 '21 at 14:59