728x90
반응형

np.where 함수
numpy에서 ndarray 인스턴스에서
특정 조건을 만족하는 elements의 위치(index, idx)를 찾는 기능을
numpy 모듈의 where 함수가 제공해줌.
사실 where 함수는 특정 조건에 따라 값을 바꾸어주는 기능도 있음.
다음 글 참조: https://dsaint31.tistory.com/212
NumPy 검색
np.where# np.where는 NumPy의 조건 기반 선택 함수np.where(condition[, x, y]) Return elements chosen from x or y depending on condition.condition : 검색에 사용될 조건.실제로 boolean array가 됨.x : condition이 True에 해당하는 위
dsaint31.tistory.com
index 나타내는 방식.
기본적으로 numpy에서 index를 나타내는 방식은
- 각각의 축마다 해당 축에서의 index인 scalar 값들을 가지고 있는
- 개별의 1차원 ndarray 객체들을 item으로 가지는
- tuple (sequence type이면됨)이 이용됨.
3차원의 ndarray에 대한 경우, 3개의 sequence type 인스턴스를 item으로 3개 가진 tuple이
where함수의 반환값이 됨 (당연히 item인 3개의 sequence 인스턴스는 같은 length를 가짐).
Example
다음의 코드로 사용법을 확인할 것.
import numpy as np
r = np.random.default_rng(seed = 23)
a = r.random((3,3,3)).astype(np.float32) * 10.
bm = a>=5.
idxs = np.where( a>=5)
print(a)
print('-----------------')
print(np.array(idxs).shape)
print(idxs)
728x90
'Python' 카테고리의 다른 글
| [Tensor] NaN Safe Aggregation Functions (0) | 2024.03.20 |
|---|---|
| [Tensor] vectorized op. (or universal func) (0) | 2024.03.19 |
| [DL] Tensor: Indexing <Simple, Slicing, Fancy, Boolean Mask> (0) | 2024.03.18 |
| [DL] Tensor: Transpose and Permute (2) | 2024.03.16 |
| [DL] Tensor 객체의 attributes: ndim, shape, dtype (0) | 2024.03.15 |