Given: a numpy array created from a string:
xy = np.array('4.9 3.5; 5.1 3.2; 4.7 3.1; 4.6 3.0; 5.0 5.4')
First off: is there a specific name for this construct?
Here is the datatype:
In [25]: xy
Out[25]:
array('4.9 3.5; 5.1 3.2; 4.7 3.1; 4.6 3.0; 5.0 5.4',
dtype='|S43')
What is |S43 ..
So OK enough with internals.. So here is the real question: how do we use the generated array:
In [31]: cov(xy)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-31-6d999a60c1da> in <module>()
----> 1 cov(xy)
..
TypeError: cannot perform reduce with flexible type
That result contrasts with the more standard usage of np.array:
In [33]: xy = np.array([[4.9, 3.5],[5.1, 3.2],[ 4.7, 3.1],[ 4.6, 3.0],[ 5.0, 5.4]], dtype=float)
In [35]: cov(xy)
Out[35]:
array([[ 0.98 , 1.33 , 1.12 , 1.12 , -0.28 ],
[ 1.33 , 1.805, 1.52 , 1.52 , -0.38 ],
[ 1.12 , 1.52 , 1.28 , 1.28 , -0.32 ],
[ 1.12 , 1.52 , 1.28 , 1.28 , -0.32 ],
[-0.28 , -0.38 , -0.32 , -0.32 , 0.08 ]])
So .. how to use the stringified numpy.array syntax to get that same result?
Update My bad here: i was mixing up numpy.array with numpy.matrix. The latter one does support the stringified syntax. See my answer below.
|S43means your type is a String with 43 charsdtype='|S43'indicates that the array is astringarray of length43(it has 43 characters). In other words, it is storing everything as a string, not as numbers.can't compute cov of a string. Yea no kidding .. The assumption were thatnumpyperforms the conversion. Maybe I am mixing upRwith numpy, checking ..