Home > Back-end >  'PCA' object has no attribute 'predict'
'PCA' object has no attribute 'predict'

Time:05-20

I have an issue with my code:

PCA runs fine, but when I combine it with grid search I get this error:

error error

This is my code:

pca = PCA()
param_grid = {'n_components': range(1,50)}
GridSearchCV(pca, param_grid, cv=5 ,scoring='f1', 
return_train_score=True, n_jobs=-1, 
error_score="raise").fit(prepared_data_train, y_train)

CodePudding user response:

checkout this:

remove scoring as F1 does not make sense for PCA by default.

Also, make sure to normalize your data before performing PCA; the reason is explained here.

pca = PCA()

scaler = StandardScaler()


pipe = Pipeline(steps=[("scaler", scaler), ("pca", pca)])

param_grid = {'pca__n_components': range(1,50)}

search = GridSearchCV(pipe, param_grid, cv=5 , return_train_score=True, n_jobs=-1, error_score="raise").fit(X_digits, y_digits)

print("Best parameter (CV score=%0.3f):" % search.best_score_)
print(search.best_params_)

here is a good example from documentation for further exploration.

  • Related