numpy:使用vstack创建一个数组数组

时间:2019-01-22 12:52:22

标签: python numpy

我想使用vstack创建一个numpy的数组,但是出现了以下错误消息:all the input array dimensions except for the concatenation axis must match exactly

到目前为止我尝试过的:

all_exp = np.array([])

for idx, rw in df_gamma_count.iterrows():
    exp = rw['Pr_A_perc'] * ( rw['gamma_index'] * float(row['spread_perc']) * (1+f) - (f+f) )
    gamma_and_exp = [exp, rw['gamma_index']]
    all_exp = np.vstack((all_exp,gamma_and_exp))

知道为什么吗?谢谢!

1 个答案:

答案 0 :(得分:0)

要使numpy.vstack()工作,所有2D数组中的列数都应匹配。例如:

In [14]: arr1 = np.arange(2*4).reshape(2, 4)
In [15]: arr2 = np.arange(5*4).reshape(5, 4) 

In [16]: np.vstack((arr1, arr2)) 
Out[16]: 
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11],
       [12, 13, 14, 15],
       [16, 17, 18, 19]])

错误消息

  

除串联轴外,所有输入数组的尺寸必须完全匹配

表示在此示例中,我们沿轴0堆叠。因此,除轴0外的所有尺寸都应匹配。

例如,形状

(2, 4)
(5, 4)
 ^  ^
 |  |
 |  dimension of this axis should match.
 dimensions along this axis can be different since we'are concatenating along axis 0.