np.where() 和 np.argwhere()的用法

一.np.where()

用法1:

   1.np.where(condition,x,y) 当where内有三个参数时,第一个参数表示条件,当条件成立时where方法返回x,当条件不成立时where返回y。

实例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]]

  用法2:

       2.np.where(condition) 当where内只有一个参数时,那个参数表示条件,当条件成立时,               where返回的是每个符合condition条件元素的坐标,返回的是以元组的形式。

  实例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]),)

二. np.argwhere( )

   用法:

        np.argwhere(a)

        此函数要实现的功能就是,返回 Array a 中所有大于1的值的索引.

   实例:

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]]


 

猜你喜欢

转载自blog.csdn.net/m0_62278731/article/details/131084227
今日推荐