I am trying to port a MATLAB/Octave program to Python using NumPy 1.8.0 and Python 2.7.3. I've used this reference as help in converting MATLAB functions to NumPy methods with great success, until I get to the point where I want to compute the correlation between two matrices.
The first matrix is 40000x25 floats, the second matrix is 40000x1 ints. In Octave I use the statement corr(a,b)
and get a 25x1 matrix of floats. Trying the corresponding method in NumPy (numpy.correlate(a,b)
) produces an error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Library/Python/2.7/site-packages/numpy-1.8.0.dev_1a9aa5a_20130415-py2.7-macosx-10.8-intel.egg/numpy/core/numeric.py", line 751, in correlate
return multiarray.correlate2(a,v,mode)
ValueError: object too deep for desired array
I can get it to work if I change the code to calculate a correlation for each column of a
, like so:
for i in range(25):
c2[i] = numpy.correlate(a[:,i], b)
However, the values in the c2
array are different than the output from Octave. Octave returns a 25x1 matrix of floats all less than 1. The values I get from NumPy are floats between -270 and 900.
I have tried to understand what the two algorithms are doing under the hood but have failed miserably. Can someone point out my logic failure?