当前位置: 代码迷 >> 综合 >> numpy选取满足特定条件的元素 numpy.extract
  详细解决方案

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

热度:32   发布时间:2024-01-28 06:11:26.0

numpy.extract(condition, arr)
Return the elements of an array that satisfy some condition.
等价于:extractis equivalent to arr[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.

示例
选出除以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])