Exercise 4 question 3

Exercise 4 question 3

by Martin Pierre Albert Cibils -
Number of replies: 2

Hello,


I was looking at the correction of the exercise 4 question 3 and even after looking at the documentation I still can't get my head around these two lines :

    indices = accumulator_array.ravel().argsort()[-top_k:]

    top_k_points = list((np.unravel_index(i, accumulator_array.shape) for i in indices))


I understand what they do but I don't understand how: why is there a minus sign in front of the top_k? What exactly does the function np.unravel_index() do?


Have a great sunday!


Martin

In reply to Martin Pierre Albert Cibils

Re: Exercise 4 question 3

by Semih Günel -

Hi Martin,

The code you pointed out selects the points with the highest accumulation form the accumulator array.

Minus sign is to select from the end of the list. So for example if you have a list L and do L[-1], you would select the last item. Similarly L[-5:] selects the last 5 items.

Since we find the top_k_points from a flattened array, we actually don't know where they were supposed to be in the 2D accumulator array (ravel does the flattening). So lets say we see the top_1 point is at index X at the flattened array, then, we can look at np.unravel_index(X, shape) to find where this point should be on the 2D matrix. So it unflattens the indices (2d locations) of the matrix. 

Best,

Semih