【发布时间】:2019-09-29 09:48:55
【问题描述】:
如果我想根据二维数组中的一维值创建掩码:
a = np.array([[3, 5], [7, 1]])
threshold = 2
mask = a > threshold
print(a)
print(mask)
我明白了:
[[3 5]
[7 2]]
[[ True True]
[ True False]]
如何为具有 nd 值的二维数组创建这样的掩码?就像以下二维数组中的二维值和二维阈值示例:
b = np.array([[[1, 5], [3, 5]], [[4, 4], [7, 2]]])
threshold = 2, 4
print(b)
看起来像这样:
[[[1 5]
[3 5]]
[[4 4]
[7 2]]]
[1, 5]、[3, 5]、[4, 4] 和 [7, 2] 是示例性二维值。在threshold 中设置的阈值,第一个值为2,第二个值为4:
-
[1, 5]的单元格应该是False,因为1 > 2 == False和5 > 4 == True -
[3, 5]的单元格应该是True,因为3 > 2 == True和5 > 4 == True -
[4, 4]的单元格应该是False,因为4 > 2 == True和4 > 4 == False -
[7, 2]的单元格应该是False,因为7 > 2 == True和2 > 4 == False
我要怎么做才能得到这个对应的面具?
[[ False True]
[ False False]]
【问题讨论】: