-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrun_rf.py
More file actions
123 lines (103 loc) · 4.28 KB
/
run_rf.py
File metadata and controls
123 lines (103 loc) · 4.28 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
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
import time
from imblearn.over_sampling import ADASYN, SMOTE
import numpy as np
from cgan_models import CGAN
from gen_class import GenClass
from warnings import simplefilter
from sklearn.ensemble import RandomForestClassifier
from utils import process_wearable_dataset
from sklearn.metrics import f1_score
import logging
from process_occutherm import process_occutherm
# ignore all future warnings
simplefilter(action='ignore', category=FutureWarning)
logging.basicConfig(level=logging.DEBUG, filename="rf_logfile_occutherm", filemode="a+",
format="%(message)s")
all_class0 = [10, 100, 200, 300, 400, 1000]
all_class1 = [10, 100, 200, 1000, 1000, 1000]
runs = 5
for j0, j1 in zip(all_class0, all_class1):
all_b = np.zeros((runs, 2))
all_g = np.zeros((runs, 3))
all_c = np.zeros((runs, 3))
all_smote = np.zeros((runs, 2))
all_ada = np.zeros((runs, 2))
imb = []
print('***********')
for i in range(runs):
start_time = time.time()
X_train, y_train, X_test, y_test, mcir = process_occutherm(j0, j1)
# X_train, y_train, X_test, y_test, mcir = process_wearable_dataset(j)
n, d = X_train.shape
imb.append(mcir)
X = 0
y = 0
model = RandomForestClassifier()
model.fit(X_train, y_train)
y_pred = model.predict(X_test)
baseline_test = (y_pred == y_test).mean()
baseline_f1 = f1_score(y_test, y_pred, average='weighted')
all_b[i] = np.array([baseline_f1, baseline_test])
gen_class = GenClass(d, num_classes=3)
gen_scores = gen_class.train(X_train, y_train, X_test, y_test, RandomForestClassifier(), num_epoch=200)
all_g[i] = np.array(gen_scores)
print(':::::')
cgan = CGAN(d, num_classes=3)
scores = cgan.train(X_train, y_train, X_test, y_test, RandomForestClassifier(), num_epoch=2000)
all_c[i] = np.array(scores)
print('SMOTE')
sm = SMOTE()
X_smote, y_smote = sm.fit_resample(X_train.copy(), y_train.copy())
print(np.bincount(y_smote))
model = RandomForestClassifier()
model.fit(X_smote, y_smote)
y_pred = model.predict(X_test)
smote_test = (y_pred == y_test).mean()
smote_f1 = f1_score(y_test, y_pred, average='weighted')
all_smote[i] = np.array([smote_f1, smote_test])
print('ADASYN')
sm = ADASYN()
X_smote, y_smote = sm.fit_resample(X_train.copy(), y_train.copy())
print(np.bincount(y_smote))
model = RandomForestClassifier()
model.fit(X_smote, y_smote)
y_pred = model.predict(X_test)
smote_test = (y_pred == y_test).mean()
smote_f1 = f1_score(y_test, y_pred, average='weighted')
all_ada[i] = np.array([smote_f1, smote_test])
# print('*************************')
# print(i)
# print('Baseline')
# print(baseline_f1)
# print('*************************')
print('Gen Class')
print(gen_scores[0])
# print('*************************')
print('CGAN')
print(scores[0])
# print('Time taken to run in minutes: '+str((time.time()-start_time)/60))
# print('*************************')
# print()
# print()
logging.info('##################################')
logging.info('MCIR: '+str(np.mean(imb)))
logging.info('##################################')
logging.info('F1 Scores')
logging.info('Baseline: '+str(np.mean(all_b[:, 0])))
logging.info('Gen: '+str(np.mean(all_g[:, 0])))
logging.info('CGAN: '+str(np.mean(all_c[:, 0])))
logging.info('SMOTE: '+str(np.mean(all_smote[:, 0])))
logging.info('ADASYN: '+str(np.mean(all_ada[:, 0])))
logging.info('##################################')
logging.info('Test Accuracy')
logging.info('Baseline: '+str(np.mean(all_b[:, 1])))
logging.info('Gen: '+str(np.mean(all_g[:, 1])))
logging.info('CGAN: '+str(np.mean(all_c[:, 1])))
logging.info('SMOTE: '+str(np.mean(all_smote[:, 1])))
logging.info('ADASYN: '+str(np.mean(all_ada[:, 1])))
logging.info('##################################')
logging.info('')
# print('##################################')
# print('Epochs')
# print('Gen: '+str(np.mean(all_g[:, 2])))
# print('CGAN: '+str(np.mean(all_c[:, 2])))