Kaggle competition: Titanic - Machine Learning from Disaster
result_df.to_csv('submissionCommit8AugmentedDataE360.csv', index=False )<load_from_csv>
train_data = pd.read_csv("/kaggle/input/titanic/train.csv")
 train_data.head()
train = pd.read_csv('.. /input/train.csv' )<load_from_csv>
test_data = pd.read_csv("/kaggle/input/titanic/test.csv")
 test_data.head()
test = pd.read_csv('.. /input/test.csv' )<feature_engineering>
train_data.isnull().sum()
train['nota_mat'] = np.log(train['nota_mat'] )<concatenate>
test_data.isnull().sum()
df = train.append(test, sort=False )<count_values>
train_data[['Pclass', 'Survived']].groupby(['Pclass'], as_index=False ).mean().sort_values(by='Survived', ascending=False )
df.count()<count_values>
train_data[train_data.Embarked.isnull() ]
for col in df.columns:
if(df[col].count() < 3124):
print(col)
 print(3124 - df[col].count())
<count_unique_values>
train_data = train_data.fillna({"Embarked": "S"} )
df.nunique()<feature_engineering>
train_data = train_data.drop(['Ticket', 'Cabin'], axis=1)
 test_data = test_data.drop(['Ticket', 'Cabin'], axis=1 )
df['participacao_transf_receita'] = df['participacao_transf_receita'].fillna(np.mean(df['participacao_transf_receita']))
df['servidores'] = df['servidores'].fillna(np.mean(df['servidores']))
df['perc_pop_econ_ativa'] = df['perc_pop_econ_ativa'].fillna(np.mean(df['perc_pop_econ_ativa']))
df['gasto_pc_saude'] = df['gasto_pc_saude'].fillna(np.mean(df['gasto_pc_saude']))
df['hab_p_medico'] = df['hab_p_medico'].fillna(np.mean(df['hab_p_medico']))
df['gasto_pc_educacao'] = df['gasto_pc_educacao'].fillna(np.mean(df['gasto_pc_educacao']))
df['exp_anos_estudo'] = df['exp_anos_estudo'].fillna(np.mean(df['exp_anos_estudo']))
df['exp_vida'] = df['exp_vida'].fillna(np.mean(df['exp_vida']))
df['idhm'] = df['idhm'].fillna(np.mean(df['idhm']))
df['indice_governanca'] = df['indice_governanca'].fillna(np.mean(df['indice_governanca'])) 
<data_type_conversions>
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')
 train_data[['Title', 'Survived']].groupby(['Title'], as_index=False ).mean()
def convertaStringParaFloat(valor):
if(type(valor)== str):
valor = valor.replace(',','')
 valor = float(valor)
 return valor
df['densidade_dem'] = df['densidade_dem'].map(convertaStringParaFloat)
 df['densidade_dem'] = df['densidade_dem'].fillna(np.mean(df['densidade_dem']))
df.info() 
<categorify>
title_mapping = {"Mr": 1, "Miss": 2, "Mrs": 3, "Master": 4, "Rare": 5}
for dataset in combine:
dataset['Title'] = dataset['Title'].map(title_mapping)
 dataset['Title'] = dataset['Title'].fillna(0)
 train_data.head()
df['area'] = df['area'].map(convertaStringParaFloat )<data_type_conversions>
mr_age = train_data[train_data["Title"] == 1]["AgeGroup"].mode()
miss_age = train_data[train_data["Title"] == 2]["AgeGroup"].mode()
mrs_age = train_data[train_data["Title"] == 3]["AgeGroup"].mode()
master_age = train_data[train_data["Title"] == 4]["AgeGroup"].mode()
royal_age = train_data[train_data["Title"] == 5]["AgeGroup"].mode()
rare_age = train_data[train_data["Title"] == 6]["AgeGroup"].mode()
age_title_mapping = {1: "Young Adult", 2: "Student", 3: "Adult", 4: "Baby", 5: "Adult", 6: "Adult"}
for x in range(len(train_data["AgeGroup"])) :
if train_data["AgeGroup"][x] == "Unknown":
train_data["AgeGroup"][x] = age_title_mapping[train_data["Title"][x]]
for x in range(len(test_data["AgeGroup"])) :
if test_data["AgeGroup"][x] == "Unknown":
test_data["AgeGroup"][x] = age_title_mapping[test_data["Title"][x]]
df['estado'] = df['estado'].astype('category' )<feature_engineering>
age_mapping = {'Baby': 1, 'Child': 2, 'Teenager': 3, 'Student': 4, 'Young Adult': 5, 'Adult': 6, 'Senior': 7}
train_data['AgeGroup'] = train_data['AgeGroup'].map(age_mapping)
 test_data['AgeGroup'] = test_data['AgeGroup'].map(age_mapping)
 train_data.head()
train_data = train_data.drop(['Age'], axis = 1)
 test_data = test_data.drop(['Age'], axis = 1 )
df['estado'] = df['estado'].cat.codes<data_type_conversions>
train_data = train_data.drop(['Name'], axis = 1)
 test_data = test_data.drop(['Name'], axis = 1 )
