Yes, the GridSearchCV
model in scikit-learn can be used to tune hyperparameters for various other machine learning models, not just the Support Vector Classifier (SVC). GridSearchCV
is a versatile tool that can be applied to any scikit-learn estimator, provided you specify the appropriate estimator as an argument when creating the GridSearchCV
object.
from sklearn.ensemble import RandomForestClassifier from sklearn.model_selection import GridSearchCV # Define the parameter grid for the Random Forest Classifier param_grid = { 'n_estimators': [10, 50, 100], 'max_depth': [None, 10, 20], 'min_samples_split': [2, 5, 10], 'min_samples_leaf': [1, 2, 4] } # Create a GridSearchCV object with the Random Forest Classifier grid_rf = GridSearchCV(RandomForestClassifier(), param_grid, cv=5) # Fit the grid search to your training data grid_rf.fit(X_train, y_train) # Access the best model and best hyperparameters best_rf_model = grid_rf.best_estimator_ best_rf_params = grid_rf.best_params_ # Evaluate the best model on your test data y_pred = best_rf_model.predict(X_test)
Comments