Kaggle competition: Titanic - Machine Learning from Disaster
def submit_pred(dset, dtest, model = linear_model.LinearRegression() , name = 'submit'):
target = 'median_house_value'
labels = dset.columns.drop('Id' ).drop(target)
 X = dset[labels]
y = dset[target]
score = cross_val_score(model, X, y, cv = 10, scoring='r2')
 print('R² = {0:.3f} ± {1:.3f}'.format(np.mean(score), 2*np.std(score)))
 model.fit(X, y)
 y_test_raw = model.predict(dtest[labels])
 y_test = [np.max([entry, 0])for entry in y_test_raw]
output = pd.concat([test['Id'], pd.Series(y_test, name=target)], axis=1)
 output.to_csv('./{0}.csv'.format(name), index=False )<predict_on_test>
warnings.filterwarnings("ignore")
submit_pred(dataset, test, name = 'raw_linear_model' )<prepare_output>
train=pd.read_csv('.. /input/titanic/train.csv')
 test=pd.read_csv('.. /input/titanic/test.csv')
 y_test=pd.read_csv('.. /input/titanic/gender_submission.csv' )
submit_pred(add_features(dataset), add_features(test), name = 'improv_linear_model' )<drop_column>
y_test.head()
PassengerId=y_test['PassengerId']
y_test=y_test.drop(['PassengerId'],axis=1)
d_dset = add_dist(add_features(dataset))
d_test = add_dist(add_features(test))<compute_test_metric>
train_m=(max(train['Age'])+min(train['Age'])) /2
values={'Cabin':'nocabin','Age':train_m,'Embarked':'notknown'}
train=train.fillna(value=values)
 test_m=(max(test['Age'])+min(test['Age'])) /2
print(test_m)
 values={'Cabin':'nocabin','Age':test_m,'Embarked':'notknown',"Fare":max(test['Fare'])}
test=test.fillna(value=values)
submit_pred(d_dset,
d_test,
name = 'linear_model_dists_gps' )<predict_on_test>
y= train["Survived"]
train=train.drop(["Survived"],axis=1 )
submit_pred(d_dset.drop('latitude', axis = 1 ).drop('longitude', axis = 1),
d_test.drop('latitude', axis = 1 ).drop('longitude', axis = 1),
name = 'linear_model_dists' )<choose_model_class>
X_train, X_cv, y_train, y_cv = train_test_split(train, y, stratify=y, test_size=0.2 )
submit_pred(d_dset.drop('latitude', axis = 1),
d_test.drop('latitude', axis = 1),
model = linear_model.LassoLars(alpha=.1),
name = 'linear_model_lasso' )<choose_model_class>
vectorizer = CountVectorizer()
X_tr_emb =vectorizer.fit_transform(X_train['Embarked'])
 X_cv_emb =vectorizer.transform(X_cv['Embarked'])
 X_te_emb =vectorizer.transform(test['Embarked'])
 enc = OneHotEncoder(handle_unknown='ignore')
 X_tr_age =enc.fit_transform(np.array(X_train['Age'] ).reshape(-1,1))
X_cv_age =enc.transform(np.array(X_cv['Age'] ).reshape(-1,1))
X_te_age =enc.transform(np.array(test['Age'] ).reshape(-1,1))
X_tr_fare =enc.fit_transform(np.array(X_train['Fare'] ).reshape(-1,1))
X_cv_fare =enc.transform(np.array(X_cv['Fare'] ).reshape(-1,1))
X_te_fare =enc.transform(np.array(test['Fare'] ).reshape(-1,1))
X_tr_Sbp =enc.fit_transform(np.array(X_train['SibSp'] ).reshape(-1,1))
X_cv_Sbp =enc.transform(np.array(X_cv['SibSp'] ).reshape(-1,1))
X_te_Sbp =enc.transform(np.array(test['SibSp'] ).reshape(-1,1))
X_tr_par =enc.fit_transform(np.array(X_train['Parch'] ).reshape(-1,1))
X_cv_par =enc.transform(np.array(X_cv['Parch'] ).reshape(-1,1))
X_te_par=enc.transform(np.array(test['Parch'] ).reshape(-1,1))
X_tr_pclass =enc.fit_transform(np.array(X_train['Pclass'] ).reshape(-1,1))
X_cv_pclass =enc.transform(np.array(X_cv['Pclass'] ).reshape(-1,1))
X_te_pclass =enc.transform(np.array(test['Pclass'] ).reshape(-1,1))
X_tr_sex =vectorizer.fit_transform(X_train['Sex'])
 X_cv_sex =vectorizer.transform(X_cv['Sex'])
 X_te_sex =vectorizer.transform(test['Sex'])
 X_tr_cabin =vectorizer.fit_transform(X_train['Cabin'])
 X_cv_cabin =vectorizer.transform(X_cv['Cabin'])
 X_te_cabin =vectorizer.transform(test['Cabin'])
 X_tr_tkt =vectorizer.fit_transform(X_train['Ticket'])
 X_cv_tkt =vectorizer.transform(X_cv['Ticket'])
 X_te_tkt =vectorizer.transform(test['Ticket'])


 X_tr = hstack(( X_tr_age,X_tr_fare,X_tr_sex,X_tr_pclass,X_tr_emb)).tocsr()