df['municipio'] = df['municipio'].astype('category' )<feature_engineering>
sex_mapping = {"male": 0, "female": 1}
train_data['Sex'] = train_data['Sex'].map(sex_mapping)
 test_data['Sex'] = test_data['Sex'].map(sex_mapping)
 train_data.head()
df['municipio'] = df['municipio'].cat.codes<categorify>
embarked_mapping = {"S": 1, "C": 2, "Q": 3}
train_data['Embarked'] = train_data['Embarked'].map(embarked_mapping)
 test_data['Embarked'] = test_data['Embarked'].map(embarked_mapping)
 train_data.head()
df = pd.concat([df, pd.get_dummies(df['regiao'], prefix='regiao' ).iloc[:, :-1]], axis =1)
 del df['regiao']
df.info()<categorify>
train_data = train_data.drop(['Fare'], axis = 1)
 test_data= test_data.drop(['Fare'], axis = 1 )
df = pd.concat([df, pd.get_dummies(df['porte'], prefix='porte' ).iloc[:, :-1]], axis =1)
 del df['porte'] 
<data_type_conversions>
X = train_data.drop(['Survived', 'PassengerId'], axis=1)
 Y = train_data["Survived"]
x_train, x_val, y_train, y_val = train_test_split(X,Y, test_size = 0.22, random_state = 0 )
f = lambda x: x.replace('ID_ID_','')
 df['codigo_mun'] = df['codigo_mun'].map(f)
 df['codigo_mun'] = df['codigo_mun'].astype(int)
<categorify>
G = GaussianNB()
G.fit(x_train, y_train)
 y_pred = G.predict(x_val)
 Accuracy_for_gaussian = round(accuracy_score(y_pred, y_val)* 100, 2)
 print("Accuracy_for_gaussian: ",Accuracy_for_gaussian)
 L = LogisticRegression()
L.fit(x_train, y_train)
 y_pred = L.predict(x_val)
 Accuracy_for_logistic_regression = round(accuracy_score(y_pred, y_val)* 100, 2)
 print("Accuracy_for_logistic_regression: ",Accuracy_for_logistic_regression)
 R = RandomForestClassifier()
R.fit(x_train, y_train)
 y_pred = R.predict(x_val)
 Accuracy_for_Random_forest = round(accuracy_score(y_pred, y_val)* 100, 2)
 print("Accuracy_for_Random_forest: ",Accuracy_for_Random_forest)
 SVC = LinearSVC()
SVC.fit(x_train, y_train)
 y_pred = SVC.predict(x_val)
 Accuracy_for_LinearSVC = round(accuracy_score(y_pred, y_val)* 100, 2)
 print("Accuracy_for_LinearSVC: ",Accuracy_for_LinearSVC)
 clf = svm.SVC()
clf.fit(x_train, y_train)
 y_pred = clf.predict(x_val)
 Accuracy_for_SVM = round(accuracy_score(y_pred, y_val)* 100, 2)
 print("Accuracy_for_SVM: ",Accuracy_for_SVM)
 MNB = MultinomialNB()
MNB.fit(x_train, y_train)
 y_pred = MNB.predict(x_val)
 Accuracy_for_MultinomialNB = round(accuracy_score(y_pred, y_val)* 100, 2)
 print("Accuracy_for_MultinomialNB: ",Accuracy_for_MultinomialNB)
 DTC = DecisionTreeClassifier()
DTC.fit(x_train, y_train)
 y_pred = DTC.predict(x_val)
 Accuracy_for_DecisionTreeClassifier = round(accuracy_score(y_pred, y_val)* 100, 2)
 print("Accuracy_for_DecisionTreeClassifier: ",Accuracy_for_DecisionTreeClassifier)
 GBC = GradientBoostingClassifier()
GBC.fit(x_train, y_train)
 y_pred = GBC.predict(x_val)
 Accuracy_for_GradientBoostingClassifier = round(accuracy_score(y_pred, y_val)* 100, 2)
 print("Accuracy_for_GradientBoostingClassifier: ",Accuracy_for_GradientBoostingClassifier)
 SGD = SGDClassifier(loss="log", penalty="l2", max_iter=5)
 SGD.fit(x_train, y_train)
 y_pred = SGD.predict(x_val)
 Accuracy_for_SGDClassifier = round(accuracy_score(y_pred, y_val)* 100, 2)
 print("Accuracy_for_SGDClassifier: ",Accuracy_for_SGDClassifier)
 KN = KNeighborsClassifier()
KN.fit(x_train, y_train)
 y_pred = KN.predict(x_val)
 Accuracy_for_KNeighborsClassifier = round(accuracy_score(y_pred, y_val)* 100, 2)
 print("Accuracy_for_KNeighborsClassifier: ",Accuracy_for_KNeighborsClassifier )
<categorify><EOS> 
ID = test_data['PassengerId']
Predictions = GBC.predict(test_data.drop('PassengerId', axis=1))
output = pd.DataFrame({ 'PassengerId' : ID, 'Survived': Predictions })
 output.to_csv('submission.csv', index=False )