<jupyter_start><jupyter_text>Titanic Workshop ⛵<jupyter_code>#cargamos las librerias
import pandas as pd 
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
sns.set(style="darkgrid")

from sklearn.ensemble import RandomForestClassifier
from sklearn.preprocessing import OneHotEncoder, LabelEncoder, StandardScaler
from sklearn.metrics import roc_curve, auc
from sklearn.model_selection import StratifiedKFold
#leer los datos
df_train = pd.read_csv('C:/Users/57313/Documents/Python Scripts/train.csv')
df_test = pd.read_csv('C:/Users/57313/Documents/Python Scripts/test.csv')

df_train.head()
df_test.head(2)
df_train.shape
df_train.describe().T<jupyter_output><empty_output><jupyter_text>Análisis de los datos<jupyter_code>df_train.info()
#nulos
total = df_train.isnull().sum().sort_values(ascending = False)
total
#Esta función toma un DataFrame (df) como entrada y devuelve dos columnas, valores nulos totales y porcentaje de valores nulos totales
def missing_percentage(df):
    total = df.isnull().sum().sort_values(ascending = False)
    percent = round(df.isnull().sum().sort_values(ascending = False)/len(df)*100,2)
    return pd.concat([total, percent], axis=1, keys=['Total','Percent'])
missing_percentage(df_train)<jupyter_output><empty_output><jupyter_text>Algunas observaciones:- Hay un total de 891 pasajeros en nuestro conjunto de entrenamiento.- A la característica Edad le falta aproximadamente el 19,8% de sus valores. Supongo que la función Edad es bastante importante para la supervivencia, por lo que probablemente deberíamos intentar llenar estos vacíos.- A la característica de cabina le falta aproximadamente el 77,1% de sus valores. Dado que falta una gran parte de la función, sería difícil completar los valores que faltan. Probablemente eliminaremos estos valores de nuestro conjunto de datos.- A la función Embarked le falta el 0,22% de sus valores, lo que debería ser relativamente inofensivo.<jupyter_code>missing_percentage(df_test)
#Esta función toma un marco de datos y una columna y encuentra el porcentaje de value_counts
def percent_value_counts(df, feature):
   
    percent = pd.DataFrame(round(df.loc[:,feature].value_counts(dropna=False, normalize=True)*100,2))
    ## creating a df with th
    total = pd.DataFrame(df.loc[:,feature].value_counts(dropna=False))
    ## concating percent and total dataframe

    total.columns = ["Total"]
    percent.columns = ['Percent']
    return pd.concat([total, percent], axis = 1)
percent_value_counts(df_train, 'Cabin')
percent_value_counts(df_train, 'Embarked')
df_train.columns.values
#Descripción de variables categoricas
df_train.describe(include=['O'])
#función para graficar variables categoricas
def bar_plot(variable):
    """
        input: variable ex: "Pclass"
        output: bar plot & value count
    """
    # get feature
    var = df_train[variable]
    # count number of categorical variable(value/sample)
    varValue = var.value_counts()
    
    # visualize
    plt.figure(figsize = (9,3))
    plt.bar(varValue.index, varValue)
    plt.xticks(varValue.index, varValue.index.values)
    plt.ylabel("Frequency")
    plt.title(variable)
    plt.show()
    print("{}: \n {}".format(variable,varValue))
    
category1 = ["Survived","Sex","Pclass","Embarked","SibSp", "Parch"]
for c in category1:
    bar_plot(c)
#función para graficar varibales numericas
def plot_hist(variable):
    plt.figure(figsize = (9,3))
    plt.hist(df_train[variable], bins = 50)
    plt.xlabel(variable)
    plt.ylabel("Frecuancia")
    plt.title("{} Distribución".format(variable))
    plt.show()
numericVar = ["Fare", "Age","PassengerId"]
for n in numericVar:
    plot_hist(n)<jupyter_output><empty_output><jupyter_text>Valores nulos<jupyter_code>#valores nulos
df_train.columns[df_train.isnull().any()]
df_train.isnull().sum()
df_train[df_train["Embarked"].isnull()]
df_train.boxplot(column="Fare",by = "Embarked")
plt.show()
df_train["Embarked"] = df_train["Embarked"].fillna("C")
df_train[df_train["Embarked"].isnull()]
df_train[df_train["Fare"].isnull()]
df_train[df_train["Age"].isnull()]
sns.factorplot(x = "Sex", y = "Age", data = df_train, kind = "box")
plt.show()
index_nan_age = list(df_train["Age"][df_train["Age"].isnull()].index)
for i in index_nan_age:
    age_pred = df_train["Age"][((df_train["SibSp"] == df_train.iloc[i]["SibSp"]) &(df_train["Parch"] == df_train.iloc[i]["Parch"])& (df_train["Pclass"] == df_train.iloc[i]["Pclass"]))].median()
    age_med = df_train["Age"].median()
    if not np.isnan(age_pred):
        df_train["Age"].iloc[i] = age_pred
    else:
        df_train["Age"].iloc[i] = age_med
df_train[df_train["Age"].isnull()]<jupyter_output><empty_output><jupyter_text>Algunos preguntas a los datos<jupyter_code># Plcass vs Survived
df_train[["Pclass","Survived"]].groupby(["Pclass"], as_index = False).mean().sort_values(by="Survived",ascending = False)
# Sex vs Survived
df_train[["Sex","Survived"]].groupby(["Sex"], as_index = False).mean().sort_values(by="Survived",ascending = False)
# Parch vs Survived
df_train[["Parch","Survived"]].groupby(["Parch"], as_index = False).mean().sort_values(by="Survived",ascending = False)<jupyter_output><empty_output><jupyter_text>Algunas predicciones:- Sexo: las mujeres tienen más probabilidades de sobrevivir.- SibSp / Parch: Las personas que viajan solas tienen más probabilidades de sobrevivir.- Pclass: Las personas de una clase socioeconómica más alta tienen más probabilidades de sobrevivir.  Visualizaciones<jupyter_code>#dibujar un diagrama de barras de sobrevivientes por gnero
sns.barplot(x="Sex", y="Survived", data=df_train)

#imprime los porcentajes de mujeres y hombres que sobreviven
print("Percentage of females who survived:", df_train["Survived"][df_train["Sex"] == 'female'].value_counts(normalize = True)[1]*100)

print("Percentage of males who survived:", df_train["Survived"][df_train["Sex"] == 'male'].value_counts(normalize = True)[1]*100)
#dibujar diagrama de barras de sobreviviente por clase
sns.barplot(x="Pclass", y="Survived", data=df_train)

#imprimir porcentajes
print("Percentage of Pclass = 1 who survived:", df_train["Survived"][df_train["Pclass"] == 1].value_counts(normalize = True)[1]*100)

print("Percentage of Pclass = 2 who survived:", df_train["Survived"][df_train["Pclass"] == 2].value_counts(normalize = True)[1]*100)

print("Percentage of Pclass = 3 who survived:", df_train["Survived"][df_train["Pclass"] == 3].value_counts(normalize = True)[1]*100)

sns.barplot(x="SibSp", y="Survived", data=df_train)


print("Percentage of SibSp = 0 who survived:", df_train["Survived"][df_train["SibSp"] == 0].value_counts(normalize = True)[1]*100)

print("Percentage of SibSp = 1 who survived:", df_train["Survived"][df_train["SibSp"] == 1].value_counts(normalize = True)[1]*100)

print("Percentage of SibSp = 2 who survived:", df_train["Survived"][df_train["SibSp"] == 2].value_counts(normalize = True)[1]*100)
#ordenar las edades en categorías l
df_train["Age"] = df_train["Age"].fillna(-0.5)
df_test["Age"] = df_test["Age"].fillna(-0.5)
bins = [-1, 0, 5, 12, 18, 24, 35, 60, np.inf]
labels = ['Unknown', 'Baby', 'Child', 'Teenager', 'Student', 'Young Adult', 'Adult', 'Senior']
df_train['AgeGroup'] = pd.cut(df_train["Age"], bins, labels = labels)
df_test['AgeGroup'] = pd.cut(df_test["Age"], bins, labels = labels)

#dibujar un diagrama de barras de la Edad vs la supervivencia
sns.barplot(x="AgeGroup", y="Survived", data=df_train)
plt.show()
df_train["CabinBool"] = (df_train["Cabin"].notnull().astype('int'))
df_test["CabinBool"] = (df_test["Cabin"].notnull().astype('int'))


print("Percentage of CabinBool = 1 who survived:", df_train["Survived"][df_train["CabinBool"] == 1].value_counts(normalize = True)[1]*100)

print("Percentage of CabinBool = 0 who survived:", df_train["Survived"][df_train["CabinBool"] == 0].value_counts(normalize = True)[1]*100)

