Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<save_to_csv>
TRAIN_PATH = ".. /input/titanic/train.csv"
TEST_PATH = ".. /input/titanic/test.csv"
SAMPLE_SUBMISSION_PATH = ".. /input/titanic/gender_submission.csv"
SUBMISSION_PATH = "submission.csv"
ID = "PassengerId"
TARGET = "Survived"
SEED = 123
N_SPLITS = 5
GRID_SEARCH_CV_NUM = 5
SCORING = "roc_auc"
file_name='submision.csv'
yt.to_csv(file_name,index=True)
<load_pretrained>
copyfile(src = ".. /input/titanic-preprocess/titanic_preprocess_py.py",
dst = ".. /working/titanic_preprocess_py.py")
 train,test = loadAndPreprocess(TRAIN_PATH,TEST_PATH)
 train.head()
filename = 'Final_Model.mod'
pickle.dump(xgc, open(filename, 'wb'))<load_from_csv>
y = train[TARGET]
X = train.drop([ID,TARGET],axis=1)
 X_test = test.drop([ID,TARGET],axis=1)
 stk = StratifiedKFold(n_splits=N_SPLITS,random_state=SEED,shuffle=True)
 for train_index,val_index in stk.split(X,y):
X_train,y_train = X.iloc[train_index],y.iloc[train_index]
X_val,y_val = X.iloc[val_index],y.iloc[val_index]
knn = KNeighborsClassifier()
knn.fit(X_train,y_train)
 pred_knn = knn.predict(X_val)
 print("knn recall:",round(recall_score(y_val,pred_knn),2))
print("knn precision:",round(precision_score(y_val,pred_knn),2))
print("knn f1_score:",round(f1_score(y_val,pred_knn),2))
print("knn rou_auc_score:",round(roc_auc_score(y_val,pred_knn),2))
params = {'algorithm': ['auto'],
'weights': ['uniform', 'distance'],
'leaf_size': range(1,30),
'n_neighbors': range(3,20)}
gs =GridSearchCV(knn, param_grid = params,
verbose=True,
cv=GRID_SEARCH_CV_NUM,
scoring = SCORING 
)
 gs.fit(X_train, y_train)
 print(gs.best_score_)
 print(gs.best_estimator_)
 print(gs.best_params_)
 pred_val = gs.predict(X_val)
 print("gs recall:",round(recall_score(y_val,pred_val),2))
print("gs precision:",round(precision_score(y_val,pred_val),2))
print("gs f1_score:",round(f1_score(y_val,pred_val),2))
print("gs rou_auc_score:",round(roc_auc_score(y_val,pred_val),2))
dftreino = pd.read_csv('.. /input/train.csv')
 dfteste = pd.read_csv('.. /input/test.csv')
 dftotal = dfteste.append(dftreino )<data_type_conversions>
sub = pd.read_csv(SAMPLE_SUBMISSION_PATH)
 sub[TARGET] = gs.predict(X_test ).astype(int)
 sub.to_csv(SUBMISSION_PATH,index=False)
 sub.head(10 )