我有一行整数,我想将其作为列添加到浮点数的2D矩阵中。因此,当组合时,第一列将是整数列,第二列将是2D矩阵的第一列,最后一列将是矩阵的最后一列。
我试着将问题隔离到只有1行,但我仍然无法让它工作。下面是最小的例子
tee = np.array( [[ 0.3322441, -0.34410527, -0.1462533 , 0.35244817, -0.3557416, -0.3362794 ], [ 0.9750831, -0.24571404 , 0.12960567, 0.14683421 ,0.00650549, -0.21060513]] )
zeros = np.array([0])
all_data = np.hstack((zeros, tee))输出
ValueError Traceback (most recent call last)
<ipython-input-34-02aa17f12182> in <module>()
----> 1 all_data = np.hstack((zeros, tee))
/usr/local/lib/python3.6/dist-packages/numpy/core/shape_base.py in hstack(tup)
336 # As a special case, dimension 0 of 1-dimensional arrays is "horizontal"
337 if arrs and arrs[0].ndim == 1:
--> 338 return _nx.concatenate(arrs, 0)
339 else:
340 return _nx.concatenate(arrs, 1)
ValueError: all the input arrays must have same number of dimensions期望输出
print(all_data)
[[0],[ 0.3322441, -0.34410527, -0.1462533 , 0.35244817, -0.3557416, -0.3362794 ], [ 0.9750831, -0.24571404 , 0.12960567, 0.14683421 ,0.00650549, -0.21060513]]发布于 2019-05-08 12:02:58
在NumPy数组中混合数据类型的惟一方法是使用数据类型:np.object。这可以像这样隐式地完成:
all_data = np.asarray((zeros, *tee))或者明确地像这样:
all_data = np.asarray((zeros, *tee), dtype=np.object)https://stackoverflow.com/questions/56033292
复制相似问题