I am discretizing my series for a learner. I really need the series to be in float, and I really need to avoid for loops.
How do I convert this series from float to int?
Here is my function that is currently failing:
def discretize_series(s,count,normalized=True):
def discretize(value,bucket_size):
return value % bucket_size
if normalized:
maximum = 1.0
else:
minimum = np.min(s)
s = s[:] - minimum
maximum = np.max(s)
bucket_size = maximum / float(count)
Here is the line that causes the function to fail:
s = int((s[:] - s[:] % bucket_size)/bucket_size)
The int() induces a casting error: I am unable to cast the pandas series as an int series.
return s
If I remove the int(), the function works, so I may just see if I can get it to work anyway.
pandasdoes something fishy, you might not be needing those[:]there.normalized==True? You might have to sets=s/np.max(s)in that case. And you can still have trouble ifnp.max(s)<0. Is that possible?normalized==Truecase, if your input series has a maximum of, say, 10? For instance, for acountof 2, you'd have abucket_sizeof0.5. But then for the maximal value ofsyou'd have(10 - 10%0.5)/0.5==20, much more than 2. I would expect that you have to do the same shifting to 0, but you also have to divide by the maximum.