Scalers
Scalers
Scalers
Reporte de práctica
1
Código para Scalers con dataset de heart.
import numpy as np
import pandas as pd
# Clean data
filtro = df [ ’ sex ’] ! = 0
cp = df [ ’ sex ’]
cpf = cp [ filtro ]
min = np . amin ( cpf )
df [ ’ sex ’] . replace ( to_replace = 0 , value = min / 2 , inplace = True )
filtro = df [ ’ cp ’] ! = 0
cp = df [ ’ cp ’]
cpf = cp [ filtro ]
min = np . amin ( cpf )
df [ ’ cp ’] . replace ( to_replace = 0 , value = min / 2 , inplace = True )
filtro = df [ ’ trestbps ’] ! = 0
cp = df [ ’ trestbps ’]
cpf = cp [ filtro ]
min = np . amin ( cpf )
df [ ’ trestbps ’] . replace ( to_replace = 0 , value = min / 2 , inplace = True )
filtro = df [ ’ fbs ’] ! = 0
cp = df [ ’ fbs ’]
cpf = cp [ filtro ]
min = np . amin ( cpf )
df [ ’ fbs ’] . replace ( to_replace = 0 , value = min / 2 , inplace = True )
filtro = df [ ’ restecg ’] ! = 0
cp = df [ ’ restecg ’]
cpf = cp [ filtro ]
min = np . amin ( cpf )
df [ ’ restecg ’] . replace ( to_replace = 0 , value = min / 2 , inplace = True )
filtro = df [ ’ exang ’] ! = 0
cp = df [ ’ exang ’]
cpf = cp [ filtro ]
min = np . amin ( cpf )
df [ ’ exang ’] . replace ( to_replace = 0 , value = min / 2 , inplace = True )
filtro = df [ ’ oldpeak ’] ! = 0
cp = df [ ’ oldpeak ’]
cpf = cp [ filtro ]
min = np . amin ( cpf )
df [ ’ oldpeak ’] . replace ( to_replace = 0 , value = min / 2 , inplace = True )
filtro = df [ ’ slope ’] ! = 0
cp = df [ ’ slope ’]
cpf = cp [ filtro ]
min = np . amin ( cpf )
df [ ’ slope ’] . replace ( to_replace = 0 , value = min / 2 , inplace = True )
filtro = df [ ’ ca ’] ! = 0
cp = df [ ’ ca ’]
cpf = cp [ filtro ]
min = np . amin ( cpf )
df [ ’ ca ’] . replace ( to_replace = 0 , value = min / 2 , inplace = True )
filtro = df [ ’ thal ’] ! = 0
cp = df [ ’ thal ’]
2
cpf = cp [ filtro ]
min = np . amin ( cpf )
df [ ’ thal ’] . replace ( to_replace = 0 , value = min / 2 , inplace = True )
y = df . iloc [ : , 13 ]
x = df . iloc [ : ,0 : 12 ]
x . hist ( column = [ ’ age ’ , ’ sex ’ , ’ cp ’ , ’ trestbps ’ , ’ chol ’ , ’ fbs ’ , ’ restecg ’ , ’ thalach
’ , ’ exang ’ , ’ oldpeak ’ , ’ slope ’ , ’ ca ’] )
# * ** NEVER *
xtrain , xtest , ytrain , ytest = tr a i n _ t e s t _ s p l i t (x , y , test_size = 0 . 3 )
model = MLPClassifier ( alpha = 0 . 01 , max_iter = 1200 )
model . fit ( xtrain , ytrain )
print ( ’\ n \ n \ n \ n \ n \ n \ n \ n * NEVER * ’)
print ( ’ Train : ’ , model . score ( xtrain , ytrain ) )
print ( ’ Test : ’ , model . score ( xtest , ytest ) )
ytestpred = model . predict ( xtest )
print ( ’ Cla ssificat ion report : \ n ’ , c l a s s i f i c a t i o n _ r e p o r t ( ytest , ytestpred ) )
class_names = [1 , 2 ]
plt . show ()
cm = metrics . c on f u s i o n _ m a t r i x ( ytest , ytestpred )
disp = metrics . C o n f u s i o n M a t r i x D i s p l a y ( c o n f u s io n _ m a t r i x = cm , display _labels =
class_names )
disp . plot ()
plt . show ()
# * ** MinMax Scaler *
# creamos los e s c a l a d o r e s
scalerMM = MinMaxScaler () # Min Max Scaler
# Para e s t a n d a r i z a r
scaledXDFMM = x . copy () # Copy for Min Max Scaler
# creamos el modelo de un P e r c e p t r o n m u l t i c a p a
3
# sacar el reporte de c l a s i f i c a c i o n
# sacar el reporte de c l a s i f i c a c i o n
class_names2 = [1 , 2 ]
plt . show ()
cm2 = metrics . c o n f u s i o n _ m a t ri x ( ytest2 , ytestpred2 )
disp2 = metrics . C o n f u s i o n M a t r i x D i s p l a y ( c o n f u s i o n _ m a t r i x = cm2 , display _labels =
class_names2 )
disp2 . plot ()
plt . show ()
# * ** Standar scaler *
# creamos los e s c a l a d o r e s
scalerS = Stan dardScal er () # Standar Scaler
# Para e s t a n d a r i z a r
scaledXDFS = x . copy () # Copy for Standar Scaler
# creamos el modelo de un P e r c e p t r o n m u l t i c a p a
# sacar el reporte de c l a s i f i c a c i o n
# sacar el reporte de c l a s i f i c a c i o n
class_names3 = [1 , 2 ]
plt . show ()
cm3 = metrics . c o n f u s i o n _ m a t ri x ( ytest3 , ytestpred3 )
disp3 = metrics . C o n f u s i o n M a t r i x D i s p l a y ( c o n f u s i o n _ m a t r i x = cm3 , display _labels =
class_names3 )
disp3 . plot ()
plt . show ()
# * ** Minmax - Standar *
# creamos los e s c a l a d o r e s
scalerMM2 = MinMaxScaler () # Min Max Scaler
4
# Para e s t a n d a r i z a r
scaledXDFMM2 = x . copy () # Copy for Min Max Scaler
# ################################################################
# creamos el modelo de un P e r c e p t r o n m u l t i c a p a
# sacar el reporte de c l a s i f i c a c i o n
# sacar el reporte de c l a s i f i c a c i o n
class_names4 = [1 , 2 ]
plt . show ()
cm4 = metrics . c o n f u s i o n _ m a t ri x ( ytest4 , ytestpred4 )
disp4 = metrics . C o n f u s i o n M a t r i x D i s p l a y ( c o n f u s i o n _ m a t r i x = cm4 , display _labels =
class_names4 )
disp4 . plot ()
plt . show ()
# * ** Standar - MinMax *
# creamos los e s c a l a d o r e s
scalerS5 = Standar dScaler ()
# Para e s t a n d a r i z a r
scaledXDFS5 = x . copy ()
5
scaledXDFS5 . hist ( column = [ ’ age ’ , ’ sex ’ , ’ cp ’ , ’ trestbps ’ , ’ chol ’ , ’ fbs ’ , ’ restecg ’
, ’ thalach ’ , ’ exang ’ , ’ oldpeak ’ , ’ slope ’ ,
’ ca ’] )
scalerMM5 = MinMaxScaler ()
scaledXDFMM5 = scaledXDFS5 . copy ()
# ################################################################
# creamos el modelo de un P e r c e p t r o n m u l t i c a p a
# sacar el reporte de c l a s i f i c a c i o n
# sacar el reporte de c l a s i f i c a c i o n
class_names5 = [1 , 2 ]
plt . show ()
cm5 = metrics . c o n f u s i o n _ m a t ri x ( ytest5 , ytestpred5 )
disp5 = metrics . C o n f u s i o n M a t r i x D i s p l a y ( c o n f u s i o n _ m a t r i x = cm5 , display _labels =
class_names5 )
disp5 . plot ()
plt . show ()
# Hacerlo con heart con varios modelos
# 5 modelos
# 1 Sin e s c a l a d o r e s
# 2 MinMax Scaler
# 3 Standar scaler
# 4 Minmax - Standar
# 5 Standar MinMax
6
Data
160
140
120
100
80
60
40
20
0
1
0
Figure 1: Dataset heart.csv, cantidad de casos positivos y negativos representados por 1 y 0.
7
Report sin nada
8
Data sin nada
age sex cp
150
60 200
150 100
40
100
20 50
50
0 0 0
30 40 50 60 70 0.5 0.6 0.7 0.8 0.9 1.0 0.5 1.0 1.5 2.0 2.5 3.0
trestbps chol fbs
100
60 200
75
40
50
100
20 25
0 0 0
100 120 140 160 180 200 200 300 400 500 0.5 0.6 0.7 0.8 0.9 1.0
restecg thalach exang
150 80
200
60 150
100
40 100
50
20 50
0 0 0
0.6 0.8 1.0 1.2 1.4 1.6 1.8 2.0 80 100 120 140 160 180 200 0.5 0.6 0.7 0.8 0.9 1.0
oldpeak slope ca
150
150
100 100
100
50 50
50
0 0 0
0 1 2 3 4 5 6 0.6 0.8 1.0 1.2 1.4 1.6 1.8 2.0 0.5 1.0 1.5 2.0 2.5 3.0 3.5 4.0
9
Matriz de confucion sin nada
50
40
1 50 6
30
True label
20
2 4 31
10
1 2
Predicted label
10
Report con MinMax Scaler.
11
Data con MinMax Scaler
age sex cp
150
60 200
150 100
40
100
20 50
50
0 0 0
0.0 0.2 0.4 0.6 0.8 1.0 0.0 0.2 0.4 0.6 0.8 1.0 0.0 0.2 0.4 0.6 0.8 1.0
trestbps chol fbs
100
60 200
75
40
50
100
20 25
0 0 0
0.0 0.2 0.4 0.6 0.8 1.0 0.0 0.2 0.4 0.6 0.8 1.0 0.0 0.2 0.4 0.6 0.8 1.0
restecg thalach exang
150 80
200
60 150
100
40 100
50
20 50
0 0 0
0.0 0.2 0.4 0.6 0.8 1.0 0.0 0.2 0.4 0.6 0.8 1.0 0.0 0.2 0.4 0.6 0.8 1.0
oldpeak slope ca
150
150
100 100
100
50 50
50
0 0 0
0.0 0.2 0.4 0.6 0.8 1.0 0.0 0.2 0.4 0.6 0.8 1.0 0.0 0.2 0.4 0.6 0.8 1.0
12
Matriz de confucion con MinMax Scaler.
40
35
1 41 20
30
25
True label
20
15
2 3 27
10
1 2
Predicted label
13
Report con Standar scaler.
14
Data con Standar scaler
age sex cp
150
60 200
150 100
40
100
20 50
50
0 0 0
−3 −2 −1 0 1 2 −1.5 −1.0 −0.5 0.0 0.5 −1.0 −0.5 0.0 0.5 1.0 1.5 2.0
trestbps chol fbs
100
60 200
75
40
50
100
20 25
0 0 0
−2 −1 0 1 2 3 4 −2 0 2 4 6 −0.5 0.0 0.5 1.0 1.5 2.0 2.5
restecg thalach exang
150 80
200
60 150
100
40 100
50
20 50
0 0 0
−1 0 1 2 3 4 −3 −2 −1 0 1 2 −0.5 0.0 0.5 1.0 1.5
oldpeak slope ca
150
150
100 100
100
50 50
50
0 0 0
−1 0 1 2 3 4 −1.5 −1.0 −0.5 0.0 0.5 1.0 0 1 2 3
15
Matriz de confucion con Standar scaler.
35
30
1 35 8
25
True label
20
2 14 34 15
10
1 2
Predicted label
16
Report con Minmax - Standar.
17
Data con Minmax - Standar
age sex cp
150
60 200
150 100
40
100
20 50
50
0 0 0
−3 −2 −1 0 1 2 −1.5 −1.0 −0.5 0.0 0.5 −1.0 −0.5 0.0 0.5 1.0 1.5 2.0
trestbps chol fbs
100
60 200
75
40
50
100
20 25
0 0 0
−2 −1 0 1 2 3 4 −2 0 2 4 6 −0.5 0.0 0.5 1.0 1.5 2.0 2.5
restecg thalach exang
150 80
200
60 150
100
40 100
50
20 50
0 0 0
−1 0 1 2 3 4 −3 −2 −1 0 1 2 −0.5 0.0 0.5 1.0 1.5
oldpeak slope ca
150
150
100 100
100
50 50
50
0 0 0
−1 0 1 2 3 4 −1.5 −1.0 −0.5 0.0 0.5 1.0 0 1 2 3
18
Matriz de confucion con Minmax - Standar.
35
1 39 10
30
True label 25
20
2 15 27
15
10
1 2
Predicted label
19
Report con Standar - MinMax.
20
Data con Standar - MinMax
age sex cp
150
60 200
150 100
40
100
20 50
50
0 0 0
0.0 0.2 0.4 0.6 0.8 1.0 0.0 0.2 0.4 0.6 0.8 1.0 0.0 0.2 0.4 0.6 0.8 1.0
trestbps chol fbs
100
60 200
75
40
50
100
20 25
0 0 0
0.0 0.2 0.4 0.6 0.8 1.0 0.0 0.2 0.4 0.6 0.8 1.0 0.0 0.2 0.4 0.6 0.8 1.0
restecg thalach exang
150 80
200
60 150
100
40 100
50
20 50
0 0 0
0.0 0.2 0.4 0.6 0.8 1.0 0.0 0.2 0.4 0.6 0.8 1.0 0.0 0.2 0.4 0.6 0.8 1.0
oldpeak slope ca
150
150
100 100
100
50 50
50
0 0 0
0.0 0.2 0.4 0.6 0.8 1.0 0.0 0.2 0.4 0.6 0.8 1.0 0.0 0.2 0.4 0.6 0.8 1.0
21
Matriz de confucion con Standar - MinMax.
35
1 38 5 30
25
True label
20
15
2 16 32
10
5
1 2
Predicted label
22
Conclusión
En general utilizar estas técnicas puede llegar a tener unos mejores resultados, sin embargo
es importante considerar que en ciertos casos cuando existan valores atı́picos y estos son de
suma importancia el aprendizaje puede llegar a verse afectado, no obstante esto no es lo común,
generalmente los problemas se presentan cuando existe una gran variedad de datos, en este
sentido y para el caso de el dataset de heart.csv tenemos un mejor train cuando se le aplica Min
Max Y Standar en ese orden llegando a una precisión de 99, mientras que en el test puede notarse
una disminución significativa comparada con un normal es decir sin aplicar Scalers, tenemos de
igual manera un valor bastante bueno para train con Standar Scaler con un valor de 0.97 pero
en el test tan solo se tiene un 0.75, quizá en este sentido se presente un sobreentrenamiento o
es posible que el standar scaler haya afectado los datos, Min Max scaler presenta un train de
0.83 mientras que en test tan solo de 0.74, finalmente tenemos el mejor caso para el test pero
el peor para el train, no aplicar ningún Scaler da como resultado un train de 0.76 pero el test
es el mejor de todos los casos con un 0.89, es importante mencionar que todos los modelos son
entrenados con los mismos hiperparametros es decir, con la misma cantidad de datos de train y
test, el mismo alpha ası́ como la misma cantidad de iteraciones.
23