-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsimple_example_classification.py
More file actions
82 lines (61 loc) · 2.31 KB
/
Copy pathsimple_example_classification.py
File metadata and controls
82 lines (61 loc) · 2.31 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import numpy
import sklearn.datasets
import sklearn.model_selection
from matplotlib import pyplot as plt
from sklearn.tree import DecisionTreeClassifier
from pytolemaic import PyTrust, FeatureTypes
def run(fast=False):
# Dataset: xtrain, ytrain, xtest, ytest
# noinspection PyUnresolvedReferences
data = sklearn.datasets.load_wine(return_X_y=False)
x = data['data']
y = data['target']
feature_names = data['feature_names']
labels = data['target_names']
train_inds, test_inds = sklearn.model_selection.train_test_split(
numpy.arange(len(data['data'])), test_size=0.3)
xtrain, ytrain = x[train_inds], y[train_inds]
xtest, ytest = x[test_inds], y[test_inds]
if fast:
inds = numpy.random.permutation(len(xtrain))[:500]
xtrain = xtrain[inds,:]
ytrain = ytrain[inds]
inds = numpy.random.permutation(len(xtest))[:500]
xtest = xtest[inds, :]
ytest = ytest[inds]
# Train estimator
estimator = DecisionTreeClassifier()
estimator.fit(xtrain, ytrain)
# Initiating PyTrust
pytrust = PyTrust(
model=estimator,
xtrain=xtrain, ytrain=ytrain,
xtest=xtest, ytest=ytest,
metric='recall')
# Initiating PyTrust with more information
pytrust = PyTrust(
model=estimator,
xtrain=xtrain, ytrain=ytrain,
xtest=xtest, ytest=ytest,
feature_names=feature_names,
feature_types=[FeatureTypes.numerical] * xtrain.shape[1],
target_labels={i: label for i, label in enumerate(labels)},
splitter='stratified',
metric='recall')
pytrust.scoring_report.plot()
pytrust.sensitivity_report.plot()
pytrust.dataset_analysis_report.plot()
pytrust.quality_report.plot()
pytrust.anomalies_in_data_report.plot()
sample = xtest[0, :].reshape(1, -1)
explainer = pytrust.create_lime_explainer(max_samples=4000 if fast else 16000)
explainer.explain(sample=sample)
uncertainty_model = pytrust.create_uncertainty_model(method='default')
prediction = uncertainty_model.predict(sample) # same as model.predict
uncertainty = uncertainty_model.uncertainty(sample) # uncertainty value
print("Let's check for insights...")
print('\n'.join(pytrust.insights))
print("Done!")
if __name__ == '__main__':
run()
plt.show()