-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_program.py
84 lines (66 loc) · 2.94 KB
/
example_program.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
# Copyright 2018-2021 VMware, Inc.
# SPDX-License-Identifier: BSD-3-Clause
from rade_classifier import RadeClassifier
from sklearn.ensemble import RandomForestClassifier
from xgboost import XGBClassifier
from sklearn.datasets import make_classification
from sklearn.metrics import classification_report
from sklearn.model_selection import train_test_split
from sklearn.metrics import f1_score
import time
X, y = make_classification(n_samples=1000000, n_features=4,
n_informative=2, n_redundant=0,
random_state=0, shuffle=False, weights=[0.99, 0.01])
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, stratify=y, random_state=42)
# RADE
rade_clf = RadeClassifier()
start = time.time()
rade_clf.fit(X_train, y_train)
end = time.time()
rade_train_time = end - start
print("RADE training time is: {:0.2f} seconds".format(rade_train_time))
start = time.time()
rade_y_predicted = rade_clf.predict(X_test)
end = time.time()
rade_predict_time = end - start
print("RADE prediction time is: {:0.2f} seconds".format(rade_predict_time))
rade_f1 = f1_score(y_test, rade_y_predicted, average='macro')
print("RADE classification_report:")
print(classification_report(y_test, rade_y_predicted, digits=5))
# Random Forest
rf_clf = RandomForestClassifier()
start = time.time()
rf_clf.fit(X_train, y_train)
end = time.time()
rf_train_time = end - start
print("Random Forest training time is: {:0.2f} seconds".format(rf_train_time))
start = time.time()
rf_y_predicted = rf_clf.predict(X_test)
end = time.time()
rf_predict_time = end - start
print("Random Forest prediction time is: {:0.2f} seconds".format(rf_predict_time))
rf_f1 = f1_score(y_test, rf_y_predicted, average='macro')
print("Random Forest classification_report:")
print(classification_report(y_test, rf_y_predicted, digits=5))
# XGBoost
xgb_clf = XGBClassifier()
start = time.time()
xgb_clf.fit(X_train, y_train)
end = time.time()
xgb_train_time = end - start
print("XGBoost training time is: {:0.2f} seconds".format(xgb_train_time))
start = time.time()
xgb_y_predicted = xgb_clf.predict(X_test)
end = time.time()
xgb_predict_time = end - start
print("XGBoost prediction time is: {:0.2f} seconds".format(xgb_predict_time))
xgb_f1 = f1_score(y_test, xgb_y_predicted, average='macro')
print("XGBoost classification_report:")
print(classification_report(y_test, xgb_y_predicted, digits=5))
print('RADE vs. Random Forest and XGBoost:')
print ('Training time: RADE is {:0.1f}x faster than Random Forest, and {:0.1f}x faster than XGBoost.'.
format(rf_train_time / rade_train_time, xgb_train_time / rade_train_time))
print ('Prediction time: RADE is {:0.1f}x faster than Random Forest, and {:0.1f}x faster than XGBoost.'.
format(rf_predict_time / rade_predict_time, xgb_predict_time / rade_predict_time))
print ('Macro F1: RADE is better by {:+0.5f} as compared to Random Forest, and by {:+0.5f} as compared to XGBoost.'.
format(rade_f1 - rf_f1, rade_f1 - xgb_f1))