How can I flatten a 2d numpy array that has a different length in the second axis?

I have a numpy array that looks like this:

myArray = np.array([[1,2],[3]]) 

But I can’t smooth it,

 In: myArray.flatten() Out: array([[1, 2], [3]], dtype=object) 

If I change the array to the same length on the second axis, I can smooth it out.

 In: myArray2 = np.array([[1,2],[3,4]]) In: myArray2.flatten() Out: array([1, 2, 3, 4]) 

My question is:

Can I use something like myArray.flatten() regardless of the size of the array and the length of its elements and get the result: array([1,2,3]) ?

+6
source share
3 answers

myArray is a 1-dimensional array of objects. Your list objects will simply stay in the same order with flatten() or ravel() . You can use hstack to stack arrays in horizontal sequence:

 >>> np.hstack(myArray) array([1, 2, 3]) 

Note that this is basically equivalent to using concatenate with the 1 axis (this should make sense intuitively):

 >>> np.concatenate(myArray, axis=1) array([1, 2, 3]) 

If you don't have this problem and you can combine the elements, it is always advisable to use flatten() or ravel() for performance:

 In [1]: u = timeit.Timer('np.hstack(np.array([[1,2],[3,4]]))'\ ....: , setup = 'import numpy as np') In [2]: print u.timeit() 11.0124390125 In [3]: u = timeit.Timer('np.array([[1,2],[3,4]]).flatten()'\ ....: , setup = 'import numpy as np') In [4]: print u.timeit() 3.05757689476 

Iluengo's answer also contains additional information on why you cannot use flatten() or ravel() for your array type.

+4
source

Well, I agree with the other answers when they say that hstack or concatenate do the job in this case. However, I would like to point out that even if it β€œfixes” the problem, the problem does not fix properly.

The problem is that even if it looks like the second axis has a different length, in practice this is not true. If you try:

 >>> myArray.shape (2,) >>> myArray.dtype dtype('O') # stands for Object >>> myArray[0] [1, 2] 

It shows you that your array is not a two-dimensional array with a variable size (as you think), it is just a 1D array of objects. In your case, the list elements, which are the first element of your array, are a 2-element list, and the second element of the array is a list of 1 element.

So, flatten and ravel will not work, because converting a 1D array to a 1D array results in exactly one 1D array. If you have an object numpy array, it will not care about what you have enclosed inside it, it will consider individual elements as unregistered elements and cannot decide how to combine them.

What you should consider if this is the behavior you want for your application. Plural arrays are especially efficient with fixed-size numerical matrices. If you play with arrays of objects, I don't understand why you want to use Numpy instead of regular python lists.

+8
source

np.hstack works in this case

 In [69]: np.hstack(myArray) Out[69]: array([1, 2, 3]) 
+3
source

Source: https://habr.com/ru/post/984850/


All Articles