Kaggle competition: Titanic - Machine Learning from Disaster
dftotal['populacao']=dftotal['populacao'].str.replace('(','' ).str.replace(')',
'' ).str.replace(',','' ).str.replace('.','' ).astype(dtype=np.int )<data_type_conversions>
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import classification_report, confusion_matrix, roc_auc_score
dftotal['area']=dftotal['area'].str.replace(',','' ).astype(float)
<data_type_conversions>
train = pd.read_csv(".. /input/titanic/train.csv", index_col="PassengerId")
 X_test = pd.read_csv(".. /input/titanic/test.csv", index_col="PassengerId")
 print(train.info())
 print(X_test.info() )
dftotal['densidade_dem']=dftotal['densidade_dem'].str.replace(',','' ).astype(float)
 dftotal['cat_porte'] = dftotal['porte'].astype('category' ).cat.codes
dftotal['cat_regiao'] = dftotal['regiao'].astype('category' ).cat.codes
dftotal['cat_estado'] = dftotal['estado'].astype('category' ).cat.codes<data_type_conversions>
print("TrainData")
 print("---------------")
 print(train.isna().sum())
 print("TestData")
 print("---------------")
 print(X_test.isna().sum() ,"
" )
dftotal['gasto_pc_educacao'].fillna(dftotal['gasto_pc_educacao'].mean() , inplace=True)
 dftotal['perc_pop_econ_ativa'].fillna(dftotal['perc_pop_econ_ativa'].mean() , inplace=True)
<define_variables>
train.loc[~train.isna().Cabin, "Cabin"] = 1
train.Cabin.fillna(0, inplace=True)
 X_test.loc[~X_test.isna().Cabin, "Cabin"] = 1
X_test.Cabin.fillna(0, inplace=True )
feats = [c for c in dftotal.columns if c not in ['codigo_mun', 'comissionados_por_servidor','nota_mat', 'densidade_dem',
'participacao_transf_receita', 'servidores',
'gasto_pc_saude', 'hab_p_medico', 'exp_vida',
'exp_anos_estudo', 'regiao', 'estado', 'porte', 'municipio']] 
<split>
train.Age = train.groupby(["Pclass",pd.cut(train.Fare, np.arange(0, 300, 2)) ],group_keys=False ).Age.apply(lambda g: g.fillna(g.mean()))
train.Age.fillna(train.Age.mean() , inplace=True)
 train = train.astype({"Age":"int64"})
 X_test.Age = X_test.groupby(["Pclass",pd.cut(X_test.Fare, np.arange(0, 300, 2)) ],group_keys=False ).Age.apply(lambda g: g.fillna(g.mean()))
X_test.Age.fillna(X_test.Age.mean() , inplace=True)
 X_test = X_test.astype({"Age":"int64"})
 print(train.Age.isna().sum() , X_test.Age.isna().sum() )
dftreino=dftotal[~dftotal.nota_mat.isnull() ]
dfsubmissao=dftotal[dftotal.nota_mat.isnull() ]<split>
X_test.Fare.fillna(X_test[X_test.Pclass==3].Fare.mean() , inplace=True )
dftreino2, dfteste = train_test_split(dftreino, test_size=0.20, random_state=42)
 dftreino2, dfvalida = train_test_split(dftreino2, test_size=0.20, random_state=42)
 rf = RandomForestClassifier(random_state=42, n_estimators=200, min_samples_split=5, max_depth=4 )<predict_on_test>
train = pd.get_dummies(train, columns=["Sex","Embarked"])
 X_test = pd.get_dummies(X_test, columns=["Sex","Embarked"] )
rf.fit(dftreino2[feats], dftreino2['nota_mat'])
 predicao = rf.predict(dfvalida[feats] )<compute_test_metric>
X = train.drop(columns=["Survived","Name","Ticket"])
 y = train["Survived"]
X_test.drop(columns=["Name","Ticket"], inplace=True)
 X_train, X_valid, y_train, y_valid = train_test_split(X, y, train_size=0.5)
 print(y_train.value_counts())
 print(y_valid.value_counts() )
accuracy_score(dfvalida['nota_mat'], predicao )<compute_test_metric>
randomforest = RandomForestClassifier(n_estimators=200, max_depth=5)
 model1 = randomforest.fit(X_train, y_train)
 pred_valid = model1.predict(X_valid)
 pred_valid_prob = model1.predict_proba(X_valid)
 print(classification_report(y_valid, pred_valid))
print(model1.score(X_valid, y_valid))
print(roc_auc_score(y_valid,pred_valid_prob[:,1]))
accuracy_score(dfteste['nota_mat'], rf.predict(dfteste[feats]))<save_to_csv>
model = randomforest.fit(X, y)
 pred = model.predict(X_test)
 result = pd.DataFrame(pred, index=X_test.index, columns=["Survived"])
 result.to_csv("submission.csv")
 result.head()