2

How I can access to the element in this array or matrix

array([[-0.0359075 ,  0.09684904, -0.03384908,  0.11583249, -0.06620416,
         0.31124461,  0.2244373 , -0.22074385,  0.2731958 ,  0.35207385,
        -0.0232635 ,  0.01991997, -0.14457113, -0.22119096, -0.23231329,
        -0.25554115,  0.20723027,  0.21642838,  0.17261602, -0.14479494,
        -0.02729147,  0.28598186, -0.14462787, -0.06030619,  0.10610376,
         0.04492712, -0.03452296, -0.079672  , -0.13708481, -0.04986167,
        -0.25361556, -0.03039704]], dtype=float32)

If this matrix is r then when I r[4] to access forth element I received

IndexError: index 1 is out of bounds for axis 0 with size 1 

when I tried to access using this command r(4) I received

TypeError: 'numpy.ndarray' object is not callable 
2
  • Use [4], ( ) is the function call operator in Python (as it is in many languages). Commented Mar 25, 2016 at 9:18
  • 1
    You should use r[0, 4] Commented Mar 25, 2016 at 9:19

1 Answer 1

2

You've got an index error because you have vector but not array:

r = np.array([[-0.0359075 ,  0.09684904, -0.03384908,  0.11583249, -0.06620416,
         0.31124461,  0.2244373 , -0.22074385,  0.2731958 ,  0.35207385,
        -0.0232635 ,  0.01991997, -0.14457113, -0.22119096, -0.23231329,
        -0.25554115,  0.20723027,  0.21642838,  0.17261602, -0.14479494,
        -0.02729147,  0.28598186, -0.14462787, -0.06030619,  0.10610376,
         0.04492712, -0.03452296, -0.079672  , -0.13708481, -0.04986167,
        -0.25361556, -0.03039704]], dtype=np.float32)

In [57]: r.shape
Out[57]: (1, 32)

To get 4-th elemen you need to call 3 for 2nd axis, because indices started from 0:

In [58]: r[0,3]
Out[58]: 0.11583249  

Or you could use reshape to make array from your vector:

In [65]: r.reshape(r.size, 1)[3]
Out[65]: array([ 0.11583249], dtype=float32) 

Note: You could find a lot of useful information about indexing from docs

Sign up to request clarification or add additional context in comments.

1 Comment

It may be helpful to say that in python r[0, 3] actually means r[(0, 3)]. (edit: but that's mentioned in the docs, you pointed to.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.