0

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)
6
  • Can you provide the code that generated a? When I load the contents of a exactly as you've listed them, the resulting shape is (2,2,7). Commented Sep 5, 2017 at 22:48
  • I suspect your individual arrays are not the same length. However, it would be helpful if you showed us how this was generated. Commented Sep 5, 2017 at 22:52
  • a = np.asarray(a) if this doesn't work your arrays are inconsistent. Commented Sep 5, 2017 at 22:53
  • This kind of thing is something you should fix in the code path that creates a, not after you have it. It tends to indicate a bug somewhere in the code leading up to the array's creation. Commented Sep 5, 2017 at 22:53
  • 1
    What are you trying to do with T1? That seems to be the source of this problem, you should start working with a real multidimensional array. Commented Sep 5, 2017 at 23:02

2 Answers 2

0

This behaviour is caused by the complex data type you are using. If you look carefully at your array, you can spot, that the dtype of the inner array is object and not complex as it should be. Please check if this is solved by setting the dtype of the inner arrays properly during the calculation/creation.

If this does not help, please check this SO thread. It's about a similar problem and might offer you an easy solution.

Sign up to request clarification or add additional context in comments.

Comments

0

a is, as noted, a 2d array, where each element is a 1d array.

np.array(...) tries to create as high a dimensional array as it can. But if the subelements (or lists) vary in size it can't, and resorts to making a object array (or in some cases raising an error). Your T1 is this kind of array.

If your subarrays are all the same shape, we can convert it in to a 3d array.

I can't recreate your T4 with a copynpaste because np.array makes a 3d array:

In [35]: array = np.array
In [36]: T4= 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)
    ...:        
In [37]: T4
Out[37]: 
array([[[0.0, 0.25, 0.5, 0.75, 1.0],
        [0.0, 0.0, 0.0, 0.0, 0.0]],

       [[0.0, 0.0, 0.0, 0.0, 0.0],
        [1.0, 1.0, 1.0, 1.0, 1.0]]], dtype=object)

But with your dot sequence:

In [41]: T1 = np.array([ [np.linspace(0,1,5),0],[0,1] ])
In [42]:  T2 = np.identity(2)
In [43]: T3 = np.dot(T1,T2)
In [44]: T4 = np.dot(T2,T3)

But I can convert it into one array with concatenate. Actually stack is best here. But first I have to turn it into a 1d array (4,). np.vstack would also work. Effectively it is now a list of 4 5-element arrays:

In [47]: np.stack(T4.ravel())
Out[47]: 
array([[ 0.  ,  0.25,  0.5 ,  0.75,  1.  ],
       [ 0.  ,  0.  ,  0.  ,  0.  ,  0.  ],
       [ 0.  ,  0.  ,  0.  ,  0.  ,  0.  ],
       [ 1.  ,  1.  ,  1.  ,  1.  ,  1.  ]])
In [48]: _.reshape(2,2,-1)
Out[48]: 
array([[[ 0.  ,  0.25,  0.5 ,  0.75,  1.  ],
        [ 0.  ,  0.  ,  0.  ,  0.  ,  0.  ]],

       [[ 0.  ,  0.  ,  0.  ,  0.  ,  0.  ],
        [ 1.  ,  1.  ,  1.  ,  1.  ,  1.  ]]])

Usually I recreate arrays like T4 by making a blank object array, e.g. a = np.zeros((2,2),object), and then filling the slots from a list. But posters also get them from third party packages. So this stack trick is a useful one to know.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.