我有形狀的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
獲取此錯誤的簡單示例:import numpy as np
x = np.ones((20640, 7))
m = np.ones((20640, 2))
np.append(x, [m[:,0], m[:,1]], axis = 1)
我怎樣才能添加那兩列?
uj5u.com熱心網友回復:
如果您只想連接x和m[:,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.]])
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/350282.html
下一篇:Python3.x中的字典和回圈
