-
Notifications
You must be signed in to change notification settings - Fork 0
/
Plotter.py
executable file
·69 lines (49 loc) · 1.86 KB
/
Plotter.py
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import numpy as np
from matplotlib import pyplot as plt
"""
plotter functions are meant to use the whole dataset
and the hyperplane is recalculated based on a fitted perceptron
"""
def plot_3d(clf, X, Y):
feature1 = 0
feature2 = 1
feature3 = 2
def calculate_weights():
w = np.zeros(len(clf.train_x[0]))
for i in range(len(clf.alpha)):
w += clf.alpha[i] * clf.train_y[i] * clf.train_x[i]
return w
w = calculate_weights()
z = lambda x, y: ((w[feature1] * x) -
(w[feature2] * y) - clf.b) / (w[feature3])
for angle in range(70, 210, 2):
tmp = np.linspace(-5, 5, 30)
x, y = np.meshgrid(tmp, tmp)
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot_surface(x, y, z(x, y), alpha=0.3)
ax.plot3D(X[Y == -1, feature1], X[Y == -1, feature2], X[Y == -1, feature3], 'ob')
ax.plot3D(X[Y == 1, feature1], X[Y == 1, feature2], X[Y == 1, feature3], 'sr')
ax.view_init(30, angle)
filename = "3D_plots/plot_step" + str(angle) + ".png"
plt.savefig(filename, dpi=96)
def plot_2d(clf, X, Y):
feature1 = 0
feature2 = 1
def calculate_weights():
w = np.zeros(len(clf.train_x[0]))
for i in range(len(clf.alpha)):
w += clf.alpha[i] * clf.train_y[i] * clf.train_x[i]
return w
x = np.linspace(-5, 5, 30)
w = calculate_weights()
y = lambda x: -(w[feature1] * x + clf.b) / (w[feature2])
plt.title("2D Hyperplane Decision Boundary")
plt.xlabel("Feature " + str(feature1))
plt.ylabel("Feature " + str(feature2))
plt.grid()
plt.plot(x, y(x))
plt.plot(X[Y == -1, feature1], X[Y == -1, feature2], 'ro')
plt.plot(X[Y == 1, feature1], X[Y == 1, feature2], 'bo')
plt.savefig("Pictures/2D Hyperplane Decision Boundary gender_voice.png")
plt.show()