import pandas as pd
x_train = pd.read_csv('train.csv')
x_train.head()
x_train.shape
import pandas as pd
x_train = pd.read_csv('train.csv')
x_train
x_train.describe()
x_train.dtypes
x_train.isnull().sum()
import seaborn as sns
sns.countplot(data=x_train,x="Survived")
print(round((x_train[x_train["Survived"] == 0]["Survived"].count() / x_train["Survived"].count()) * 100,2) ,"%")
print(round((x_train[x_train["Survived"] == 1]["Survived"].count() / x_train["Survived"].count()) * 100,2),"%")
import seaborn as sns
sns.countplot(data=x_train,x="Pclass")
sns.countplot(data=x_train,x="Pclass", hue="Survived")
sns.displot(x_train["Age"])
sns.kdeplot(data=x_train, x="Age", hue="Survived", multiple="stack")
menores_0 = x_train[x_train["Age"] < 1]
menores_0
menores_0["Age"].count()
sns.displot(x_train["Embarked"])
sns.countplot(data=x_train,x="Embarked", hue="Survived")
sns.displot(x_train["Sex"])
sns.countplot(data=x_train,x="Sex",hue="Survived")
import matplotlib.pyplot as plt
figura = plt.figure(figsize=(10,10))
axes = figura.add_subplot()
sns.kdeplot(data=x_train,x="Age", hue="Pclass", ax=axes, color='red', fill=True)
import re
x_train["trato"] = x_train["Name"].str.extract(r'(, [A-z]*\.)')
x_train["trato"] = x_train["trato"].str.replace(r'\s', '')
x_train["trato"] = x_train["trato"].str.replace(r',', '')
x_train["trato"]
x_train["trato"].count()
x_train["trato"].unique()
x_train.groupby("trato")["Survived"].count()
sns.countplot(data=x_train,x="trato")
sns.countplot(x_train["SibSp"])
sns.countplot(data=x_train,x="SibSp", hue="Survived")
sns.countplot(x_train["Parch"])
sns.countplot(data=x_train,x="Parch", hue="Survived")
# Mínimo edad
min_edad = x_train["Age"].min()
# Edad máxima
max_edad = x_train["Age"].max()
from sklearn.preprocessing import MinMaxScaler
from sklearn.preprocessing import LabelEncoder
def one_hot_encoder(columna):
return pd.get_dummies(columna)
def label_encoder(columna):
le = LabelEncoder()
return le.fit_transform(columna.values.reshape(-1, 1))
def limpiar_datos(df):
# Eliminación de filas nulas
df.dropna(subset=['Age'], inplace=True)
df.dropna(subset=['Embarked'], inplace=True)
# Label Encoder y One Hot
# Los tipos de Embarque
df = pd.concat([df, one_hot_encoder(df["Embarked"])], axis=1)
# El Sexo
df['sex_cat'] = label_encoder(df["Sex"])
# Pclass
df = pd.concat([df, one_hot_encoder(df["Pclass"])], axis=1)
# Normalizamos los datos
df["Age_norm"] = (df["Age"] - min_edad) / (max_edad - min_edad)
return df
def devolver_datos_entrenamiento(df):
return df[["sex_cat","Age_norm","1","2","3","C","Q","S"]]
def devolver_datos_entrenamiento_test(df):
return df[["Survived","sex_cat","Age_norm","1","2","3","C","Q","S"]]
def devolver_target(df):
return df["Survived"]
x_train_limpio = limpiar_datos(x_train)
x_train_limpio.columns
x_train_limpio.to_csv("train_limpio.csv", index=False)
x_train_limpio.head(5)
x_train_limpio = pd.read_csv("train_limpio.csv")
target = devolver_target(x_train_limpio)
target.to_csv("target_train.csv", index=False)
x_train_modelo = devolver_datos_entrenamiento(x_train_limpio)
x_train_modelo
x_train_modelo.to_csv("train_modelo.csv", index=False)
x_test = pd.read_csv("test.csv")
y_test = pd.read_csv("gender_submission.csv")
x_test = pd.concat([x_test, y_test["Survived"]], axis=1)
x_test = limpiar_datos(x_test)
x_test.to_csv("test_limpio.csv",index=False)
x_test = pd.read_csv("test_limpio.csv")
x_test = devolver_datos_entrenamiento_test(x_test)
y_test = devolver_target(x_test)
x_test = x_test.drop(["Survived"], axis=1)
def devolver_datos_test():
x_test = pd.read_csv("test_limpio.csv")
x_test = devolver_datos_entrenamiento_test(x_test)
y_test = devolver_target(x_test)
x_test = x_test.drop(["Survived"], axis=1)
return x_test, y_test
import pandas as pd
x_train_modelo = pd.read_csv("train_modelo.csv")
target = pd.read_csv("target_train.csv")
import os
os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
os.environ["CUDA_VISIBLE_DEVICES"] = ""
import tensorflow as tf
from keras.models import Sequential
from keras.layers import Dense
import numpy as np
def create_model(neurons,activacion, ini, optimizador):
tf.random.set_seed(5)
model = Sequential()
for index,i in enumerate(neurons):
if index==0:
if ini==None:
model.add(Dense(i,activation=activacion, input_dim=x_train_modelo.shape[1] ))
else:
model.add(Dense(i,activation=activacion, kernel_initializer=ini, input_dim=x_train_modelo.shape[1] ))
if ini==None:
model.add(Dense(i,activation=activacion,kernel_initializer=ini))
else:
model.add(Dense(i,activation=activacion))
model.add(Dense(1, activation="sigmoid"))
if optimizador==None:
model.compile(loss="binary_crossentropy", metrics=["accuracy",tf.keras.metrics.Recall(),tf.keras.metrics.Precision()])
elif optimizador!=None:
model.compile(loss="binary_crossentropy",metrics=["accuracy",tf.keras.metrics.Recall(),tf.keras.metrics.Precision()], optimizer=optimizador)
return model
def compile_models_activation(models,activations):
MODELS_COMPILED = []
CARACTERISTICAS = []
for model in models:
for act in activations:
CARACTERISTICAS.append([model,act,'','',''])
MODELS_COMPILED.append(create_model(model,act,None,None))
return MODELS_COMPILED,CARACTERISTICAS
def compile_models_activations_ini(models, activations, initializers):
MODELS_COMPILED = []
CARACTERISTICAS = []
for model in models:
for act in activations:
for ini in initializers:
CARACTERISTICAS.append([model,act,ini,'',''])
MODELS_COMPILED.append(create_model(model,act,ini,None))
return MODELS_COMPILED,CARACTERISTICAS
callback = tf.keras.callbacks.EarlyStopping(monitor='val_loss', patience=5)
def fit_models(models,caract,batch_size):
HISTORYS = []
for index,model in enumerate(models):
if batch_size != None:
HISTORYS.append(model.fit(x_train_modelo, target, epochs=50, verbose=0, validation_data=(x_test, y_test), callbacks=callback,batch_size=batch_size))
else:
HISTORYS.append(model.fit(x_train_modelo, target, epochs=50, verbose=0, validation_data=(x_test, y_test), callbacks=callback))
for index_item,item in enumerate(models[index].history.history):
# Loss
if index_item == 0:
caract[index].append(models[index].history.history[item][-1])
caract[index].append(models[index].history.history[item])
# Accuracy
if index_item == 1:
caract[index].append(models[index].history.history[item][-1])
caract[index].append(models[index].history.history[item])
# Recall
if index_item == 2:
caract[index].append(models[index].history.history[item][-1])
caract[index].append(models[index].history.history[item])
# Precision
if index_item == 3:
caract[index].append(models[index].history.history[item][-1])
caract[index].append(models[index].history.history[item])
# Val Loss
if index_item == 4:
caract[index].append(models[index].history.history[item][-1])
caract[index].append(models[index].history.history[item])
# Val Accuracy
if index_item == 5:
caract[index].append(models[index].history.history[item][-1])
caract[index].append(models[index].history.history[item])
# Val Recall
if index_item == 6:
caract[index].append(models[index].history.history[item][-1])
caract[index].append(models[index].history.history[item])
# Val Precision
if index_item == 7:
caract[index].append(models[index].history.history[item][-1])
caract[index].append(models[index].history.history[item])
return HISTORYS,caract
def ordenar_redes(caracteristicas):
ordenadas = caracteristicas.copy()
ordenadas.sort(key=lambda x: x[13])
return ordenadas
def imprimir_mejores_redes(caracteristicas,n):
caracteristicas = ordenar_redes(caracteristicas)
for index in range(n):
print(caracteristicas[index][0],caracteristicas[index][1],"Ini:",caracteristicas[index][2],"Optimizador:",caracteristicas[index][3]
,"Lr:",caracteristicas[index][4], "Loss:",caracteristicas[index][5],
"Accuracy:",caracteristicas[index][7],"Recall:",caracteristicas[index][9],"Precisión:",caracteristicas[index][11]
,"Val Loss:", caracteristicas[index][13], "Val Precision:",caracteristicas[index][19]
,"Val Recall:",caracteristicas[index][17])
def mejores_n_redes(caracteristica,n):
ordenadas = ordenar_redes(caracteristica)
return ordenadas[:n]
def la_mejor(caracteristicas):
print(min(caracteristicas, key=lambda x: x[14]))
MODELS = [
[2,4]
,[2,4,2]
,[2,3,2]
,[2,4,8,4,2]
,[2,4,8,16,8,4,2]
,[2,4,8,16,32,16,8,4,2]
,[2,6,3,2]
,[2,8,4,2]
,[2,8,16,8,2]]
activations = ["sigmoid","tanh", "relu",tf.keras.layers.LeakyReLU(),"elu","selu","gelu","swish"]
models,caract = compile_models_activation(MODELS,activations)
historys,caract = fit_models(models,caract,None)
imprimir_mejores_redes(caract,3)
import matplotlib.pyplot as plt
import numpy as np
def plot_metrics(axes,medidas,title,label_x,label_y, label1, label2):
x = np.linspace(0,len(medidas[0]),len(medidas[0]))
axes.set_ylim(0,1)
axes.plot(x,medidas[0], color="blue", label=label1, linestyle="--")
axes.plot(x,medidas[1], color="blue", label=label2)
axes.set_title(str(title))
axes.set_xlabel(label_x)
axes.set_ylabel(label_y)
axes.legend()
axes.set_facecolor("lightblue")
figura = plt.figure(figsize=(70,45))
plt.subplots_adjust(left=0.1,
bottom=0.1,
right=0.6,
top=0.9,
wspace=0.4,
hspace=0.4)
axes = []
col = len(models) // 8
for index in range(len(models)):
axes.append(figura.add_subplot(col,8,index+1))
for index,ax in enumerate(axes):
medidas = []
medidas.append(caract[index][6])
medidas.append(caract[index][14])
if type(caract[index][1]) != str:
plot_metrics(ax,medidas=medidas,title=str(caract[index][0]) + " " + "LeakyRelu" + " " + str(caract[index][13]),label_x="Épocas",label_y="Loss", label1="Loss", label2="Val Loss")
else:
plot_metrics(ax,medidas=medidas,title=str(caract[index][0]) + " " + str(caract[index][1]) + " " + str(caract[index][13]),label_x="Épocas",label_y="Loss", label1="Loss", label2="Val Loss")
figura = plt.figure(figsize=(14,5))
axes = []
col = 1
redes = mejores_n_redes(caract,3)
for index in range(len(redes)):
axes.append(figura.add_subplot(col,3,index+1))
for index,ax in enumerate(axes):
medidas = []
medidas.append(redes[index][6])
medidas.append(redes[index][14])
if type(redes[index][1]) != str:
plot_metrics(ax,medidas=medidas,title=str(redes[index][0]) + " " + "LeakyRelu" + " " + str(redes[index][13]),label_x="Épocas",label_y="Loss", label1="Loss", label2="Val Loss")
else:
plot_metrics(ax,medidas=medidas,title=str(redes[index][0]) + " " + str(redes[index][1]) + " " + str(round(redes[index][13],4)),label_x="Épocas",label_y="Loss", label1="Loss", label2="Val Loss")
figura = plt.figure(figsize=(70,45))
plt.subplots_adjust(left=0.1,
bottom=0.1,
right=0.6,
top=0.9,
wspace=0.4,
hspace=0.4)
axes = []
col = len(models) // 8
for index in range(len(models)):
axes.append(figura.add_subplot(col,8,index+1))
for index,ax in enumerate(axes):
medidas = []
medidas.append(caract[index][12])
medidas.append(caract[index][20])
if type(caract[index][1]) != str:
plot_metrics(ax,medidas=medidas,title=str(caract[index][0]) + " " + "LeakyRelu" + " " + str(caract[index][11]),label_x="Épocas",label_y="Precisión", label1="Precisión", label2="Val Precisión")
else:
plot_metrics(ax,medidas=medidas,title=str(caract[index][0]) + " " + str(caract[index][1]) + " " + str(caract[index][11]),label_x="Épocas",label_y="Precisión", label1="Precisión", label2="Val Precisión")
figura = plt.figure(figsize=(14,5))
axes = []
col = 1
redes = mejores_n_redes(caract,3)
for index in range(len(redes)):
axes.append(figura.add_subplot(col,3,index+1))
for index,ax in enumerate(axes):
medidas = []
medidas.append(redes[index][12])
medidas.append(redes[index][20])
if type(redes[index][1]) != str:
plot_metrics(ax,medidas=medidas,title=str(redes[index][0]) + " " + "LeakyRelu" + " " + str(redes[index][11]),label_x="Épocas",label_y="Precision", label1="Precision", label2="Val Precision")
else:
plot_metrics(ax,medidas=medidas,title=str(redes[index][0]) + " " + str(redes[index][1]) + " " + str(round(redes[index][11],4)),label_x="Épocas",label_y="Precision", label1="Precision", label2="Val Precision")
MODELS_2 = [[2,3,2],[2, 4,2],[2,4]]
activations_2 = ["selu","swish"]
initializers=["random_uniform","random_normal","glorot_uniform","glorot_normal","he_uniform","he_normal","lecun_uniform","lecun_normal","zeros","ones"]
models_2,caract_2 = compile_models_activations_ini(MODELS_2,activations_2,initializers)
historys_2,caract_2 = fit_models(models_2,caract_2,None)
imprimir_mejores_redes(caract_2,3)
figura = plt.figure(figsize=(14,5))
axes = []
col = 1
redes = mejores_n_redes(caract_2,3)
for index in range(len(redes)):
axes.append(figura.add_subplot(col,3,index+1))
for index,ax in enumerate(axes):
medidas = []
medidas.append(redes[index][6])
medidas.append(redes[index][14])
plot_metrics(ax,medidas=medidas,title=str(redes[index][0]) + " " + str(redes[index][1] + " " + redes[index][2]) + " " + str(round(redes[index][13],4)),label_x="Épocas",label_y="Loss", label1="Loss", label2="Val Loss")
figura = plt.figure(figsize=(14,5))
axes = []
col = 1
redes = mejores_n_redes(caract_2,3)
for index in range(len(redes)):
axes.append(figura.add_subplot(col,3,index+1))
for index,ax in enumerate(axes):
medidas = []
medidas.append(redes[index][12])
medidas.append(redes[index][20])
plot_metrics(ax,medidas=medidas,title=str(redes[index][0]) + " " + str(redes[index][1] + " " + redes[index][2]) + " " + str(round(redes[index][11],4)),label_x="Épocas",label_y="Precision", label1="Loss", label2="Val Loss")
MODELS_3 = [[2,4,2]]
activations_3 = ["swish","selu"]
initializers_3=["lecun_uniform","ones"]
learning_rate = [0.001,0.01,0.1]
optimizers=[]
for lr in learning_rate:
optimizers.append([tf.keras.optimizers.SGD(),"SGD"])
optimizers.append([tf.keras.optimizers.Adagrad(learning_rate=lr),"Adagrad"])
optimizers.append([tf.keras.optimizers.RMSprop(learning_rate=lr),"RMSprop"])
optimizers.append([tf.keras.optimizers.Adadelta(learning_rate=lr),"Adadelta"])
optimizers.append([tf.keras.optimizers.Adam(learning_rate=lr),"Adam"])
optimizers.append([tf.keras.optimizers.Adam(learning_rate=lr,amsgrad=True),"AMSGrad"])
optimizers.append([tf.keras.optimizers.Adamax(learning_rate=lr),"Adamax"])
optimizers.append([tf.keras.optimizers.Nadam(learning_rate=lr),"Nadam"])
def compile_models_activations_ini_opt_lr(models, activations, initializers,optimiezers):
MODELS_COMPILED = []
CARACTERISTICAS = []
for model in models:
for act in activations:
for ini in initializers:
for opt in optimiezers:
for lr in learning_rate:
CARACTERISTICAS.append([model,act,ini,opt[1],lr])
MODELS_COMPILED.append(create_model(model,act,ini,opt[0]))
return MODELS_COMPILED,CARACTERISTICAS
models_3,caract_3 = compile_models_activations_ini_opt_lr(MODELS_3,activations_3,initializers_3,optimizers)
historys_3,caract_3 = fit_models(models_3,caract_3,None)
imprimir_mejores_redes(caract_3,3)
figura = plt.figure(figsize=(14,5))
axes = []
col = 1
redes = mejores_n_redes(caract_3,3)
for index in range(len(redes)):
axes.append(figura.add_subplot(col,3,index+1))
for index,ax in enumerate(axes):
medidas = []
medidas.append(redes[index][6])
medidas.append(redes[index][14])
plot_metrics(ax,medidas=medidas,title=str(redes[index][0]) + " " + str(redes[index][1] + " " + redes[index][2]) + " " + str(round(redes[index][13],4)),label_x="Épocas",label_y="Loss", label1="Loss", label2="Val Loss")
figura = plt.figure(figsize=(14,5))
axes = []
col = 1
redes = mejores_n_redes(caract_2,3)
for index in range(len(redes)):
axes.append(figura.add_subplot(col,3,index+1))
for index,ax in enumerate(axes):
medidas = []
medidas.append(redes[index][12])
medidas.append(redes[index][20])
plot_metrics(ax,medidas=medidas,title=str(redes[index][0]) + " " + str(redes[index][1] + " " + redes[index][2]) + " " + str(round(redes[index][11],4)),label_x="Épocas",label_y="Precisión", label1="Loss", label2="Val Loss")
MODELS_4 = [[2, 4, 2]]
activations_4 = ["swish"]
initializers_4=["ones"]
learning_rate_4 = [0.001,0.01,0.1]
optimizers_4=[]
for lr in learning_rate_4:
optimizers_4.append([tf.keras.optimizers.Adam(learning_rate=lr),"RMSprop"])
BATCH_SIZE = [16,32,64,128,256]
models_4,caract_4 = compile_models_activations_ini_opt_lr(MODELS_4,activations_4,initializers_4,optimizers_4)
for size in BATCH_SIZE:
print("BATCH SIZE: " + str(size))
historys_4,caract_4 = fit_models(models_4,caract_4,size)
imprimir_mejores_redes(caract_4,3)
import tensorflow as tf
from keras.models import Sequential
from keras.layers import Dense
import numpy as np
import pandas as pd
import os
os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
os.environ["CUDA_VISIBLE_DEVICES"] = ""
x_train_modelo = pd.read_csv("train_modelo.csv")
target = pd.read_csv("target_train.csv")
x_test,y_test = devolver_datos_test()
callback = tf.keras.callbacks.EarlyStopping(monitor='val_loss', patience=5)
model_1 = Sequential()
model_1.add(Dense(2,activation="selu",input_dim=x_train_modelo.shape[1], kernel_initializer="ones"))
model_1.add(Dense(4,activation="selu"))
model_1.add(Dense(2,activation="selu"))
model_1.add(Dense(1, activation="sigmoid"))
model_1.compile(loss="binary_crossentropy",metrics=["accuracy",tf.keras.metrics.Recall(),tf.keras.metrics.Precision()], optimizer=tf.keras.optimizers.RMSprop(learning_rate=0.001))
model_2 = Sequential()
model_2.add(Dense(2,activation="selu",input_dim=x_train_modelo.shape[1], kernel_initializer="ones"))
model_2.add(Dense(4,activation="selu"))
model_2.add(Dense(2,activation="selu"))
model_2.add(Dense(1, activation="sigmoid"))
model_2.compile(loss="binary_crossentropy",metrics=["accuracy",tf.keras.metrics.Recall(),tf.keras.metrics.Precision()], optimizer=tf.keras.optimizers.RMSprop(learning_rate=0.01))
model_3 = Sequential()
model_3.add(Dense(2,activation="selu",input_dim=x_train_modelo.shape[1], kernel_initializer="ones"))
model_3.add(Dense(4,activation="selu"))
model_3.add(Dense(2,activation="selu"))
model_3.add(Dense(1, activation="sigmoid"))
model_3.compile(loss="binary_crossentropy",metrics=["accuracy",tf.keras.metrics.Recall(),tf.keras.metrics.Precision()], optimizer=tf.keras.optimizers.RMSprop(learning_rate=0.1))
semillas = np.arange(0,100,1)
precisiones_media = []
for semilla in semillas:
tf.random.set_seed(semilla)
history = model_1.fit(x_train_modelo,target,epochs=60, verbose=0,callbacks=[callback], validation_data=(x_test, y_test))
for index_item,item in enumerate(history.history):
# Precisión
if index_item == 3:
precisiones_media.append(np.mean(history.history[item]))
import seaborn as sns
sns.boxplot(precisiones_media, orient='v')
precisiones_media_2 = []
for semilla in semillas:
tf.random.set_seed(semilla)
history = model_2.fit(x_train_modelo,target,epochs=60, verbose=0,callbacks=[callback], validation_data=(x_test, y_test))
for index_item,item in enumerate(history.history):
# Precisión
if index_item == 3:
precisiones_media_2.append(np.mean(history.history[item]))
sns.boxplot(precisiones_media_2, orient='v')
precisiones_media_3 = []
for semilla in semillas:
tf.random.set_seed(semilla)
history = model_3.fit(x_train_modelo,target,epochs=60, verbose=0,callbacks=[callback], validation_data=(x_test, y_test))
for index_item,item in enumerate(history.history):
# Precisión
if index_item == 3:
precisiones_media_3.append(np.mean(history.history[item]))
sns.boxplot(precisiones_media_3, orient='v')
sns.boxenplot(data=[precisiones_media,precisiones_media_2,precisiones_media_3], orient='v')