Python numpy.extract()
The numpy.extract()函数返回input_array的元素,如果它们满足某些指定的条件。
语法: numpy.extract(condition, array)
参数 :
array : 输入数组。用户在input_array元素上应用条件
condition : [array_like]条件,用户在此基础上提取元素。在input_array上应用条件,如果我们打印条件,它将返回一个数组充满True或False。数组中的元素是由具有真值。
返回值 :
满足条件的数组元素。
# Python Program illustrating
# numpy.compress method
import numpy as geek
array = geek.arange(10).reshape(5, 2)
print("Original array : \n", array)
a = geek.mod(array, 4) !=0
# This will show element status of satisfying condition
print("\nArray Condition a : \n", a)
# This will return elements that satisfy condition "a" condition
print("\nElements that satisfy condition a : \n", geek.extract(a, array))
b = array - 4 == 1
# This will show element status of satisfying condition
print("\nArray Condition b : \n", b)
# This will return elements that satisfy condition "b" condition
print("\nElements that satisfy condition b : \n", geek.extract(b, array))
输出 :
Original array :
[[0 1]
[2 3]
[4 5]
[6 7]
[8 9]]
Array Condition a :
[[False True]
[ True True]
[False True]
[ True True]
[False True]]
Elements that satisfy condition a :
[1 2 3 5 6 7 9]
Array Condition b :
[[False False]
[False False]
[False True]
[False False]
[False False]]
Elements that satisfy condition b :
[5]