簡體   English   中英

通過索引刪除numpy數組的元素

[英]Delete elements of numpy array by indexes

我正在嘗試通過索引刪除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)

您的代碼對我有用。.但是,也可以嘗試將索引本身定義為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)]

我對這兩種方式都得到了相同的答案,所以我的直覺告訴我們,在定義數組時發生了奇怪的事情,或者在嘗試修改之前發生了更改

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM