Home > Net >  Recovering features names of StandardScaler().fit_transform() with sklearn
Recovering features names of StandardScaler().fit_transform() with sklearn

Time:03-18

Edited from enter image description here

If I comment out these two lines:

scaler = StandardScaler()
X = scaler.fit_transform(X)

I get the output:

enter image description here

How could I use scaler.fit_transform() for X and get a feature importance plot with the original feature names?

CodePudding user response:

The reason behind this is that StandardScaler returns a numpy.ndarray of your feature values (same shape as pandas.DataFrame.values, but not normalized) and you need to convert it back to pandas.DataFrame with the same column names.

Here's the part of your code that needs changing.

scaler = StandardScaler()
X = pd.DataFrame(scaler.fit_transform(X), columns=X.columns)
  • Related