简体   繁体   中英

Why np.concatenate changes dimension

In the following program, I am trying to understand how np.concatenate command works. After accessing each row of the array a by for loop, when I concatenate along row axis I expect a 2-dimensional array having the shape of (5,5) but it changes.

I want to have the same dimension (5,5) after concatenation. How can I do that?

I tried to repeat the above method for the 2-dimensional array by storing them in a list [(2,5),(2,5),(2,5)] . At the end when I concatenate it gives me the shape of (6,5) as expected but in the following case, it is different.

a = np.arange(25).reshape(5,5)


ind =[0,1,2,3,4]
list=[]
for i in ind:
    list.append(a[i])

new= np.concatenate(list, axis=0)
print(list)
print(len(list))
print(new)
print(new.shape)

This gives the following results for new :

[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24]

and for new.shape :

(25,)

To preface this you really should not be using concatenate here.

Setup

a = np.arange(25).reshape(5,5)
L = [i for i in a]

You're question asks:

Why is np.concatenate changing dimension?

It's not changing dimension, it is doing exactly what it is supposed to based on the input you are giving it. From the documentation :

Join a sequence of arrays along an existing axis

When you pass your list to concatenate , don't think of it as passing a (5, 5) list, think of it as passing 5 (5,) shape arrays, which are getting joined along axis 0 , which will intuitively produce a (25,) shape output.

Now this behavior also gives insight on how to work around this. If passing 5 (5,) shape arrays produces a (25,) shape output, we just need to pass (1, 5) shape arrays to produce a (5, 5) shape output. We can accomplish this by simply adding a dimension to each element of L :

np.concatenate([[i] for i in L])

array([[ 0,  1,  2,  3,  4],
       [ 5,  6,  7,  8,  9],
       [10, 11, 12, 13, 14],
       [15, 16, 17, 18, 19],
       [20, 21, 22, 23, 24]])

However , the much better way to approach this is to simply use stack , vstack , etc..

>>> np.stack(L)
array([[ 0,  1,  2,  3,  4],
       [ 5,  6,  7,  8,  9],
       [10, 11, 12, 13, 14],
       [15, 16, 17, 18, 19],
       [20, 21, 22, 23, 24]])

>>> np.vstack(L)    
array([[ 0,  1,  2,  3,  4],
       [ 5,  6,  7,  8,  9],
       [10, 11, 12, 13, 14],
       [15, 16, 17, 18, 19],
       [20, 21, 22, 23, 24]])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM