基于某个值的索引过滤numpy数组

ckx4rj1h  于 2023-06-23  发布在  其他
关注(0)|答案(2)|浏览(121)

我有一个numpy数组,像这样:

array([[ 1, 17, 33, ..., 28,  9, 22],
       [ 3, 11,  1, ..., 25, 45, 14],
       [ 3, 11,  1, ..., 21, 23,  5],
       ...,
       [20,  6, 27, ..., 43, 15, 14],
       [27,  6, 39, ..., 37, 17,  2],
       [ 3, 11,  8, ..., 27, 35, 32]], dtype=int32)

从这里,我想过滤掉值4出现在索引10或之前的行。
例如[1,2,3,4,...,34,35] -过滤器,因为值4出现在索引3处,索引3在索引10之前
例如[35,34,33,32…,4,3,2,1] -保持,因为值4出现在索引10之后。
使用numpy masking实现这种过滤的方法是什么?

nnsrf1az

nnsrf1az1#

你可以试试这个:

arr = np.array(
    [[1, 2, 3, 4, 34, 35, 2, 1],
     [1, 2, 3, 5, 6, 7, 8, 9],
     [1, 2, 4, 5, 6, 7, 8, 9],
     [4, 2, 3, 5, 6, 7, 8, 9],
     [35, 34, 33, 32, 4, 3, 2, 1]]
)

V, T = 4, 3 # <-- change the threshold to 10

m = np.any(arr[:, :T+1] == V, axis=1)

out = arr[~m]

输出:

print(out)

array([[ 1,  2,  3,  5,  6,  7,  8,  9],
       [35, 34, 33, 32,  4,  3,  2,  1]])
  • 中间体:*
>>> arr[:, :T+1]
array([[ 1,  2,  3,  4],
       [ 1,  2,  3,  5],
       [ 1,  2,  4,  5],
       [ 4,  2,  3,  5],
       [35, 34, 33, 32]])

>>> arr[:, :T+1] == V
array([[False, False, False,  True],
       [False, False, False, False],
       [False, False,  True, False],
       [ True, False, False, False],
       [False, False, False, False]])

>>> np.any(arr[:, :T+1] == V, axis=1)
array([ True, False,  True,  True, False])
pnwntuvh

pnwntuvh2#

这里有一种方法,关键位是[4 not in i for i in vals[:,:10]],它通过遍历每行前10个元素的视图来创建掩码。

import numpy as np

np.random.seed(0)

vals = np.random.choice(50, size=(10, 100))

print(vals[:,:10])
print(vals.shape)
vals = vals[[4 not in i for i in vals[:,:10]]]
print(vals.shape)
print(vals[:,:10])

其应产生:

[[44 47  0  3  3 39  9 19 21 36]
 [ 5 41 35  0 31  5 30  0 49 36]
 [24 15 41 18 40 15 11 38 47 29]
 [ 2  5 37 12 44  2 47 27 21 39]
 [42 48 30 16 26 35 49 42  9 44]
 [ 3 34 40 33 28  4 26 32 45  9]
 [41 38 43 18  7 28  1 41  2 28]
 [36 49 24 33 18 33 14 49  7 43]
 [ 6 27 35  6 19 34 38 20 43  0]
 [ 9 20 37 48 17  9 44 15 38 14]]
(10, 100)
(9, 100)
[[44 47  0  3  3 39  9 19 21 36]
 [ 5 41 35  0 31  5 30  0 49 36]
 [24 15 41 18 40 15 11 38 47 29]
 [ 2  5 37 12 44  2 47 27 21 39]
 [42 48 30 16 26 35 49 42  9 44]
 [41 38 43 18  7 28  1 41  2 28]
 [36 49 24 33 18 33 14 49  7 43]
 [ 6 27 35  6 19 34 38 20 43  0]
 [ 9 20 37 48 17  9 44 15 38 14]]

相关问题