Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<concatenate>
%matplotlib inline
warnings.filterwarnings("ignore" )
final_test = pd.concat([test,sex,pclass,embarked],axis = 1)
 final_test.head()<drop_column>
train_df = pd.read_csv('.. /input/titanic/train.csv',index_col='PassengerId')
 test_df = pd.read_csv('.. /input/titanic/test.csv',index_col='PassengerId' )
final_test = final_test.drop(['sex','embarked','pclass'],axis = 1)
 final_test.head()<rename_columns>
df1 = train_df.copy()
final_test.columns = ['age','sibsp','parch','fare','sex_male','pclass_2','pclass_3','embarked_q','embarked_s']
final_test.head()<prepare_x_and_y>
df2 = df1.dropna(subset=['Embarked'] )
X_train = final_train.drop('survived',axis=1)
 y_train = final_train['survived']<prepare_x_and_y>
df3 = df2.drop('Cabin',axis=1 )
X_test = final_test<import_modules>
df3['Title'] = df3['Name'].str.replace(r' (.*,)|(\.. *)','' )
from sklearn.preprocessing import StandardScaler<normalization>
df4 = df3.drop(['Name','Ticket'],axis=1 )
scaler = StandardScaler()<normalization>
cat_col = ['Pclass','Sex','Embarked','Title']
num_col = ['Age','SibSp','Parch','Fare']
scaled_X_train = scaler.fit_transform(X_train)
 scaled_X_test = scaler.transform(X_test)
<prepare_x_and_y>
df5 = df4.copy()
df5['Title'] = df5['Title'].apply(lambda x: 'others' if x not in ['Mr','Mrs','Miss','Master'] else x)
y_test = gen_sub['Survived']<import_modules>
df6 = df5.copy()
df6['SibSp'] = df6['SibSp'].apply(lambda x: 3 if x>2 else x)
 df6['Parch'] = df6['Parch'].apply(lambda x: 3 if x>2 else x )
from sklearn.linear_model import LogisticRegression<import_modules>
for col in cat_col:
print(df6.groupby(col)['Survived'].mean() )
from sklearn.model_selection import GridSearchCV<import_modules>
df6.groupby('Pclass')['Age'].mean()
from sklearn.model_selection import GridSearchCV<choose_model_class>
df7 = df6.copy()
def age_miss(df):
if np.isnan(df['Age']):
if df['Pclass']==1:
return 38
elif df['Pclass']==2:
return 29
elif df['Pclass']==3:
return 25
else:
return df['Age']
df7['Age'] = df6.apply(age_miss,axis=1 )
log_model = LogisticRegression(solver='saga',multi_class="ovr",max_iter=5000 )<define_search_space>
final_df = df7.copy()
penalty = ['l1', 'l2']
C = np.logspace(0, 4, 10 )<choose_model_class>
test_df = test_df.dropna(subset=['Embarked'])
 test_df = test_df.drop('Cabin',axis=1)
 test_df['Title'] = test_df['Name'].str.replace(r' (.*,)|(\.. *)','')
 test_df = test_df.drop(['Name','Ticket'],axis=1)
 test_df['Title'] = test_df['Title'].apply(lambda x: 'others' if x not in ['Mr','Mrs','Miss','Master'] else x)
 def age_miss(df):
if np.isnan(df['Age']):
if df['Pclass']==1:
return 38
elif df['Pclass']==2:
return 29
elif df['Pclass']==3:
return 25
else:
return df['Age']
test_df['Age'] = test_df.apply(age_miss,axis=1 )
grid_model = GridSearchCV(log_model,param_grid={'C':C,'penalty':penalty} )<train_model>
features = ['Pclass', 'Sex','Age','Embarked', 'Title']
X = final_df[features]
y = final_df.Survived
test = test_df[features]
grid_model.fit(scaled_X_train,y_train )<find_best_params>
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=101 )
grid_model.best_params_<import_modules>
from sklearn.pipeline import make_pipeline,Pipeline
from sklearn.compose import make_column_transformer
from sklearn.preprocessing import OneHotEncoder
from sklearn.ensemble import RandomForestClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.neighbors import KNeighborsClassifier
from sklearn.model_selection import GridSearchCV
from sklearn.metrics import accuracy_score,confusion_matrix,classification_report,plot_confusion_matrix<predict_on_test>
ohe = OneHotEncoder()
params = [{'classifier':[RandomForestClassifier() ],
'classifier__n_estimators':[10,25,50,75,100],
'classifier__max_depth':[5,10,15,20]},
{'classifier':[LogisticRegression() ],
'classifier__penalty':['l1','l2'],
'classifier__C':[0.01,0.1,1,10,100,1000],
'classifier__max_iter':[10,100,1000]},
{'classifier':[KNeighborsClassifier() ],
'classifier__n_neighbors':[2,5,10]}]
preprocessing = make_column_transformer(( ohe,['Pclass', 'Sex','Embarked', 'Title']),remainder='passthrough')
 pipe = Pipeline([('preprocessing',preprocessing), 
('classifier',RandomForestClassifier())] )
y_lr_pred = grid_model.predict(scaled_X_test )<compute_test_metric>
gr = GridSearchCV(pipe,param_grid=params,cv=5 ).fit(X_train,y_train )
accuracy_score(y_test,y_lr_pred )<compute_test_metric>
gr.best_params_
confusion_matrix(y_test,y_lr_pred )<compute_test_metric>
submit = pd.DataFrame(gr.predict(test),index=test_df.index,columns=['Survived'] )
<import_modules><EOS> 
submit.to_csv('Submission.csv',index=False )