Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Append dimensional numpy array

I have two numpy array matrix A and B of length 32 and shape (32, 32, 3) each . I want to combine them into a new array so that the dimensions of my new array is (2, 32, 32,3).

Using np. concatenate is throwing an error.

like image 843
Manasi Avatar asked Feb 20 '26 19:02

Manasi


2 Answers

Use np.stack

def dim0_stack(*arrays):
    return np.stack(arrays, axis = 0)
like image 157
Daniel F Avatar answered Feb 23 '26 07:02

Daniel F


Another way to do it:

a = np.random.randn(32, 32, 3)
b = np.random.randn(32, 32, 3)
c = np.concatenate([np.expand_dims(a,0), np.expand_dims(b, 0)], axis=0)
print(c.shape)

Because you mentioned using concatenate, I thought to show you how you can use it.

like image 22
Moher Avatar answered Feb 23 '26 08:02

Moher