How to use KBinsDiscretizer to make continuous data into bins in Sklearn?

Okay I was able to solve it. In any case I post the answer if anyone else need this in the future. I used pandas.qcut

target['Temp_class'] = pd.qcut(target['Temeratue'], 10, labels=False)

This has solved my problem.


The mistake in your first attempt is you are giving the output of fit function into transform. .fit() returns the fitted model and not the input data. The correct way would be either of one of the below.

from sklearn.preprocessing import KBinsDiscretizer  
est = KBinsDiscretizer(n_bins=3, encode='ordinal', strategy='uniform')
Xt = est.fit_transform(target) 

or

from sklearn.preprocessing import KBinsDiscretizer  
est = KBinsDiscretizer(n_bins=3, encode='ordinal', strategy='uniform')
est.fit(target)
Xt = est.transform(target)

I was having a similar problem while working with the Titanic dataset. I found that one of my functions had converted my column into a float, and by changing it to an integer, that seemed to help the problem. Also, calling the specific column name with double square brackets worked for me:

from sklearn.preprocessing import KBinsDiscretizer
est = KBinsDiscretizer(n_bins=5, encode='onehot-dense', strategy='uniform')
new = est.fit_transform(dataset[['column_name']])