Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<compute_test_metric>
%matplotlib inline
def corrijaValoresAcimaDeCem(valor):
if(valor > 100):
print(valor)
 return 0
else:
return valor<categorify>
testandtrain_df = pd.read_csv('/kaggle/input/titanic/train.csv')
 validation_df = pd.read_csv('/kaggle/input/titanic/test.csv')
df['comissionados_por_servidor'] = df['comissionados_por_servidor'].map(corrijaValoresAcimaDeCem )<feature_engineering>
print("Training dataset(rows, columns):", testandtrain_df.shape)
 print("Validation dataset(rows, columns):", validation_df.shape )
df['comissionados_por_servidor'] = df['comissionados_por_servidor'].fillna(np.mean(df['comissionados_por_servidor']))<categorify>
validation_df[validation_df.Fare.isna() ]
df['ranking_igm'] = df['ranking_igm'].map(retiraSinalGrau )<data_type_conversions>
temp1_df = pd.concat([testandtrain_df.copy() , validation_df.copy() ], sort = False)
 temp1_df = temp1_df.filter(["Pclass", "Fare"])
 temp1_df = temp1_df.dropna()
print("Combined dataframe of all passengers except those with missing 'Fare' values
", "Shape(rows, columns):", temp1_df.shape )
df['ranking_igm'] = df['ranking_igm'].astype('category' )<feature_engineering>
avg_fares = temp1_df.groupby('Pclass' ).mean()
avg_fares
df['ranking_igm'] = df['ranking_igm'].cat.codes<compute_test_metric>
validation_df.loc[152, "Fare"] = avg_fares.loc[3, 'Fare']
def retirarOutliers(valor):
if(valor > 2174):
return 2174
else:
return valor<categorify>
validation_df[validation_df.index == 152]
df['hab_p_medico'] = df['hab_p_medico'].map(retirarOutliers )<statistical_test>
testandtrain_df[testandtrain_df.Embarked.isna() ]
def retirarOutliers(valor):
if(valor < 0.3):
print(valor)
 return 0.3
else:
return valor<categorify>
testandtrain_df.Embarked.value_counts() + validation_df.Embarked.value_counts()
df['perc_pop_econ_ativa'] = df['perc_pop_econ_ativa'].map(retirarOutliers )<concatenate>
testandtrain_df.loc[61, "Embarked"] = "S"
testandtrain_df.loc[829, "Embarked"] = "S"
def limparAnosEstudos(valor):
if(valor < 1.9):
return 2
elif(valor > 11):
return 10
else:
return valor 
<categorify>
for df in [testandtrain_df, validation_df]:
df.drop(columns = ['Cabin', 'Ticket'], inplace = True )
df['anos_estudo_empreendedor'] = df['anos_estudo_empreendedor'].map(limparAnosEstudos )<concatenate>
testandtrain_df.drop(columns = ['PassengerId'], inplace = True )
def limparJornada(valor):
if(valor > 50):
return 50
elif(valor < 30):
return 30
else:
return valor<categorify>
for df in [testandtrain_df, validation_df]:
for row in df.index:
df.loc[row, 'Title'] = df.loc[row, 'Name'].split(", ")[1].split(".")[0]
df['jornada_trabalho'] = df['jornada_trabalho'].map(limparJornada )<concatenate>
temp2_df = pd.concat([testandtrain_df.copy() , validation_df.copy() ], sort = False)
 print("Number of unique titles(in both dataframes):", temp2_df.Title.nunique())
 print("Number of null titles(in both dataframes):", temp2_df.Title.isna().sum())
 temp2_df.groupby("Title" ).count().Name
def limparAnosEstudo(valor):
if(valor > 11):
return 11
elif(valor < 7.5):
return 7.5
else:
return valor<categorify>
prof_titles = ["Capt", "Col", "Dr", "Major", "Rev"]
other_titles = ["Don", "Dona", "Jonkheer", "Lady", "Mlle", "Mme", "Ms", "Sir", "the Countess"]
for df in [testandtrain_df, validation_df]:
for row in df.index:
if df.loc[row, 'Title'] in prof_titles:
df.loc[row, 'Title'] = "Professional"
elif df.loc[row, 'Title'] in other_titles:
df.loc[row, 'Title'] = "Other"
df['exp_anos_estudo'] = df['exp_anos_estudo'].map(limparAnosEstudo )<concatenate>
for df in [testandtrain_df, validation_df]:
df.drop(columns = ['Name'], inplace = True )
def limparParticipacao(valor):
if(valor < 50):
return 55
else:
return valor<categorify>
temp3_df = pd.concat([testandtrain_df.copy() , validation_df.copy() ], sort = False)
 title_ages = temp3_df.groupby("Title" ).Age.mean()
title_ages
df['participacao_transf_receita'] = df['participacao_transf_receita'].map(limparParticipacao )<compute_test_metric>
for df in [testandtrain_df, validation_df]:
for row in df.index:
if pd.isna(df.loc[row, 'Age']):
df.loc[row, 'Age'] = title_ages[df.loc[row, 'Title']]
def limparNotaMat(valor):
if(valor > 6.32):
return 6.32
else:
return valor<categorify>
print("Total null values in both dataframes:", testandtrain_df.isna().sum().sum() + validation_df.isna().sum().sum() )
df['nota_mat'] = df['nota_mat'].map(limparNotaMat )<set_options>
for df in [testandtrain_df, validation_df]:
df["Family_Onboard"] = df["SibSp"] + df["Parch"]
df.drop(columns = ['SibSp', 'Parch'], inplace = True )
np.percentile(df['populacao'], 75)
 np.percentile(df['populacao'], 25 )<define_variables>
testandtrain_df[testandtrain_df["Fare"] == 0]
irq = np.percentile(df['populacao'], 75)- np.percentile(df['populacao'], 25)
<feature_engineering>
validation_df[validation_df["Fare"] == 0]
df['populacao'] = irq<set_options>
print("Training dataset(rows, columns):", testandtrain_df.shape)
 print("Validation dataset(rows, columns):", validation_df.shape)
np.percentile(df['gasto_pc_saude'], 75)
<set_options>
temp4_df = pd.concat([testandtrain_df.copy() , validation_df.copy() ], sort = False)
 temp4_df = temp4_df[temp4_df.Fare != 0]
temp4_df.shape
np.percentile(df['gasto_pc_saude'], 25 )<categorify>
temp4_df = pd.concat([testandtrain_df.copy() , validation_df.copy() ], sort=False)
 print("Number of passengers in each bin/grouping(out of 1,309 - both dataframes):-
")
 temp4_df.groupby("FareBinned" ).Fare.count()
df['gasto_pc_saude'] = df['gasto_pc_saude'].map(limparGastoSaude )<compute_test_metric>
testandtrain_df.groupby("FareBinned" ).Survived.agg([("Count", "count"),("Survival(mean)", "mean")], axis = "rows")
def limpaTaxaEmpreendedorismo(valor):
if(valor > 0.4):
return 0.4
elif(valor < 0.12):
return 0.12
else:
return valor<categorify>
for df in [testandtrain_df, validation_df]:
df.drop(columns = ["Fare"], inplace = True )
df['taxa_empreendedorismo'] = df['taxa_empreendedorismo'].map(limpaTaxaEmpreendedorismo )<concatenate>
testandtrain_df.groupby("Family_Onboard" ).Survived.agg([("Count", "count"),("Survival(mean)", "mean")], axis = "rows" )
def limpaGastoEducacao(valor):
if(valor > 1100):
return 1100
elif(valor < 250):
return 250
else:
return valor<categorify>
for df in [testandtrain_df, validation_df]:
df.drop(columns = ["Age", "Family_Onboard"], inplace = True )
df['gasto_pc_educacao'] = df['gasto_pc_educacao'].map(limpaGastoEducacao )<feature_engineering>
for df in [testandtrain_df, validation_df]:
df['Sex'] = df['Sex'].map({"female": 1, "male": 0} )
df['area'] = 1173.985<split>
dummy_cols = ["Pclass", "Embarked", "Title", "FareBinned", "AgeBinned", "FamilyBinned"]
dummy_prefixes = ["Pclass", "Port", "Title", "Fare", "Age", "Family"]
testandtrain_df = pd.get_dummies(testandtrain_df, columns = dummy_cols, prefix = dummy_prefixes)
 validation_df = pd.get_dummies(validation_df, columns = dummy_cols, prefix = dummy_prefixes )
train_raw = df[~df['nota_mat'].isnull() ]<compute_test_metric>
testandtrain_df.corr() ["Survived"][:].sort_values(ascending = False )
for coluna in train_raw.columns:
if(coluna == 'ranking_igm'):
continue
corr = np.corrcoef(train_raw['nota_mat'],train_raw[coluna])[0,1]
if(corr < 0.4 and corr > -0.4):
print(str(coluna)+ ': ' + str(corr)) 
<compute_test_metric>
print("Survival % split for train.csv data")
 testandtrain_df.Survived.value_counts(normalize = True )
for coluna in train_raw.columns:
if(coluna == 'ranking_igm'):
continue
corr = np.corrcoef(train_raw['nota_mat'],train_raw[coluna])[0,1]
if(corr < -0.4):
print(str(coluna)+ ': ' + str(corr))<split>
X = testandtrain_df.iloc[:, 1:]
y = testandtrain_df.iloc[:, 0]
X_train, X_test, y_train, y_test = train_test_split(X, y, stratify = y, test_size = 0.3, random_state = 0)
test = df[df['nota_mat'].isnull() ]<drop_column>
X_train
del test['nota_mat']<import_modules>
X_test
from sklearn.model_selection import train_test_split<import_modules>
dt_model = tree.DecisionTreeClassifier(random_state = 0)
 dt_model.fit(X_train, y_train )
from sklearn.model_selection import train_test_split<split>
print("Train Accuracy:", dt_model.score(X_train, y_train))
print("Test Accuracy:", dt_model.score(X_test, y_test))
train, valid = train_test_split(train_raw, random_state=42 )<define_variables>
train_accuracy = []
test_accuracy = []
trial_range = [0.05, 0.1, 0.15, 0.2, 0.25, 0.3, 0.35, 0.4, 0.45, 0.5]
for test_split in trial_range:
temp_X_train, temp_X_test, temp_y_train, temp_y_test = train_test_split(X, y, stratify = y, test_size = test_split, random_state=1)
 temp_dt = tree.DecisionTreeClassifier(random_state = 0)
 temp_dt.fit(temp_X_train, temp_y_train)
 train_accuracy.append(temp_dt.score(temp_X_train, temp_y_train))
test_accuracy.append(temp_dt.score(temp_X_test, temp_y_test))
testtrainsplit_df = pd.DataFrame({"test_split": trial_range, "train_acc": train_accuracy, "test_acc": test_accuracy})
 plt.figure(figsize =(20,5))
plt.plot(testtrainsplit_df["test_split"], testtrainsplit_df["train_acc"], marker="o")
 plt.plot(testtrainsplit_df["test_split"], testtrainsplit_df["test_acc"], marker="o")
 plt.xlabel("Ratio of test data(vs train data)")
 plt.xticks(trial_range)
 plt.ylabel("Model accuracy score")
 plt.title("Decision Tree model accuracy with different test:train ratios")
 plt.legend()
removed_cols = ['nota_mat', 'Unnamed: 0', 'gasto_pc_saude'] 
<define_variables>
train_accuracy = []
test_accuracy = []
trial_range = range(1,11)
 for depth in trial_range:
temp_dt = tree.DecisionTreeClassifier(random_state = 0, max_depth = depth)
 temp_dt.fit(X_train, y_train)
 train_accuracy.append(temp_dt.score(X_train, y_train))
test_accuracy.append(temp_dt.score(X_test, y_test))
maxdepth_df = pd.DataFrame({"max_depth": trial_range, "train_acc": train_accuracy, "test_acc": test_accuracy})
 maxdepth_df
feats = [c for c in df.columns if c not in removed_cols]<import_modules>
max_depth_fixed = 4
from sklearn.ensemble import RandomForestRegressor, ExtraTreesRegressor
from sklearn.ensemble import AdaBoostRegressor, GradientBoostingRegressor
from sklearn.tree import DecisionTreeRegressor
from sklearn.linear_model import LinearRegression
from sklearn.neighbors import KNeighborsRegressor
from sklearn.svm import SVR
from xgboost import XGBClassifier<choose_model_class>
train_accuracy = []
test_accuracy = []
trial_range = range(2,51)
 for samples in trial_range:
temp_dt = tree.DecisionTreeClassifier(random_state = 0, max_depth = max_depth_fixed, min_samples_leaf = samples)
 temp_dt.fit(X_train, y_train)
 train_accuracy.append(temp_dt.score(X_train, y_train))
test_accuracy.append(temp_dt.score(X_test, y_test))
minsamples_df = pd.DataFrame({"min_samples_leaf": trial_range, "train_acc": train_accuracy, "test_acc": test_accuracy})
 plt.figure(figsize =(20,5))
plt.plot(minsamples_df["min_samples_leaf"], minsamples_df["train_acc"], marker = "o")
 plt.plot(minsamples_df["min_samples_leaf"], minsamples_df["test_acc"], marker = "o")
 plt.xlabel("Minimum samples in terminal nodes")
 plt.xticks(range(0, max(trial_range)+ 2, 2))
plt.ylabel("Model accuracy score")
 plt.title("Decision Tree model accuracy with varying 'min_samples_leaf'")
 plt.legend()
models = {'RandomForest': RandomForestRegressor(random_state=42),
'ExtraTrees': ExtraTreesRegressor(random_state=42),
'GBM': GradientBoostingRegressor(random_state=42),
'DecisionTree': DecisionTreeRegressor(random_state=42),
'AdaBoost': AdaBoostRegressor(random_state=42),
'KNN 1': KNeighborsRegressor(n_neighbors=1),
'KNN 3': KNeighborsRegressor(n_neighbors=3),
'KNN 11': KNeighborsRegressor(n_neighbors=11),
'SVR': SVR() ,
'Linear Regression': LinearRegression() }<import_modules>
dt_model = tree.DecisionTreeClassifier(random_state = 0, max_depth = max_depth_fixed, min_samples_leaf = 10)
 dt_model.fit(X_train, y_train)
 print("Train Accuracy:", dt_model.score(X_train, y_train))
print("Test Accuracy:", dt_model.score(X_test, y_test))
from sklearn.metrics import mean_squared_error<import_modules>
regression = LogisticRegression(solver = 'lbfgs')
 regression.fit(X_train, y_train)
 print("Logistic Regression model")
 print("="*25)
 print("Train Accuracy:", regression.score(X_train, y_train))
print("Test Accuracy:", regression.score(X_test, y_test))
from sklearn.metrics import mean_squared_error<compute_train_metric>
random_forest = RandomForestClassifier(n_estimators = 100)
 random_forest.fit(X_train, y_train)
 print("Random Forest Classifier model")
 print("="*30)
 print("Train Accuracy:", random_forest.score(X_train, y_train))
print("Test Accuracy:", random_forest.score(X_test, y_test))
def run_model(model, train, valid, feats, y_name):
model.fit(train[feats], train[y_name])
 preds = model.predict(valid[feats])
 return mean_squared_error(valid[y_name], preds)**(1/2 )<filter>
xgb = XGBClassifier()
xgb.fit(X_train, y_train)
 print("XGB Classifier model")
 print("="*20)
 print("Train Accuracy:", xgb.score(X_train, y_train))
print("Test Accuracy:", xgb.score(X_test, y_test))
train_feat = train[feats]<find_best_params>
validation_df
scores = []
for name, model in models.items() :
score = run_model(model, train, valid, feats, 'nota_mat')
 scores.append(score)
 print(name+':', score )<train_model>
X_validate = validation_df.drop("PassengerId", axis = 1 )
rf = RandomForestRegressor(random_state=42)
 rf.fit(train[feats], train['nota_mat'])
 pd.Series(rf.feature_importances_, index=feats ).sort_values().plot.barh()<choose_model_class>
dt_model.fit(X, y)
 prediction = dt_model.predict(X_validate)
 prediction
<train_model><EOS> 
output = pd.DataFrame({'PassengerId': validation_df.PassengerId, 'Survived': prediction})
 output.to_csv('my_submission.csv', index = False )