"indexerror: Too Many Indices" In Numpy Python
I know many people asked this question, but I could not get an appropriate answer that can solve my problem. I have an array X:: X= [1. 2. -10.] Now I am trying to make a
Solution 1:
I suggest using numpy.matrix
instead of ndarray
, it keeps a dimension of 2 regardless of how many rows you have:
In [17]: x
Out[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]: 4
In [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
Thx for @askewchan mentioning, if you want to use the numpy array arithmetic, use np.atleast_2d
:
In[85]: np.atleast_2d(x[1])[0, 1]Out[85]: 4
Post a Comment for ""indexerror: Too Many Indices" In Numpy Python"