<html><head><style type="text/css"><!-- DIV {margin:0px;} --></style></head><body><div style="font-family:"arial", "helvetica", sans-serif;font-size:12pt"><div>Thanks again,<br></div><div><br></div><div>I was trying to follow <span><a target="_blank" href="http://www.scipy.org/Numpy_Example_List#head-779283aaa3cc20a3786ad33c2ee1fee9d68a4a53">http://www.scipy.org/Numpy_Example_List#head-779283aaa3cc20a3786ad33c2ee1fee9d68a4a53</a></span></div><div><br></div><div>but with the key difference being, that instead of the data for the three arrays being constructed from literals, that i'd be sourcing from a relational database. The data is quite simple, in that I have 199 recorded days for 8 different variables, but am just trying to do a proof-of-concept first with three, when I had this problem. </div><div><br></div><div>The functionality I am seeking to reproduce is in Excel as a 3x3
 grid:</div><div><br></div><div>=CORREL(rf1,rf1)</div><div>=CORREL(rf1,rf2)</div><div>=CORREL(rf1,rf3)</div><div><div>=CORREL(rf2,rf1)</div><div>=CORREL(rf2,rf2)</div><div>=CORREL(rf2,rf3)</div><div><div>=CORREL(rf3,rf1)</div><div>=CORREL(rf3,rf2)</div><div>=CORREL(rf3,rf3)</div></div></div><div><br></div><div>I tried to simplify and isolate the problem, and hence the confusion.</div><div><br></div><div>So In sourcing from the DB, I have 3 different cursors (taking a subset of 3 of the 8), each 199 rows in length.</div><div>From reading the lists and blogs, the way to put these into numpy seemed to be:</div><div>rf1 = np.asarray(cursor1.fetchall(), dtype=np.float64)</div><div>rf2 = np.asarray(cursor2.fetchall(), dtype=np.float64)</div><div>rf3 = np.asarray(cursor3.fetchall(), dtype=np.float64)</div><div><br></div><div>Then to try and produce a correlation matrix:</div><div>print (np.corrcoef([rf1, rf2, rf3]))</div><div><br></div><div>But this is where I
 get the original error. </div><div>From looking at column_stack, this doesn't seem like what I want to achieve, or am I looking at this the wrong way somehow?.</div><div><br></div><div>Thanks again,</div><div>Rui</div><div><br></div><div style="font-family:arial, helvetica, sans-serif;font-size:12pt"><br><div style="font-family:arial, helvetica, sans-serif;font-size:13px"><font size="2" face="Tahoma"><hr size="1"><b><span style="font-weight: bold;">From:</span></b> "josef.pktd@gmail.com" <josef.pktd@gmail.com><br><b><span style="font-weight: bold;">To:</span></b> Discussion of Numerical Python <numpy-discussion@scipy.org><br><b><span style="font-weight: bold;">Sent:</span></b> Wed, September 8, 2010 2:34:42 PM<br><b><span style="font-weight: bold;">Subject:</span></b> Re: [Numpy-discussion] ValueError: objects are not aligned<br></font><br>
