-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRandom_Forest.py
59 lines (47 loc) · 2.11 KB
/
Random_Forest.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
from Preprocessing import Load_mushroom_dataset_RF
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score,f1_score,precision_score,recall_score
import time as time
class Mushroom:
def __init__(self, x_train, x_test, y_train, y_test):
self.x_train = x_train
self.x_test = x_test
self.y_train = y_train
self.y_test = y_test
def Random_Forest_classifier(self):
rf_classifier = RandomForestClassifier(criterion='gini', n_estimators=10, random_state=10, max_features=10)
start_time = time.time()
# train the classifier
rf_classifier.fit(self.x_train, self.y_train)
# test the classifier
y_predicted = rf_classifier.predict(self.x_test)
# calculating accuracy
accuracy = accuracy_score(self.y_test, y_predicted)
f1score = f1_score(self.y_test, y_predicted)
precisionscore = precision_score(self.y_test, y_predicted)
recallscore = recall_score(self.y_test, y_predicted)
# end time
end_time = time.time()
# calculate total time
total_time = end_time - start_time
# display accuracy and time
print("-------------------------------------")
print("Classifier: Random Forest")
print("Accuracy:", (accuracy * 100), "%")
# print("F1 Score:", (f1score * 100), "%")
# print("Precision Score:", (precisionscore * 100), "%")
# print("Recall Score:", (recallscore * 100), "%")
print("Time:", total_time, "sec")
print("-------------------------------------")
return accuracy
def ClassifyRF():
x_train, x_test, y_train, y_test = Load_mushroom_dataset_RF()
mushroom_model = Mushroom(x_train, x_test, y_train, y_test)
accuracy = mushroom_model.Random_Forest_classifier()
return accuracy
def timeRF():
x_train, x_test, y_train, y_test = Load_mushroom_dataset_RF()
mushroom_model = Mushroom(x_train, x_test, y_train, y_test)
total_time=mushroom_model.Random_Forest_classifier()
return total_time
ClassifyRF()