25
>>> arr = np.array([[1,2,3,4], [5,6,7,8], [9,10,11,12]])
>>> arr
array([[ 1,  2,  3,  4],
       [ 5,  6,  7,  8],
       [ 9, 10, 11, 12]])

我将第三列删除为

>>> np.hstack(((np.delete(arr, np.s_[2:], 1)),(np.delete(arr, np.s_[:3],1))))
array([[ 1,  2,  4],
       [ 5,  6,  8],
       [ 9, 10, 12]])

有没有更好的方法?请认为这是一个新手问题。

4

3 回答 3

47

如果您想删除多个列,只需将要删除的列的索引作为列表传递,如下所示:

>>> a = np.arange(12).reshape(3,4)
>>> a
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])
>>> np.delete(a, [1,3], axis=1)
array([[ 0,  2],
       [ 4,  6],
       [ 8, 10]])
于 2013-05-19T12:40:40.370 回答
8
>>> import numpy as np
>>> arr = np.array([[1,2,3,4], [5,6,7,8], [9,10,11,12]])
>>> np.delete(arr, 2, axis=1)
array([[ 1,  2,  4],
       [ 5,  6,  8],
       [ 9, 10, 12]])
于 2013-05-19T08:07:49.147 回答
2

像这样的东西:

In [7]: x = range(16)

In [8]: x = np.reshape(x, (4, 4))

In [9]: x
Out[9]: 
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11],
       [12, 13, 14, 15]])

In [10]: np.delete(x, 1, 1)
Out[10]: 
array([[ 0,  2,  3],
       [ 4,  6,  7],
       [ 8, 10, 11],
       [12, 14, 15]])
于 2013-05-19T08:09:17.760 回答