Kaggle competition: Titanic - Machine Learning from Disaster
train_EB7_ns = np.load('.. /input/cats-and-dogs-embedded-data/cats_and_dogs_4/cats_and_dogs_4/train_EB7_ns.npy')
 test_EB7_ns = np.load('.. /input/cats-and-dogs-embedded-data/cats_and_dogs_4/cats_and_dogs_4/test_EB7_ns.npy' )<split>
path = '.. /input/train.csv'
test_path = '.. /input/test.csv'
test_data = pd.read_csv(test_path)
 train_data = pd.read_csv(path)
 total_data = train_data.append(test_data)
 print(( total_data.isnull().sum()))
data = total_data.drop('Cabin', axis = 1)
 droplist = ['PassengerId','Name','Sex','Ticket','Embarked', 'Survived', 'Pclass', 'Parch', 'Fare', 'SibSp' ]
data1=data.drop(droplist, axis = 1)
 imputed_age = my_age_imputer.fit_transform(data1)
 age_corrected = pd.DataFrame({'ImputedAge':imputed_age[:,0]})
 data['ImputedAge'] = age_corrected
data.Embarked.fillna('S', inplace = True)
 data.Embarked = data.Embarked.replace(['S', 'Q', 'C'], [0,1,2])
 corr = data.corr()
print(corr.Survived )
X_train, X_val, y_train, y_val = train_test_split(train_EB7_ns, target, test_size=0.1, random_state=42 )<train_model>
data['fsize'] = data.Parch+data.SibSp+1
data.groupby('Survived' ).fsize.value_counts()
uniq_fsize = list(data.fsize.unique())
 print(( uniq_fsize))
uniq_fsize_2 = []
for i in uniq_fsize :
if i == 1:
uniq_fsize_2.append('Single')
 elif i > 4:
uniq_fsize_2.append('Large')
 elif i<=4 and i>1:
uniq_fsize_2.append('Medium')
 print(uniq_fsize_2)
 data.fsize.replace(uniq_fsize,uniq_fsize_2, inplace= True)
 data.groupby('Survived' ).fsize.value_counts()
%%time
svc = SVC(C= 0.07, probability=True)
 svc.fit(X_train, y_train )<predict_on_test>
data['isChild'] =np.where(data.ImputedAge<18, 'Child','Adult')
 data.groupby('Survived' ).isChild.value_counts()
val_preds = svc.predict_proba(X_val)[:,1]<compute_test_metric>
data['lastName'] =(data.Name.str.split(',', expand = True)) [:][0]
data['name_title'] =(data.Name.str.split('.',expand = True)) [:][0].str.split(',', expand = True)[:][1]
titles = data.name_title.value_counts().keys().tolist()
title_freq = data.name_title.value_counts().tolist()
miss_titles = [' Mme', ' Ms', ' Mlle']
rare_titles = [' Dr', ' Rev', ' Major', ' Col', ' Don', ' Capt', ' Sir', ' Lady', ' Jonkheer', ' the Countess', ' Dona']
data.name_title.replace(rare_titles,' Rare Titles', inplace = True)
 data.name_title.replace(miss_titles,' Miss', inplace = True)
(data.groupby('Sex' ).name_title.value_counts())
roc_auc_score(y_val, val_preds )<compute_test_metric>
features = ['Pclass', 'Sex', 'Fare_bin', 'ImputedAge_bin', 'fsize', 'isChild', 'name_title', 'isMother']
train_data = data.iloc[0:891,:]
test_data = data.iloc[891:1310,:]
y = train_data.Survived
X = train_data[features]
log_loss(y_val, val_preds )<predict_on_test>
X_train, X_val, y_train, y_val = train_test_split(X,y, random_state = 1 )
test_preds = svc.predict_proba(test_EB7_ns)[:,1]<save_to_csv>
knn = KNeighborsClassifier()
n_neigh = [i for i in range(1,50,2)]
algorithm = ['auto']
weight = ['uniform','distance']
leaf = [i for i in range(1,50,5)]
param = {'n_neighbors':n_neigh, 'algorithm':algorithm, 'weights':weight, 'leaf_size':leaf}
grid = GridSearchCV(knn,param, cv = 10, scoring = 'accuracy')
 grid.fit(X_train, y_train)
 print(grid.best_score_)
 print(grid.best_estimator_)
 grid.best_estimator_.fit(X_train,y_train)
 grid_pred = grid.best_estimator_.predict(X_val)
 print("=== Confusion Matrix for Grid KNN ===")
 print(( confusion_matrix(y_val, grid_pred)))
 print("=== Classification Report ===")
 print(classification_report(y_val, grid_pred))
sample_submission['id'] = ids
sample_submission['label'] = test_preds
sample_submission.to_csv('submission.csv', index=False)
 sample_submission.head()<import_modules>
t_data = test_data.drop('Age', axis =1)
 t_data.Fare.fillna(t_data.Fare.median() , inplace = True)
 t_data_X = t_data[features]
predict_t_data =(grid.best_estimator_.predict(t_data_X ).astype(int))
submission = pd.DataFrame({"PassengerId": test_data["PassengerId"], "Survived": predict_t_data})
 submission.to_csv('submission_GridCV_KNN.csv', index=False )