1

I'm trying to get values from an ndarray with indices in another ndarray but I keep getting this error

IndexError too many indices for array.

The array that I'm trying to get the values from, scores , has scores.shape = (10,10000) and the array pointing out the indices, indices , has indices.shape = (10000,2)

I'm trying to get the values this way:

values = scores[tuple(indices)]

but this is where I get the error.

What I'm trying to do this way is to access several individual values of scores, e.g. scores[0,6], scores[1,9] in another array so I get something like

[scores[0,6],scores[1,9],...] 

all in one go and avoiding loops. Those [[0,6] , [1,9], ...] are stored in the indices array. I mention the previous in case that could lead to a work around.

1 Answer 1

2

Try the following: scores[indices[:,0],indices[:,1]]. Or alternatively, scores[tuple(indices.T)].

When you do scores[tuple(indices)], tuple(indices) is creating a tuple of 2-element arrays. Numpy interprets this as you trying to get 2 elements of a 10,000 dimensional array! For the sort of indexing you need, Numpy expects arrays of values for each dimension. In other words, rather than ( [x1,y1], [x2,y2] ), it wants ( [x1,x2], [y1, y2] ).

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

1 Comment

The last line you wrote down illuminated me so much! Thanks @cge ;)

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.