我建议使用
numpy.matrix代替
ndarray,无论您有多少行,它都保持2的尺寸:
In [17]: xOut[17]: array([[0, 1, 2], [3, 4, 5], [6, 7, 8]])In [18]: m=np.asmatrix(x)In [19]: m[1]Out[19]: matrix([[3, 4, 5]])In [20]: m[1][0, 1]Out[20]: 4In [21]: x[1][0, 1]---------------------------------------------------------------------------IndexError Traceback (most recent call last)<ipython-input-21-bef99eb03402> in <module>()----> 1 x[1][0, 1]IndexError: too many indices
@askewchan提到的Thx,如果要使用numpy数组算法,请使用
np.atleast_2d:
In [85]: np.atleast_2d(x[1])[0, 1]Out[85]: 4



