I have a Python script and somehow in my Numpy calculation I got a variable like this:
In [72]: a
Out[72]:
array([[ array([-0.02134025+0.1445159j , -0.02136137+0.14458584j,
-0.02138250+0.14465578j, ..., -0.01568173+0.12424096j,
-0.01569507+0.12429295j, -0.01570842+0.12434494j]),
array([-0.14451590+0.97865975j, -0.14458584+0.97863863j,
-0.14465578+0.9786175j , ..., -0.12424096+0.98431827j,
-0.12429295+0.98430493j, -0.12434494+0.98429158j])],
[ array([-0.14451590+0.97865975j, -0.14458584+0.97863863j,
-0.14465578+0.9786175j , ..., -0.12424096+0.98431827j,
-0.12429295+0.98430493j, -0.12434494+0.98429158j]),
array([ 0.02134025-0.1445159j , 0.02136137-0.14458584j,
0.02138250-0.14465578j, ..., 0.01568173-0.12424096j,
0.01569507-0.12429295j, 0.01570842-0.12434494j])]], dtype=object)
In [73]: np.shape(a)
Out[73]: (2, 2)
So it's basically a 2D array where each element is an array of length n, but somehow a is not a 3D array of shape (2,2,n). You can see the extra word array in each element in the print out. I don't understand how this happened but I'd like to turn a into a 3D array of shape (2,2,n) because I have other (2,2,n) variables and they're not inter-operable. (If I can turn other (2,2,n) arrays into same thing as a that would probably work too, I guess).
For example an array of shape (2,2,3) like this won't have the extra array wording print out in each element. And the shape is (2,2,3) not (2,2).
In [75]: np.zeros((2,2,3))
Out[75]:
array([[[ 0., 0., 0.],
[ 0., 0., 0.]],
[[ 0., 0., 0.],
[ 0., 0., 0.]]])
I tried np.asarray(a) and a.reshape((2,2,)) but they didn't do anything, I guess it's already an array.
Update: This is the similar code that generates something like a:
In [80]: T1 = np.array([ [np.linspace(0,1,5),0],[0,1] ])
In [81]: T1
Out[81]:
array([[array([ 0. , 0.25, 0.5 , 0.75, 1. ]), 0],
[0, 1]], dtype=object)
In [82]: T2 = np.identity(2)
In [83]: T2
Out[83]:
array([[ 1., 0.],
[ 0., 1.]])
In [84]: T3 = np.dot(T1,T2)
In [85]: T3
Out[85]:
array([[array([ 0. , 0.25, 0.5 , 0.75, 1. ]),
array([ 0., 0., 0., 0., 0.])],
[0.0, 1.0]], dtype=object)
In [86]: T4 = np.dot(T2,T3)
In [87]: T4
Out[87]:
array([[array([ 0. , 0.25, 0.5 , 0.75, 1. ]),
array([ 0., 0., 0., 0., 0.])],
[array([ 0., 0., 0., 0., 0.]), array([ 1., 1., 1., 1., 1.])]], dtype=object)
The elements are same length and asarray doesn't work.
In [88]: np.asarray(T4)
Out[88]:
array([[array([ 0. , 0.25, 0.5 , 0.75, 1. ]),
array([ 0., 0., 0., 0., 0.])],
[array([ 0., 0., 0., 0., 0.]), array([ 1., 1., 1., 1., 1.])]], dtype=object)
a? When I load the contents ofaexactly as you've listed them, the resultingshapeis(2,2,7).a = np.asarray(a)if this doesn't work your arrays are inconsistent.a, not after you have it. It tends to indicate a bug somewhere in the code leading up to the array's creation.T1? That seems to be the source of this problem, you should start working with a real multidimensional array.