numpy选取满足特定条件的元素numpy.extract

numpy.extract(condition, arr)

Return the elements of an array that satisfy some condition.

Parameters
condition [array_like]
An array whose nonzero or True entries indicate the elements of arr to extract.
arr [array_like]
Returns
extract [ndarray]
Rank 1 array of values from arr where condition is True.

选出a中满足条件condition的元素,形成新的array,等价于:extractis equivalent to arr[condition].
这里的condition是一个和arr具有相同shape的array,其元素和arr的元素一一对应,arr中的元素如果在condition中对应的元素为True,则被保留,为False,则被舍弃.
在numpy中,像这里的condition表征数组哪些元素有效的boolean数组被定义为mask

示例

选出除以3余数为0的元素

#Input array of the same size as condition.
>>>import numpy as np
>>>arr = np.arange(12).reshape((3, 4))
>>>print(arr)

[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]]
condition = np.mod(arr, 3)==0
np.extract(condition, arr)
 
array([0, 3, 6, 9])

等价于

arr[np.mod(arr,3)==0]

array([0, 3, 6, 9])
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值