Module 12 · K nearest neighbors · Lesson 50 of 66
Project: K nearest neighbors
The same workflow on a new mystery data set, on your own.
In this lesson · about 20 minutes
- Scale, split, train and tune a KNN model without step-by-step help
Another anonymized data set: unnamed features and a TARGET CLASS. Since the data is artificial, a pair plot colored by class is the quickest way to get a feel for it. It's big, so give it a few seconds.
Exercise
1. Scale and split
Scale every column except TARGET CLASS with a StandardScaler into scaled_features, then split it and df['TARGET CLASS'] with test_size=0.30 and random_state=101.
Code that runs before yours
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
df = pd.read_csv('KNN_Project_Data.csv')Exercise
2. A first model
Fit a KNN model with n_neighbors=1, save its predictions as pred, and print a classification report.
Code that runs before yours
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
df = pd.read_csv('KNN_Project_Data.csv')
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
scaler = StandardScaler()
scaled_features = scaler.fit_transform(df.drop('TARGET CLASS', axis=1))
X_train, X_test, y_train, y_test = train_test_split(scaled_features, df['TARGET CLASS'],
test_size=0.30, random_state=101)Exercise
3. Tune K
Build error_rate for K from 1 to 39, plot it, and make best_k the K with the lowest error (the first one, if several tie).
Code that runs before yours
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
df = pd.read_csv('KNN_Project_Data.csv')
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
scaler = StandardScaler()
scaled_features = scaler.fit_transform(df.drop('TARGET CLASS', axis=1))
X_train, X_test, y_train, y_test = train_test_split(scaled_features, df['TARGET CLASS'],
test_size=0.30, random_state=101)4. Retrain and compare
Exercises solved: 0 of 3
Noviq Academy's Data Science course is free and for learning. Python runs in your browser with Pyodide, which downloads about 20 MB the first time, plus a few more for scikit-learn. Your code and progress stay on this device. The salaries data is a sample of San Francisco's public pay records; the purchases, customers, housing, advertising and classified data are made up. The Titanic, tips, flights and iris data sets come from Seaborn's examples; the maps use public 2011 to 2014 US and world figures. The 911 data is a 20,000-call sample of Montgomery County, Pennsylvania's public 911 records; the movie ratings are the MovieLens 100K data from GroupLens Research; the reviews are a 3,000-review sample from the Yelp Dataset Challenge; the loans are public LendingClub data; the colleges are the College data from An Introduction to Statistical Learning; the bank notes, kyphosis and breast cancer data come from the UCI Machine Learning Repository. Spark and TensorFlow can't run in a browser, so those lessons use stand-ins that run here, and show the code for the real tools.