numpy高級函數:where與extract

numpy高級函數:where與extract

一、numpy.where()函數,此函數返回數組中知足某個條件的元素的索引:

import numpy as np

x = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print("x")
print(x)
y=np.where(x>5)
print(y)
print(x[y])

二、numpy.extract()函數,和where函數有一點相,不過extract函數是返回知足條件的元素,而不是元素索引,下面咱們的例子返回數組x 的元素 模2等於0的元素。

condition=np.mod(x,2)==0
print(condition)
print(np.extract(condition,x))
相關文章
相關標籤/搜索