-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrndforest.py
245 lines (167 loc) · 6.78 KB
/
rndforest.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
import csv
import random
import numpy as np
from sklearn.ensemble import RandomForestClassifier, ExtraTreesClassifier
from sklearn.metrics import recall_score, precision_score, f1_score, accuracy_score
from sklearn.preprocessing import StandardScaler, OneHotEncoder
from sklearn.feature_selection import SelectFromModel
import matplotlib.pyplot as plt
from imblearn.over_sampling import SMOTE
class CSV_Data:
def __init__(self, file_name):
self.file_name = file_name
self.csv_header = []
self.data_matrix = np.array([])
def read_data(self):
print("Reading started...")
rows_readed = 0
with open(self.file_name, "r") as csv_file:
csv_reader = csv.reader(csv_file, delimiter=",")
header_skipped = False
tmp = []
for row in csv_reader:
l = len(row)
if header_skipped == False:
self.csv_header = row
header_skipped = True
else:
if row[1] == "yes":
row[1] = 1
else:
row[1] = 0
for i in range(2, l):
row[i] = float(row[i])
tmp.append(row[1:])
self.data_matrix = np.array(tmp)
print("Reading finished. Records number: %d" % self.data_matrix.shape[0])
def get_data_matrix(self):
return self.data_matrix
def get_y_vector(self):
return self.data_matrix[0:, 0]
def get_X_matrix(self):
return self.data_matrix[0:, 1:]
def get_header(self):
return self.csv_header
def get_positive_class_objects(m):
l = []
for row in m:
if row[0] == 1:
l.append(row[0:])
return np.array(l)
def get_negative_class_objects(m):
l = []
for row in m:
if row[0] == 0:
l.append(row[0:])
return np.array(l)
def select_n_random_elements(l, n):
np.random.shuffle(l)
return l[0:n], l[n:]
def extract_y_X(m):
return m[0:, 0], m[0:, 1:]
data = CSV_Data("data.csv")
data.read_data()
acc_train = []
accs_test = []
precs = []
recalls = []
f1s = []
y_X_data = data.get_data_matrix()
del data
print("Removing examples started")
data_dict = {}
lst = []
for row in y_X_data:
s = np.sum(row[1:])
if s not in data_dict and row[0] == 0:
data_dict[s] = row
lst.append(row)
elif row[0] == 1:
lst.append(row)
y_X_data = np.array(lst)
del data_dict
print("Removing examples finished")
y_data, X_data = extract_y_X(y_X_data)
print("Searching feature importance started")
clf = RandomForestClassifier(n_estimators=150, max_depth=15)
clf.fit(X_data, y_data)
print("Searching feature importance finished")
print("\n-- Feature importance: %r" % clf.feature_importances_)
print("\nSelecting started.")
X_data = SelectFromModel(clf, prefit=True, threshold="0.75*median").transform(X_data) # medial - 0.75-1
print("Selecting finished.")
print("--X_data shape(after feature selection): (%d, %d)" % (X_data.shape[0], X_data.shape[1]))
categorical_f = []
for i in range(X_data.shape[1]):
lst = len(np.unique(X_data[:, i]))
if lst <= 8 and lst > 2:
categorical_f.append(i)
print("Encoding started.")
X_data = OneHotEncoder(categorical_features=categorical_f).fit_transform(X_data, y_data).toarray()
print("Encoding finished.")
print("--X_data shape(after encoding): (%d, %d)" % (X_data.shape[0], X_data.shape[1]))
# print("SMOTE started")
# sm = SMOTE(kind='regular', ratio=0.1, k_neighbors=10, random_state=42)
# X_data, y_data = sm.fit_sample(X_data, y_data)
# print("SMOTE finished")
y_data = y_data.reshape((y_data.shape[0], 1))
y_X_data = np.hstack((y_data, X_data))
print("--y_X_data shape: (%d, %d)" % (y_X_data.shape[0], y_X_data.shape[1]))
num_of_iterations = 10
for i in range(num_of_iterations):
print("Experiment %d" % (i + 1))
print("\t\tGenerating train and test data...")
pos_objects = get_positive_class_objects(y_X_data)
neg_objects = get_negative_class_objects(y_X_data)
pos_objects_train, pos_objects_test = select_n_random_elements(pos_objects, len(pos_objects) * 2 // 3)
neg_objects_train, neg_objects_test = select_n_random_elements(neg_objects, len(neg_objects) * 2 // 3)
train_data = np.vstack((pos_objects_train, neg_objects_train))
test_data = np.vstack((pos_objects_test, neg_objects_test))
np.random.shuffle(train_data)
y_train, X_train = extract_y_X(train_data)
y_test, X_test = extract_y_X(test_data)
y_test_pos, X_test_pos = extract_y_X(get_positive_class_objects(test_data))
y_test_neg, X_test_neg = extract_y_X(get_negative_class_objects(test_data))
print("\t\tGenerating finished.")
print("\t\t -- Train Examples: %d" % X_train.shape[0])
print("\t\t -- Test Examples: %d" % X_test.shape[0])
print("\t\tTraining started")
clf = RandomForestClassifier(n_estimators=150, max_depth=15, class_weight={1: 1, 0: 1}) # 0.25-1
clf.fit(X_train, y_train)
print("\t\tTraining finished")
print("\t\tTesting started")
prediction = clf.predict(X_test)
accuracy_train = clf.score(X_train, y_train)
accuracy_test = accuracy_score(y_test, prediction)
accuracy_test_pos = clf.score(X_test_pos, y_test_pos)
accuracy_test_neg = clf.score(X_test_neg, y_test_neg)
print("\t\tTesting finished")
prec = precision_score(y_test, prediction) * 100
rcl = recall_score(y_test, prediction) * 100
f1 = f1_score(y_test, prediction) * 100
print("\t\tAccuracy - train: %f" % (accuracy_train * 100))
print("\t\tAccuracy - test: %f" % (accuracy_test * 100))
print("\t\tAccuracy - test positive: %f" % (accuracy_test_pos * 100))
print("\t\tAccuracy - test negative: %f" % (accuracy_test_neg * 100))
print("\t\tPrecision - test: %f" % prec)
print("\t\tRecall - test: %f" % rcl)
print("\t\tF1 - test: %f" % f1)
acc_train.append(accuracy_train * 100)
accs_test.append(accuracy_test * 100)
precs.append(prec)
recalls.append(rcl)
f1s.append(f1)
accuracy_train_average = np.average(acc_train)
accuracy_test_average = np.average(accs_test)
print("Average Accuracy - train: %f" % (accuracy_train_average))
print("Average Accuracy - test: %f" % (accuracy_test_average))
print("Average Precision - train: %f" % (np.average(precs)))
print("Average Recall - test: %f" % (np.average(recalls)))
print("Average F1 - test: %f" % (np.average(f1s)))
plt.plot(range(1, len(accs_test) + 1), accs_test, "ro")
plt.plot(range(1, len(acc_train) + 1), acc_train, "go")
plt.xlim(0, num_of_iterations + 2)
plt.ylim(90, 101)
plt.axhline(accuracy_test_average, xmax=(num_of_iterations + 2), color='r', linestyle='dashed', linewidth=2)
plt.axhline(accuracy_train_average, xmax=(num_of_iterations + 2), color='g', linestyle='dashed', linewidth=2)
plt.show()