Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Oppiskele Grid Search Essentials | Hyperparameter Optimization
Practice
Projects
Quizzes & Challenges
Visat
Challenges
/
Introduction to AutoML
Osio 2. Luku 1
single

single

bookGrid Search Essentials

Pyyhkäise näyttääksesi valikon

Grid Search is a fundamental technique in the field of AutoML for systematically exploring the hyperparameter space of machine learning models.

Its primary purpose is to automate the process of finding the best combination of hyperparameters for a given model, which can significantly improve model performance.

In the context of AutoML, Grid Search helps you avoid manual trial-and-error by evaluating all possible parameter combinations within a specified grid, allowing for a more objective and reproducible approach to hyperparameter tuning.

12345678910111213141516171819202122
from sklearn import datasets from sklearn.model_selection import GridSearchCV from sklearn.svm import SVC # Load the iris dataset iris = datasets.load_iris() X = iris.data y = iris.target # Define the parameter grid for SVM param_grid = { "C": [0.1, 1, 10], "kernel": ["linear", "rbf"], "gamma": [0.01, 0.1, 1] } # Create a GridSearchCV object with SVC grid_search = GridSearchCV(SVC(), param_grid, cv=3) grid_search.fit(X, y) print("Best parameters found:", grid_search.best_params_) print("Best cross-validation score:", grid_search.best_score_)
copy
Note
Note

Grid Search evaluates all combinations of hyperparameters, which can become computationally expensive as the number of parameters and their possible values increases. It guarantees finding the best combination but requires exponentially more time with larger grids. While thorough, it may not be practical for large parameter spaces or limited resources. Alternatives like Random Search or Bayesian Optimization can be more efficient.

Tehtävä

Swipe to start coding

You need to tune a K-Nearest Neighbors (KNN) classifier using GridSearchCV to find the best combination of hyperparameters.

  1. Load the Iris dataset.
  2. Split the data into training and test sets.
  3. Use GridSearchCV to search for the best hyperparameters for KNeighborsClassifier.
  4. Use the following parameter grid:
    {
      "n_neighbors": [3, 5, 7], 
      "weights": ["uniform", "distance"]
    }
    
  5. Fit the grid search on the training data.
  6. Assign the best parameters to best_params and the best cross-validation score to best_score.
  7. Print both values.

Ratkaisu

Switch to desktopVaihda työpöytään todellista harjoitusta vartenJatka siitä, missä olet käyttämällä jotakin alla olevista vaihtoehdoista
Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 2. Luku 1
single

single

Kysy tekoälyä

expand

Kysy tekoälyä

ChatGPT

Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme

some-alt