通过索引删除numpy数组的元素

时间:2019-08-25 18:22:28

标签: python arrays numpy

我试图通过索引删除Numpy Array的几行,但是我不知道为什么它会产生不同的值。它应该删除三行。

# The original array descritoresFaciais
[[ -9.30438712e-02   7.69939125e-02]   # index 0
[ -8.71469826e-02   9.11752135e-02]    # index 1    
[ -9.40909833e-02   1.10907182e-01]    # index 2
[ -1.45724729e-01   3.04837357e-02]    # index 3
[ -1.72051653e-01   5.80535792e-02]    # index 4
[ -1.44777581e-01   2.88028345e-02]]   # index 5

indexes = [0, 1, 2] # indexes I want to remove
x = np.delete(descritoresFaciais, indexes, axis=0)
print(x)

#Result of deleting
[[-0.14572473  0.01929282]
[-0.17205165  -0.00352619]
[-0.14477758   0.00853495]]

我也尝试了此命令行,但是得到了相同的值:

x = np.delete(descritoresFaciais, np.s_[0:3], axis=0)

1 个答案:

答案 0 :(得分:0)

您的代码对我有用。.但是,也可以尝试将索引本身定义为np.array:

indexes = np.array([0,1,2])
x = np.delete(descritoresFaciais, indexes, axis=0)

如果由于某种奇怪的原因它仍然无法正常工作..获取所有行和要删除的行之间的设置差异,然后返回数组的其余部分:

indexes = np.array([0,1,2])
allrows = np.array(range(len(descritoresFaciais)))
x = d[np.setdiff1d(allrows,indexes)]

我对这两种方式都得到了相同的答案,所以我的直觉告诉我们,在定义数组时发生了奇怪的事情,或者在尝试修改数组之前发生了改变