forked from spmallick/learnopencv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
108 lines (81 loc) · 2.82 KB
/
utils.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import numpy as np
import matplotlib.pyplot as plt
data_colors = [(1, 0, 0), (0, 0, 1)]
def read_points_file(filename):
pts = []
with open(filename, "r") as f:
for pt in f:
pt = pt.strip("\n").split()
pts.append([float(pt[0]), float(pt[1])])
return pts
def read_data(class_0_file, class_1_file):
pts_0 = read_points_file(class_0_file)
pts_1 = read_points_file(class_1_file)
x = pts_0 + pts_1
labels = [0] * len(pts_0) + [1] * len(pts_1)
x = np.array(x)
return (x, labels)
def plot_data(X_train, y_train, X_test, y_test):
X = np.concatenate((X_train, X_test))
y = np.concatenate((y_train, y_test))
colors = get_colors(y)
colors_train = get_colors(y_train)
colors_test = get_colors(y_test)
plt.figure(figsize=(12, 4), dpi=150)
# Plot all data plot
plt.subplot(131)
plt.axis('equal')
plt.scatter(X[:, 0], X[:, 1], c = colors, s = 10, edgecolors=colors)
plt.title("Data (100%)")
# training data plot
plt.subplot(132)
plt.axis('equal')
#plt.axis('off')
plt.scatter(X_train[:, 0], X_train[:, 1], c = colors_train, s = 10, edgecolors=colors_train)
plt.title("Training Data (80%)")
# testing data plot
plt.subplot(133)
plt.axis('equal')
#plt.axis('off')
plt.scatter(X_test[:, 0], X_test[:, 1], c = colors_test, s = 10, edgecolors=colors_test)
plt.title("Test Data (20%)")
plt.tight_layout()
plt.show()
def get_colors(y):
return [data_colors[item] for item in y]
def plot_decision_function(X_train, y_train, X_test, y_test, clf):
plt.figure(figsize=(8, 4), dpi=150)
plt.subplot(121)
plt.title("Training data")
plot_decision_function_helper(X_train, y_train, clf)
plt.subplot(122)
plt.title("Test data")
plot_decision_function_helper(X_test, y_test, clf, True)
plt.show()
def plot_decision_function_helper(X, y, clf, show_only_decision_function = False):
colors = get_colors(y)
plt.axis('equal')
plt.tight_layout()
#plt.axis('off')
plt.scatter(X[:, 0], X[:, 1], c = colors, s = 10, edgecolors=colors)
ax = plt.gca()
xlim = ax.get_xlim()
ylim = ax.get_ylim()
# Create grid to evaluate model
xx = np.linspace(xlim[0], xlim[1], 30)
yy = np.linspace(ylim[0], ylim[1], 30)
YY, XX = np.meshgrid(yy, xx)
xy = np.vstack([XX.ravel(), YY.ravel()]).T
Z = clf.decision_function(xy).reshape(XX.shape)
if show_only_decision_function:
# Plot decision boundary
ax.contour(XX, YY, Z, colors='k', levels=[0], alpha=0.5,
linestyles=['-'])
else :
# Plot decision boundary and margins
ax.contour(XX, YY, Z, colors='k', levels=[-1, 0, 1], alpha=0.5,
linestyles=['--', '-', '--'])
# Plot support vectors
#ax.scatter(clf.support_vectors_[:, 0], clf.support_vectors_[:, 1], s = 10,
# linewidth=1, facecolors='k', c = 'k', label='Support Vectors')
#plt.legend(fontsize='small')