-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththreshold.py
More file actions
33 lines (26 loc) · 851 Bytes
/
threshold.py
File metadata and controls
33 lines (26 loc) · 851 Bytes
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
import numpy as np
def learn_model(features, labels):
best_acc = -1.0
for fi in range(features.shape[1]):
thresh = features[:, fi].copy()
thresh.sort()
for t in thresh:
pred = (features[:, fi] > t)
acc = (pred == labels).mean()
if acc > best_acc:
best_acc = acc
best_fi = fi
best_t = t
return best_t, best_fi
def apply_model(features, model):
t, fi = model
return features[:, fi] > t
def accuracy(features, labels, model):
preds = apply_model(features, model)
return np.mean(preds == labels)