Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Challenge: Tuning Hyperparameters with RandomizedSearchCV | Section
Machine Learning Foundations with Scikit-Learn
Seksjon 1. Kapittel 30
single

single

bookChallenge: Tuning Hyperparameters with RandomizedSearchCV

Sveip for å vise menyen

RandomizedSearchCV works like GridSearchCV, but instead of checking every hyperparameter combination, it evaluates a random subset. In the example below, the grid contains 100 combinations. GridSearchCV tests all of them, while RandomizedSearchCV can sample, for example, 20 — controlled by n_iter. This makes tuning faster, while usually finding a score close to the best.

Oppgave

Sveip for å begynne å kode

You have a preprocessed penguin dataset. Tune a KNeighborsClassifier using both search methods:

  1. Create param_grid with values for n_neighbors, weights, and p.
  2. Initialize RandomizedSearchCV(..., n_iter=20).
  3. Initialize GridSearchCV with the same grid.
  4. Fit both searches on X, y.
  5. Print the grid search’s .best_estimator_.
  6. Print the randomized search’s .best_score_.

Løsning

Note
Note

Try running the code multiple times. RandomizedSearchCV may match the grid search score when it randomly samples the best hyperparameters.

Switch to desktopBytt til skrivebordet for virkelighetspraksisFortsett der du er med et av alternativene nedenfor
Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 1. Kapittel 30
single

single

Spør AI

expand

Spør AI

ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

some-alt