[Numpy-discussion] ValueError: objects are not aligned

josef.pktd@gmai... josef.pktd@gmai...
Wed Sep 8 07:34:42 CDT 2010


On Wed, Sep 8, 2010 at 8:08 AM, Rui DaCosta <ruidc@yahoo.com> wrote:
> Thanks for your assistance,
> I was following this example:
> http://www.scipy.org/Numpy_Example_List#head-779283aaa3cc20a3786ad33c2ee1fee9d68a4a53

I didn't know,
If arrays are 1d, then numpy does the stacking for you., but it
doesn't work with column-vectors or 2d arrays.
>>> np.corrcoef([np.arange(3), np.arange(3)], rowvar=1)
array([[ 1.,  1.],
       [ 1.,  1.]])
>>> np.corrcoef([np.arange(3), np.arange(3)], rowvar=0)
array([[ NaN,  NaN,  NaN],
       [ NaN,  NaN,  NaN],
       [ NaN,  NaN,  NaN]])
>
> I intend to use different arrays once I have resolved this problem.
> Trying your suggestion:
> ...
> print (rf1)
> print (np.corrcoef(rf1, rf1, rowvar=0))
> results in:
> [[-0.00641625]
> [-0.00498411]
> [-0.0038015 ]]
> [[ 1. 1.]
> [ 1. 1.]]
>
> which should have been a 3x3 matrix

I'm not sure what you want here, you only have three values
variance would also be zero, and corrcoef wouldn't be defined if you
only have 1 observation per variable

>>> np.corrcoef(np.arange(3)[:,None])
array([[ NaN,  NaN,  NaN],
       [ NaN,  NaN,  NaN],
       [ NaN,  NaN,  NaN]])

>>> np.corrcoef(np.column_stack([np.arange(5), np.arange(5)**2, np.arange(5)**3]), rowvar=0)
array([[ 1.        ,  0.9589266 ,  0.90588235],
       [ 0.9589266 ,  1.        ,  0.98713033],
       [ 0.90588235,  0.98713033,  1.        ]])

examples are in docstring for np.cov




> as for "stacking rf1 rf1 into a single array first" how would I do this?

>>> np.column_stack([np.arange(3)[:,None], np.arange(3)[:,None]])
array([[0, 0],
       [1, 1],
       [2, 2]])
>>> np.column_stack([np.arange(3)[:,None], np.arange(3)])  #doesn't required 2d
array([[0, 0],
       [1, 1],
       [2, 2]])

>>> np.c_[np.arange(3)[:,None], np.arange(3)[:,None]]
array([[0, 0],
       [1, 1],
       [2, 2]])
>>> np.hstack([np.arange(3)[:,None], np.arange(3)[:,None]])
array([[0, 0],
       [1, 1],
       [2, 2]])


Josef

> Regards,
> Rui
> ________________________________
> From: "josef.pktd@gmail.com" <josef.pktd@gmail.com>
> To: Discussion of Numerical Python <numpy-discussion@scipy.org>
> Sent: Wed, September 8, 2010 1:09:46 PM
> Subject: Re: [Numpy-discussion] ValueError: objects are not aligned
>
> On Wed, Sep 8, 2010 at 5:42 AM, RuiDC <ruidc@yahoo.com> wrote:
>>
>> I'm getting this error, which must be a simple error in getting the result
>> from the cursor in the right shape, how do I get the cursor into the array
>> as a single dimension?:
>>
>>    import numpy as np
>>    import sqlite3
>>    conn = sqlite3.connect("simpledb")
>>    cursor1 = conn.execute("select LogReturn from tblReturns limit 3")
>>    rf1 = np.asarray(cursor1.fetchall(), dtype=np.float64)
>>    print (rf1)
>>    print (np.corrcoef([rf1, rf1]))
>
> numpy.corrcoef(x, y=None, rowvar=1, bias=0)
>
> try
> np.corrcoef(rf1, rf1, rowvar=0)  no [ ]
> or
> stacking rf1 rf1 into a single array first
>
> Josef
>
>>
>> result:
>> [[-0.00641625]
>>  [-0.00498411]
>>  [-0.0038015 ]]
>> Traceback (most recent call last):
>>  File "C:\Documents and
>> Settings\rui\workspace\numpytest\src\numpytest.py",
>> line 38, in <module>
>>    print (np.corrcoef([rf1, rf1]))
>>  File "C:\Python26\lib\site-packages\numpy\lib\function_base.py", line
>> 2003, in corrcoef
>>    c = cov(x, y, rowvar, bias, ddof)
>>  File "C:\Python26\lib\site-packages\numpy\lib\function_base.py", line
>> 1953, in cov
>>    return (dot(X, X.T.conj()) / fact).squeeze()
>> --
>> View this message in context:
>> http://old.nabble.com/ValueError%3A-objects-are-not-aligned-tp29641661p29641661.html
>> Sent from the Numpy-discussion mailing list archive at Nabble.com.
>>
>> _______________________________________________
>> NumPy-Discussion mailing list
>> NumPy-Discussion@scipy.org
>> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>
>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>
>


More information about the NumPy-Discussion mailing list