-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRandomForestsL4.py
More file actions
316 lines (244 loc) · 12.9 KB
/
Copy pathRandomForestsL4.py
File metadata and controls
316 lines (244 loc) · 12.9 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
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
# -*- coding: utf-8 -*-
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
import forestci as fci
from sklearn.metrics import accuracy_score
from sklearn.metrics import confusion_matrix
#from sklearn import make_classification
import pandas as pd
from dummy_var import preprocessData as ppd
import matplotlib.pyplot as plt
from sklearn.preprocessing import normalize
from sklearn.model_selection import StratifiedKFold as skf
#from sklearn.feature_extraction
"""<<<<<<<<<<<<<<<<<<<<<<<<<<Datasets' Filepaths>>>>>>>>>>>>>>>>>>>>>>>>>"""
TRAIN_PATH = 'C:\\Users\\joao-\\OneDrive\\Documentos\\Ciencia de Dados\\Project\\CD\\datasets\\under_sampling_aps_training.csv'
TEST_PATH = 'C:\\Users\\joao-\\OneDrive\\Documentos\\Ciencia de Dados\\Project\\CD\\datasets\\aps_test_average.csv'
"""Train Dataset Import and dummyfication"""
df_train = pd.read_csv(TRAIN_PATH, delimiter=',')
df_train= ppd(df_train)
"""Test Dataset Import and dummyfication"""
df_test = pd.read_csv(TEST_PATH, delimiter=',')
df_test = ppd(df_test)
"""Train Dataset Normalization"""
df_train['ab_000']=df_train['ab_000'].astype(float)
X_train = df_train.iloc[:, 2:(len(df_train.columns))].values
X_train = normalize(X_train)
"""Making the class attribution of the Train dataset"""
y_train = df_train.iloc[:, 0:2].values
"""Test Dataset Normalization"""
df_test['ab_000']=df_test['ab_000'].astype(float)
X_test =df_test.iloc[:, 2:(len(df_test.columns))].values
X_test = normalize(X_test)
"""Making the class attribution of the Test dataset"""
y_test = df_test.iloc[:, 0:2].values
#print(df.columns)
#X = df.iloc[:, 0:2].values
##print (X)
##print(df.columns)
#y = df.iloc[:, 2:(len(df.columns))]
K_Fold=skf(n_splits=10)
"""<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"""
"""<<<<<<<<<<<<<<<<<<<<<<<<<<<<< Callables >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"""
"""<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"""
"""<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"""
"""<<<<<<<<<<<<<<<<<<<<<<<<<<< minDifference >>>>>>>>>>>>>>>>>>>>>>>>>>>>"""
"""<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"""
"""Returns the index whose accuracy difference between """
def minDifference(lists, test_accuracy, train_accuracy):
n=0
curIndex=0
curMin=10
for accDiff in lists:
if accDiff<curMin and test_accuracy[n] > 0.8 and train_accuracy[n] > 0.8:
curIndex=n
curMin=accDiff
n+=1
return curIndex
"""<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"""
"""<<<<<<<<<<<<<<<<<<<<<<<<< Vary Estimators >>>>>>>>>>>>>>>>>>>>>>>>>"""
"""<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"""
"""calulates the accuracies for each value of estimators and depths"""
def forestEstimatorDepthAccuracies(crit, XTrain, YTrain):
depths = range(1,11)
estimators = [10, 20, 30]#, 40, 50, 60, 70, 80, 90, 100]
#for each value of estimators
for estimator in estimators:
test_accuracy=[]
train_accuracy = []
#for each value of depth
for depth in depths:
clf = RandomForestClassifier( n_estimators = estimator, criterion=crit, max_depth=depth, max_features=12)
clf.fit(XTrain, YTrain)
predict = clf.predict(X_test)
acc = accuracy_score(y_test, predict)*100
print("Accuracy test: " + str(acc))
test_accuracy.append(acc)
conf_matrix = confusion_matrix(y_test.argmax(axis=1), predict.argmax(axis=1))
print(conf_matrix)
pred1= clf.predict(XTrain)
acc1 = accuracy_score(YTrain, pred1)*100
print("Accuracy train: " + str(acc1))
train_accuracy.append(acc1)
conf_matrix = confusion_matrix(YTrain.argmax(axis=1), pred1.argmax(axis=1))
print(conf_matrix)
# confidence = fci.random_forest_error(clf, X_train, X_test )
#confidence=0
# print ('confidence = ' + str(confidence))
Labels = range(1,12)
plt.plot( minSplit, test_accuracy, color = 'r', label = 'Test')
plt.plot( minSplit, train_accuracy, color='b', label = 'Train)
plt.xticks(depths, Labels)
plt.ylabel('Accuracy')
plt.xlabel('Tree Depth')
plt.savefig(('Estimators&Depth\\graph'+str(estimator)+'est.png'), dpi=100)
plt.show()
"""<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"""
"""<<<<<<<<<<<<<<<<<<<<<<<<<<<< Vary Features >>>>>>>>>>>>>>>>>>>>>>>>>>>"""
"""<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"""
def maxFeaturesForest(crit, XTrain, YTrain):
test_accuracy=[]
train_accuracy = []
differenceBetweenAccuracies=[]
max_feat=range(1,171,1)
#for each value of depth
for feat in max_feat:
d_train=open("Matrix\\"+str(feat)+"features_"+str(crit)+"_train.txt",'w')
d_test=open("Matrix\\"+str(feat)+"features_"+str(crit)+"_test.txt",'w')
clf = RandomForestClassifier(criterion=crit, max_features=feat)
clf.fit(XTrain, YTrain)
predict = clf.predict(X_test)
acc = accuracy_score(y_test, predict)*100
print("Accuracy test: " + str(acc))
test_accuracy.append(acc)
conf_matrix = confusion_matrix(y_test.argmax(axis=1), predict.argmax(axis=1))
# print(conf_matrix)
d_test.write(str(conf_matrix))
pred1= clf.predict(XTrain)
acc1 = accuracy_score(YTrain, pred1)*100
print("Accuracy train: " + str(acc1))
train_accuracy.append(acc1)
conf_matrix = confusion_matrix(YTrain.argmax(axis=1), pred1.argmax(axis=1))
# print(conf_matrix)
d_train.write(str(conf_matrix))
differenceBetweenAccuracies.append((acc1-acc))
# confidence = fci.random_forest_error(clf, X_train, X_test )
#confidence=0
# print ('confidence = ' + str(confidence))
# Labels = depths
plt.plot( minSplit, test_accuracy, color = 'r', label = 'Test')
plt.plot( minSplit, train_accuracy, color='b', label = 'Train)
# plt.xticks(depths, Labels)
plt.ylabel('Accuracy')
plt.xlabel('N of Features')
plt.savefig(('Features\\graph'+str(feat)+'feat'+str(crit)), dpi=100)
plt.show()
print(differenceBetweenAccuracies)
n = (minDifference(differenceBetweenAccuracies, test_accuracy, train_accuracy))
print('index: '+str(n)+'\nvalue: '+str(differenceBetweenAccuracies[n])+'\ntrain_acc: ' + str(train_accuracy[n]) + '\ntest_acc' + str(test_accuracy[n]))
# print(str(n)+diffe)
"""<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"""
"""<<<<<<<<<<<<<<<<<<<<<<<<< Vary Sample Splits >>>>>>>>>>>>>>>>>>>>>>>>>"""
"""<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"""
def minSampleSplitForest(crit, XTrain, YTrain):
test_accuracy=[]
train_accuracy = []
differenceBetweenAccuracies=[]
minSplit=[0.05,0.1, 0.15, 0.2, 0.25,0.3,0.35,0.4,0.45, 0.5, 0.55,0.6, 0.65,0.7, 0.75,0.8,0.85,0.9]
#for each value of depth
for split in minSplit:
d_train=open("Matrix\\"+str(split)+"splits_"+str(crit)+"_train.txt",'w')
d_test=open("Matrix\\"+str(split)+"splits_"+str(crit)+"_test.txt",'w')
clf = RandomForestClassifier(criterion=crit, min_samples_split=split)
clf.fit(X_train, y_train)
predict = clf.predict(X_test)
acc = accuracy_score(y_test, predict)
# print("Accuracy test: ")# + str(acc))
test_accuracy.append(acc)
conf_matrix = confusion_matrix(y_test.argmax(axis=1), predict.argmax(axis=1))
d_test.write(str(conf_matrix))
pred1= clf.predict(X_train)
acc1 = accuracy_score(y_train, pred1)
# print("Accuracy train: " )#+ str(acc1))
train_accuracy.append(acc1)
conf_matrix = confusion_matrix(y_train.argmax(axis=1), pred1.argmax(axis=1))
d_train.write(str(conf_matrix))
differenceBetweenAccuracies.append(abs(acc1-acc))
# confidence = fci.random_forest_error(clf, X_train, X_test )
#confidence=0
# print ('confidence = ' + str(confidence))
Labels = [0.05,0.1, 0.15, 0.2, 0.25,0.3,0.35,0.4,0.45, 0.5, 0.55,0.6, 0.65,0.7, 0.75,0.8,0.85,0.9]
#[0.1,0.2,0.3,0.4,0.5,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
plt.plot( minSplit, test_accuracy, color = 'r', label = 'Test')
plt.plot( minSplit, train_accuracy, color='b', label = 'Train)
plt.xticks(minSplit, Labels)
plt.ylabel('Accuracy')
plt.xlabel('Sample Splits')
plt.savefig(('SampleSplits\\graph'+str(split)+'split'+str(crit)+'.png'), dpi=100)
plt.show()
# print(differenceBetweenAccuracies)
n = (minDifference(differenceBetweenAccuracies, test_accuracy, train_accuracy))
print('index: '+str(n)+'\nvalue: '+str(differenceBetweenAccuracies[n])+'\ntrain_acc: ' + str(train_accuracy[n]) + '\ntest_acc' + str(test_accuracy[n]))
"""<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"""
"""<<<<<<<<<<<<<<<<<<<<<<<<< Calls to functions >>>>>>>>>>>>>>>>>>>>>>>>>"""
"""<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"""
def minSampleLeaf(crit, XTrain, YTrain):
test_accuracy=[]
train_accuracy = []
differenceBetweenAccuracies=[]
minLeaf=[0.1, 0.15,0.2,0.25,0.3,0.35, 0.4, 0.45, 0.5]#range(1,1000,50)
#for each value of depth
for leafs in minLeaf:
d_train=open("Matrix\\"+str(leafs)+"leafs"+str(crit)+"_train.txt",'w')
d_test=open("Matrix\\"+str(leafs)+"leafs"+str(crit)+"_test.txt",'w')
clf = RandomForestClassifier(criterion=crit, min_samples_leaf=leafs)
clf.fit(X_train, y_train)
predict = clf.predict(X_test)
acc = accuracy_score(y_test, predict)
# print("Accuracy test: ")# + str(acc))
test_accuracy.append(acc)
conf_matrix = confusion_matrix(y_test.argmax(axis=1), predict.argmax(axis=1))
d_test.write(str(conf_matrix))
pred1= clf.predict(X_train)
acc1 = accuracy_score(y_train, pred1)
# print("Accuracy train: " )#+ str(acc1))
train_accuracy.append(acc1)
conf_matrix = confusion_matrix(y_train.argmax(axis=1), pred1.argmax(axis=1))
d_train.write(str(conf_matrix))
differenceBetweenAccuracies.append(abs(acc1-acc))
# confidence = fci.random_forest_error(clf, X_train, X_test )
# confidence=0
# print ('confidence = ' + str(confidence))
plt.plot( minSplit, test_accuracy, color = 'r', label = 'Test')
plt.plot( minSplit, train_accuracy, color='b', label = 'Train)
# plt.xticks(depths, Labels)
plt.ylabel('Accuracy')
plt.xlabel('Min Sample Leafs')
plt.savefig('SampleLeaf\\graph'+str(leafs)+'leafs'+str(crit)+'.png', dpi=100) #(
plt.show()
# print(differenceBetweenAccuracies)
n = (minDifference(differenceBetweenAccuracies, test_accuracy,train_accuracy))
print('index: '+str(minLeaf[n])+'\nvalue: '+str(differenceBetweenAccuracies[n])+'\ntrain_acc: ' + str(train_accuracy[n]) + '\ntest_acc' + str(test_accuracy[n]))
"""<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"""
"""<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< RUN >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"""
"""<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"""
def runAll():
for train_index, test_index in K_Fold.split(X_train, y_train.argmax(axis=1)):
forestEstimatorDepthAccuracies('entropy',X_train[train_index], y_train[train_index])
forestEstimatorDepthAccuracies('gini', X_train[train_index], y_train[train_index])
minSampleLeaf('entropy', X_train[train_index], y_train[train_index])
minSampleLeaf('gini',X_train[train_index], y_train[train_index])
maxFeaturesForest('entropy', X_train[train_index], y_train[train_index])
maxFeaturesForest('gini', X_train[train_index], y_train[train_index])
minSampleSplitForest('entropy', X_train[train_index], y_train[train_index])
minSampleSplitForest('gini', X_train[train_index], y_train[train_index])
runAll()
"""<<<<<<<<<<<<<<<<<<<<<< IGNORE FROM HERE >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"""
######## Outra implementação das random forests (a primeira funcionou, por isso pus este em comentario)
#RandomForestClassifier(bootstrap=True, class_weight=None, criterion='gini',
# max_depth=2, max_features='auto', max_leaf_nodes=None,
# min_impurity_decrease=0.0, min_impurity_split=None,
# min_samples_leaf=1, min_samples_split=2,
# min_weight_fraction_leaf=0.0, n_estimators=100, n_jobs=None,
# oob_score=False, random_state=0, verbose=0, warm_start=False)
#X, y = make_classification(n_samples=1000, n_features=4, n_informative=2, n_redundant=0, random_state=0, shuffle=False)