-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcase_adult_explain.py
More file actions
70 lines (58 loc) · 2.63 KB
/
Copy pathcase_adult_explain.py
File metadata and controls
70 lines (58 loc) · 2.63 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
import pandas as pd
import numpy as np
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
from externals.LORE.datamanager import prepare_dataset
from xailib.explainers.lime_explainer import LimeXAITabularExplainer
from xailib.explainers.lore_explainer import LoreTabularExplainer
from xailib.models.sklearn_classifier_wrapper import sklearn_classifier_wrapper
import time
from catboost import CatBoostClassifier
from sklearn.linear_model import LogisticRegression
if __name__ == '__main__':
source_file = 'datasets/adult.csv'
class_field = 'class'
# Load and transform dataset and select one row to classify and explain
df = pd.read_csv(source_file, skipinitialspace=True, na_values='?', keep_default_na=True)
df, feature_names, class_values, numeric_columns, \
rdf, real_feature_names, features_map = prepare_dataset(df, class_field)
print(df.head())
print(class_values)
# Learn a model from the data
test_size = 0.3
random_state = 0
X_train, X_test, Y_train, Y_test = train_test_split(df[feature_names].values, df[class_field].values,
test_size=test_size,
random_state=random_state,
stratify=df[class_field].values)
#bb = RandomForestClassifier(n_estimators=20, random_state=random_state)
bb = CatBoostClassifier(custom_loss=['Accuracy'], random_seed=42, logging_level='Silent')
#bb = LogisticRegression(C=1, penalty='l2')
bb.fit(X_train, Y_train)
# Build a wrapper aroung the bbox
bbox = sklearn_classifier_wrapper(bb)
inst = df[feature_names].values[18]
print(inst, bbox.predict(inst.reshape(1, -1)))
start = time.time()
config = {'neigh_type' : 'geneticp', 'size' : 1000, 'ocr' : 0.1, 'ngen' : 10 }
print("hello")
print(config)
# Create an explainer: LORE
explainer = LoreTabularExplainer(bbox)
# let the explainer to scan the training or test set
explainer.fit(df, class_field, config)
print('building an explanation')
exp = explainer.explain(inst)
print(exp)
end = time.time()
print('print ', end - start)
start = time.time()
print("hello")
# Use another explainer: LIME
config = {'feature_selection': 'lasso_path', 'discretize_continuous' : True, 'discretizer' : 'decile'}
limeExplainer = LimeXAITabularExplainer(bbox)
limeExplainer.fit(df, class_field, config)
lime_exp = limeExplainer.explain(inst)
print(lime_exp.as_list())
end = time.time()
print('print ', end - start)