numpy.argwhere()函數用於查找按元素分組的非零數組元素的索引。
用法: numpy.argwhere(arr)
參數:
arr :[數組]輸入數組。
Return :[ndarray]非零元素的索引。索引按元素分組。
代碼1:
# Python program explaining
# argwhere() function
import numpy as geek
# input array
in_arr = [[ 2, 0, 7], [ 0, 5, 9]]
print ("Input array:", in_arr)
out_arr = geek.argwhere(in_arr)
print ("Output indices of non zero array element:\n", out_arr)
輸出:
Input array: [[2, 0, 7], [0, 5, 9]] Output indices of non zero array element: [[0 0] [0 2] [1 1] [1 2]]
代碼2:
# Python program explaining
# argwhere() function
import numpy as geek
# input array
in_arr = geek.arange(8).reshape(4, 2)
print ("Input array:", in_arr)
out_arr = geek.argwhere(in_arr>4)
print ("Output indices greater than 4:\n", out_arr)
輸出:
Input array: [[0 1] [2 3] [4 5] [6 7]] Output indices greater than 4: [[2 1] [3 0] [3 1]]
相關用法
注:本文由純淨天空篩選整理自jana_sayantan大神的英文原創作品 numpy.argwhere() in Python。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。