I have a list of values and a 1-d numpy array, and I would like to calculate the correlation coefficient using numpy.corrcoef(x,y,rowvar=0). I get the following error:
Traceback (most recent call last):
File "testLearner.py", line 25, in <module>
corr = np.corrcoef(valuesToCompare,queryOutput,rowvar=0)
File "/usr/local/lib/python2.6/site-packages/numpy/lib/function_base.py", line 2003, in corrcoef
c = cov(x, y, rowvar, bias, ddof)
File "/usr/local/lib/python2.6/site-packages/numpy/lib/function_base.py", line 1935, in cov
X = concatenate((X,y), axis)
ValueError: array dimensions must agree except for d_0
I printed out the shape for my numpy array and got (400,1). When I convert my list to an array with numpy.asarray(y) I get (400,)!
I believe this is the problem. I did an array.reshape to (400,1) and printed out the shape, and I still get (400,). What am I missing?
Thanks in advance.
I think you might have assumed that
reshapemodifies the value of the original array. It doesn’t:np.asarraytreats a regular list as a 1d array, but your original numpy array that you said was 1d is actually 2d (because its shape is(400,1)). If you want to use your list like a 2d array, there are two easy approaches:np.asarray(lst).reshape((-1, 1))–-1means “however many it needs” for that dimension”.np.asarray([lst]).T–.Tmeans array transpose, which switches from(1,5)to(5,1).-You could also reshape your original array to 1d via
ary.reshape((-1,)).