-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEnsambleAvalonAL.py
More file actions
executable file
·233 lines (198 loc) · 6.85 KB
/
EnsambleAvalonAL.py
File metadata and controls
executable file
·233 lines (198 loc) · 6.85 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
from matplotlib import pyplot
import matplotlib.pyplot as plt
import pybrain
from pybrain.tools.shortcuts import buildNetwork
from pybrain.datasets import SupervisedDataSet
from pybrain.supervised.trainers import BackpropTrainer
from mpl_toolkits.axes_grid1.inset_locator import inset_axes
from sklearn.svm import SVC
import random as rnd
import pandas as pd
import numpy as np
import csv
from mpl_toolkits.mplot3d import Axes3D
from matplotlib.ticker import LinearLocator
from sklearn.metrics import roc_curve, auc
from sklearn.cross_validation import train_test_split
from sklearn.metrics import confusion_matrix
from sklearn import tree
import pylab as pl
from matplotlib.ticker import MultipleLocator
from pandas_confusion import ConfusionMatrix
from sklearn.metrics import mean_squared_error
from sklearn.metrics import classification_report
from sklearn.metrics import accuracy_score
from sklearn import tree
from sklearn.ensemble import RandomForestClassifier
from sklearn.ensemble import AdaBoostClassifier
df = pd.read_csv("abalonecsv.csv")
col_names = df.columns.tolist()
print (col_names)
to_show = col_names[:8] #+ col_names[-9:]
print "\nSample data:"
print(df[to_show].head(8))
X_train, X_test, Y_train, Y_test = train_test_split(df[["Lenght","Diameter","Height","Wweight","Shuweight","Vweight","Sheweight","Rings"]],df[["Sex"]], train_size=0.75)
###################DECISION TREE###########################33
clf = tree.DecisionTreeClassifier()
clf.fit(X_train, np.ravel(Y_train))
Y_pred = clf.predict(X_test)
#Creamos la matriz
y = np.array(Y_test)
class_names = np.unique(y)
print(class_names)
cmtrx = confusion_matrix(Y_test,Y_pred,labels=class_names)
labels = class_names
print("Confusion matrix Tree:\n%s" % cmtrx)
#Metrics de la matriz
#print(pd.DataFrame(Y_test).tail())
#print(pd.DataFrame(Y_pred).tail())
y_true = Y_test
y_predi = Y_pred
#print(pd.crosstab(y_true, y_predi, rownames=['True'], colnames=['Predicted'], margins=True))
print('classif_report tree',classification_report(y_true, y_predi))
print('accuracy_score tree',accuracy_score(y_true, y_predi))
MSE = mean_squared_error(Y_test, Y_pred)
print('MSE tree',MSE)
norm_conf = []
for i in cmtrx:
a = 0
tmp_arr = []
a = sum(i, 0)
for j in i:
tmp_arr.append(float(j)/float(a))
norm_conf.append(tmp_arr)
fig = plt.figure()
plt.clf()
ax = fig.add_subplot(111)
ax.set_aspect(1)
res = ax.imshow(np.array(norm_conf), cmap=plt.cm.coolwarm,
interpolation='nearest')
width = len(cmtrx)
height = len(cmtrx[0])
for x in xrange(width):
for y in xrange(height):
ax.annotate(str(cmtrx[x][y]), xy=(y, x),
horizontalalignment='center',
verticalalignment='center')
cb = fig.colorbar(res)
plt.title('Confusion Matrix Tree')
plt.xticks(range(width), labels[:width])
plt.yticks(range(height), labels[:height])
plt.xlabel('Predicted')
plt.ylabel('True')
plt.savefig('confusion_matrix.png', format='png')
plt.show()
###################RANDOM FOREST###########################33
clf = RandomForestClassifier(n_estimators=10)
clf.fit(X_train, np.ravel(Y_train))
Y_pred = clf.predict(X_test)
#Creamos la matriz
y = np.array(Y_test)
class_names = np.unique(y)
print(class_names)
cmtrx = confusion_matrix(Y_test,Y_pred,labels=class_names)
labels = class_names
print("Confusion matrix RndForest:\n%s" % cmtrx)
#Metrics de la matriz
#print(pd.DataFrame(Y_test).tail())
#print(pd.DataFrame(Y_pred).tail())
y_true = Y_test
y_predi = Y_pred
#print(pd.crosstab(y_true, y_predi, rownames=['True'], colnames=['Predicted'], margins=True))
print('classif_report RndForst',classification_report(y_true, y_predi))
print('accuracy_score RndForst',accuracy_score(y_true, y_predi))
MSE = mean_squared_error(Y_test, Y_pred)
print('MSE RndForst',MSE)
norm_conf = []
for i in cmtrx:
a = 0
tmp_arr = []
a = sum(i, 0)
for j in i:
tmp_arr.append(float(j)/float(a))
norm_conf.append(tmp_arr)
fig = plt.figure()
plt.clf()
ax = fig.add_subplot(111)
ax.set_aspect(1)
res = ax.imshow(np.array(norm_conf), cmap=plt.cm.coolwarm,
interpolation='nearest')
width = len(cmtrx)
height = len(cmtrx[0])
for x in xrange(width):
for y in xrange(height):
ax.annotate(str(cmtrx[x][y]), xy=(y, x),
horizontalalignment='center',
verticalalignment='center')
cb = fig.colorbar(res)
plt.title('Confusion Matrix RndForst')
plt.xticks(range(width), labels[:width])
plt.yticks(range(height), labels[:height])
plt.xlabel('Predicted')
plt.ylabel('True')
plt.savefig('confusion_matrix.png', format='png')
plt.show()
###################DECISION TREE ADABOOST###########################33
clf = AdaBoostClassifier(tree.DecisionTreeClassifier(),n_estimators=5000,learning_rate=1)
clf.fit(X_train, np.ravel(Y_train))
Y_pred = clf.predict(X_test)
#Creamos la matriz
y = np.array(Y_test)
class_names = np.unique(y)
print(class_names)
cmtrx = confusion_matrix(Y_test,Y_pred,labels=class_names)
labels = class_names
print("Confusion matrix AdBTree:\n%s" % cmtrx)
#Metrics de la matriz
#print(pd.DataFrame(Y_test).tail())
#print(pd.DataFrame(Y_pred).tail())
y_true = Y_test
y_predi = Y_pred
#print(pd.crosstab(y_true, y_predi, rownames=['True'], colnames=['Predicted'], margins=True))
print('classif_report AdBtree',classification_report(y_true, y_predi))
print('accuracy_score AdBtree',accuracy_score(y_true, y_predi))
MSE = mean_squared_error(Y_test, Y_pred)
print('MSE AdBtree',MSE)
norm_conf = []
for i in cmtrx:
a = 0
tmp_arr = []
a = sum(i, 0)
for j in i:
tmp_arr.append(float(j)/float(a))
norm_conf.append(tmp_arr)
fig = plt.figure()
plt.clf()
ax = fig.add_subplot(111)
ax.set_aspect(1)
res = ax.imshow(np.array(norm_conf), cmap=plt.cm.coolwarm,
interpolation='nearest')
width = len(cmtrx)
height = len(cmtrx[0])
for x in xrange(width):
for y in xrange(height):
ax.annotate(str(cmtrx[x][y]), xy=(y, x),
horizontalalignment='center',
verticalalignment='center')
cb = fig.colorbar(res)
plt.title('Confusion Matrix AdBTree')
plt.xticks(range(width), labels[:width])
plt.yticks(range(height), labels[:height])
plt.xlabel('Predicted')
plt.ylabel('True')
plt.savefig('confusion_matrix.png', format='png')
plt.show()
'''
# Graficamos la curva ROC
fpr, tpr, thresholds = roc_curve(Y_test, Y_pred)
fig = plt.figure()
ax = fig.add_subplot(111)
plt.plot(fpr, tpr, lw=2, label='Roc Curve Tree', color='gray')
plt.axis([-0.5, 1.05, -0.05, 1.05])
plt.title("Roc Curve Tree", color='darkblue')
plt.plot([-.05, 1.5], [-.05, 1.5], '--', color=(.6, .6, .6), label='Barrera')
plt.xlabel('FP', color='red')
plt.ylabel('TP', color="red")
plt.legend(loc="upper left")
plt.show()
'''''