Python의 numpy.where()
그만큼 numpy.where() 함수는 주어진 조건이 만족되는 입력 배열의 요소 인덱스를 반환합니다.
구문: numpy.where(조건[, x, y])
매개변수:
상태 : True이면 x를 산출하고, 그렇지 않으면 y를 산출합니다.
x, y: 선택할 값입니다. x, y 및 조건은 어떤 형태로든 방송될 수 있어야 합니다.보고:
밖으로 : [ndarray 또는 ndarray의 튜플] x와 y가 모두 지정된 경우 출력 배열에는 조건이 True인 x의 요소가 포함되고 다른 곳에서는 y의 요소가 포함됩니다.조건만 주어지면 조건이 True인 인덱스인 튜플 Condition.nonzero()를 반환합니다.
코드 #1:
# Python program explaining> # where() function> > import> numpy as np> > np.where([[> True> ,> False> ], [> True> ,> True> ]],> > [[> 1> ,> 2> ], [> 3> ,> 4> ]], [[> 5> ,> 6> ], [> 7> ,> 8> ]])> |
출력 :
array([[1, 6], [3, 4]])
코드 #2:
# Python program explaining> # where() function> > import> numpy as np> > # a is an array of integers.> a> => np.array([[> 1> ,> 2> ,> 3> ], [> 4> ,> 5> ,> 6> ]])> > print> (a)> > print> (> 'Indices of elements <4'> )> > b> => np.where(a <> 4> )> print> (b)> > print> (> 'Elements which are <4'> )> print> (a[b])> |
출력 :
[[1 2 3] [4 5 6]] Indices of elements <4 (array([0, 0, 0], dtype=int64), array([0, 1, 2], dtype=int64)) Elements which are <4 array([1, 2, 3])