Learn practical skills, build real-world projects, and advance your career

Exercise 6 - Support Vector Machines

Support vector machines (SVMs) let us predict categories. This exercise will demonstrate a simple support vector machine that can predict a category from a small number of features.

Our problem is that we want to be able to categorise which type of tree an new specimen belongs to. To do this, we will use features of three different types of trees to train an SVM.

Run the code in the cell below.

# Run this code!
# It sets up the graphing configuration.
import warnings
warnings.filterwarnings("ignore")
import matplotlib.pyplot as graph
%matplotlib inline
graph.rcParams['figure.figsize'] = (15,5)
graph.rcParams["font.family"] = 'DejaVu Sans'
graph.rcParams["font.size"] = '12'
graph.rcParams['image.cmap'] = 'rainbow'

Step 1

First, we will take a look at the raw data first to see what features we have.

Replace <printDataHere> with print(dataset.head()) and then run the code.
import pandas as pd
import numpy as np

# Loads the SVM library
from sklearn import svm

# Loads the dataset
dataset = pd.read_csv('Data/trees.csv')

###
print(dataset.head())
###
leaf_width leaf_length trunk_girth trunk_height tree_type 0 5.13 6.18 8.26 8.74 0 1 7.49 4.02 8.07 6.78 0 2 9.22 4.16 5.46 8.45 1 3 6.98 11.10 6.96 4.06 2 4 3.46 5.19 8.72 10.40 0

It looks like we have four features (leaf_width, leaf_length, trunk_girth, trunk_height) and one label (tree_type).

Let's plot it.

Run the code in the cell below.