Skip to content

Commit 2fea061

Browse files
committed
ENH Match the book exactly
Including import order and comments
1 parent f1b0812 commit 2fea061

File tree

3 files changed

+18
-8
lines changed

3 files changed

+18
-8
lines changed

ch02/figure1.py

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

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

10+
# We load the data with load_iris from sklearn
11+
from sklearn.datasets import load_iris
12+
13+
# load_iris returns an object with several fields
1114
data = load_iris()
1215
features = data.data
1316
feature_names = data.feature_names

ch02/figure2.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,18 @@
1010
from matplotlib import pyplot as plt
1111
from sklearn.datasets import load_iris
1212
data = load_iris()
13-
features = data['data']
14-
feature_names = data['feature_names']
15-
species = data['target_names'][data['target']]
13+
features = data.data
14+
feature_names = data.feature_names
15+
target = data.target
16+
target_names = data.target_names
1617

17-
is_setosa = (species == 'setosa')
18+
# We use NumPy fancy indexing to get an array of strings:
19+
labels = target_names[target]
20+
21+
is_setosa = (labels == 'setosa')
1822
features = features[~is_setosa]
19-
species = species[~is_setosa]
20-
is_virginica = (species == 'virginica')
23+
labels = labels[~is_setosa]
24+
is_virginica = (labels == 'virginica')
2125

2226
# Hand fixed threshold:
2327
t = 1.75

ch02/simple_threshold.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,12 @@
1212
target = data['target']
1313
target_names = data['target_names']
1414
labels = target_names[target]
15-
1615
plength = features[:, 2]
16+
17+
# To use numpy operations to get setosa features,
18+
# we build a boolean array
1719
is_setosa = (labels == 'setosa')
20+
1821
max_setosa = plength[is_setosa].max()
1922
min_non_setosa = plength[~is_setosa].min()
2023

0 commit comments

Comments
 (0)