【问题标题】:Error while appending to columns (to ndarray)附加到列时出错(到 ndarray)
【发布时间】:2021-11-06 09:24:13
【问题描述】:

我有形状的ndarray:

(20640, 7)

我正在尝试添加新列:

np.append(x, [m[:,0], m[:,1]], axis = 1)

地点:

m[:,0].shape = m[:,1].shape = (20640,)

我得到了错误:

ValueError: all the input array dimensions for the concatenation axis must match exactly, but along dimension 0, the array at index 0 has size 20640 and the array at index 1 has size 2

获取此错误的简单示例: 将 numpy 导入为 np

x = np.ones((20640, 7))
m = np.ones((20640, 2))
np.append(x, [m[:,0], m[:,1]], axis = 1)

如何添加这两列?

【问题讨论】:

  • 你能提供一个最小的例子吗? (以及预期的输出)

标签: python python-3.x numpy


【解决方案1】:

如果您只想将xm[:,0]/m[:,1] 连接为列,请使用np.c_

np.c_[x, m[:,0], m[:,1]]

例子:

x = np.zeros((20640, 7))
m = np.ones((20640, 2))
np.c_[x, m[:,0], m[:,1]]

输出:

array([[0., 0., 0., ..., 0., 1., 1.],
       [0., 0., 0., ..., 0., 1., 1.],
       [0., 0., 0., ..., 0., 1., 1.],
       ...,
       [0., 0., 0., ..., 0., 1., 1.],
       [0., 0., 0., ..., 0., 1., 1.],
       [0., 0., 0., ..., 0., 1., 1.]])

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-12
    • 2021-02-25
    • 2019-02-12
    • 2016-07-30
    相关资源
    最近更新 更多