简体   繁体   中英

Combining 2d arrays to 3d initialization error

So I know how to combine 2 2d arrays into a 3d. I did something like this:

a = np.arange(25).reshape(5,5)
b = np.arange(26,51).reshape(5,5)
c = np.stack((a,b))

That seems to work fine. The problem is I need to iterate over a loop to stack multiple 2d arrays and I need to input the number of such 2d arrays(ie the # is unknown).

So I did:

t = np.array([]).reshape(0,5,5)
t = np.stack((t,a)) #loop over each 2d array represented by a

I am getting this error:

ValueError Traceback (most recent call last) in ----> 1 t = np.stack((t,a))

< array_function internals> in stack(*args, **kwargs)

~/anaconda3/envs/pytorch/lib/python3.7/site-packages/numpy/core/shape_base.py in stack(arrays, axis, out) 423 shapes = {arr.shape for arr in arrays} 424 if len(shapes) != 1: --> 425 raise ValueError('all input arrays must have the same shape') 426 427 result_ndim = arrays[0].ndim + 1

ValueError: all input arrays must have the same shape

Any suggestions please? Thanks!

Nevermind. I did this:

data = np.empty((0,25))
data = np.vstack((data,one_row(random.randint(1, 3)))) #one_row returns a 1d array
input_data = np.reshape(input_data,(num_samples,5,5))

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