-
Notifications
You must be signed in to change notification settings - Fork 32
/
test.py
56 lines (45 loc) · 1.67 KB
/
test.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
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from sklearn import preprocessing
from sklearn.naive_bayes import GaussianNB
from classifier.detector_classifier import DetectorClassifier
from concept_drift.adwin import AdWin
from concept_drift.page_hinkley import PageHinkley
from evaluation.prequential import prequential
def read_data(filename):
df = pd.read_csv(filename)
data = df.values
return data[:, :-1], data[:, -1]
if __name__ == '__main__':
n_train = 100
X, y = read_data("data/elecNormNew.csv")
# Set x,y as numeric
X = X.astype(float)
label = ["UP", "DOWN"]
le = preprocessing.LabelEncoder()
le.fit(label)
y = le.transform(y)
w = 1000
clfs = [
GaussianNB(),
DetectorClassifier(GaussianNB(), PageHinkley(), np.unique(y)),
DetectorClassifier(GaussianNB(), AdWin(), np.unique(y))
]
clfs_label = ["GaussianNB", "Page-Hinkley", "AdWin"]
plt.title("Accuracy (exact match)")
plt.xlabel("Instances")
plt.ylabel("Accuracy")
for i in range(len(clfs)):
print("\n{}:".format(clfs_label[i]))
with np.errstate(divide='ignore', invalid='ignore'):
y_pre, time = prequential(X, y, clfs[i], n_train)
if clfs[i].__class__.__name__ == "DetectorClassifier":
print("Drift detection: {}".format(clfs[i].change_detected))
estimator = (y[n_train:] == y_pre) * 1
acc_run = np.convolve(estimator, np.ones((w,)) / w, 'same')
print("Mean acc within the window {}: {}".format(w, np.mean(acc_run)))
plt.plot(acc_run, "-", label=clfs_label[i])
plt.legend(loc='lower right')
plt.ylim([0, 1])
plt.show()