-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmodel_utils.py
66 lines (57 loc) · 1.73 KB
/
model_utils.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import print_function
from builtins import zip
import numpy
from sklearn.tree import export_graphviz
def evaluate_model(
model,
features,
classes,
sample_names,
class_names,
output=True
):
total_correct = 0.0
for i, (feature, _class) in \
enumerate(zip(features, classes)):
feature = numpy.array(feature).reshape(1, -1)
predicted_class = model.predict(feature)[0]
accurate = predicted_class == _class
accuracy_string = u"✅" if accurate else u"❌"
if accurate:
total_correct += 1.
if output:
print(
"\t%s Predicted %s as %s." % (
accuracy_string,
sample_names[i],
class_names[predicted_class]
)
)
else:
if output:
print(
"\t%s Predicted %s as %s, was actually %s." % (
accuracy_string,
sample_names[i],
class_names[predicted_class],
class_names[_class]
)
)
print("Total of %d correct of %d. (%2.2f%%)" % (
total_correct,
len(features),
100. * total_correct / len(features)))
def explain_model(model, feature_names, class_names):
import graphviz
dot_data = export_graphviz(
model,
out_file=None,
feature_names=feature_names,
class_names=class_names,
filled=True,
rounded=True,
special_characters=True)
graph = graphviz.Source(dot_data)
graph.render("graph")