forked from dionny/ai-for-testing-binder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
56 lines (47 loc) · 1.81 KB
/
utils.py
File metadata and controls
56 lines (47 loc) · 1.81 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
53
54
55
56
import numpy as np, matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap
def plot_decision(X, y, classifier, test_idx=None, resolution=0.02, figsize=(6,6)):
# setup marker generator and color map
markers = ('s', 'x', 'o', '^', 'v')
colors = ('#cc0000', '#003399', '#00cc00', '#999999', '#66ffff')
cmap = ListedColormap(colors[:len(np.unique(y))])
# get dimensions
x1_min, x1_max = X[:, 0].min() - 1, X[:, 0].max() + 1
x2_min, x2_max = X[:, 1].min() - 1, X[:, 1].max() + 1
xx1, xx2 = np.meshgrid(np.arange(x1_min, x1_max, resolution), np.arange(x2_min, x2_max, resolution))
xmin = xx1.min()
xmax = xx1.max()
ymin = xx2.min()
ymax = xx2.max()
# create the figure
fig, ax = plt.subplots(figsize=figsize)
ax.set_xlim(xmin, xmax)
ax.set_ylim(ymin, ymax)
# plot the decision surface
Z = classifier.predict(np.array([xx1.ravel(), xx2.ravel()]).T)
Z = Z.reshape(xx1.shape)
ax.contourf(xx1, xx2, Z, alpha=0.4, cmap=cmap, zorder=1)
# plot all samples
for idx, cl in enumerate(np.unique(y)):
ax.scatter(x=X[y == cl, 0],
y=X[y == cl, 1],
alpha=0.6,
c=cmap(idx),
edgecolor='black',
marker='o',#markers[idx],
s=50,
label=cl,
zorder=3)
# highlight test samples
if test_idx:
X_test, y_test = X[test_idx, :], y[test_idx]
ax.scatter(X_test[:, 0],
X_test[:, 1],
c='w',
alpha=1.0,
edgecolor='black',
linewidths=1,
marker='o',
s=150,
label='test set',
zorder=2)