简体   繁体   English

np.concatenate() 二维数组而不是一维

[英]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.我正在尝试向某些变量添加零,以使它们的长度相同(100,)并将它们放入掩码表中。

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.当将零连接到我的变量propNum时它工作正常,但当我尝试使用具有 62 个值的变量time_04_window时创建一个 (2,) 长度数组。

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 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.因此, time_04_window是一个长度为 62 且全部为浮点数的数组,我想向该数组添加 38 个零以使其长度为 100。

At first it was running for table_time_04_window and now it is giving me起初它为table_time_04_window运行,现在它给了我

ValueError: all the input arrays must have same number of dimension ValueError:所有输入数组必须具有相同的维数

You should pass time_04_window rather than [time_04_window] , that's the dimension issue, ie您应该通过time_04_window而不是[time_04_window] ,这是维度问题,即

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 :注意:您也可以使用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])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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