Numpy: Boolean Indexing

Numpy Boolean Indexing Mask(Numpy 布尔索引掩码 )

 
import numpy as np

A = np.array([4, 7, 3, 4, 2, 8])

print(A == 4)
 
[ True False False  True False False]
 

Every element of the Array A is tested, if it is equal to 4. The results of these tests are the Boolean elements of the result array.

Of course, it is also possible to check on "<", "<=", ">" and ">=".

 
print(A < 5)
 
[ True False  True  True  True False]
 

It works also for higher dimensions:

 
B = np.array([[42,56,89,65],
              [99,88,42,12],
              [55,42,17,18]])

print(B>=42)
 
[[ True  True  True  True]
 [ True  True  True False]
 [ True  True False False]]
 

It is a convenient way to threshold images.

 
import numpy as np

A = np.array([
[12, 13, 14, 12, 16, 14, 11, 10,  9],
[11, 14, 12, 15, 15, 16, 10, 12, 11],
[10, 12, 12, 15, 14, 16, 10, 12, 12],
[ 9, 11, 16, 15, 14, 16, 15, 12, 10],
[12, 11, 16, 14, 10, 12, 16, 12, 13],
[10, 15, 16, 14, 14, 14, 16, 15, 12],
[13, 17, 14, 10, 14, 11, 14, 15, 10],
[10, 16, 12, 14, 11, 12, 14, 18, 11],
[10, 19, 12, 14, 11, 12, 14, 18, 10],
[14, 22, 17, 19, 16, 17, 18, 17, 13],
[10, 16, 12, 14, 11, 12, 14, 18, 11],
[10, 16, 12, 14, 11, 12, 14, 18, 11],
[10

相关文章:

  • 2021-09-13
  • 2022-12-23
  • 2021-10-24
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-10-17
猜你喜欢
  • 2021-11-02
  • 2022-01-06
  • 2023-03-27
  • 2021-09-07
  • 2021-09-08
  • 2021-08-24
相关资源
相似解决方案