forked from SuryodayBasak/StockMarketPedicition
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ModelEvaluation.py
145 lines (113 loc) · 3.98 KB
/
ModelEvaluation.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
import os
import numpy as np
from sklearn.metrics import roc_curve,auc, confusion_matrix
from matplotlib import pyplot as plt
from matplotlib import animation
from sklearn.ensemble import RandomForestClassifier
class Evaluator:
def __init__(self,xtest,ytest,y_pred,LearningModel):
self.xtest = xtest
self.ytest = ytest
self.y_pred = y_pred
self.model = LearningModel
def confusionMatrix(self):
self.confusion_matrix = confusion_matrix(self.ytest, self.y_pred)
#print self.ytest
#print self.y_pred
"""
def getPerformanceMetrics(self):
self.confusionMatrix()
accuracy = (
float((self.confusion_matrix[0][0]+self.confusion_matrix[1][1]))/
(self.confusion_matrix[0][0]+self.confusion_matrix[0][1]+self.confusion_matrix[1][0]+self.confusion_matrix[1][1])
)
precision = (
float((self.confusion_matrix[1][1]))/
(self.confusion_matrix[1][1] + self.confusion_matrix[0][1])
)
recall = (
float((self.confusion_matrix[1][1]))/
(self.confusion_matrix[1][1]+self.confusion_matrix[1][0])
)
specificity = (
float((self.confusion_matrix[0][0]))/
(self.confusion_matrix[0][0] + self.confusion_matrix[0][1])
)
return accuracy, recall, precision, specificity
"""
def getPerformanceMetrics(self):
flag = 0
for i in range(len(self.ytest)):
if self.ytest[i] != self.y_pred[i]:
flag = 1
break
if flag == 0:
if -1 in self.ytest:
return len(self.ytest), 0, 0, 0
else:
return 0, 0, len(self.ytest), 0
self.confusionMatrix()
return self.confusion_matrix[0][0],self.confusion_matrix[1][0],self.confusion_matrix[1][1],self.confusion_matrix[0][1]
def drawROC(self):
base_dir = os.path.dirname
abspath = os.path.abspath
dir_name = base_dir(base_dir(base_dir(abspath(__file__))))
y_prob = self.model.predict_proba(self.xtest)
true_probability_estimate = y_prob[:,1]
fpr,tpr,threshold = roc_curve(self.ytest,true_probability_estimate)
area = auc(fpr,tpr)
plt.figure()
plt.plot(fpr,tpr,linewidth = 2.0,label = "ROC curve (Area= %0.2f)" % area)
plt.plot([0,1],[0,1],"r--")
plt.xlabel("False Postive Rate")
plt.ylabel("True Positive Rate")
plt.legend(loc = "lower right")
plt.show(block = False)
#plt.savefig(savepath)
#plt.close()
def oob_vs_n_trees(self,max_trees,Xtrain, ytrain):
# First set up the figure, the axis, and the plot element we want to animate
print("")
print("Number of Trees\t\tOOB Error Rate")
fig = plt.figure()
ax = plt.axes(xlim=(0, max_trees), ylim=(0,1))
line, = ax.plot([], [], lw=2)
# initialization function: plot the background of each frame
def init():
line.set_data([], [])
return line,
number_of_trees = range(2,max_trees + 1)
oob_errors = []
# animation function. This is called sequentially
def animate(i):
model = RandomForestClassifier(warm_start = True, oob_score = True, n_estimators = i)
model.fit(Xtrain,ytrain)
oob_error = 1 - model.oob_score_
oob_errors.append(oob_error)
print("{}\t\t\t{}".format(i,oob_error))
line.set_data(number_of_trees[:len(oob_errors)], oob_errors)
return line,
# call the animator. blit=True means only re-draw the parts that have changed.
anim = animation.FuncAnimation(fig, animate, init_func=init, frames=number_of_trees, interval=100, blit=True, repeat = False)
plt.xlabel("Number of trees")
plt.ylabel("OOB error")
plt.show()
# for i in xrange(2,max_trees + 1):
# model = RandomForestClassifier(warm_start = True,
# oob_score = True,
# n_estimators = i)
# model.fit(Xtrain,ytrain)
# oob_error = 1 - model.oob_score_
# oob_errors.append(oob_error)
# print i,oob_error
def plotClassificationResult(self):
self.confusionMatrix()
#x = [i + 3.0 for i in xrange(4)]
x = [i + 3.0 for i in range(4)]
xlabel = ["TP","FN","FP","TN"]
plt.figure()
plt.grid(True)
plt.bar(x,self.confusion_matrix.reshape(-1), color= np.random.random((4,3)))
#plt.xticks([i + 3.0 for i in xrange(4)],xlabel)
plt.xticks([i + 3.0 for i in range(4)],xlabel)
plt.show(block = False)