naren_paste

pipeline_hyper

Sep 9th, 2024
780
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.70 KB | Source Code | 0 0
  1. from sklearn.pipeline import Pipeline
  2. from sklearn.preprocessing import StandardScaler
  3. from sklearn.decomposition import PCA
  4. from sklearn.ensemble import RandomForestClassifier
  5.  
  6. pipeline = Pipeline([
  7.     ('scaler', StandardScaler()),
  8.     ('pca', PCA()),
  9.     ('classifier', RandomForestClassifier())
  10. ])
  11.  
  12.  
  13. from sklearn.model_selection import GridSearchCV
  14.  
  15. param_grid = {
  16.     'pca__n_components': [2, 5, 10],
  17.     'classifier__n_estimators': [50, 100, 200],
  18.     'classifier__max_depth': [None, 10, 20, 30]
  19. }
  20.  
  21. grid_search = GridSearchCV(pipeline, param_grid, cv=5)
  22. grid_search.fit(X_train, y_train)
  23.  
  24. print("Best parameters:", grid_search.best_params_)
  25. print("Best score:", grid_search.best_score_)
  26.  
Advertisement
Add Comment
Please, Sign In to add comment