Non-linear algorithms
10/28/22
When \(1 + 2x_1 + 3x_2 < 0\)
When \(1 + 2x_1 + 3x_2 > 0\)
When \(1 + 2x_1 + 3x_2 = 0\)
The linearly separable case:
The SV
’s represent the so-called support vectors:
Suppose we have a case that is not linearly separable like this. We have two classes but class 1 is “sandwiched” in between class 2.
Let’s start with a (linear) support vector classifier function
\[ f(x) = \beta_0 + \sum_{i\in\mathcal{S}}^{} \alpha_i\left\langle x_i, x_{i'} \right\rangle \]
A couple of points:
Two well-known kernels:
Iris
data Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1 5.1 3.5 1.4 0.2 setosa
2 4.9 3.0 1.4 0.2 setosa
3 4.7 3.2 1.3 0.2 setosa
4 4.6 3.1 1.5 0.2 setosa
5 5.0 3.6 1.4 0.2 setosa
6 5.4 3.9 1.7 0.4 setosa
library(datasets) # to load the iris data
library(tidyverse) # to use things like the pipe (%>%), mutate and if_else
library(ggsci) # just for pretty colours! It enables functions scale_fill_lancet() and scale_colour_lancet().
library(e1071) # to load the SVM algorithm
data(iris) # load the dataset `iris`
# Train the model! change the parameter `kernel`. It accepts 'linear', 'polynomial', 'radial' and 'sigmoid'
model <- svm(Species ~ Sepal.Length + Sepal.Width, data = iris, kernel = 'linear')
# Generate all possible combinations of Sepal.Length and Sepal.Width
kernel.points <- crossing(Sepal.Length = seq(4, 8, 0.1), Sepal.Width = seq(2, 5, 0.1)) %>% mutate(pred = predict(model, .))
# Create a dataframe just for plotting (with predictions)
plot_df <- iris %>% mutate(pred=predict(model, iris), correct = if_else(pred == Species, TRUE, FALSE))
plot_df %>%
ggplot() +
geom_tile(data = kernel.points, aes(x=Sepal.Length, y=Sepal.Width, fill = pred), alpha = 0.25) +
geom_point(aes(x=Sepal.Length, y=Sepal.Width, colour = Species, shape = correct), size = 4) +
scale_shape_manual(values = c(4, 1)) +
scale_colour_lancet() +
scale_fill_lancet() +
theme_minimal() +
theme(panel.grid = element_blank(), legend.position = 'bottom', plot.title = element_text(hjust = 0.5)) +
labs(x = 'Sepal.Length', y = 'Sepal.Width', fill = 'Species', colour = 'Species', shape = 'Correct prediction?',
title = sprintf('Overall Training Accuracy = %.2f %%', 100*(sum(plot_df$correct)/nrow(plot_df))))
Simple models can sometimes be better than complex models.
Look what happens when I set a different seed (nothing else changes) to construct a test set.
iris
SVM with Radial Kernel but tweaking parameters, namely cost
and gamma
:
library(datasets) # to load the iris data
library(tidyverse) # to use things like the pipe (%>%), mutate and if_else
library(ggsci) # just for pretty colours! It enables functions scale_fill_lancet() and scale_colour_lancet().
library(e1071) # to load the SVM algorithm
data(iris) # load the dataset `iris`
# Train the model! change the parameter `kernel`. It accepts 'linear', 'polynomial', 'radial' and 'sigmoid'
model <- svm(Species ~ Sepal.Length + Sepal.Width, data = iris, kernel = 'radial', gamma = 10^2, cost = 10^4)
# Generate all possible combinations of Sepal.Length and Sepal.Width
kernel.points <- crossing(Sepal.Length = seq(4, 8, 0.1), Sepal.Width = seq(2, 5, 0.1)) %>% mutate(pred = predict(model, .))
# Create a dataframe just for plotting (with predictions)
plot_df <- iris %>% mutate(pred=predict(model, iris), correct = if_else(pred == Species, TRUE, FALSE))
plot_df %>%
ggplot() +
geom_tile(data = kernel.points, aes(x=Sepal.Length, y=Sepal.Width, fill = pred), alpha = 0.25) +
geom_point(aes(x=Sepal.Length, y=Sepal.Width, colour = Species, shape = correct), size = 4) +
scale_shape_manual(values = c(4, 1)) +
scale_colour_lancet() +
scale_fill_lancet() +
theme_minimal() +
theme(panel.grid = element_blank(), legend.position = 'bottom', plot.title = element_text(hjust = 0.5)) +
labs(x = 'Sepal.Length', y = 'Sepal.Width', fill = 'Species', colour = 'Species', shape = 'Correct prediction?',
title = sprintf('Overall Training Accuracy = %.2f %%', 100*(sum(plot_df$correct)/nrow(plot_df))))
Split 1:
Test
Train
Train
Train
Train
Split 2:
Train
Test
Train
Train
Train
Split 3:
Train
Train
Test
Train
Train
Split 4:
Train
Train
Train
Test
Train
Split 5:
Train
Train
Train
Train
Test
We experimented with k-fold CV in 🗓️ Week 04’s lecture/workshop
ISLR2
package (link) - Load any dataset and explore it with these new algorithms
DS202 - Data Science for Social Scientists 🤖 🤹