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
/
SVM_padel_fe.py
228 lines (157 loc) · 6.59 KB
/
SVM_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
# -*- coding: utf-8 -*-
"""
Created on Sun Oct 31 08:45:58 2021
@author: Guillermo
"""
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())
#%% eliminamos las columnas que no nos interesan
datos.drop(columns = ["mano", "reves", "altura", "edad", "sexo", "nivel","id", "numero_golpe", "tiempo_golpe"], inplace=True)
#%% 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)
#%% 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)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, stratify=y)
#%% matriz de confusión
import numpy as np
import matplotlib.pyplot as plt
import itertools
from sklearn.metrics import confusion_matrix
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)
#%% entrenamiento modelo
from sklearn import svm
from sklearn.metrics import accuracy_score
def evaluate_model(param_C,kernel_type):
model = svm.SVC(C=param_C, decision_function_shape='ovo', kernel=kernel_type)
model.fit(X_train, y_train)
#%% resultados test
ypred = model.predict(X_test)
accuracy = accuracy_score(y_test, ypred)
#print(accuracy)
cm = confusion_matrix(y_test, ypred)
#plt.figure()
#plot_confusion_matrix(cm, classes = range(3))
return accuracy
from numpy import mean
from numpy import std
import matplotlib.pyplot as plt
# summarize scores
def summarize_results(scores, C, kernel):
print(scores)
best_accuracy = 0
best_params = []
for i in range(len(scores)):
m, s = mean(scores[i]), std(scores[i])
print('Kernel=%s: %.3f%% (+/-%.3f)' % (kernel[i], m, s))
score = scores[i]
for j in range(len(C)):
if score[j]>best_accuracy:
best_accuracy = score[j]
best_params = [score[j], C[j], kernel[i]]
# boxplot of scores
plt.figure()
plt.boxplot(scores, labels=['linear', 'poly', 'rbf', 'sigmoid'])
plt.title('Accuracy en función de Kernel y C')
plt.xlabel("Kernel_type")
plt.ylabel("Accuracy (%)")
plt.grid(linestyle='-', linewidth=0.3)
print('Best Params: Kernel=%s, C=%f: %.3f%%' % (best_params[2], best_params[1], best_params[0]))
#Matriz de Confusion de mejores parametros
model = svm.SVC(C=best_params[1], decision_function_shape='ovo', kernel=best_params[2])
model.fit(X_train, y_train)
ypred = model.predict(X_test)
accuracy = accuracy_score(y_test, ypred)
print(accuracy)
cm = confusion_matrix(y_test, ypred)
plt.figure()
plot_confusion_matrix(cm, classes = range(3))
# run an experiment
def run_experiment(C, Kernel):
all_scores = list()
for i in Kernel:
scores = list()
for j in C:
score = evaluate_model(j, i)
score = score * 100.00
print('>#%s #%.2f: %.2f' % (i, j, score))
scores.append(score)
all_scores.append(scores)
# summarize results
params = summarize_results(all_scores, C, Kernel)
# run the experiment
C = [0.01, 0.1, 1, 10, 100, 200, 300, 500, 1000]
kernel = ['linear', 'poly', 'rbf', 'sigmoid']
run_experiment(C, kernel)