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!
In [1]:
import numpy as np
np.random.seed(4242)
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]:
Code up your own SVM solution below:
In [ ]:
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]:
Code up your own SVM solution below:
In [ ]:
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]:
Code up your own solution:
In [ ]:
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]:
Code up your own solution below:
In [ ]: