Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<compute_train_metric>
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.preprocessing import StandardScaler, RobustScaler
from sklearn.model_selection import StratifiedKFold, cross_validate
from sklearn.metrics import accuracy_score
from sklearn.linear_model import LogisticRegression
from sklearn.neighbors import KNeighborsClassifier
from sklearn.svm import SVC
from sklearn.ensemble import RandomForestClassifier, AdaBoostClassifier
from lightgbm import LGBMClassifier
from sklearn.neural_network import MLPClassifier
from hyperopt import hp, tpe, Trials, fmin, STATUS_OK, space_eval
random_forest = RandomForestClassifier()
random_forest.fit(X_tr, y_tr)
 prediction = random_forest.predict(X_vld)
 print('Random Forest2 : 총 {}명 중 {:.2f}% 정확도로 생존을 맞춤'.format(y_vld.shape[0], 100 * metrics.accuracy_score(prediction, y_vld)))
 acc_random_forest2 =100 * metrics.accuracy_score(prediction, y_vld )<compute_train_metric>
train = pd.read_csv('/kaggle/input/titanic/train.csv')
 train.head()
logreg = LogisticRegression()
logreg.fit(X_tr, y_tr)
 prediction = logreg.predict(X_vld)
 print('Logistic : 총 {}명 중 {:.2f}% 정확도로 생존을 맞춤'.format(y_vld.shape[0], 100 * metrics.accuracy_score(prediction, y_vld)))
 acc_log = 100 * metrics.accuracy_score(prediction, y_vld )<compute_train_metric>
test = pd.read_csv('/kaggle/input/titanic/test.csv')
 test.head()
svc = SVC()
svc.fit(X_tr, y_tr)
 prediction = svc.predict(X_vld)
 print(' Support Vector Machines: 총 {}명 중 {:.2f}% 정확도로 생존을 맞춤'.format(y_vld.shape[0], 100 * metrics.accuracy_score(prediction, y_vld)))
 acc_svc=100 * metrics.accuracy_score(prediction, y_vld )<compute_train_metric>
submission = pd.read_csv('/kaggle/input/titanic/gender_submission.csv' )
knn = KNeighborsClassifier(n_neighbors=26)
 knn.fit(X_tr, y_tr)
 prediction = knn.predict(X_vld)
 print(' k-Nearest Neighbor : 총 {}명 중 {:.2f}% 정확도로 생존을 맞춤'.format(y_vld.shape[0], 100 * metrics.accuracy_score(prediction, y_vld)))
 acc_knn=100 * metrics.accuracy_score(prediction, y_vld )<compute_train_metric>
