Numpy: ๋‹ค์ฐจ์› ๋ถ€์šธ ๋งˆ์Šคํฌ๋ฅผ ์‚ฌ์šฉํ•œ ๋ธŒ๋กœ๋“œ์บ์ŠคํŠธ ์˜ค๋ฅ˜

์— ๋งŒ๋“  2019๋…„ 04์›” 03์ผ  ยท  3์ฝ”๋ฉ˜ํŠธ  ยท  ์ถœ์ฒ˜: numpy/numpy

๋‘ ๊ฐœ์˜ 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,)

Numpy/Python ๋ฒ„์ „ ์ •๋ณด:

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,) 

๋ชจ๋“  3 ๋Œ“๊ธ€

๋ถ€์šธ ๋ฐฐ์—ด์„ ์‚ฌ์šฉํ•˜๋ฉด ์ฝ”๋“œ๋Š” ๋‹จ์ผ ์ฐจ์› ๋˜๋Š” ๋ชจ๋“  ์š”์†Œ๋ฅผ โ€‹โ€‹๋™์‹œ์— ์ธ๋ฑ์‹ฑํ•˜๋ ค๊ณ  ํ•œ๋‹ค๊ณ  ๊ฐ€์ •ํ•ฉ๋‹ˆ๋‹ค. ์„ ํƒ ํ•ญ๋ชฉ์€ ๋ถˆํ–‰ํžˆ๋„ ๋‹จ์ผ 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๋Š” ๋˜ํ•œ ์ตœ์†Œํ•œ ํ˜„์žฌ ์ธ๋ฑ์‹ฑ์— ๋Œ€ํ•ด ์—ฌ๋Ÿฌ ๋ถ€์šธ ์ธ๋ฑ์Šค๊ฐ€ ๋” ์ด์ƒ ์‚ฌ์šฉ๋˜์ง€ ์•Š์•„์•ผ ํ•œ๋‹ค๊ณ  ๋งํ•ฉ๋‹ˆ๋‹ค. ์–ด๋–ค ๊ฒฝ์šฐ๋ผ๋„).

์ด ํŽ˜์ด์ง€๊ฐ€ ๋„์›€์ด ๋˜์—ˆ๋‚˜์š”?
0 / 5 - 0 ๋“ฑ๊ธ‰