3 回答

TA貢獻(xiàn)1876條經(jīng)驗(yàn) 獲得超6個(gè)贊
如果您想一次訪問多個(gè)列,則可以執(zhí)行以下操作:
>>> test = np.arange(9).reshape((3,3))
>>> test
array([[0, 1, 2],
[3, 4, 5],
[6, 7, 8]])
>>> test[:,[0,2]]
array([[0, 2],
[3, 5],
[6, 8]])

TA貢獻(xiàn)1828條經(jīng)驗(yàn) 獲得超13個(gè)贊
>>> test[:,0]
array([1, 3, 5])
該命令為您提供了行向量,如果您只想在其上循環(huán),就可以了,但是如果您要與其他尺寸為3xN的數(shù)組進(jìn)行堆疊,則可以
ValueError:所有輸入數(shù)組的維數(shù)必須相同
而
>>> test[:,[0]]
array([[1],
[3],
[5]])
為您提供列向量,以便您可以進(jìn)行串聯(lián)或hstack操作。
例如
>>> np.hstack((test, test[:,[0]]))
array([[1, 2, 1],
[3, 4, 3],
[5, 6, 5]])
添加回答
舉報(bào)