print('---- train null ----
', train.isnull().sum())
 print('
---- test null ----
', test.isnull().sum() )
gaussian = GaussianNB()
gaussian.fit(X_tr, y_tr)
 prediction = gaussian.predict(X_vld)
 print('naive Bayes : 총 {}명 중 {:.2f}% 정확도로 생존을 맞춤'.format(y_vld.shape[0], 100 * metrics.accuracy_score(prediction, y_vld)))
 acc_gaussian=100 * metrics.accuracy_score(prediction, y_vld )<compute_train_metric>
sns.set_palette('rainbow')
 sns.set_style('darkgrid' )
perceptron = Perceptron()
perceptron.fit(X_tr, y_tr)
 prediction = perceptron.predict(X_vld)
 print('Perceptron : 총 {}명 중 {:.2f}% 정확도로 생존을 맞춤'.format(y_vld.shape[0], 100 * metrics.accuracy_score(prediction, y_vld)))
 acc_perceptron=100 * metrics.accuracy_score(prediction, y_vld )<compute_train_metric>
combine = pd.concat([train, test], sort=False )
linear_svc = LinearSVC()
linear_svc.fit(X_tr, y_tr)
 prediction = linear_svc.predict(X_vld)
 print('Linear SVC: 총 {}명 중 {:.2f}% 정확도로 생존을 맞춤'.format(y_vld.shape[0], 100 * metrics.accuracy_score(prediction, y_vld)))
 acc_linear_svc=100 * metrics.accuracy_score(prediction, y_vld )<compute_train_metric>
combine['Sex'].replace(['male', 'female'], [0, 1], inplace=True )
sgd = SGDClassifier()
sgd.fit(X_tr, y_tr)
 prediction = sgd.predict(X_vld)
 print('Stochastic Gradient Descent: 총 {}명 중 {:.2f}% 정확도로 생존을 맞춤'.format(y_vld.shape[0], 100 * metrics.accuracy_score(prediction, y_vld)))
 acc_sgd=100 * metrics.accuracy_score(prediction, y_vld )<compute_train_metric>
combine['Age'].fillna(combine['Age'].median() , inplace=True )
decision_tree = DecisionTreeClassifier()
decision_tree.fit(X_tr, y_tr)
 prediction = decision_tree.predict(X_vld)
 print('Decision Tree: 총 {}명 중 {:.2f}% 정확도로 생존을 맞춤'.format(y_vld.shape[0], 100 * metrics.accuracy_score(prediction, y_vld)))
 acc_decision_tree= 100 * metrics.accuracy_score(prediction, y_vld )<train_model>
standard = StandardScaler()
combine[['Age']] = standard.fit_transform(combine[['Age']] )
gbk = GradientBoostingClassifier()
gbk.fit(X_tr,y_tr)
 prediction = gbk.predict(X_vld)
 acc_gbk = round(accuracy_score(prediction,y_vld)*100,2)
 print(acc_gbk )<create_dataframe>
combine['Fare'].fillna(combine['Fare'].median() , inplace=True )
models = pd.DataFrame({
'Model': ['XGB','Support Vector Machines', 'KNN', 'Logistic Regression',
'Random Forest1','Random Forest2', 'Naive Bayes', 'Perceptron',
'Stochastic Gradient Decent', 'Linear SVC',
'Decision Tree'],
'Score': [acc_xgb, acc_svc, acc_knn, acc_log,
acc_random_forest1,acc_random_forest2, acc_gaussian, acc_perceptron,
acc_sgd, acc_linear_svc, acc_decision_tree]})
 print(models.sort_values(by='Score', ascending=False))<load_from_csv>
rscaler = RobustScaler(quantile_range=(25., 75.))
 combine[['Fare']] = rscaler.fit_transform(combine[['Fare']] )
submission = pd.read_csv('.. /input/sample_submission.csv' )<predict_on_test>
combine['Pclass'] =(combine['Pclass'] - 1)/ 2
prediction = logreg.predict(X_test)
 submission['Survived'] = prediction<save_to_csv>
combine['FamilySize'] = combine['SibSp'] + combine['Parch']
submission.to_csv('./my_first_submission.csv', index=False )<load_from_csv>
standard = StandardScaler()
combine[['FamilySize']] = standard.fit_transform(combine[['FamilySize']] )
df_train = pd.read_csv('/kaggle/input/1056lab-diabetes-diagnosis/train.csv', index_col=0)
 df_test = pd.read_csv('/kaggle/input/1056lab-diabetes-diagnosis/test.csv', index_col=0 )<categorify>
combine['Embarked'].fillna(combine['Embarked'].mode() [0], inplace=True )
df_train_dummies = pd.get_dummies(df_train, columns=['Gender'], drop_first=True)
 df_test_dummies = pd.get_dummies(df_test, columns=['Gender'], drop_first=True )<train_model>
combine = pd.get_dummies(combine, columns=['Embarked'], drop_first=True )
X_train_dummies = df_train_dummies.drop(columns='Diabetes' ).values
y_train_dummies = df_train_dummies['Diabetes'].values
X_train, X_valid, y_train, y_valid = train_test_split(X_train_dummies, y_train_dummies, test_size=0.2, random_state=0)
 rfc = RandomForestClassifier(random_state=0)
 rfc.fit(X_train, y_train )<compute_train_metric>
combine = pd.get_dummies(combine, columns=['Title'], drop_first=True )
y_pred = rfc.predict_proba(X_valid)[:,1]
fpr, tpr, thresholds = roc_curve(y_valid, y_pred)
 auc(fpr, tpr )<find_best_params>
tmp = pd.DataFrame()
tmp['Cabin'] = combine['Cabin']
tmp['Cabin'].loc[tmp['Cabin'].notnull() ] = 1
combine['Cabin'] = tmp['Cabin']
combine['Cabin'].replace(np.nan, 0, inplace=True )
def objective(trial):
criterion = trial.suggest_categorical('criterion', ['gini', 'entropy'])
 max_depth = trial.suggest_int('max_depth', 1, 30)
 n_estimators = trial.suggest_int('n_estimators',10,300)
 model = RandomForestClassifier(criterion=criterion, max_depth=max_depth, n_estimators=n_estimators, random_state=0,n_jobs=-1)
 model.fit(X_train, y_train)
 y_pred = model.predict_proba(X_valid)[:,1]
fpr, tpr, thresholds = roc_curve(y_valid, y_pred)
 return(auc(fpr, tpr))
study = optuna.create_study()
study.optimize(objective, n_trials=100)
 study.best_params<find_best_params>
combine_cleaned = combine.drop(['PassengerId', 'Name', 'SibSp', 'Parch', 'Ticket'], axis=1)
 combine_cleaned.head()
criterion=study.best_params['criterion']
max_depth=study.best_params['max_depth']
n_estimators=study.best_params['n_estimators']
model = RandomForestClassifier(criterion=criterion, max_depth=max_depth, n_estimators=n_estimators, random_state=0,n_jobs=-1)
 model.fit(X_train, y_train)
 y_pred = model.predict_proba(X_valid)[:,1]
fpr, tpr, thresholds = roc_curve(y_valid, y_pred)
 auc(fpr, tpr )<train_model>
def hyperopt_and_pred(models, max_evals=100):
preds = []
for model in models:
clf = model['classifier']
space = model['space']
def objective(space):
classifier = clf(**space)
 classifier.fit(X_train, y_train)
 skf = StratifiedKFold(n_splits=5, shuffle=True, random_state=0)
 acc = cross_validate(estimator=classifier, X=X_train, y=y_train, cv=skf)
 mean_acc = np.mean(acc['test_score'])
 return{'loss': 1 - mean_acc, 'status': STATUS_OK}
print('='*10, str(clf ).split('.')[-1].replace(''>', ''), '='*10)
 trials = Trials()
best = fmin(objective, space, algo=tpe.suggest, max_evals=max_evals, trials=trials, verbose=1)
 best_params = space_eval(space, best)
 predict = clf(**best_params ).fit(X_train, y_train ).predict(X_test)
 acc = 1 - trials.best_trial['result']['loss']
preds.append(predict)
 print('
', 'best parameters:', best_params)
 print('accuracy:', f'{acc:.04f}', '
')
 return preds
model.fit(X_train_dummies, y_train_dummies)
 X_test = df_test_dummies.values
y_pred = rfc.predict_proba(X_test)[:, 1]<save_to_csv>
models = [
{
'classifier': LogisticRegression,
'space': {
'C': hp.uniform('C', 0, 100),
'max_iter': hp.choice('max_iter', [2000]),
'solver': hp.choice('solver', ['lbfgs', 'liblinear', 'sag', 'saga'])
 }
},
{
'classifier': RandomForestClassifier,
'space': {
'n_estimators': hp.choice('n_estimators', np.arange(10, 401, 10)) ,
'max_depth': hp.uniform('max_depth', 1, 5),
'criterion': hp.choice('criterion', ['gini', 'entropy'])
 }
},
{
'classifier': KNeighborsClassifier,
'space': {
'n_neighbors': hp.choice('n_neighbors', np.arange(1, 15))
}
},
{
'classifier': AdaBoostClassifier,
'space': {
'n_estimators': hp.choice('n_estimators', [30,50,100,200,300]),
'learning_rate': hp.uniform('learning_rate', 0.8, 1.4)
 }
},
{
'classifier': SVC,
'space': {
'C': hp.uniform('C', 0, 2),
'gamma': hp.loguniform('gamma', -8, 2),
'kernel': hp.choice('kernel', ['rbf', 'poly', 'sigmoid'])
 }
},
{
'classifier': LGBMClassifier,
'space': {
'objective': hp.choice('objective', ['binary']),
'max_bin': hp.choice('max_bin', np.arange(64, 513, 1)) ,
'num_leaves': hp.choice('num_leaves', np.arange(30, 201, 10)) ,
'max_depth': hp.choice('max_depth', np.arange(3, 10, 1)) ,
'learning_rate': hp.uniform('learning_rate', 0.03, 0.2)
 }
},
{
'classifier': MLPClassifier,
'space': {
'hidden_layer_sizes': hp.choice('hidden_layer_sizes', [8, 16, 32,(8,8),(16,16)]),
'activation': hp.choice('activation', ['relu', 'tanh']),
'max_iter': hp.choice('max_iter', [3000])
 }
}
]
submit = pd.read_csv('/kaggle/input/1056lab-diabetes-diagnosis/sampleSubmission.csv')
 submit['Diabetes'] = y_pred
submit.to_csv('submission.csv', index=False )<load_from_csv>
predictions = hyperopt_and_pred(models )
train = pd.read_csv('.. /input/gl-hack-landmarks/train.csv')
 sample_submission = pd.read_csv('.. /input/gl-hack-landmarks/sample_submission.csv')
 coord_list = list(sample_submission.columns)[1:]
for c in coord_list:
sample_submission[c] = train[c].mean()
sample_submission.to_csv('submission.csv', index=False )<set_options>
ensembled_pred = np.round(sum(predictions)/ len(predictions)).astype('int' )
<load_from_csv><EOS> 
results = pd.Series(ensembled_pred, name='Survived')
 submission = pd.concat([submission['PassengerId'], results], axis=1)
 submission.to_csv('submission.csv', index=False )