5

I have a NumPy array (of length X) of arrays, all of which are of the same length (Y), but which has type "object" and thus has dimension (X,). I would like to "convert" this into an array of dimension (X, Y) with the type of the elements of the member arrays ("float").

The only way I can see to do this is "manually" with something like

[x for x in my_array]

Is there a better idiom for accomplishing this "conversion"?


For example I have something like:

array([array([ 0.,  0.,  1.,  0.,  0.,  0.,  0.,  0.,  0.,  0.]),
       array([ 0.,  1.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.]),
       array([ 0.,  0.,  1.,  0.,  0.,  0.,  0.,  0.,  0.,  0.]), ...,
       array([ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  1.]),
       array([ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  1.,  0.,  0.]),
       array([ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  1.])], dtype=object)

which has shape (X,) rather than (X, 10).

5
  • How did you get this array in the first place? This kind of thing usually means you did something wrong earlier in your program. I suspect the subarrays don't really have the same length. Commented Jul 27, 2017 at 21:52
  • np.transpose(your_array).astype(float) Commented Jul 27, 2017 at 21:59
  • @Mr_U4913: "ValueError: setting an array element with a sequence" Commented Jul 27, 2017 at 22:02
  • @user2357112: The subarrays do have the same length. @user2357112: I begin with a list of two lists of X arrays, where all of the arrays in one list are of length Y, I then transpose that (fore many reasons) and slice it with, e.g., [:,1], which gives the problematic structure. Commented Jul 27, 2017 at 22:05
  • @raxacoricofallapatorius: That definitely sounds like you did something wrong in there. Maybe in the transpose. Commented Jul 27, 2017 at 22:07

1 Answer 1

3

You can concatenate the arrays on a new axis. For example:

In [1]: a=np.array([1,2,3],dtype=object)
   ...: b=np.array([4,5,6],dtype=object)

To make an array of arrays we can't just combine them with array, as the deleted answer did:

In [2]: l=np.array([a,b])
In [3]: l
Out[3]: 
array([[1, 2, 3],
       [4, 5, 6]], dtype=object)
In [4]: l.shape
Out[4]: (2, 3)

Instead we have to create an empty array of the right shape, and fill it:

In [5]: arr = np.empty((2,), object)
In [6]: arr[:]=[a,b]
In [7]: arr
Out[7]: array([array([1, 2, 3], dtype=object), 
               array([4, 5, 6], dtype=object)], 
              dtype=object)

np.stack acts like np.array, but uses concatenate:

In [8]: np.stack(arr)
Out[8]: 
array([[1, 2, 3],
       [4, 5, 6]], dtype=object)
In [9]: _.astype(float)
Out[9]: 
array([[ 1.,  2.,  3.],
       [ 4.,  5.,  6.]])

We could also use concatenate, hstack or vstack to combine the arrays on different axes. They all treat the array of arrays as a list of arrays.

If arr is 2d (or higher) we have to ravel it first.

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

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.