[英]How can I apply a 2D mask in numpy but keep the structure of the rows?
I have two numpy arrays eg:我有两个 numpy arrays 例如:
names = np.array(['A', 'B', 'C', 'D']) # (B, 1)
mask = np.array([ # (N, B)
[True, False, False, False],
[False, True, False, True ],
[True, True, False, False]
])
I want to get an 1 dimensional array of lists with shape (N,)
, where for each row in mask
I have a list of the names in names
for which the value was True
.我想获得一个形状为(N,)
的列表的一维数组,其中对于mask
中的每一行,我都有一个names
列表,其值为True
。 Here that would be:这将是:
result = [
['A'],
['B', 'C'],
['A', 'B']
]
Any idea how?知道怎么做吗?
For each array m
in mask
, you can subset names
based on the values of m
.对于mask
中的每个数组m
,您可以根据m
的值对names
进行子集化。
[list(names[m]) for m in mask]
# [['A'], ['B', 'D'], ['A', 'B']]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.