Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Challenge: Evaluating the Model with Cross-Validation | Modeling
ML Introduction with scikit-learn

bookChallenge: Evaluating the Model with Cross-Validation

In this challenge, build and evaluate a model using both the train-test split and cross-validation on the preprocessed penguins dataset.

The following functions will be useful:

  • cross_val_score() from sklearn.model_selection;
  • train_test_split() from sklearn.model_selection;
  • .fit() and .score() methods of the model.
Task

Swipe to start coding

You are given a preprocessed version of the penguin dataset, where the feature matrix X and the target variable y are ready for modeling. Your goal is to train and evaluate a KNeighborsClassifier model using both cross-validation and a train-test split.

  1. Initialize a KNeighborsClassifier object with n_neighbors=4.
  2. Use the cross_val_score() function with cv=3 to calculate cross-validation scores for the model.
  3. Split the data into training and test sets using the train_test_split() function.
  4. Fit the model on the training set using the .fit() method.
  5. Evaluate the model on the test set using the .score() method and print the result.

Solution

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 4. ChapterΒ 5
single

single

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Suggested prompts:

What is the difference between train-test split and cross-validation?

Can you explain how to use cross_val_score() with the penguins dataset?

How do I choose which evaluation method to use?

close

Awesome!

Completion rate improved to 3.13

bookChallenge: Evaluating the Model with Cross-Validation

Swipe to show menu

In this challenge, build and evaluate a model using both the train-test split and cross-validation on the preprocessed penguins dataset.

The following functions will be useful:

  • cross_val_score() from sklearn.model_selection;
  • train_test_split() from sklearn.model_selection;
  • .fit() and .score() methods of the model.
Task

Swipe to start coding

You are given a preprocessed version of the penguin dataset, where the feature matrix X and the target variable y are ready for modeling. Your goal is to train and evaluate a KNeighborsClassifier model using both cross-validation and a train-test split.

  1. Initialize a KNeighborsClassifier object with n_neighbors=4.
  2. Use the cross_val_score() function with cv=3 to calculate cross-validation scores for the model.
  3. Split the data into training and test sets using the train_test_split() function.
  4. Fit the model on the training set using the .fit() method.
  5. Evaluate the model on the test set using the .score() method and print the result.

Solution

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 4. ChapterΒ 5
single

single

some-alt