Recap: Wine Neighbors
Part of the Supervised Learning section of Coddy's Machine Learning journey. Lesson 10 of 58.
Challenge
EasyDraw a map like this chapter's first, for wine. The starter splits 178 wines (two features, cultivars 0 to 2), lists ks and labels the chart.
- Scale both sets, fit on training wines.
- Print the k with the best test accuracy (for now: chapter 7 has a fairer judge).
- With that k, fill the map: grid
xx,yy, 100 by 100, -3 to 3 both ways, answers inzz. - Scatter the scaled training wines on top, labelled from
names; add a legend.
From alcohol and flavanoids:
Best k: 13 (test accuracy 0.956)Coddy describes your map.
Try it yourself
import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import load_wine
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
from sklearn.preprocessing import StandardScaler
# 178 wines from three cultivars, classes 0, 1, 2
wine = load_wine()
# columns 6 and 12: flavanoids and proline
X = wine.data[:, [6, 12]]
y = wine.target
names = ["cultivar A", "cultivar B", "cultivar C"]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=0)
ks = range(1, 32, 2) # k = 1, 3, 5, ..., 31
fig, ax = plt.subplots()
ax.set_title("Wine Neighbors")
ax.set_xlabel("flavanoids (scaled)")
ax.set_ylabel("proline (scaled)")
# Write your code below
All lessons in Supervised Learning
1Your First Model
Learning from ExamplesTrain and Test SetsFit and PredictScoring a ModelRecap: Seed Sorter2Nearest Neighbors
How k-NN DecidesChoosing kDecision BoundariesScaling for NeighborsRecap: Wine Neighbors5Measuring Classifiers
The Confusion MatrixPrecision and RecallThe F1 ScoreImbalanced ClassesROC CurvesRecap: Screening Test11Project: Penguin Species
Meet the PenguinsPrepare the DataChoose a ModelTune the WinnerShip the ModelPractice on your own: Online Python compiler