简体   繁体   中英

np.concatenate() 2D array instead of 1D

I am trying to add zeros to certain variables in order to make them all the same length (100,) and put them in a masked table.

It works fine when concatenate zeros to my variable propNum that is a single number but creates a (2,) length array when I try it with my variable time_04_window that has 62 values.

Code that works:

propNum = 100
table_propNum = np.concatenate([[propNum], np.zeros(len(x)-1, dtype=float)])

Code that is not working:

time_04_window = [ 20029625.91881907  20029626.91881907  20029627.91881907 20029628.91881907  20029629.91881907  20029630.91881907 20029631.91881907  20029632.91881907  20029633.91881907 20029634.91881907  20029635.91881907  20029636.91881907 20029637.91881907  20029638.91881907  20029639.91881907 20029640.91881907  20029641.91881907  20029642.91881907 20029643.91881907  20029644.91881907  20029645.91881907 20029646.91881907  20029647.91881907  20029648.91881907 20029649.91881907  20029650.91881907  20029651.91881907 20029652.91881907  20029653.91881907  20029654.91881907 20029655.91881907  20029656.91881907  20029657.91881907 20029658.91881907  20029659.91881907  20029660.91881907 20029661.91881907  20029662.91881907  20029663.91881907 20029664.91881907  20029665.91881907  20029666.91881907 20029667.91881907  20029668.91881907  20029669.91881907 20029670.91881907  20029671.91881907  20029672.91881907 20029673.91881907  20029674.91881907  20029675.91881907 20029676.91881907  20029677.91881907  20029678.91881907 20029679.91881907  20029680.91881907  20029681.91881907 20029682.91881907  20029683.91881907  20029684.91881907 20029685.91881907  20029686.91881907]

table_time_04_window = np.concatenate([[time_04_window], np.zeros(len(x)-len(time_04_window), dtype='i')])

len(x) = 100

So, time_04_window is an array with length 62 and all floats, and I want to add 38 zeros to the array to give it a length of 100.

At first it was running for table_time_04_window and now it is giving me

ValueError: all the input arrays must have same number of dimension

You should pass time_04_window rather than [time_04_window] , that's the dimension issue, ie

In [11]: table_time_04_window = np.concatenate([time_04_window, np.zeros(100-len(time_04_window), dtype='i')])

Note: Rather than concatenate you can also right pad with np.pad :

In [21]: np.pad(np.array([1, 2, 3]), (0, 5), mode='constant')
Out[21]: array([1, 2, 3, 0, 0, 0, 0, 0])

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