Usage of np.where() and np.argwhere()

1.np.where()

Usage 1:

   1.np.where(condition,x,y) When there are three parameters in where, the first parameter represents the condition. When the condition is true, the where method returns x, and when the condition is not true, where returns y.

Example 1:

a = np.array([[1,2,3],[4,5,6],[7,8,9]])
print("a:", a)
b = np.where( a>5, a, 0)
print("b:",b)
==============================================
a: [[1 2 3]
 [4 5 6]
 [7 8 9]]
b: [[0 0 0]
 [0 0 6]
 [7 8 9]]

  Usage 2:

       2.np.where(condition) When there is only one parameter in where, that parameter represents the condition. When the condition is true, where returns the coordinates of each element that meets the condition in the form of a tuple .

  Example 2:

a = np.array([1,2,3,4,5,6,7])
print("a:", a)
b = np.where(a > 3)
print("b:", b)
============================================
a: [1 2 3 4 5 6 7]
b: (array([3, 4, 5, 6]),)

2. np.argwhere( )

   usage:

        np.argwhere(a)

        The purpose of this function is to return the index of all values ​​greater than 1 in Array a.

   Example:

a = np.arange(6).reshape(2,3)
print("a:",a)
b = np.argwhere(a>1)
print("b:",b)
=======================================
a: [[0 1 2]
 [3 4 5]]
b: [[0 2]
 [1 0]
 [1 1]
 [1 2]]


 

Guess you like

Origin blog.csdn.net/m0_62278731/article/details/131084227