我有一个矩阵a,创建方式如下:
>>> a = np.matrix("1 2 3; 4 5 6; 7 8 9; 10 11 12")
我有一个矩阵标签,是这样创建的:
>>> labels = np.matrix("1;0;1;1")
这是两个矩阵的样子:
>>> a
matrix([[ 1, 2, 3],
[ 4, 5, 6],
[ 7, 8, 9],
[10, 11, 12]])
>>> labels
matrix([[1],
[0],
[1],
[1]])
您可以看到,当我选择所有列时,没有问题。
>>> a[labels == 1, :]
matrix([[ 1, 7, 10]])
但当我尝试指定一列时,出现了错误。
>>> a[labels == 1, 1]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/site-packages/numpy/matrixlib/defmatrix.py", line 305, in __getitem__
out = N.ndarray.__getitem__(self, index)
IndexError: too many indices for array
>>>
有人知道这是为什么吗? 我知道已经有类似的问题,但是没有一个能够很好地解释我的问题,也没有一个答案对我有帮助。