Module 14 · Support vector machines · Lesson 54 of 66

Project: iris flowers

Classify three iris species from four measurements, and tune the model.

In this lesson · about 20 minutes

  • Explore the iris data
  • Train, evaluate and grid-search an SVC on your own

The iris data set, from 1936, is one of the most famous in statistics: 150 flowers from three species (setosa, versicolor, virginica), each with four measurements in centimeters: sepal length and width, petal length and width. (Sepals are the green leaf-like parts under the petals.)

Exercise

1. Which species stands apart?

Make a pair plot of iris colored by species with the 'Dark2' palette. Which species looks easiest to separate?

Code that runs before yours
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
iris = sns.load_dataset('iris')

Exercise

2. Setosa up close

Make setosa, the rows where species is 'setosa'. Draw a filled 2D density plot of its sepal_width (x) against sepal_length (y) with the 'plasma' color map.

Code that runs before yours
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
iris = sns.load_dataset('iris')

Exercise

3. Train and evaluate

Split X (every column but species) and y (species) with test_size=0.30 and random_state=101. Train an SVC() called svc_model, save its predictions as predictions, and print a classification report.

Code that runs before yours
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
iris = sns.load_dataset('iris')

Exercise

4. Grid search practice

Search 'C' in [0.1, 1, 10, 100] and 'gamma' in [1, 0.1, 0.01, 0.001] with GridSearchCV(SVC(), param_grid, refit=True) called grid. Save its test predictions as grid_predictions and end with grid.best_params_.

Code that runs before yours
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
iris = sns.load_dataset('iris')
from sklearn.model_selection import train_test_split
from sklearn.svm import SVC
X = iris.drop('species', axis=1)
y = iris['species']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.30, random_state=101)

You probably scored about the same before and after tuning. The default SVC was already nearly perfect on this easy data, and there's a single tricky flower at the border between versicolor and virginica. That's a useful lesson too: tuning can't fix what the features can't tell apart.

Exercises solved: 0 of 4

Tip: solve each exercise first. Press Check to test your code.

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.