ここで何が間違っているのかを理解しようとして、私は夢中になっています。
私は NumPy を使用しており、選択したい特定の行インデックスと特定の列インデックスがあります。これが私の問題の要点です:
import numpy as np
a = np.arange(20).reshape((5,4))
# array([[ 0, 1, 2, 3],
# [ 4, 5, 6, 7],
# [ 8, 9, 10, 11],
# [12, 13, 14, 15],
# [16, 17, 18, 19]])
# If I select certain rows, it works
print a[[0, 1, 3], :]
# array([[ 0, 1, 2, 3],
# [ 4, 5, 6, 7],
# [12, 13, 14, 15]])
# If I select certain rows and a single column, it works
print a[[0, 1, 3], 2]
# array([ 2, 6, 14])
# But if I select certain rows AND certain columns, it fails
print a[[0,1,3], [0,2]]
# Traceback (most recent call last):
# File "<stdin>", line 1, in <module>
# ValueError: shape mismatch: objects cannot be broadcast to a single shape
なぜこうなった?1行目、2行目、4行目、1列目、3列目は選択できるのでしょうか?私が期待している結果は次のとおりです。
a[[0,1,3], [0,2]] => [[0, 2],
[4, 6],
[12, 14]]