X_cv = hstack(( X_cv_age,X_cv_fare,X_cv_sex,X_cv_pclass,X_cv_emb)).tocsr()
X_te = hstack(( X_te_age,X_te_fare,X_te_sex,X_te_pclass,X_te_emb)).tocsr()
print(X_tr.shape)
 print(X_te.shape)
 print(X_cv.shape )
submit_pred(d_dset.drop('latitude', axis = 1 ).drop('longitude', axis = 1),
d_test.drop('latitude', axis = 1 ).drop('longitude', axis = 1),
model = linear_model.LassoLars(alpha=.1),
name = 'linear_model_lasso' )<prepare_output>
max_depth=[1,5,10,15]
alpha=[1,3,7,15,24,31,60]
cv_log_error_array=[]
min_i=0
val=0
v=99999
for i in alpha:
clf = KNeighborsClassifier(n_neighbors=i,weights='distance')
 clf.fit(X_tr, y_train)
 sig_clf = CalibratedClassifierCV(clf, method="sigmoid")
 sig_clf.fit(X_tr, y_train)
 predict_y = sig_clf.predict_proba(X_cv)
 val=log_loss(y_cv, predict_y, labels=clf.classes_, eps=1e-15)
 if val<v:
min_i=i
v=val
cv_log_error_array.append(val)
 print('For values of alpha = ', i,"The log loss is:",log_loss(y_cv, predict_y, labels=clf.classes_, eps=1e-15))
clf = KNeighborsClassifier(n_neighbors=i,weights='distance')
 clf.fit(X_tr, y_train)
 sig_clf = CalibratedClassifierCV(clf, method="sigmoid")
 sig_clf.fit(X_tr, y_train)
 predict_y = sig_clf.predict_proba(X_tr)
 print('For values of best alpha = ',min_i, "The train log loss is:",log_loss(y_train, predict_y, labels=clf.classes_, eps=1e-15))
predict_y = sig_clf.predict_proba(X_cv)
 print('For values of best alpha = ',min_i, "The cross validation log loss is:",log_loss(y_cv, predict_y, labels=clf.classes_, eps=1e-15))
predict_y = sig_clf.predict_proba(X_te)
 print('For values of best alpha = ',min_i, "The test log loss is:",log_loss(y_test, predict_y, labels=clf.classes_, eps=1e-15))
knn=min_i
mean = []
stds = []
K = 1
M = 60
for i in range(K, M, 3):
knn = neighbors.KNeighborsRegressor(n_neighbors = i)
 score = cross_val_score(knn, d_dset.drop(target, axis = 1), d_dset[target], scoring = 'r2', cv = 10)
 mean.append(np.mean(score))
stds.append(np.std(score))
table = pd.DataFrame()
table['Mean'] = mean
table['Std'] = stds
table<compute_test_metric>
pred=sig_clf.predict(X_te)
 df=pd.DataFrame(zip(PassengerId,pred),columns=['PassengerId',"Survived"])
 df
df.to_csv('/kaggle/working/knn_output.csv',index=False)
submit_pred(d_dset,
d_test,
model = linear_model.BayesianRidge() ,
name = 'linear_model_bayes_gps' )<choose_model_class>
d=pd.read_csv('/kaggle/working/knn_output.csv')
 d