sns.barplot(x="CabinBool", y="Survived", data=df_train)
plt.show()
df_test.describe(include="all")
#eliminando el campo Cabin, ya que no se puede extraer mucha más información útil 
df_train = df_train.drop(['Cabin'], axis = 1)
df_test = df_test.drop(['Cabin'], axis = 1)
df_train = df_train.drop(['Ticket'], axis = 1)
df_test = df_test.drop(['Ticket'], axis = 1)
#now we need to fill in the missing values in the Embarked feature
print("Number of people embarking in Southampton (S):")
southampton = df_train[df_train["Embarked"] == "S"].shape[0]
print(southampton)

print("Number of people embarking in Cherbourg (C):")
cherbourg = df_train[df_train["Embarked"] == "C"].shape[0]
print(cherbourg)

print("Number of people embarking in Queenstown (Q):")
queenstown = df_train[df_train["Embarked"] == "Q"].shape[0]
print(queenstown)
# reemplazando los valores faltantes de Embarked con S
df_train = df_train.fillna({"Embarked": "S"})
#combiar los dos set de datos
combine = [df_train, df_test]

#extraer el título para cada nombre 
for dataset in combine:
    dataset['Title'] = dataset.Name.str.extract(' ([A-Za-z]+)\.', expand=False)

pd.crosstab(df_train['Title'], df_train['Sex'])
#reemplazar varios títulos con nombres más comunes
for dataset in combine:
    dataset['Title'] = dataset['Title'].replace(['Lady', 'Capt', 'Col',
    'Don', 'Dr', 'Major', 'Rev', 'Jonkheer', 'Dona'], 'Rare')
    
    dataset['Title'] = dataset['Title'].replace(['Countess', 'Lady', 'Sir'], 'Royal')
    dataset['Title'] = dataset['Title'].replace('Mlle', 'Miss')
    dataset['Title'] = dataset['Title'].replace('Ms', 'Miss')
    dataset['Title'] = dataset['Title'].replace('Mme', 'Mrs')

df_train[['Title', 'Survived']].groupby(['Title'], as_index=False).mean()
title_mapping = {"Mr": 1, "Miss": 2, "Mrs": 3, "Master": 4, "Royal": 5, "Rare": 6}
for dataset in combine:
    dataset['Title'] = dataset['Title'].map(title_mapping)
    dataset['Title'] = dataset['Title'].fillna(0)

df_train.head()
# llene la edad que falta con el grupo de edad del modo para cada título
mr_age = df_train[df_train["Title"] == 1]["AgeGroup"].mode() #Young Adult
miss_age = df_train[df_train["Title"] == 2]["AgeGroup"].mode() #Student
mrs_age = df_train[df_train["Title"] == 3]["AgeGroup"].mode() #Adult
master_age = df_train[df_train["Title"] == 4]["AgeGroup"].mode() #Baby
royal_age = df_train[df_train["Title"] == 5]["AgeGroup"].mode() #Adult
rare_age = df_train[df_train["Title"] == 6]["AgeGroup"].mode() #Adult

age_title_mapping = {1: "Young Adult", 2: "Student", 3: "Adult", 4: "Baby", 5: "Adult", 6: "Adult"}


for x in range(len(df_train["AgeGroup"])):
    if df_train["AgeGroup"][x] == "Unknown":
        df_train["AgeGroup"][x] = age_title_mapping[df_train["Title"][x]]
        
for x in range(len(df_test["AgeGroup"])):
    if df_test["AgeGroup"][x] == "Unknown":
        df_test["AgeGroup"][x] = age_title_mapping[df_test["Title"][x]]
age_mapping = {'Baby': 1, 'Child': 2, 'Teenager': 3, 'Student': 4, 'Young Adult': 5, 'Adult': 6, 'Senior': 7}
df_train['AgeGroup'] = df_train['AgeGroup'].map(age_mapping)
df_test['AgeGroup'] = df_test['AgeGroup'].map(age_mapping)

df_train.head()


df_train = df_train.drop(['Age'], axis = 1)
df_test = df_test.drop(['Age'], axis = 1)
#eliminar el nombre
df_train = df_train.drop(['Name'], axis = 1)
df_test = df_test.drop(['Name'], axis = 1)
df_train.head()
sex_mapping = {"male": 0, "female": 1}
df_train['Sex'] = df_train['Sex'].map(sex_mapping)
df_test['Sex'] = df_test['Sex'].map(sex_mapping)

df_train.head()
df_train.head()
embarked_mapping = {"S": 1, "C": 2, "Q": 3}
df_train['Embarked'] = df_train['Embarked'].map(embarked_mapping)
df_test['Embarked'] = df_test['Embarked'].map(embarked_mapping)

