The python package sklearn.grid_search provides the GridSearchCV class which implements a search through parameter space for an estimator. This class allows us to define a grid of hyperparameters and then tries to find the optimal combination of these hyperparameters that best fits the data. This is useful for exploring large sets of hyperparameters when the default values are not good enough.
Code examples:
Example 1: In this example, we define a parameter grid for the Support Vector Machine estimator and then use the GridSearchCV class to find the optimal set of hyperparameters.
from sklearn import svm from sklearn.model_selection import GridSearchCV
# Create the GridSearchCV object grid_search = GridSearchCV(estimator, param_grid=param_grid, cv=5)
# Fit the GridSearchCV object to the data grid_search.fit(X, y)
# Print the best parameters print(grid_search.best_params_)
# Determine package library: The package is scikit-learn or sklearn.
Example 2: In this example, we define a parameter grid for the Random Forest estimator and then use the GridSearchCV class to find the optimal set of hyperparameters.
from sklearn.ensemble import RandomForestClassifier from sklearn.model_selection import GridSearchCV
# Create the Random Forest estimator estimator = RandomForestClassifier()
# Create the GridSearchCV object grid_search = GridSearchCV(estimator, param_grid=param_grid, cv=5)
# Fit the GridSearchCV object to the data grid_search.fit(X, y)
# Print the best parameters print(grid_search.best_params_)
# Determine package library: The package is scikit-learn or sklearn.
Python GridSearchCV.get_params - 32 examples found. These are the top rated real world Python examples of sklearn.grid_search.GridSearchCV.get_params extracted from open source projects. You can rate examples to help us improve the quality of examples.