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

I want to select some of the elements of a list using the following code:

random_indices = np.random.permutation(len(Y))[:how_many_to_select]

But I am getting the error: TypeError: slice indices must be integers or None or have an __index__ method

1 Answer

+2 votes
by (349k points)
selected by
 
Best answer

Your variable 'how_many_to_select' is not an integer. If it's a float, change its type to int and then you will not get the error. You can try one of the following approaches:

Approach 1

random_indices = np.random.permutation(len(Y))[:int(how_many_to_select)]

Approach 2

how_many_to_select = int(how_many_to_select)

random_indices = np.random.permutation(len(Y))[:how_many_to_select]


...