+3 votes
in Programming Languages by (40.5k points)

I am trying to change the labels of records by randomly selecing some of them. But the random.sample() function gives the error - "TypeError: can't multiply sequence by non-int of type 'numpy.float64'". How can I fix the error?

n = round(np.sum(Y) * fraction)

pos_of_1 = list(np.where(Y == 1)[0])  

random_indices = random.sample(pos_of_1, n)  

Y[random_indices] = 0   

1 Answer

+1 vote
by (353k points)
selected by
Best answer

It seems that round(np.sum(Y) * fraction) is returning a float value. In the random.sample(), the value of n should be an integer. If n is a float value, it will throw the error. Make the following change and it should fix the error.


n = round(np.sum(Y) * fraction)


n = int(round(np.sum(Y) * fraction))