SVM - demo

Important: Run this notebook in the cloud by clicking any of the options at the top of the page (e.g. google colab).

Run the following cell and play around with the slide bar to construct your own hyperplane with "maximum margin". The red line correspond to the "true" margin (used to generate the data, and you should never know what that is in practice).

Question:

  • Does your solution (orange) match up with the red line? Put another way, is the red line (truth) always the maximum margin hyperplane given the data?
  • What is the effect of increasing n? i.e. if we collect more data, does the optimised SVM boundary line up better with the true (red) boundary when n is large?

import numpy as np
import matplotlib.pyplot as plt
from ipywidgets import interact
w0 = -1.5 # y = w0 * x + b0
b0 = 1
xaxis = np.linspace(0, 1, num=10)


max_n = 100
all_data = np.random.rand(max_n, 2)
all_y = (all_data[:, 1] > all_data[:, 0] * w0 + b0)


@interact(w=(-3., 3.), b=(-3., 3.), n=(10, 100))
def plot(w=-0.5, b=0.5, n=10):
    data = all_data[:n, :]
    y = all_y[:n]
    fig, ax = plt.subplots(1, 1, figsize=(8, 8))
    ax.plot(data[y, 0], data[y, 1], "r*", markersize=15)
    ax.plot(data[~y, 0], data[~y, 1], "b*", markersize=15)
    ax.set_xlim(0, 1)
    ax.set_ylim(0, 1)
    ax.plot(xaxis, w0 * xaxis + b0, 'r--')
    ax.plot(xaxis, w * xaxis + b, color="orange")