mirror of
https://github.com/aladdinpersson/Machine-Learning-Collection.git
synced 2026-02-20 13:50:41 +00:00
48 lines
1.3 KiB
Python
48 lines
1.3 KiB
Python
"""
|
|
These were (shamelessly) taken from cs231n course github code.
|
|
I believe these were coded by Andrej Karpathy so credit goes to him
|
|
for coding these.
|
|
"""
|
|
|
|
import numpy as np
|
|
import matplotlib.pyplot as plt
|
|
|
|
|
|
def create_dataset(N, D=2, K=2):
|
|
X = np.zeros((N * K, D)) # data matrix (each row = single example)
|
|
y = np.zeros(N * K) # class labels
|
|
|
|
for j in range(K):
|
|
ix = range(N * j, N * (j + 1))
|
|
r = np.linspace(0.0, 1, N) # radius
|
|
t = np.linspace(j * 4, (j + 1) * 4, N) + np.random.randn(N) * 0.2 # theta
|
|
X[ix] = np.c_[r * np.sin(t), r * np.cos(t)]
|
|
y[ix] = j
|
|
|
|
# lets visualize the data:
|
|
plt.scatter(X[:, 0], X[:, 1], c=y, s=40, cmap=plt.cm.Spectral)
|
|
plt.show()
|
|
|
|
y[y == 0] -= 1
|
|
|
|
return X, y
|
|
|
|
|
|
def plot_contour(X, y, svm):
|
|
# plot the resulting classifier
|
|
h = 0.01
|
|
x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1
|
|
y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1
|
|
|
|
xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h))
|
|
|
|
points = np.c_[xx.ravel(), yy.ravel()]
|
|
|
|
Z = svm.predict(points)
|
|
Z = Z.reshape(xx.shape)
|
|
plt.contourf(xx, yy, Z, cmap=plt.cm.Spectral, alpha=0.8)
|
|
|
|
# plt the points
|
|
plt.scatter(X[:, 0], X[:, 1], c=y, s=40, cmap=plt.cm.Spectral)
|
|
plt.show()
|