-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathseeds_threshold.py
33 lines (24 loc) · 904 Bytes
/
seeds_threshold.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
# This code is supporting material for the book
# Building Machine Learning Systems with Python
# by Willi Richert and Luis Pedro Coelho
# published by PACKT Publishing
#
# It is made available under the MIT License
from load import load_dataset
import numpy as np
from threshold import fit_model, accuracy
features, labels = load_dataset('seeds')
# Turn the labels into a binary array
labels = (labels == 'Canadian')
error = 0.0
for fold in range(10):
training = np.ones(len(features), bool)
# numpy magic to make an array with 10% of 0s starting at fold
training[fold::10] = 0
# whatever is not training is for testing
testing = ~training
model = fit_model(features[training], labels[training])
test_error = accuracy(features[testing], labels[testing], model)
error += test_error
error /= 10.0
print('Ten fold cross-validated error was {0:.1%}.'.format(error))