Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Вивчайте Challenge: Choosing the Best K Value | k-NN Classifier
Classification with Python

Свайпніть щоб показати меню

book
Challenge: Choosing the Best K Value

As shown in the previous chapters, the model's predictions can vary depending on the value of k (the number of neighbors). When building a k-NN model, it's important to choose the k value that gives the best performance.

A common approach is to use cross-validation to evaluate model performance. You can run a loop and calculate cross-validation scores for a range of k values, then select the one with the highest score. This is the most widely used method.

To perform this, sklearn offers a convenient tool: the GridSearchCV class.

The param_grid parameter takes a dictionary where the keys are parameter names and the values are lists of options to try. For example, to test values from 1 to 99 for n_neighbors, you can write:

python

Calling the .fit(X, y) method on the GridSearchCV object will search through the parameter grid to find the best parameters and then re-train the model on the entire dataset using those best parameters.

You can access the best score using the .best_score_ attribute and make predictions with the optimized model using the .predict() method. Similarly, you can retrieve the best model itself using the .best_estimator_ attribute.

Завдання

Swipe to start coding

You are given the Star Wars ratings dataset stored as a DataFrame in the df variable.

  • Initialize param_grid as a dictionary containing the n_neighbors parameter with the values [3, 9, 18, 27].
  • Create a GridSearchCV object using param_grid with 4-fold cross-validation, train it, and store it in the grid_search variable.
  • Retrieve the best model from grid_search and store it in the best_model variable.
  • Retrieve the score of the best model and store it in the best_score variable.

Рішення

Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 1. Розділ 7
single

single

Запитати АІ

expand

Запитати АІ

ChatGPT

Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат

close

Awesome!

Completion rate improved to 4.17

book
Challenge: Choosing the Best K Value

As shown in the previous chapters, the model's predictions can vary depending on the value of k (the number of neighbors). When building a k-NN model, it's important to choose the k value that gives the best performance.

A common approach is to use cross-validation to evaluate model performance. You can run a loop and calculate cross-validation scores for a range of k values, then select the one with the highest score. This is the most widely used method.

To perform this, sklearn offers a convenient tool: the GridSearchCV class.

The param_grid parameter takes a dictionary where the keys are parameter names and the values are lists of options to try. For example, to test values from 1 to 99 for n_neighbors, you can write:

python

Calling the .fit(X, y) method on the GridSearchCV object will search through the parameter grid to find the best parameters and then re-train the model on the entire dataset using those best parameters.

You can access the best score using the .best_score_ attribute and make predictions with the optimized model using the .predict() method. Similarly, you can retrieve the best model itself using the .best_estimator_ attribute.

Завдання

Swipe to start coding

You are given the Star Wars ratings dataset stored as a DataFrame in the df variable.

  • Initialize param_grid as a dictionary containing the n_neighbors parameter with the values [3, 9, 18, 27].
  • Create a GridSearchCV object using param_grid with 4-fold cross-validation, train it, and store it in the grid_search variable.
  • Retrieve the best model from grid_search and store it in the best_model variable.
  • Retrieve the score of the best model and store it in the best_score variable.

Рішення

Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

close

Awesome!

Completion rate improved to 4.17

Свайпніть щоб показати меню

some-alt