def _recursive_category_gen(col, num_bins):
"""
Generate number of bins recursively
Parameters
----------
col : string
the name of the column in the dataframe with the continuous variable
num_bins : int
how many quantiles
Returns
-------
num_bins : int
categories : list
"""
bin_labels = range(num_bins)
# base case catch
if num_bins == 0:
raise ValueError('Unable to perform qcut to 0 bins.')
# we assume the num_bins count will work
try:
categories = pd.qcut(x=col, q=num_bins, labels=bin_labels)
return num_bins, categories
# if it does not, then we need to go down 1 number of bins
except ValueError:
new_bin_count = num_bins - 1
return _recursive_category_gen(col, new_bin_count)
评论列表
文章目录