1
$\begingroup$

According to SciKit Learn's documentation for sklearn.preprocessing.KBinsDiscretizer, the quantile strategy is suppose the ensure that

All bins in each feature have the same number of points

However, the following piece of code shows that despite the use of the quantile strategy, the resulting bins contain widely differing numbers of points.

Why is that? How can I change the code so that the number of points in each bin will be the same?

import pandas as pd
from seaborn import load_dataset
from sklearn.preprocessing import KBinsDiscretizer

tips = load_dataset('tips')

dsc = KBinsDiscretizer(
        n_bins = 9,
        encode = 'ordinal',
        strategy = 'quantile')

tips_dsc = pd.DataFrame(
            data = dsc.fit_transform(tips[['tip']]),
            columns = ['tip_dsc'])

tips_dsc.value_counts()

Output:

tip_ord_q
5.0          38
2.0          36
7.0          28
8.0          28
0.0          27
3.0          27
6.0          27
1.0          18
4.0          15
dtype: int64
$\endgroup$

1 Answer 1

2
$\begingroup$

You have non unique values, so that the quantile fall in the middle of a bucket of value. For exemple: 0,1,1 and you ask two splits... you'll get different counts in the bins.

Alternatively if you change the values to unique values you'll get even splits:

tips.tip = np.arange(len(tips))
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.