This notebook contains an excerpt from the book Machine Learning for OpenCV by Michael Beyeler. The code is released under the MIT license, and is available on GitHub.

Note that this excerpt contains only the raw code - the book is rich with additional explanations and illustrations. If you find this content useful, please consider supporting the work by buying the book!

Additional SVM Exercises

The book refers to a couple of follow-up exercises, where you should think about which SVM kernel is most likely to perform best on the data.

Think you know the answer? Code it up!

You can use the pre-generated datasets below.


In [1]:
import numpy as np
np.random.seed(4242)

Exercise 1


In [2]:
n_samples = 500
n_features = 2
X1 = np.random.rand(n_samples, n_features)

In [3]:
y1 = np.ones((n_samples, 1))
idx_neg = (X1[:, 0] - 0.5) ** 2 + (X1[:, 1] - 0.5) ** 2 < 0.03
y1[idx_neg] = 0

In [4]:
import matplotlib.pyplot as plt
%matplotlib inline
plt.figure(figsize=(10, 6))
plt.scatter(X1[:, 0], X1[:, 1], c=y1, s=100)


Out[4]:
<matplotlib.collections.PathCollection at 0x7f94e104df28>

Code up your own SVM solution below:


In [ ]:

Exercise 2


In [5]:
X2 = np.random.rand(n_samples, n_features)

In [6]:
y2 = np.ones((n_samples, 1))
idx_neg = (X2[:, 0] < 0.5) * (X2[:, 1] < 0.5) + (X2[:, 0] > 0.5) * (X2[:, 1] > 0.5)
y2[idx_neg] = 0

In [7]:
plt.figure(figsize=(10, 6))
plt.scatter(X2[:, 0], X2[:, 1], c=y2, s=100)


Out[7]:
<matplotlib.collections.PathCollection at 0x7f94e1013208>

Code up your own SVM solution below:


In [ ]:

Exercise 3


In [8]:
rho_pos = np.random.rand(n_samples // 2, 1) / 2.0 + 0.5
rho_neg = np.random.rand(n_samples // 2, 1) / 4.0
rho = np.vstack((rho_pos, rho_neg))
phi_pos = np.pi * 0.75 + np.random.rand(n_samples // 2, 1) * np.pi * 0.5
phi_neg = np.random.rand(n_samples // 2, 1) * 2 * np.pi
phi = np.vstack((phi_pos, phi_neg))

In [9]:
X3 = np.array([[r * np.cos(p), r * np.sin(p)] for r, p in zip(rho, phi)])
y3 = np.vstack((np.ones((n_samples // 2, 1)), np.zeros((n_samples // 2, 1))))

In [10]:
plt.figure(figsize=(10, 6))
plt.scatter(X3[:, 0], X3[:, 1], c=y3, s=100)


Out[10]:
<matplotlib.collections.PathCollection at 0x7f94e0f91780>

Code up your own solution:


In [ ]:

Exercise 4


In [11]:
rho_pos = np.linspace(0, 2, n_samples // 2)
rho_neg = np.linspace(0, 2, n_samples // 2) + 0.5
rho = np.vstack((rho_pos, rho_neg))
phi_pos = 2 * np.pi * rho_pos
phi = np.vstack((phi_pos, phi_pos))

In [12]:
X4 = np.array([[r * np.cos(p), r * np.sin(p)] for r, p in zip(rho, phi)])
y4 = np.vstack((np.ones((n_samples // 2, 1)), np.zeros((n_samples // 2, 1))))

In [13]:
plt.figure(figsize=(10, 6))
plt.scatter(X4[:, 0], X4[:, 1], c=y4, s=100)


Out[13]:
<matplotlib.collections.PathCollection at 0x7f94e0f7f6d8>

Code up your own solution below:


In [ ]: