-
Notifications
You must be signed in to change notification settings - Fork 0
/
agrawal_experiment.py
153 lines (133 loc) · 6.16 KB
/
agrawal_experiment.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# -*- coding: utf-8 -*-
from changeExplainer import changeExplainer
from river import metrics, synth, ensemble, linear_model, compose, neighbors, preprocessing
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
from datetime import datetime
import tikzplotlib
def run_experiment(run_id, n_stream_length, drift_position, drift_width, len_explanation_interval, adwin_delta, tau):
seed_value = np.random.randint(1000)
stream_1 = synth.Agrawal(1, seed=seed_value,balance_classes=True)
stream_2 = synth.Agrawal(2, seed=seed_value,balance_classes=True)
stream = synth.ConceptDriftStream(
stream=stream_1,
drift_stream=stream_2,
position=drift_position,
width=drift_width
)
scaler = preprocessing.StandardScaler()
oh = compose.Select('elevel','car','zipcode') | preprocessing.OneHotEncoder()
oh += compose.Select('salary','commission','age','hvalue','hyears','loan')
feature_list = ['salary','commission','age','elevel','car','zipcode','hvalue','hyears','loan']
arf_accuracy = metrics.Accuracy()
lm_accuracy = metrics.Accuracy()
ibl_accuracy = metrics.Accuracy()
arf_model = ensemble.AdaptiveRandomForestClassifier(seed=seed_value)
arf_explainer = changeExplainer(arf_model, "Adaptive Random Forest", len_explanation_interval, adwin_delta, tau,
seed_value,feature_list)
ibl_model = neighbors.SAMKNNClassifier()
ibl_explainer = changeExplainer(ibl_model, "SAMKNN Classifier", len_explanation_interval, adwin_delta, tau,
seed_value,feature_list)
for (n, (x, y)) in enumerate(stream):
scaler.learn_one(x)
x=scaler.transform_one(x)
# Prediction
y_pred_arf = arf_model.predict_one(x)
y_pred_ibl = ibl_model.predict_one(x)
# Accuracy Update
arf_accuracy.update(y, y_pred_arf)
ibl_accuracy.update(y,y_pred_ibl)
# Learning
arf_model.learn_one(x, y)
ibl_model.learn_one(x,y)
# Explaining
arf_explainer.explain_sample(x, y, y_pred_arf)
ibl_explainer.explain_sample(x,y,y_pred_ibl)
arf_explainer.explanations["Total Accuracy"] = arf_accuracy.get()
ibl_explainer.explanations["Total Accuracy"] = ibl_accuracy.get()
if n > n_stream_length:
break
results = pd.concat([arf_explainer.explanations, ibl_explainer.explanations])
results["run_id"] = run_id
return results
def plot_accuracy(n_stream_length, drift_position, drift_width, len_explanation_interval, adwin_delta, tau,
seed_value=43, window_size=200):
results = []
stream_1 = synth.Agrawal(1, balance_classes=True,seed=seed_value)
stream_2 = synth.Agrawal(2, balance_classes=True,seed=seed_value)
stream = synth.ConceptDriftStream(
stream=stream_1,
drift_stream=stream_2,
position=drift_position,
width=drift_width,
seed=seed_value
)
scaler = preprocessing.StandardScaler()
oh = compose.Select('elevel','car','zipcode') | preprocessing.OneHotEncoder()
oh += compose.Select('salary','commission','age','hvalue','hyears','loan')
feature_list = ['salary','commission','age','elevel','car','zipcode','hvalue','hyears','loan']
arf_rolling_accuracy = metrics.Rolling(metrics.Accuracy(), window_size=window_size)
arf_model = ensemble.AdaptiveRandomForestClassifier(seed=seed_value)
arf_explainer = changeExplainer(arf_model, "Adaptive Random Forest", len_explanation_interval, adwin_delta, tau,
seed_value,feature_list)
for (n, (x, y)) in enumerate(stream):
scaler.learn_one(x)
x=scaler.transform_one(x)
# Prediction
y_pred_arf = arf_model.predict_one(x)
# Accuracy Update
arf_rolling_accuracy.update(y, y_pred_arf)
if n % 50 == 0:
results.append(arf_rolling_accuracy.get())
# Learning
arf_model.learn_one(x, y)
# Explaining
arf_explainer.explain_sample(x, y, y_pred_arf)
if n > n_stream_length:
break
plt.figure()
plt.plot(range(0,len(results)*50,50),results)
run_id = datetime.now().strftime("%Y%m%d_%H%M%S")
tikzplotlib.save("figures/"+str(run_id)+"_agrawal_rolling_accuracy_arf.tex")
plt.show()
# Run Experiment
n_runs = 50
model_parameter = pd.DataFrame(index=[0])
n_stream_length = 20000
drift_position = int(n_stream_length*2/3)
drift_width = 50
len_explanation_interval = max(300, int(n_stream_length / 20))
adwin_delta = 0.025
tau = 0.4
experiment_results = pd.DataFrame()
for run_id in range(n_runs):
print(run_id,"/",n_runs," : ",datetime.now().strftime("%Y%m%d_%H%M%S"))
run_results = run_experiment(run_id, n_stream_length, drift_position, drift_width, len_explanation_interval,
adwin_delta, tau)
experiment_results = pd.concat([experiment_results, run_results])
# Plot rolling accuracy for ARF
plot_accuracy(n_stream_length, drift_position, drift_width, len_explanation_interval, adwin_delta, tau)
experiment_means = experiment_results.groupby("Model Name").mean()
experiment_stds = experiment_results.groupby("Model Name").std()
#Output results and parameter
model_parameter["Stream Length"] = n_stream_length
model_parameter["Drift Position"] = drift_position
model_parameter["Explanation Collection Interval"] = len_explanation_interval
model_parameter["ADWIN Delta"] = adwin_delta
model_parameter["Experiment Iterations"] = n_runs
run_id = datetime.now().strftime("%Y%m%d_%H%M%S")
experiment_results.to_csv("results/"+str(run_id)+"_experiment_agrawal_results.csv")
experiment_means.to_csv("results/"+str(run_id)+"_experiment_agrawal_results_mean.csv")
experiment_stds.to_csv("results/"+str(run_id)+"_experiment_agrawal_results_std.csv")
model_parameter.to_csv("results/"+str(run_id)+"_experiment_agrawal_params.csv")
means = experiment_results.groupby("Model Name").mean()
deltas = means.loc[:,means.columns.str.startswith('PFI_delta')].transpose()
plt.figure()
plt.bar(deltas.index.str.slice(10),deltas.iloc[:,0],label="ARF")
plt.legend()
plt.show()
plt.figure()
plt.bar(deltas.index.str.slice(10),deltas.iloc[:,1],label="SAM-kNN")
plt.legend()
plt.show()