This repository has been archived by the owner on Nov 20, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
decision_tree_padel_fe.py
255 lines (176 loc) · 7.25 KB
/
decision_tree_padel_fe.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
246
247
248
249
250
251
252
253
254
255
# -*- coding: utf-8 -*-
"""
Created on Thu Jul 8 20:19:42 2021
@author: dguti
ARBOL DE DECISION CON FEATURE ENGINEERING
"""
import pandas as pd
datos = pd.read_csv("/Users/guill/OneDrive/Escritorio/Master/TFM/base de datos/guardados/Dataset12.csv")
#print(datos.shape)
#print(datos.info())
#%%
import matplotlib.pyplot as plt
plt.hist(datos["tipo_golpe"], bins = 13)
#print(datos.columns)
#%% eliminamos las columnas que no nos interesan
datos.drop(columns = ["mano", "reves", "altura", "edad", "sexo", "nivel","id", "numero_golpe", "tiempo_golpe"], inplace=True)
#%%
#print(datos.columns)
#print(datos.shape)
#%% representamos los datos
datos["Ax0"].hist()
datos["Ay0"].hist()
datos["Az0"].hist()
#%%
plt.figure()
datos["Ax20"].hist()
datos["Ay20"].hist()
datos["Az20"].hist()
plt.figure()
datos["Ax30"].hist()
datos["Ay30"].hist()
datos["Az30"].hist()
#%% feature engineering, realizamos un nuevo data frame con
# las siguientes características para cada golpe
"""
Ax_mean: valor medio aceleración eje x
Ay_mean: valor medio aceleración eje y
Az_mean: valor medio aceleración eje z
Vx_mean: valor medio velocidad eje x
Vy_mean: valor medio velocidad eje y
Vz_mean: valor medio velocidad eje z
Ax_max: valor máximo aceleración eje x
Ay_max: valor máximo aceleración eje y
Az_max: valor máximo aceleración eje z
Vx_max: valor máximo velocidad eje x
Vy_max: valor máximo velocidad eje y
Vz_max: valor máximo velocidad eje z
Ax_min: valor mínimo aceleración eje x
Ay_min: valor mínimo aceleración eje y
Az_min: valor mínimo aceleración eje z
Vx_min: valor mínimo velocidad eje x
Vy_min: valor mínimo velocidad eje y
Vz_min: valor mínimo velocidad eje z
"""
datos_features = pd.DataFrame()
datos_features["Ax_mean"] = datos.loc[:, "Ax0":"Ax39"].mean(axis=1)
datos_features["Ay_mean"] = datos.loc[:, "Ay0":"Ay39"].mean(axis=1)
datos_features["Az_mean"] = datos.loc[:, "Az0":"Az39"].mean(axis=1)
datos_features["Vx_mean"] = datos.loc[:, "Vx0":"Vx39"].mean(axis=1)
datos_features["Vy_mean"] = datos.loc[:, "Vy0":"Vy39"].mean(axis=1)
datos_features["Vz_mean"] = datos.loc[:, "Vz0":"Vz39"].mean(axis=1)
datos_features["Ax_max"] = datos.loc[:, "Ax0":"Ax39"].max(axis=1)
datos_features["Ay_max"] = datos.loc[:, "Ay0":"Ay39"].max(axis=1)
datos_features["Az_max"] = datos.loc[:, "Az0":"Az39"].max(axis=1)
datos_features["Vx_max"] = datos.loc[:, "Vx0":"Vx39"].max(axis=1)
datos_features["Vy_max"] = datos.loc[:, "Vy0":"Vy39"].max(axis=1)
datos_features["Vz_max"] = datos.loc[:, "Vz0":"Vz39"].max(axis=1)
datos_features["Ax_min"] = datos.loc[:, "Ax0":"Ax39"].min(axis=1)
datos_features["Ay_min"] = datos.loc[:, "Ay0":"Ay39"].min(axis=1)
datos_features["Az_min"] = datos.loc[:, "Az0":"Az39"].min(axis=1)
datos_features["Vx_min"] = datos.loc[:, "Vx0":"Vx39"].min(axis=1)
datos_features["Vy_min"] = datos.loc[:, "Vy0":"Vy39"].min(axis=1)
datos_features["Vz_min"] = datos.loc[:, "Vz0":"Vz39"].min(axis=1)
datos_features["tipo_golpe"] = datos["tipo_golpe"].astype(int)
#%% nuevos datos que tenemos
#print(datos_features.info())
#print(datos_features.shape)
plt.hist(datos_features.tipo_golpe, bins = 13)
#%% matriz de confusión
import numpy as np
import itertools
golpes = ['D','R','DP','RP','GD','GR','GDP','GRP','VD','VR','B','RM','S']
def plot_confusion_matrix(cm, classes,
normalize=False,
title='Confusion matrix',
cmap=plt.cm.Blues):
"""
This function prints and plots the confusion matrix.
Normalization can be applied by setting `normalize=True`.
"""
plt.imshow(cm, interpolation='nearest', cmap=cmap)
plt.colorbar()
tick_marks = np.arange(len(classes))
plt.xticks(tick_marks, classes, rotation=45)
plt.yticks(tick_marks, classes)
if normalize:
cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
thresh = cm.max() / 2.
for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
plt.text(j, i, cm[i, j],
horizontalalignment="center",
color="white" if cm[i, j] > thresh else "black")
plt.tight_layout()
plt.ylabel('Actual')
plt.xlabel('Prediction')
plt.xticks(range(13), golpes)
plt.yticks(range(13), golpes)
#%% dividimos los datos
from sklearn.model_selection import train_test_split
X = datos_features.drop(columns = ["tipo_golpe"])
y = datos_features["tipo_golpe"]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, stratify=y, random_state=5)
# =============================================================================
# #%% Entrenamiento simple
#
# from sklearn.tree import DecisionTreeClassifier
#
# tree_clf = DecisionTreeClassifier(max_depth=20, min_samples_leaf=5, min_samples_split=2,
# criterion="entropy", min_impurity_split=(0.1))
# tree_clf.fit(X_train, y_train)
#
# #%% resultados de test
#
# ypred = tree_clf.predict(X_test)
#
# from sklearn.metrics import accuracy_score
#
# print(accuracy_score(y_test, ypred))
# =============================================================================
#%% resultados hiper parametrización Grid
from sklearn import model_selection
from sklearn.tree import DecisionTreeClassifier
param_grid = {"max_depth": [1, 10, 20, 30, 40],
"min_samples_split":[2, 4, 8, 10, 20, 100],
"min_samples_leaf": [1, 2, 3, 4, 5, 6, 10],
"criterion":["entropy","gini"]}
print("GridSearch starts")
model = model_selection.GridSearchCV(estimator= DecisionTreeClassifier(),
param_grid=param_grid,
scoring="accuracy",
cv=5)
model.fit(X_train, y_train)
#%% resultados
print("val. score: %s" % model.best_score_)
print("test score: %s" % model.score(X_test, y_test))
print("Mejores parámetros:", model.best_params_)
parametros = model.best_params_
print(type(parametros))
#%% comprobamos los mejores resultados
from sklearn.metrics import accuracy_score
from sklearn.metrics import confusion_matrix
scores = list()
for i in range(10):
modelo_final = DecisionTreeClassifier()
modelo_final.set_params(**model.best_params_)
modelo_final.fit(X_train, y_train)
ypred_final = modelo_final.predict(X_test)
score = accuracy_score(y_test, ypred_final) *100.0
print("Iteration",i,":",score)
scores.append(score)
#matriz de confusion
ypred = modelo_final.predict(X_test)
cm = confusion_matrix(y_test, ypred)
#print(cm)
plt.figure()
plot_confusion_matrix(cm, classes = range(3))
print(scores)
from numpy import mean
from numpy import std
print("Accuracy: %.3f%% (+/-%.3f)" % (mean(scores) , std(scores)))
from matplotlib import pyplot
pyplot.figure()
pyplot.boxplot(scores)
pyplot.title('Accuracy para max_deph=%s, min_samples_split=%s, min_samples_leaf=%s, criterion=%s' % (parametros['max_depth'], parametros['min_samples_split'], parametros['min_samples_leaf'], parametros['criterion']))
pyplot.ylabel("Accuracy (%)")
pyplot.grid(linestyle='-', linewidth=0.3)