-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfeature_drift_result.py
188 lines (147 loc) · 6.29 KB
/
feature_drift_result.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import pandas as pd
import numpy as np
from river import drift
from scipy import stats
def custom_cd(features, drift_index, w_size, confidence, cdd_1, cdd_2, cdd_3):
reference_window_1, reference_window_2, reference_window_3 = [], [], []
test_window_1, test_window_2, test_window_3 = [], [], []
fp_count_1, fp_count_2, fp_count_3 = 0,0,0
tp_1, tp_2, tp_3 = None, None, None
for idx in range(len(features[1900:2000])):
idx = idx+1900
reference_window_1.append(features[idx][0])
reference_window_2.append(features[idx][1])
reference_window_3.append(features[idx][2])
for idx in range(len(features[2000:])):
idx = idx+2000
#feature 1
if len(test_window_1)==w_size:
res = cdd_1(test_window_1, reference_window_1)
if res[1]<confidence:
if idx>=drift_index:
tp_1 = idx
return tp_1, tp_2, tp_3, fp_count_1, fp_count_2, fp_count_3
else:
fp_count_1+=1
test_window_1.pop(0)
test_window_1.append(features[idx][0])
else:
test_window_1.append(features[idx][0])
#feature 2
if len(test_window_2)==w_size:
res = cdd_2(test_window_2, reference_window_2)
if res[1]<confidence:
if idx>=drift_index:
tp_2 = idx
return tp_1, tp_2, tp_3, fp_count_1, fp_count_2, fp_count_3
else:
fp_count_2+=1
test_window_2.pop(0)
test_window_2.append(features[idx][1])
else:
test_window_2.append(features[idx][1])
#feature 3
if len(test_window_3)==w_size:
res = cdd_3(test_window_3, reference_window_3)
if res[1]<confidence:
if idx>=drift_index:
tp_3 = idx
return tp_1, tp_2, tp_3, fp_count_1, fp_count_2, fp_count_3
else:
fp_count_3+=1
test_window_3.pop(0)
test_window_3.append(features[idx][2])
else:
test_window_3.append(features[idx][2])
return tp_1, tp_2, tp_3, fp_count_1, fp_count_2, fp_count_3
def river_cd(features, drift_index, cdd_1, cdd_2, cdd_3):
fp_count_1, fp_count_2, fp_count_3 = 0,0,0
tp_1, tp_2, tp_3 = None, None, None
for idx in range(len(features[1900:])):
idx = idx+1900
#first feature
cdd_1.update(features[idx][0])
if cdd_1.drift_detected:
if idx >= drift_index:
tp_1 = idx
return tp_1, tp_2, tp_3, fp_count_1, fp_count_2, fp_count_3
else:
fp_count_1+=1
#second feature
cdd_2.update(features[idx][1])
if cdd_2.drift_detected:
if idx >= drift_index:
tp_2 = idx
return tp_1, tp_2, tp_3, fp_count_1, fp_count_2, fp_count_3
else:
fp_count_2+=1
#third feature
cdd_3.update(features[idx][2])
if cdd_3.drift_detected:
if idx >= drift_index:
tp_3 = idx
return tp_1, tp_2, tp_3, fp_count_1, fp_count_2, fp_count_3
else:
fp_count_3+=1
return tp_1, tp_2, tp_3, fp_count_1, fp_count_2, fp_count_3
def error_rate_drift(dataset_name, drift_index):
#retrieve actual labels and predicted labels
ddm_tp, ddm_fp, ddm_missed =[], [], []
eddm_tp, eddm_fp, eddm_missed =[], [], []
adwin_tp, adwin_fp, adwin_missed =[], [], []
kswin_tp, kswin_fp, kswin_missed =[], [], []
mannu_tp, mannu_fp, mannu_missed =[], [], []
ks_tp, ks_fp, ks_missed =[], [], []
for i in range(1,6):
print(i, "next dataset")
features = np.load(f"datasets/features_{dataset_name}_{i}.npy")
#ddm
ddm_drift_1 = drift.binary.DDM()
ddm_drift_2 = drift.binary.DDM()
ddm_drift_3 = drift.binary.DDM()
tp_1, tp_2, tp_3, fp_count_1, fp_count_2, fp_count3 = river_cd(features, drift_index, ddm_drift_1,ddm_drift_2, ddm_drift_3)
print(tp_1, tp_2, tp_3, fp_count_1, fp_count_2, fp_count3)
#eddm
eddm_drift_1 = drift.binary.EDDM()
eddm_drift_2 = drift.binary.EDDM()
eddm_drift_3 = drift.binary.EDDM()
tp_1, tp_2, tp_3, fp_count_1, fp_count_2, fp_count3 = river_cd(features, drift_index, eddm_drift_1, eddm_drift_2, eddm_drift_3)
print(tp_1, tp_2, tp_3, fp_count_1, fp_count_2, fp_count3)
#adwin
adwin_drift_1 = drift.ADWIN()
adwin_drift_2 = drift.ADWIN()
adwin_drift_3 = drift.ADWIN()
tp_1, tp_2, tp_3, fp_count_1, fp_count_2, fp_count3 = river_cd(features, drift_index, adwin_drift_1, adwin_drift_2, adwin_drift_3)
print(tp_1, tp_2, tp_3, fp_count_1, fp_count_2, fp_count3)
#kswin
kswin_drift_1 = drift.KSWIN(window_size=200, stat_size=100)
kswin_drift_2 = drift.KSWIN(window_size=200, stat_size=100)
kswin_drift_3 = drift.KSWIN(window_size=200, stat_size=100)
tp_1, tp_2, tp_3, fp_count_1, fp_count_2, fp_count3 = river_cd(features, drift_index, kswin_drift_1, kswin_drift_2, kswin_drift_3)
print(tp_1, tp_2, tp_3, fp_count_1, fp_count_2, fp_count3)
#mannu
w_size = 100
confidence = 0.005
mannu_1 = stats.mannwhitneyu
mannu_2 = stats.mannwhitneyu
mannu_3 = stats.mannwhitneyu
tp_1, tp_2, tp_3, fp_count_1, fp_count_2, fp_count3 = custom_cd(features, drift_index, w_size, confidence, mannu_1, mannu_2, mannu_3)
print(tp_1, tp_2, tp_3, fp_count_1, fp_count_2, fp_count3)
#ks
w_size = 100
confidence = 0.005
ks_1 = stats.ks_2samp
ks_2 = stats.ks_2samp
ks_3 = stats.ks_2samp
tp_1, tp_2, tp_3, fp_count_1, fp_count_2, fp_count3 = custom_cd(features, drift_index, w_size, confidence, ks_1, ks_2, ks_3)
print(tp_1, tp_2, tp_3, fp_count_1, fp_count_2, fp_count3)
def main():
#params
dataset_name = "SEA_2_3"
drift_index = 5000
#3 functions for error-rate, features and 3-drift
error_rate_drift(dataset_name, drift_index)
#data = [89,32,55,51]
#print(np.mean(data), np.std(data))
if __name__ == '__main__':
main()