Skip to content

Commit 24cfe69

Browse files
committed
ENH Use function names that match scikit-learn
1 parent 3b287d2 commit 24cfe69

File tree

6 files changed

+20
-18
lines changed

6 files changed

+20
-18
lines changed

ch02/figure4_5.py

Lines changed: 3 additions & 3 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 learn_model, apply_model, accuracy
14+
from knn import fit_model, predict, accuracy
1515

1616
feature_names = [
1717
'area',
@@ -31,8 +31,8 @@ def train_plot(features, labels):
3131
Y = np.linspace(y0, y1, 100)
3232
X, Y = np.meshgrid(X, Y)
3333

34-
model = learn_model(1, features[:, (0, 2)], np.array(labels))
35-
C = apply_model(
34+
model = fit_model(1, features[:, (0, 2)], np.array(labels))
35+
C = predict(
3636
np.vstack([X.ravel(), Y.ravel()]).T, model).reshape(X.shape)
3737
if COLOUR_FIGURE:
3838
cmap = ListedColormap([(1., .6, .6), (.6, 1., .6), (.6, .6, 1.)])

ch02/heldout.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from matplotlib import pyplot as plt
1212
import numpy as np
1313
from sklearn.datasets import load_iris
14-
from threshold import learn_model, apply_model, accuracy
14+
from threshold import fit_model, accuracy
1515

1616
data = load_iris()
1717
features = data['data']
@@ -29,7 +29,7 @@
2929
testing = np.tile([True, False], 50) # testing = [True,False,True,False,True,False...]
3030
training = ~testing
3131

32-
model = learn_model(features[training], virginica[training])
32+
model = fit_model(features[training], virginica[training])
3333
train_accuracy = accuracy(features[training], virginica[training], model)
3434
test_accuracy = accuracy(features[testing], virginica[testing], model)
3535

ch02/knn.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77

88
import numpy as np
99

10-
11-
def learn_model(k, features, labels):
10+
# This function was called ``learn_model`` in the first edition
11+
def fit_model(k, features, labels):
1212
'''Learn a k-nn model'''
1313
# There is no model in k-nn, just a copy of the inputs
1414
return k, features.copy(), labels.copy()
@@ -25,8 +25,8 @@ def plurality(xs):
2525
if v == maxv:
2626
return k
2727

28-
29-
def apply_model(features, model):
28+
# This function was called ``apply_model`` in the first edition
29+
def predict(features, model):
3030
'''Apply k-nn model'''
3131
k, train_feats, labels = model
3232
results = []
@@ -42,5 +42,5 @@ def apply_model(features, model):
4242

4343

4444
def accuracy(features, labels, model):
45-
preds = apply_model(features, model)
45+
preds = predict(features, model)
4646
return np.mean(preds == labels)

ch02/seeds_knn.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
from load import load_dataset
99
import numpy as np
10-
from knn import learn_model, apply_model, accuracy
10+
from knn import fit_model, accuracy
1111

1212
features, labels = load_dataset('seeds')
1313

@@ -19,7 +19,7 @@ def cross_validate(features, labels):
1919
training = np.ones(len(features), bool)
2020
training[fold::10] = 0
2121
testing = ~training
22-
model = learn_model(1, features[training], labels[training])
22+
model = fit_model(1, features[training], labels[training])
2323
test_error = accuracy(features[testing], labels[testing], model)
2424
error += test_error
2525

ch02/seeds_threshold.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
from load import load_dataset
99
import numpy as np
10-
from threshold import learn_model, apply_model, accuracy
10+
from threshold import fit_model, accuracy
1111

1212
features, labels = load_dataset('seeds')
1313

@@ -24,7 +24,7 @@
2424
# whatever is not training is for testing
2525
testing = ~training
2626

27-
model = learn_model(features[training], labels[training])
27+
model = fit_model(features[training], labels[training])
2828
test_error = accuracy(features[testing], labels[testing], model)
2929
error += test_error
3030

ch02/threshold.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
import numpy as np
99

1010

11-
def learn_model(features, labels):
11+
# This function was called ``learn_model`` in the first edition
12+
def fit_model(features, labels):
1213
'''Learn a simple threshold model'''
1314
best_acc = -1.0
1415
# Loop over all the features:
@@ -30,13 +31,14 @@ def learn_model(features, labels):
3031
return best_t, best_fi
3132

3233

33-
def apply_model(features, model):
34+
# This function was called ``apply_model`` in the first edition
35+
def predict(features, model):
3436
'''Apply a learned model'''
35-
# A model is a pair as returned by learn_model
37+
# A model is a pair as returned by fit_model
3638
t, fi = model
3739
return features[:, fi] > t
3840

3941
def accuracy(features, labels, model):
4042
'''Compute the accuracy of the model'''
41-
preds = apply_model(features, model)
43+
preds = predict(features, model)
4244
return np.mean(preds == labels)

0 commit comments

Comments
 (0)