Kaggle competition: Titanic - Machine Learning from Disaster
for col in df_train.columns:
msg = 'column: {:>10}\t Percent of NaN value: {:.2f}%'.format(col, 100 *(df_train[col].isnull().sum() / df_train[col].shape[0]))
print(msg )<count_missing_values>
import seaborn as sns
import matplotlib.pyplot as plt
import time
for col in df_test.columns:
msg = 'column: {:>10}\t Percent of NaN value: {:.2f}%'.format(col, 100 *(df_test[col].isnull().sum() / df_test[col].shape[0]))
print(msg )<feature_engineering>
train_data = pd.read_csv('/kaggle/input/titanic/train.csv')
 train_data.head()
df_train['FamilySize'] = df_train['SibSp'] + df_train['Parch'] + 1
df_test['FamilySize'] = df_test['SibSp'] + df_test['Parch'] + 1<feature_engineering>
test_data = pd.read_csv('/kaggle/input/titanic/test.csv')
 test_data.head()
test_data.isna().sum()
df_train['Initial']= df_train.Name.str.extract('([A-Za-z]+)\.')
 df_test['Initial']= df_test.Name.str.extract('([A-Za-z]+)\.' )<categorify>
test_data["Fare"] = test_data["Fare"].fillna(0)
 test_data["Fare"].isna().sum()
df_train['Initial'].replace(['Mlle','Mme','Ms','Dr','Major','Lady','Countess','Jonkheer','Col','Rev','Capt','Sir','Don', 'Dona'],
['Miss','Miss','Miss','Mr','Mr','Mrs','Mrs','Other','Other','Other','Mr','Mr','Mr', 'Mr'],inplace=True)
 df_test['Initial'].replace(['Mlle','Mme','Ms','Dr','Major','Lady','Countess','Jonkheer','Col','Rev','Capt','Sir','Don', 'Dona'],
['Miss','Miss','Miss','Mr','Mr','Mrs','Mrs','Other','Other','Other','Mr','Mr','Mr', 'Mr'],inplace=True )<feature_engineering>
women = train_data.loc[train_data.Sex == "female"]["Survived"]
rate_women = sum(women)/ len(women)
 print("Rate of Women :",rate_women )
df_train.loc[(df_train.Age.isnull())&(df_train.Initial=='Mr'),'Age'] = 33
df_train.loc[(df_train.Age.isnull())&(df_train.Initial=='Mrs'),'Age'] = 36
df_train.loc[(df_train.Age.isnull())&(df_train.Initial=='Master'),'Age'] = 5
df_train.loc[(df_train.Age.isnull())&(df_train.Initial=='Miss'),'Age'] = 22
df_train.loc[(df_train.Age.isnull())&(df_train.Initial=='Other'),'Age'] = 46
df_test.loc[(df_test.Age.isnull())&(df_test.Initial=='Mr'),'Age'] = 33
df_test.loc[(df_test.Age.isnull())&(df_test.Initial=='Mrs'),'Age'] = 36
df_test.loc[(df_test.Age.isnull())&(df_test.Initial=='Master'),'Age'] = 5
df_test.loc[(df_test.Age.isnull())&(df_test.Initial=='Miss'),'Age'] = 22
df_test.loc[(df_test.Age.isnull())&(df_test.Initial=='Other'),'Age'] = 46<feature_engineering>
men = train_data.loc[train_data.Sex == "male"]["Survived"]
rate_men = sum(men)/ len(men)
 print("Rate of Men :",rate_men )
df_train.Cabin.fillna('0', inplace=True)
 df_train.loc[df_train.Cabin.str[0] == 'A', 'Cabin'] = 1
df_train.loc[df_train.Cabin.str[0] == 'B', 'Cabin'] = 2
df_train.loc[df_train.Cabin.str[0] == 'C', 'Cabin'] = 3
df_train.loc[df_train.Cabin.str[0] == 'D', 'Cabin'] = 4
df_train.loc[df_train.Cabin.str[0] == 'E', 'Cabin'] = 5
df_train.loc[df_train.Cabin.str[0] == 'F', 'Cabin'] = 6
df_train.loc[df_train.Cabin.str[0] == 'G', 'Cabin'] = 7
df_train.loc[df_train.Cabin.str[0] == 'T', 'Cabin'] = 8
df_test.Cabin.fillna('0', inplace=True)
 df_test.loc[df_test.Cabin.str[0] == 'A', 'Cabin'] = 1
df_test.loc[df_test.Cabin.str[0] == 'B', 'Cabin'] = 2
df_test.loc[df_test.Cabin.str[0] == 'C', 'Cabin'] = 3
df_test.loc[df_test.Cabin.str[0] == 'D', 'Cabin'] = 4
df_test.loc[df_test.Cabin.str[0] == 'E', 'Cabin'] = 5
df_test.loc[df_test.Cabin.str[0] == 'F', 'Cabin'] = 6
df_test.loc[df_test.Cabin.str[0] == 'G', 'Cabin'] = 7
df_test.loc[df_test.Cabin.str[0] == 'T', 'Cabin'] = 8 
<feature_engineering>
y = train_data["Survived"]
features = ["Sex","Pclass","SibSp","Parch","Fare"]
X = pd.get_dummies(train_data[features])
 X_test = pd.get_dummies(test_data[features])
 model = RandomForestClassifier(n_estimators = 20,max_depth = 5 ,random_state=42)
 model.fit(X,y)
 predictions = model.predict(X_test)
 output = pd.DataFrame({'PassengerId': test_data.PassengerId, 'Survived': predictions})
 output.to_csv('my_submission.csv', index=False)
<categorify><EOS> 
model2 = LogisticRegression(penalty= 'none' ,random_state=42 ,max_iter=150 ).fit(X, y)
 predictions2 = model2.predict(X_test)
 output2 = pd.DataFrame({'PassengerId': test_data.PassengerId, 'Survived': predictions2})
 output2.to_csv('my_submission2.csv', index=False)