Is get_feature_names_out from scikit-learn SimpleImputer working?

1.2k Views Asked by At

I'd like to access to the name of the columns that were imputed by scikit-learn SimpleImputer and create a DataFrame. According to documentation, it should be possible with function get_feature_names_out.

However, when I try the following example (taken from here) it raises an error.

import pandas as pd
data = pd.DataFrame({'col1': [1,np.nan,2], 'col2': [3,4,5]})

from sklearn.impute import SimpleImputer
si = SimpleImputer()
pd.DataFrame(si.fit_transform(data),
             columns = si.get_feature_names_out())

The error is:

AttributeError: 'SimpleImputer' object has no attribute 'get_feature_names_out'

Then, is there something I'm missing?

Edit:

My current scikit-learn version is: 1.0.2.

1

There are 1 best solutions below

0
On

get_feature_names_out became generally available in 1.1. You'll need to upgrade to get this method:

pip install scikit-learn==1.1.0
# or for latest version:
pip install --upgrade scikit-learn

See also: the release notes for scikit-learn 1.1.


Workaround for older versions. SimpleImputer does not change column names, so you can reuse them:

df = pd.DataFrame(si.fit_transform(data), columns=data.columns)