pythonNumpy元素特定条件查找过滤[博]

 

where

1
2
3
4
5
a = np.array([[1,2,3,4,5],[6,7,8,9,10]]) #原始数据
e = (a > 6) |  (a <2)    #构造对原始数据进行筛选的条件
a4 = np.where(e,a,0) #把满足条件的选择出来,原封不动的保存,不满足条件的元素置零
                                   #本质上,就是把矩阵元素,按照条件分类.
a5 = a[e]                     #把满足条件的元素选择出来,构成新的元素子集合

argwhere
argwhere: arg(argument的缩写),where(表示索引在哪里),用这种方法记忆就不会混淆了

1
2
3
4
5
6
7
8
9
>>> x = np.arange(6).reshape(2,3)
>>> x
array([[0, 1, 2],
       [3, 4, 5]])
>>> np.argwhere(x>1)
array([[0, 2],
       [1, 0],
       [1, 1],
       [1, 2]])
  •  
发布了238 篇原创文章 · 获赞 144 · 访问量 86万+

猜你喜欢

转载自blog.csdn.net/u011331731/article/details/105189649