Kaggle competition: Titanic - Machine Learning from Disaster
data.show_batch(3 )<groupby>
%matplotlib inline
data.batch_stats()<choose_model_class>
train=pd.read_csv('/kaggle/input/titanic/train.csv')
 test=pd.read_csv('/kaggle/input/titanic/test.csv')
 y=train['Survived']
submission=pd.DataFrame(test['PassengerId'])
 total=pd.concat([train.drop(['PassengerId','Survived'],axis=1),test.drop('PassengerId',axis=1)] )
arch = models.resnet34<choose_model_class>
encoder=LabelEncoder()
total['Sex']=encoder.fit_transform(total['Sex'])
 total['Embarked']=encoder.fit_transform(total['Embarked'])
 total['Age']=total['Age'].astype(int)
 total.head()
learn = cnn_learner(data, arch, metrics=accuracy ).to_fp16()<define_search_space>
total['Famile_no']=total['SibSp']+total['Parch']+1
def family_gp(size):
a=''
if(size<=1):
a='Alone'
elif(size<=3):
a='nuclear family'
elif(size<=5):
a='middle'
else:
a='large'
return a
total['Family_grp']=total['Famile_no'].map(family_gp)
lr = 5e-2<train_model>
total.drop(['Name','Ticket','Cabin','SibSp','Parch'],axis=1,inplace=True)
 total.head()
learn.fit_one_cycle(6, max_lr=slice(lr))<save_model>
total=pd.get_dummies(total,columns=['Embarked','Family_grp','Pclass'])
 c=StandardScaler()
train=total[:len(train)]
train=c.fit_transform(train)
 test=total[len(train):]
test=c.fit_transform(test )
learn.save('stage-1' )<predict_on_test>
xtrain,xvalid,ytrain,yvalid=train_test_split(train,y,test_size=0.3)
 models = {"KNN": KNeighborsClassifier() ,
"Logistic Regression": LogisticRegression(max_iter=10000),
"Random Forest": RandomForestClassifier() ,
"SVC" : SVC(probability=True),
"DecisionTreeClassifier" : DecisionTreeClassifier() ,
"AdaBoostClassifier" : AdaBoostClassifier() ,
"GradientBoostingClassifier" : GradientBoostingClassifier() ,
"GaussianNB" : GaussianNB() ,
"LinearDiscriminantAnalysis" : LinearDiscriminantAnalysis() ,
"QuadraticDiscriminantAnalysis" : QuadraticDiscriminantAnalysis() }
def fit_and_score(models, X_train, X_valid, y_train, y_valid):
np.random.seed(42)
 model_scores = {}
for name, model in models.items() :
model.fit(X_train, y_train)
 y_pred = model.predict(X_valid)
 model_scores[name] = roc_auc_score(y_pred, y_valid)
 return model_scores
fit_and_score(models,xtrain,xvalid,ytrain,yvalid)
<save_to_csv><EOS> 
leaks = {
897:1,
899:1,
930:1,
932:1,
949:1,
987:1,
995:1,
998:1,
999:1,
1016:1,
1047:1,
1083:1,
1097:1,
1099:1,
1103:1,
1115:1,
}
model=GradientBoostingClassifier()
model.fit(train,target)
 a=model.predict(test)
 submission['Survived']=a
submission['Survived'] = sub['Survived'].apply(lambda x: 1 if x>0.8 else 0)
 submission['Survived'] = sub.apply(lambda r: leaks[int(r['PassengerId'])] if int(r['PassengerId'])in leaks else r['Survived'], axis=1)
 submission.to_csv('sub_tit.csv', index=False )