Marwik
Marwik

Reputation: 39

UserWarning: X has feature names, but StandardScaler was fitted without feature names

I have this error message but I don't know what it means and what I can do to resolve it.

This is the first part of my function:

X = df.drop(['Position'], axis = 1)                                                                     
y = df['Position']                                                                                      
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=1234)

pipelines = {                                                                                           
        'lr':make_pipeline(StandardScaler(), LogisticRegression()),
        'rc':make_pipeline(StandardScaler(), RidgeClassifier()),
        'rf':make_pipeline(StandardScaler(), RandomForestClassifier()),
        'gb':make_pipeline(StandardScaler(), GradientBoostingClassifier()),
                 }

Thanks to anyone that can help!

Upvotes: 3

Views: 4203

Answers (1)

thehappycheese
thehappycheese

Reputation: 353

The following code will trigger the warning

scaler = StandardScaler().fit(some_dataframe["some_column"].values.reshape(-1,1))
scaled_column = scaler.transform(some_dataframe[["some_column"]])

The reason is that

  • .fit() was called on a a numpy array: some_dataframe["some_column"].values.reshape(-1,1) where pandas row & column labels are removed
  • but .transform() was called on a dataframe: some_dataframe[["some_column"]] which keeps pandas row & column labels

The following code does not trigger the warning:

scaler = StandardScaler().fit(some_dataframe[["some_column"]])
scaled_column = scaler.transform(some_dataframe[["some_column"]])

Note the double square brackets [["some_column"]]; when indexing a single column, you can still get a dataframe by placing a list of one column name into the indexer.

Your question does not provide sufficient context to suggest a fix in your case. Hopefully the above is enough to solve your problem. Please consider updating your question if you got rid of the warning since google directed me here with the same problem and it would have been useful.

Upvotes: 2

Related Questions