+2 votes
in Programming Languages by (17.0k points)
edited by

I am using the percentile() function of Numpy in the following code, but it gives an error: "IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices"

th_0 = np.percentile(probs_0[idx_0], frac)

In the above code, probs_0 is a Numpy array, idx_0 is a set of indices, and frac is 50.

What is wrong this code?

1 Answer

0 votes
by (28.9k points)

As it is obvious from the error message that the data type of idx_0 is wrong. You cannot use idx_0 as a set; you need to change it to a list or NumPy array.

Convert set to a list as follows and it will fix the error.

th_0 = np.percentile(probs_0[list(idx_0)], frac)