Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
GridSearchCV | Modeling
ML Introduction with scikit-learn

GridSearchCVGridSearchCV

Now it is time to try improving the model's performance!
This is done by finding the best hyperparameters fitting our task.
This process is called Hyperparameter Tuning. The default approach is to try different hyperparameter values and calculate a cross-validation score for them. Then just choose the value that results in the best score.

This process can be done using the GridSearchCV class of the sklearn.model_selection module.

While constructing a GridSearchCV object, we need to pass the model and the parameters grid (and optionally scoring and the number of folds).
The parameters grid (param_grid) is a dictionary containing all the hyperparameters configurations we want to try.
For example, param_grid={'n_neighbors': [1, 3, 5, 7]} will try values 1, 3, 5, and 7 as the number of neighbors.
Then we need to train it using the .fit(X, y).
After that, we can access the model with the best parameters using the .best_estimator_ attribute and its cross-validation score using the .best_score_ attribute.

Let's try it out!

The next step would be to take the best_estimator_ and train it on the whole dataset since we already know it has the best parameters (out of those we tried), and we know its score.
This step is so obvious that GridSearchCV does it by default.
So the object (grid_search in our example) becomes a trained model with the best parameters.
We can now use this object for predicting or evaluating.
That's why GridSearchCV has .predict() and .score() methods.

Once you trained a GridSearchCV object, you can use it to make predictions using the .predict() method. Is it correct?

Selecione a resposta correta

Tudo estava claro?

Seção 4. Capítulo 6
course content

Conteúdo do Curso

ML Introduction with scikit-learn

GridSearchCVGridSearchCV

Now it is time to try improving the model's performance!
This is done by finding the best hyperparameters fitting our task.
This process is called Hyperparameter Tuning. The default approach is to try different hyperparameter values and calculate a cross-validation score for them. Then just choose the value that results in the best score.

This process can be done using the GridSearchCV class of the sklearn.model_selection module.

While constructing a GridSearchCV object, we need to pass the model and the parameters grid (and optionally scoring and the number of folds).
The parameters grid (param_grid) is a dictionary containing all the hyperparameters configurations we want to try.
For example, param_grid={'n_neighbors': [1, 3, 5, 7]} will try values 1, 3, 5, and 7 as the number of neighbors.
Then we need to train it using the .fit(X, y).
After that, we can access the model with the best parameters using the .best_estimator_ attribute and its cross-validation score using the .best_score_ attribute.

Let's try it out!

The next step would be to take the best_estimator_ and train it on the whole dataset since we already know it has the best parameters (out of those we tried), and we know its score.
This step is so obvious that GridSearchCV does it by default.
So the object (grid_search in our example) becomes a trained model with the best parameters.
We can now use this object for predicting or evaluating.
That's why GridSearchCV has .predict() and .score() methods.

Once you trained a GridSearchCV object, you can use it to make predictions using the .predict() method. Is it correct?

Selecione a resposta correta

Tudo estava claro?

Seção 4. Capítulo 6
some-alt