Skip to content

Commit bdefa87

Browse files
committed
RFCT Improve code & documentation
1 parent 987bdb4 commit bdefa87

3 files changed

Lines changed: 62 additions & 35 deletions

File tree

‎ch02/figure1.py‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
#
66
# It is made available under the MIT License
77

8-
import numpy as np
98
from sklearn.datasets import load_iris
109
from matplotlib import pyplot as plt
1110

@@ -19,11 +18,14 @@
1918
pairs = [(0, 1), (0, 2), (0, 3), (1, 2), (1, 3), (2, 3)]
2019
for i, (p0, p1) in enumerate(pairs):
2120
ax = axes.flat[i]
21+
22+
# Use a different marker/color for each class `t`
2223
for t, marker, c in zip(range(3), ">ox", "rgb"):
2324
ax.scatter(features[target == t, p0], features[
2425
target == t, p1], marker=marker, c=c)
2526
ax.set_xlabel(feature_names[p0])
2627
ax.set_ylabel(feature_names[p1])
2728
ax.set_xticks([])
2829
ax.set_yticks([])
30+
fig.tight_layout()
2931
fig.savefig('figure1.png')

‎ch02/figure2.py‎

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,16 @@
1414
feature_names = data['feature_names']
1515
species = data['target_names'][data['target']]
1616

17-
setosa = (species == 'setosa')
18-
features = features[~setosa]
19-
species = species[~setosa]
20-
virginica = species == 'virginica'
17+
is_setosa = (species == 'setosa')
18+
features = features[~is_setosa]
19+
species = species[~is_setosa]
20+
is_virginica = (species == 'virginica')
2121

22+
# Hand fixed threshold:
2223
t = 1.75
23-
p0, p1 = 3, 2
24+
25+
# Features to use: 3 & 2
26+
f0, f1 = 3, 2
2427

2528
if COLOUR_FIGURE:
2629
area1c = (1., .8, .8)
@@ -29,19 +32,27 @@
2932
area1c = (1., 1, 1)
3033
area2c = (.7, .7, .7)
3134

32-
x0, x1 = [features[:, p0].min() * .9, features[:, p0].max() * 1.1]
33-
y0, y1 = [features[:, p1].min() * .9, features[:, p1].max() * 1.1]
34-
35-
plt.fill_between([t, x1], [y0, y0], [y1, y1], color=area2c)
36-
plt.fill_between([x0, t], [y0, y0], [y1, y1], color=area1c)
37-
plt.plot([t, t], [y0, y1], 'k--', lw=2)
38-
plt.plot([t - .1, t - .1], [y0, y1], 'k:', lw=2)
39-
plt.scatter(features[virginica, p0],
40-
features[virginica, p1], c='b', marker='o')
41-
plt.scatter(features[~virginica, p0],
42-
features[~virginica, p1], c='r', marker='x')
43-
plt.ylim(y0, y1)
44-
plt.xlim(x0, x1)
45-
plt.xlabel(feature_names[p0])
46-
plt.ylabel(feature_names[p1])
47-
plt.savefig('figure2.png')
35+
# Plot from 90% of smallest value to 110% of largest value
36+
# (all feature values are positive, otherwise this would not work very well)
37+
38+
x0 = features[:, f0].min() * .9
39+
x1 = features[:, f0].max() * 1.1
40+
41+
y0 = features[:, f1].min() * .9
42+
y1 = features[:, f1].max() * 1.1
43+
44+
fig,ax = plt.subplots()
45+
ax.fill_between([t, x1], [y0, y0], [y1, y1], color=area2c)
46+
ax.fill_between([x0, t], [y0, y0], [y1, y1], color=area1c)
47+
ax.plot([t, t], [y0, y1], 'k--', lw=2)
48+
ax.plot([t - .1, t - .1], [y0, y1], 'k:', lw=2)
49+
ax.scatter(features[is_virginica, f0],
50+
features[is_virginica, f1], c='b', marker='o')
51+
ax.scatter(features[~is_virginica, f0],
52+
features[~is_virginica, f1], c='r', marker='x')
53+
ax.set_ylim(y0, y1)
54+
ax.set_xlim(x0, x1)
55+
ax.set_xlabel(feature_names[f0])
56+
ax.set_ylabel(feature_names[f1])
57+
fig.tight_layout()
58+
fig.savefig('figure2.png')

‎ch02/figure4_5.py‎

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from matplotlib.colors import ListedColormap
1212
from load import load_dataset
1313
import numpy as np
14-
from knn import fit_model, predict, accuracy
14+
from knn import fit_model, predict
1515

1616
feature_names = [
1717
'area',
@@ -24,7 +24,19 @@
2424
]
2525

2626

27-
def train_plot(features, labels):
27+
def plot_decision(features, labels):
28+
'''Plots decision boundary for KNN
29+
30+
Parameters
31+
----------
32+
features : ndarray
33+
labels : sequence
34+
35+
Returns
36+
-------
37+
fig : Matplotlib Figure
38+
ax : Matplotlib Axes
39+
'''
2840
y0, y1 = features[:, 2].min() * .9, features[:, 2].max() * 1.1
2941
x0, x1 = features[:, 0].min() * .9, features[:, 0].max() * 1.1
3042
X = np.linspace(x0, x1, 100)
@@ -38,28 +50,30 @@ def train_plot(features, labels):
3850
cmap = ListedColormap([(1., .6, .6), (.6, 1., .6), (.6, .6, 1.)])
3951
else:
4052
cmap = ListedColormap([(1., 1., 1.), (.2, .2, .2), (.6, .6, .6)])
41-
plt.xlim(x0, x1)
42-
plt.ylim(y0, y1)
43-
plt.xlabel(feature_names[0])
44-
plt.ylabel(feature_names[2])
45-
plt.pcolormesh(X, Y, C, cmap=cmap)
53+
fig,ax = plt.subplots()
54+
ax.set_xlim(x0, x1)
55+
ax.set_ylim(y0, y1)
56+
ax.set_xlabel(feature_names[0])
57+
ax.set_ylabel(feature_names[2])
58+
ax.pcolormesh(X, Y, C, cmap=cmap)
4659
if COLOUR_FIGURE:
4760
cmap = ListedColormap([(1., .0, .0), (.0, 1., .0), (.0, .0, 1.)])
48-
plt.scatter(features[:, 0], features[:, 2], c=labels, cmap=cmap)
61+
ax.scatter(features[:, 0], features[:, 2], c=labels, cmap=cmap)
4962
else:
5063
for lab, ma in zip(range(3), "Do^"):
51-
plt.plot(features[labels == lab, 0], features[
64+
ax.plot(features[labels == lab, 0], features[
5265
labels == lab, 2], ma, c=(1., 1., 1.))
66+
return fig,ax
5367

5468

5569
features, labels = load_dataset('seeds')
5670
names = sorted(set(labels))
5771
labels = np.array([names.index(ell) for ell in labels])
5872

59-
train_plot(features, labels)
60-
plt.savefig('figure4.png')
73+
fig,ax = plot_decision(features, labels)
74+
fig.savefig('figure4.png')
6175

6276
features -= features.mean(0)
6377
features /= features.std(0)
64-
train_plot(features, labels)
65-
plt.savefig('figure5.png')
78+
fig,ax = plot_decision(features, labels)
79+
fig.savefig('figure5.png')

0 commit comments

Comments
 (0)