๋ ๊ฐ์ 1D ๋ถ์ธ ๋ง์คํฌ(๋ชจ์ N ๋ฐ M)๋ฅผ ์ฌ์ฉํ์ฌ [N, M] ๋ชจ์์ 2D ๋ฐฐ์ด์ ์ธ๋ฑ์ฑํ๋ ค๊ณ ํ๋ฉด True์ False์ ํน์ ์กฐํฉ์ด ๋ธ๋ก๋์บ์คํ ์ค๋ฅ(ํนํ ํ๋๊ฐ ๋ชจ๋ false์ธ ๊ฒฝ์ฐ)๊ฐ ๋ฐ์ํฉ๋๋ค. ์ด ๋์์ด ์์๋๋์ง ํ์คํ์ง ์์ง๋ง ๋งค์ฐ ๋๋๊ณ ๋ฐ๋์งํ์ง ์์ ๊ฒ ๊ฐ์ต๋๋ค.
์๋ ์์์ x[[False, True, True], [True, True, True]]
์ค๋ฅ๊ฐ ๋ฐ์ํ๋ ๋ฐ๋ฉด x[[False, True, True], True]
๋ฐ x[[False, True, True]]
์๋ ์์๋ ๋์์ด ์์ต๋๋ค.
import numpy as np
from itertools import product
x = np.zeros((3,3))
mask_1d = [*product([True, False], repeat=3)]
for row_mask, col_mask in product(mask_1d, mask_1d):
try:
x[row_mask, col_mask]
except IndexError as e:
print(row_mask, col_mask)
print(e)
(True, True, True) (True, True, False)
shape mismatch: indexing arrays could not be broadcast together with shapes (3,) (2,)
(True, True, True) (True, False, True)
shape mismatch: indexing arrays could not be broadcast together with shapes (3,) (2,)
(True, True, True) (False, True, True)
shape mismatch: indexing arrays could not be broadcast together with shapes (3,) (2,)
(True, True, True) (False, False, False)
shape mismatch: indexing arrays could not be broadcast together with shapes (3,) (0,)
(True, True, False) (True, True, True)
shape mismatch: indexing arrays could not be broadcast together with shapes (2,) (3,)
(True, True, False) (False, False, False)
shape mismatch: indexing arrays could not be broadcast together with shapes (2,) (0,)
(True, False, True) (True, True, True)
shape mismatch: indexing arrays could not be broadcast together with shapes (2,) (3,)
(True, False, True) (False, False, False)
shape mismatch: indexing arrays could not be broadcast together with shapes (2,) (0,)
(False, True, True) (True, True, True)
shape mismatch: indexing arrays could not be broadcast together with shapes (2,) (3,)
(False, True, True) (False, False, False)
shape mismatch: indexing arrays could not be broadcast together with shapes (2,) (0,)
(False, False, False) (True, True, True)
shape mismatch: indexing arrays could not be broadcast together with shapes (0,) (3,)
(False, False, False) (True, True, False)
shape mismatch: indexing arrays could not be broadcast together with shapes (0,) (2,)
(False, False, False) (True, False, True)
shape mismatch: indexing arrays could not be broadcast together with shapes (0,) (2,)
(False, False, False) (False, True, True)
shape mismatch: indexing arrays could not be broadcast together with shapes (0,) (2,)
1.16.2 3.6.8 |Anaconda, Inc.| (default, Dec 30 2018, 01:22:34)
[GCC 7.3.0]
๋ถ์ธ ๋ฐฐ์ด์ ์ฌ์ฉํ๋ฉด ์ฝ๋๋ ๋จ์ผ ์ฐจ์ ๋๋ ๋ชจ๋ ์์๋ฅผ โโ๋์์ ์ธ๋ฑ์ฑํ๋ ค๊ณ ํ๋ค๊ณ ๊ฐ์ ํฉ๋๋ค. ์ ํ ํญ๋ชฉ์ ๋ถํํ๋ ๋จ์ผ True
๋ฅผ ์ ๊ฑฐํ ์ ์๋ ๋ฐฉ์์ผ๋ก ์ถ์ธก๋ฉ๋๋ค. ์ฆ, row_mask, col_mask
๋ฅผ (2,3) ๋ถ์ธ ๋ฐฐ์ด๋ก ๋ฐ๊พผ ๋ค์ (3,3) ๋ฐฐ์ด์ ์ธ๋ฑ์ฑํ ์ ์์์ ์ฐพ์ต๋๋ค.
๋ฌธ์ ์ ์ผ๋ถ๋ ํํ๊ณผ ๋ชฉ๋ก์ด ๋๋ฑํ๊ฒ ์ทจ๊ธ๋๋ค๋ ์ ์ ๋๋ค. ๊ฒฐ๊ตญ ๋ง์คํฌ๊ฐ ์ด์ค ๋ชฉ๋ก์ธ์ง ํ์ธํ์ฌ ๋ถ์ธ ๋ฐฐ์ด ์ธ๋ฑ์ค๋ฅผ ์ฒ๋ฆฌํ๊ฒ ๋ฉ๋๋ค.
๊ทธ๋ฌ๋ ํ์ฌ๋ก์๋ ์ ์ผํ ํด๊ฒฐ์ฑ
์ด x[row_mask][:, col_mask]
๋ฅผ) ์ํํ๋ ๊ฒ์ด ๋๋ ต์ต๋๋ค.
cc @eric-wieser๋ ์ธ๋ฑ์ฑ ์์ ์ ๋ํด "ํํ์ ๋ชฉ๋ก์ผ๋ก ์ทจ๊ธ"์ ๋ ์ด์ ์ฌ์ฉํ์ง ์๋๋ก ๋ ธ๋ ฅํด ์์ต๋๋ค.
ps ๊ฐ์ฅ ์ง์ฆ๋๋ ์ฐจ์ด์ ์ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
x = np.arange(9).reshape(3, 3)
# x[[False, True, True], True]
# array([[3, 4, 5],
# [6, 7, 8]])
x[[False, True, True], False]
# IndexError: shape mismatch: indexing arrays could not be broadcast together with shapes (2,) (0,)
์, x[row_mask][:, col_mask]
๋ ๋ด๊ฐ ํ ์ผ์
๋๋ค. ์ค๋ช
๊ฐ์ฌํฉ๋๋ค. ๊ฒํ ์ค์ธ ํญ๋ชฉ์ด๋ผ ๊ธฐ์ฉ๋๋ค.
๋๋ arr[np.ix_(index)]
์ด ๋น์ ์ด ์ฌ๊ธฐ์์ ์ํ๋/๊ธฐ๋ํ๋ ๊ฒ์ด๋ผ๊ณ ์๊ฐํฉ๋๋ค. ๋๋ NEP 21์์์ ๊ฐ์ด ์ธ๋ถ ์ธ๋ฑ์ฑ ๋ก์ง์ ๋ํ ๋ค๋ฅธ ๋ง๋ก: https://github.com/numpy/numpy/blob/master/doc/neps /nep-0021-advanced-indexing.rst
์๋ง๋ ๊ทธ๊ฒ์ ์๊ฐ์ด ์ง๋๋ฉด ์กํ ๊ฒ์ ๋๋ค. NEP๋ ๋ํ ์ต์ํ ํ์ฌ ์ธ๋ฑ์ฑ์ ๋ํด ์ฌ๋ฌ ๋ถ์ธ ์ธ๋ฑ์ค๊ฐ ๋ ์ด์ ์ฌ์ฉ๋์ง ์์์ผ ํ๋ค๊ณ ๋งํฉ๋๋ค. ์ด๋ค ๊ฒฝ์ฐ๋ผ๋).
๊ฐ์ฅ ์ ์ฉํ ๋๊ธ
๋ถ์ธ ๋ฐฐ์ด์ ์ฌ์ฉํ๋ฉด ์ฝ๋๋ ๋จ์ผ ์ฐจ์ ๋๋ ๋ชจ๋ ์์๋ฅผ โโ๋์์ ์ธ๋ฑ์ฑํ๋ ค๊ณ ํ๋ค๊ณ ๊ฐ์ ํฉ๋๋ค. ์ ํ ํญ๋ชฉ์ ๋ถํํ๋ ๋จ์ผ
True
๋ฅผ ์ ๊ฑฐํ ์ ์๋ ๋ฐฉ์์ผ๋ก ์ถ์ธก๋ฉ๋๋ค. ์ฆ,row_mask, col_mask
๋ฅผ (2,3) ๋ถ์ธ ๋ฐฐ์ด๋ก ๋ฐ๊พผ ๋ค์ (3,3) ๋ฐฐ์ด์ ์ธ๋ฑ์ฑํ ์ ์์์ ์ฐพ์ต๋๋ค.๋ฌธ์ ์ ์ผ๋ถ๋ ํํ๊ณผ ๋ชฉ๋ก์ด ๋๋ฑํ๊ฒ ์ทจ๊ธ๋๋ค๋ ์ ์ ๋๋ค. ๊ฒฐ๊ตญ ๋ง์คํฌ๊ฐ ์ด์ค ๋ชฉ๋ก์ธ์ง ํ์ธํ์ฌ ๋ถ์ธ ๋ฐฐ์ด ์ธ๋ฑ์ค๋ฅผ ์ฒ๋ฆฌํ๊ฒ ๋ฉ๋๋ค.
๊ทธ๋ฌ๋ ํ์ฌ๋ก์๋ ์ ์ผํ ํด๊ฒฐ์ฑ ์ด
x[row_mask][:, col_mask]
๋ฅผ) ์ํํ๋ ๊ฒ์ด ๋๋ ต์ต๋๋ค.cc @eric-wieser๋ ์ธ๋ฑ์ฑ ์์ ์ ๋ํด "ํํ์ ๋ชฉ๋ก์ผ๋ก ์ทจ๊ธ"์ ๋ ์ด์ ์ฌ์ฉํ์ง ์๋๋ก ๋ ธ๋ ฅํด ์์ต๋๋ค.
ps ๊ฐ์ฅ ์ง์ฆ๋๋ ์ฐจ์ด์ ์ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.