问题
I would like to convert my continuous predictor variable (stock returns) into a categorical variable (5 bins, either 5 bins with equal number of stocks OR absolute thresholds, say -30% to -20%, then -19% to -10% etc.)
Is there some Python package that does this? scikit learn etc?
回答1:
It seems like what you are looking for is either pandas.cut for absolute thresholds or pandas.qcut for equal sized bins.
回答2:
numpy.histogram is also a useful option for discretizing stuff
来源:https://stackoverflow.com/questions/52433738/create-multiple-classes-from-continuous-variables-python