On Wed, Sep 8, 2010 at 8:08 AM, Rui DaCosta <<a ymailto="mailto:ruidc@yahoo.com" href="mailto:ruidc@yahoo.com">ruidc@yahoo.com</a>> wrote:<br>> Thanks for your assistance,<br>> I was following this example:<br><span>> <a target="_blank" href="http://www.scipy.org/Numpy_Example_List#head-779283aaa3cc20a3786ad33c2ee1fee9d68a4a53">http://www.scipy.org/Numpy_Example_List#head-779283aaa3cc20a3786ad33c2ee1fee9d68a4a53</a></span><br><br>I didn't know,<br>If arrays are 1d, then numpy does the stacking for you., but it<br>doesn't work with column-vectors or 2d arrays.<br>>>> np.corrcoef([np.arange(3), np.arange(3)], rowvar=1)<br>array([[ 1.,  1.],<br>       [ 1.,  1.]])<br>>>> np.corrcoef([np.arange(3), np.arange(3)], rowvar=0)<br>array([[ NaN,  NaN,  NaN],<br>       [ NaN,  NaN,  NaN],<br>       [ NaN,  NaN,  NaN]])<br>><br>> I intend to use different arrays once I have resolved this problem.<br>> Trying
 your suggestion:<br>> ...<br>> print (rf1)<br>> print (np.corrcoef(rf1, rf1, rowvar=0))<br>> results in:<br>> [[-0.00641625]<br>> [-0.00498411]<br>> [-0.0038015 ]]<br>> [[ 1. 1.]<br>> [ 1. 1.]]<br>><br>> which should have been a 3x3 matrix<br><br>I'm not sure what you want here, you only have three values<br>variance would also be zero, and corrcoef wouldn't be defined if you<br>only have 1 observation per variable<br><br>>>> np.corrcoef(np.arange(3)[:,None])<br>array([[ NaN,  NaN,  NaN],<br>       [ NaN,  NaN,  NaN],<br>       [ NaN,  NaN,  NaN]])<br><br>>>> np.corrcoef(np.column_stack([np.arange(5), np.arange(5)**2, np.arange(5)**3]), rowvar=0)<br>array([[ 1.        ,  0.9589266 ,  0.90588235],<br>       [ 0.9589266 ,  1.        ,  0.98713033],<br>       [ 0.90588235,  0.98713033,  1.        ]])<br><br>examples are in docstring for np.cov<br><br><br><br><br>> as
 for "stacking rf1 rf1 into a single array first" how would I do this?<br><br>>>> np.column_stack([np.arange(3)[:,None], np.arange(3)[:,None]])<br>array([[0, 0],<br>       [1, 1],<br>       [2, 2]])<br>>>> np.column_stack([np.arange(3)[:,None], np.arange(3)])  #doesn't required 2d<br>array([[0, 0],<br>       [1, 1],<br>       [2, 2]])<br><br>>>> np.c_[np.arange(3)[:,None], np.arange(3)[:,None]]<br>array([[0, 0],<br>       [1, 1],<br>       [2, 2]])<br>>>> np.hstack([np.arange(3)[:,None], np.arange(3)[:,None]])<br>array([[0, 0],<br>       [1, 1],<br>       [2, 2]])<br><br><br>Josef<br><br>> Regards,<br>> Rui<br>> ________________________________<br>> From: "<a ymailto="mailto:josef.pktd@gmail.com" href="mailto:josef.pktd@gmail.com">josef.pktd@gmail.com</a>" <<a ymailto="mailto:josef.pktd@gmail.com" href="mailto:josef.pktd@gmail.com">josef.pktd@gmail.com</a>><br>> To:
 Discussion of Numerical Python <<a ymailto="mailto:numpy-discussion@scipy.org" href="mailto:numpy-discussion@scipy.org">numpy-discussion@scipy.org</a>><br>> Sent: Wed, September 8, 2010 1:09:46 PM<br>> Subject: Re: [Numpy-discussion] ValueError: objects are not aligned<br>><br>> On Wed, Sep 8, 2010 at 5:42 AM, RuiDC <<a ymailto="mailto:ruidc@yahoo.com" href="mailto:ruidc@yahoo.com">ruidc@yahoo.com</a>> wrote:<br>>><br>>> I'm getting this error, which must be a simple error in getting the result<br>>> from the cursor in the right shape, how do I get the cursor into the array<br>>> as a single dimension?:<br>>><br>>>    import numpy as np<br>>>    import sqlite3<br>>>    conn = sqlite3.connect("simpledb")<br>>>    cursor1 = conn.execute("select LogReturn from tblReturns limit 3")<br>>>    rf1 = np.asarray(cursor1.fetchall(), dtype=np.float64)<br>>>  
  print (rf1)<br>>>    print (np.corrcoef([rf1, rf1]))<br>><br>> numpy.corrcoef(x, y=None, rowvar=1, bias=0)<br>><br>> try<br>> np.corrcoef(rf1, rf1, rowvar=0)  no [ ]<br>> or<br>> stacking rf1 rf1 into a single array first<br>><br>> Josef<br>><br>>><br>>> result:<br>>> [[-0.00641625]<br>>>  [-0.00498411]<br>>>  [-0.0038015 ]]<br>>> Traceback (most recent call last):<br>>>  File "C:\Documents and<br>>> Settings\rui\workspace\numpytest\src\numpytest.py",<br>>> line 38, in <module><br>>>    print (np.corrcoef([rf1, rf1]))<br>>>  File "C:\Python26\lib\site-packages\numpy\lib\function_base.py", line<br>>> 2003, in corrcoef<br>>>    c = cov(x, y, rowvar, bias, ddof)<br>>>  File "C:\Python26\lib\site-packages\numpy\lib\function_base.py", line<br>>> 1953, in cov<br>>>    return (dot(X, X.T.conj()) /
 fact).squeeze()<br>>> --<br>>> View this message in context:<br><span>>> <a target="_blank" href="http://old.nabble.com/ValueError%3A-objects-are-not-aligned-tp29641661p29641661.html">http://old.nabble.com/ValueError%3A-objects-are-not-aligned-tp29641661p29641661.html</a></span><br>>> Sent from the Numpy-discussion mailing list archive at <a target="_blank" href="http://Nabble.com">Nabble.com</a>.<br>>><br>>> _______________________________________________<br>>> NumPy-Discussion mailing list<br>>> <a ymailto="mailto:NumPy-Discussion@scipy.org" href="mailto:NumPy-Discussion@scipy.org">NumPy-Discussion@scipy.org</a><br><span>>> <a target="_blank" href="http://mail.scipy.org/mailman/listinfo/numpy-discussion">http://mail.scipy.org/mailman/listinfo/numpy-discussion</a></span><br>>><br>> _______________________________________________<br>> NumPy-Discussion mailing list<br>> <a
 ymailto="mailto:NumPy-Discussion@scipy.org" href="mailto:NumPy-Discussion@scipy.org">NumPy-Discussion@scipy.org</a><br>> <a href="http://mail.scipy.org/mailman/listinfo/numpy-discussion" target="_blank">http://mail.scipy.org/mailman/listinfo/numpy-discussion</a><br>><br>><br>> _______________________________________________<br>> NumPy-Discussion mailing list<br>> <a ymailto="mailto:NumPy-Discussion@scipy.org" href="mailto:NumPy-Discussion@scipy.org">NumPy-Discussion@scipy.org</a><br>> <a href="http://mail.scipy.org/mailman/listinfo/numpy-discussion" target="_blank">http://mail.scipy.org/mailman/listinfo/numpy-discussion</a><br>><br>><br>_______________________________________________<br>NumPy-Discussion mailing list<br><a ymailto="mailto:NumPy-Discussion@scipy.org" href="mailto:NumPy-Discussion@scipy.org">NumPy-Discussion@scipy.org</a><br><a href="http://mail.scipy.org/mailman/listinfo/numpy-discussion"
 target="_blank">http://mail.scipy.org/mailman/listinfo/numpy-discussion</a><br></div></div>


</div><br>

      </body></html>