+2 votes
in Programming Languages by (7k points)

I have the following code which is giving error "TypeError: 'numpy.float64' object cannot be interpreted as an integer". The error is thrown by the statement "np.random.choice(pos_of_1, how_many_to_change)"

how_many_to_change = np.sum(Y)/2  #half of class 1 needs to be changed
pos_of_1 = np.where(Y==1)[0]  #np.where gives tuple. 1st elemetn is an np array
print (len(pos_of_1))
random_indices = np.random.choice(pos_of_1, how_many_to_change)  #randomly select half of them

1 Answer

0 votes
by (13.5k points)

Print the value of how_many_to_change. It is a float value and hence you are getting the error. Make the following change in your code and it should work.

Change 

how_many_to_change = np.sum(Y)/2

to

how_many_to_change = int(np.sum(Y)/2)

...