-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecision_boundary_plot.py
More file actions
52 lines (38 loc) · 1.74 KB
/
Copy pathdecision_boundary_plot.py
File metadata and controls
52 lines (38 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# Print the decision boundary of a neural network.
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap
def one_hot_encode(Y):
n_labels = Y.shape[0]
result = np.zeros((n_labels, 2))
for i in range(n_labels):
result[i][Y[i]] = 1
return result
# Uncomment one of the next three lines to decide which dataset to load
x1, x2, y = np.loadtxt('linearly_separable.txt', skiprows=1, unpack=True)
# not a straight line, the accuracy is no longer 100%
# x1, x2, y = np.loadtxt('non_linearly_separable.txt', skiprows=1, unpack=True)
# x1, x2, y = np.loadtxt('circles.txt', skiprows=1, unpack=True)
X_train = X_test = np.column_stack((x1, x2))
Y_train_unencoded = Y_test = y.astype(int).reshape(-1, 1)
Y_train = one_hot_encode(Y_train_unencoded)
# Generate a mesh over one-dimensional data
# (The mesh() and plot_boundary() functionality were inspired by the
# documentation of the BSD-licensed scikit-learn library.)
def mesh(values):
range = values.max() - values.min()
padding_percent = 5
padding = range * padding_percent * 0.01
resolution = 1000
interval = (range + 2 * range * padding) / resolution
return np.arange(values.min() - padding, values.max() + padding, interval)
def plot_data_by_label(input_variables, labels, label_selector, symbol):
points = input_variables[(labels == label_selector).flatten()]
plt.plot(points[:, 0], points[:, 1], symbol, markersize=4)
plot_data_by_label(X_train, Y_train_unencoded, 0, 'bs')
plot_data_by_label(X_train, Y_train_unencoded, 1, 'g^')
plt.gca().axes.set_xlabel("Input A", fontsize=20)
plt.gca().axes.set_ylabel("Input B", fontsize=20)
plt.gca().axes.xaxis.set_ticklabels([])
plt.gca().axes.yaxis.set_ticklabels([])
plt.show()