Get startedGet started for free

KNN classification

In this exercise you'll explore a subset of the Large Movie Review Dataset. The variables X_train, X_test, y_train, and y_test are already loaded into the environment. The X variables contain features based on the words in the movie reviews, and the y variables contain labels for whether the review sentiment is positive (+1) or negative (-1).

This course touches on a lot of concepts you may have forgotten, so if you ever need a quick refresher, download the scikit-learn Cheat Sheet and keep it handy!

This exercise is part of the course

Linear Classifiers in Python

View Course

Exercise instructions

  • Create a KNN model with default hyperparameters.
  • Fit the model.
  • Print out the prediction for the test example 0.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

from sklearn.neighbors import KNeighborsClassifier

# Create and fit the model
knn = ____
knn.____

# Predict on the test features, print the results
pred = knn.____[0]
print("Prediction for test example 0:", pred)
Edit and Run Code