I have an object of dtype='<U77' type, consisting of a string of numbers, separated with the spaces:
array('[ 0.20988965 0.05172284 -0.13468404 ... 2.06070718 -0.6160391\n 3. ]',
dtype='<U77')
How can I convert it into numpy array?
I have an object of dtype='<U77' type, consisting of a string of numbers, separated with the spaces:
array('[ 0.20988965 0.05172284 -0.13468404 ... 2.06070718 -0.6160391\n 3. ]',
dtype='<U77')
How can I convert it into numpy array?
Even if you wanted to do some kludgy string parsing to try to fix this object, you can't. You've already lost almost all of the original data, and there's no way to get it back just by looking at the string.
See that ... in the middle? That's what happens when you print an array large enough to trigger summarization:
>>> print(numpy.arange(1001))
[ 0 1 2 ... 998 999 1000]
It looks like you printed a large array and then called array on the resulting string. NumPy isn't designed for print to be reversible, and even in the cases where it is reversible, calling array on the printed output isn't how you'd reverse it.
You need to redo the computation that originally produced the array, and pick a better way to save the result, like numpy.save.
So here is a quick solution:
save the original data string as np.savetxt('filename', data_string), then when loading you get something like the following:
array('[ 0.119871 -0.50688947 0.27891722 0.58804999 -2.03537473 0.63659631\n 1.2 -0.83374409 -1.04955507 -0.6538087 -0.05 -0.23323881\n 1.2 3. 1.2 ]', dtype='<U183')
use np.fromstring(c1[1:-2], dtype=float, sep=' ') as a converter, this will come back with a similar numpy array:array([ 0.119871 , -0.50688947, 0.27891722, 0.58804999, -2.03537473,
0.63659631, 1.2 , -0.83374409, -1.04955507, -0.6538087 ,
-0.05 , -0.23323881, 1.2 , 3. , 1.2 ])