Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

vectorize numpy append loop

I am trying to vectorize the following loop in I am trying to append multiple arrays to an empty array.

# ff is a matrix of shape [100,1,96]
temp = np.array([]).reshape(0,96)
for kk in range(1,10,1):
   temp = np.append(tr,ff[kk],axis=0)
temp = temp.reshape(1,10,96)

Is it possible to vectorize the above loop using numpy? Any help is welcome!

like image 906
dissw.geek9 Avatar asked Feb 28 '26 02:02

dissw.geek9


1 Answers

You can use slicing to extract the data you need:

ff[:10,:,:]

That will yield an array of shape (10, 1, 96). To get rid of the empty dimension, you can run it through numpy.squeeze():

numpy.squeeze(ff[:10,:,:])

and get an array of shape (10, 96)

like image 57
Nils Werner Avatar answered Mar 01 '26 16:03

Nils Werner



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!