我想根据一些列索引和行索引选择一个子矩阵。
我收到一个奇怪的错误。我能够根据列索引的行索引切片矩阵,但不能同时根据两者的切片。
我该如何解决?
>>> X.shape
(1000, 30)
>>> type(X)
<class 'numpy.ndarray'>
>>> X
array([[ 0.06349252, -0.19222932, -0.51720414, ..., 0.17566853,
0.15821072, 0.0478738 ],
[ 0.88497758, 0.22215627, 1.63248497, ..., 0.77716638,
0.76535743, 0.11670681],
[ 0.13308973, -0.12106689, -0.51353645, ..., 1.32546684,
0.8276816 , 1.25001549],
...,
[-0.25907157, -0.24458445, -0.87298188, ..., 0.6467455 ,
0.43216921, 0.57972136],
[ 1.23272918, 0.14475037, 0.16869452, ..., 0.27710557,
-1.39863587, -0.10482702],
[-0.57754589, 0.77061869, 1.88473625, ..., 0.31680682,
1.64699058, 0.92152533]])
>>> j = np.random.choice(10, 5)
>>> i = np.random.choice(10,1000)
>>> X[i, :]
array([[-0.90775982, 0.82286474, -0.94136182, ..., 1.11494763,
0.04252439, 1.08999938],
[-2.51998203, -0.47154878, -0.88228892, ..., -0.03526119,
0.40444398, 0.27545503],
[-0.90775982, 0.82286474, -0.94136182, ..., 1.11494763,
0.04252439, 1.08999938],
...,
[ 0.29236619, -1.53595325, 0.77567467, ..., 0.45090184,
1.49180382, 1.04571078],
[ 0.13308973, -0.12106689, -0.51353645, ..., 1.32546684,
0.8276816 , 1.25001549],
[ 0.57790133, -1.11712824, -0.47716697, ..., 0.27169274,
-0.84223531, -0.99293644]])
>>> X[:, j]
array([[-0.51720414, 0.60436212, 0.54243319, 0.06349252, -0.19222932],
[ 1.63248497, -0.75034999, -0.41102324, 0.88497758, 0.22215627],
[-0.51353645, 0.74373642, -0.76499708, 0.13308973, -0.12106689],
...,
[-0.87298188, -0.14638175, 0.0278893 , -0.25907157, -0.24458445],
[ 0.16869452, -0.42747292, 0.49202016, 1.23272918, 0.14475037],
[ 1.88473625, -0.21566782, -0.52799588, -0.57754589, 0.77061869]])
>>> X[i, j]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: shape mismatch: objects cannot be broadcast to a single shape
>>>
答案 0 :(得分:2)
我认为您必须单独编制索引:
X[i, :][:, j]
问题在于NumPy尝试将i
中的每个行索引与j
中的相应列索引匹配,但它们的长度不同。
例如,X[(1, 2), (3, 4)]
会选择元素X[1, 3]
和X[2, 4]
,但X[(1, 2), (3, 4, 5)]
将不匹配。