df_train.head()<jupyter_output><empty_output><jupyter_text>competencia de modelos<jupyter_code>from sklearn.model_selection import train_test_split

predictors = df_train.drop(['Survived', 'PassengerId'], axis=1)
target = df_train["Survived"]
x_train, x_val, y_train, y_val = train_test_split(predictors, target, test_size = 0.22, random_state = 0)
# Gaussian Naive Bayes
from sklearn.naive_bayes import GaussianNB
from sklearn.metrics import accuracy_score

gaussian = GaussianNB()
gaussian.fit(x_train, y_train)
y_pred = gaussian.predict(x_val)
acc_gaussian = round(accuracy_score(y_pred, y_val) * 100, 2)
print(acc_gaussian)
# Regresión logistica
from sklearn.linear_model import LogisticRegression

logreg = LogisticRegression()
logreg.fit(x_train, y_train)
y_pred = logreg.predict(x_val)
acc_logreg = round(accuracy_score(y_pred, y_val) * 100, 2)
print(acc_logreg)
# Support Vector Machines
from sklearn.svm import SVC

svc = SVC()
svc.fit(x_train, y_train)
y_pred = svc.predict(x_val)
acc_svc = round(accuracy_score(y_pred, y_val) * 100, 2)
print(acc_svc)
# Linear SVC
from sklearn.svm import LinearSVC

linear_svc = LinearSVC()
linear_svc.fit(x_train, y_train)
y_pred = linear_svc.predict(x_val)
acc_linear_svc = round(accuracy_score(y_pred, y_val) * 100, 2)
print(acc_linear_svc)
# Perceptron
from sklearn.linear_model import Perceptron

perceptron = Perceptron()
perceptron.fit(x_train, y_train)
y_pred = perceptron.predict(x_val)
acc_perceptron = round(accuracy_score(y_pred, y_val) * 100, 2)
print(acc_perceptron)
#Decision Tree
from sklearn.tree import DecisionTreeClassifier

decisiontree = DecisionTreeClassifier()
decisiontree.fit(x_train, y_train)
y_pred = decisiontree.predict(x_val)
acc_decisiontree = round(accuracy_score(y_pred, y_val) * 100, 2)
print(acc_decisiontree)
# Random Forest
from sklearn.ensemble import RandomForestClassifier

randomforest = RandomForestClassifier()
randomforest.fit(x_train, y_train)
y_pred = randomforest.predict(x_val)
acc_randomforest = round(accuracy_score(y_pred, y_val) * 100, 2)
print(acc_randomforest)
# KNN or k-Nearest Neighbors
from sklearn.neighbors import KNeighborsClassifier

knn = KNeighborsClassifier()
knn.fit(x_train, y_train)
y_pred = knn.predict(x_val)
acc_knn = round(accuracy_score(y_pred, y_val) * 100, 2)
print(acc_knn)
# Stochastic Gradient Descent
from sklearn.linear_model import SGDClassifier

sgd = SGDClassifier()
sgd.fit(x_train, y_train)
y_pred = sgd.predict(x_val)
acc_sgd = round(accuracy_score(y_pred, y_val) * 100, 2)
print(acc_sgd)
# Gradient Boosting Classifier
from sklearn.ensemble import GradientBoostingClassifier

gbk = GradientBoostingClassifier()
gbk.fit(x_train, y_train)
y_pred = gbk.predict(x_val)
acc_gbk = round(accuracy_score(y_pred, y_val) * 100, 2)
print(acc_gbk)
models = pd.DataFrame({
    'Model': ['Support Vector Machines', 'KNN', 'Logistic Regression', 
              'Random Forest', 'Naive Bayes', 'Perceptron', 'Linear SVC', 
              'Decision Tree', 'Stochastic Gradient Descent', 'Gradient Boosting Classifier'],
    'Score': [acc_svc, acc_knn, acc_logreg, 
              acc_randomforest, acc_gaussian, acc_perceptron,acc_linear_svc, acc_decisiontree,
              acc_sgd, acc_gbk]})
models.sort_values(by='Score', ascending=False)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1542081204624899, 'fraction_numerical': 0.013511696692659317, 'mean_word_length': 6.464626191670848, 'pattern_counts': {'":': 14, '<': 29, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '50066', 'license': 'Apache-2.0', 'n_tokens_mistral': 5731, 'n_tokens_neox': 5419, 'n_words': 1172, 'path': '_Titanic Workshop.ipynb', 'repo_name': 'marisbotero/sapiencia', 'size': '14876'}