Kaggle competition: Titanic - Machine Learning from Disaster
print('Logistic Regression
')
 scores = cross_val_score(lr, train_X, y_mind, cv=5, scoring='roc_auc')
 print("AUC(Mind): %0.2f(+/- %0.2f)" %(scores.mean() , scores.std() * 2))
scores = cross_val_score(lr, train_X, y_energy, cv=5, scoring='roc_auc')
 print("AUC(Energy): %0.2f(+/- %0.2f)" %(scores.mean() , scores.std() * 2))
scores = cross_val_score(lr, train_X, y_nature, cv=5, scoring='roc_auc')
 print("AUC(Nature): %0.2f(+/- %0.2f)" %(scores.mean() , scores.std() * 2))
scores = cross_val_score(lr, train_X, y_tactics, cv=5, scoring='roc_auc')
 print("AUC(Tactics): %0.2f(+/- %0.2f)" %(scores.mean() , scores.std() * 2))
print('-'*30)
 print('Random Forest
')
 scores = cross_val_score(rf, train_X, y_mind, cv=5, scoring='roc_auc')
 print("AUC(Mind): %0.2f(+/- %0.2f)" %(scores.mean() , scores.std() * 2))
scores = cross_val_score(rf, train_X, y_energy, cv=5, scoring='roc_auc')
 print("AUC(Energy): %0.2f(+/- %0.2f)" %(scores.mean() , scores.std() * 2))
scores = cross_val_score(rf, train_X, y_nature, cv=5, scoring='roc_auc')
 print("AUC(Nature): %0.2f(+/- %0.2f)" %(scores.mean() , scores.std() * 2))
scores = cross_val_score(rf, train_X, y_tactics, cv=5, scoring='roc_auc')
 print("AUC(Tactics): %0.2f(+/- %0.2f)" %(scores.mean() , scores.std() * 2))
print('-'*30)
 print('AdaBoost
')
 scores = cross_val_score(adb, train_X, y_mind, cv=5, scoring='roc_auc')
 print("AUC(Mind): %0.2f(+/- %0.2f)" %(scores.mean() , scores.std() * 2))
scores = cross_val_score(adb, train_X, y_energy, cv=5, scoring='roc_auc')
 print("AUC(Energy): %0.2f(+/- %0.2f)" %(scores.mean() , scores.std() * 2))
scores = cross_val_score(adb, train_X, y_nature, cv=5, scoring='roc_auc')
 print("AUC(Nature): %0.2f(+/- %0.2f)" %(scores.mean() , scores.std() * 2))
scores = cross_val_score(adb, train_X, y_tactics, cv=5, scoring='roc_auc')
 print("AUC(Tactics): %0.2f(+/- %0.2f)" %(scores.mean() , scores.std() * 2))
print('-'*30)
 print('KNN
')
 scores = cross_val_score(knn, train_X, y_mind, cv=5, scoring='roc_auc')
 print("AUC(Mind): %0.2f(+/- %0.2f)" %(scores.mean() , scores.std() * 2))
scores = cross_val_score(knn, train_X, y_energy, cv=5, scoring='roc_auc')
 print("AUC(Energy): %0.2f(+/- %0.2f)" %(scores.mean() , scores.std() * 2))
scores = cross_val_score(knn, train_X, y_nature, cv=5, scoring='roc_auc')
 print("AUC(Nature): %0.2f(+/- %0.2f)" %(scores.mean() , scores.std() * 2))
scores = cross_val_score(knn, train_X, y_tactics, cv=5, scoring='roc_auc')
 print("AUC(Tactics): %0.2f(+/- %0.2f)" %(scores.mean() , scores.std() * 2))<split>
train_data = pd.read_csv("/kaggle/input/titanic/train.csv")
 train_data.head()
test_X = X[len_train:]<choose_model_class>
test_data = pd.read_csv("/kaggle/input/titanic/test.csv")
 test_data.head()
lr_mind = LogisticRegression(n_jobs=-1)
 lr_energy = LogisticRegression(n_jobs=-1)
 lr_nature = LogisticRegression(n_jobs=-1)
 lr_tactics = LogisticRegression(n_jobs=-1 )<train_model>
women = train_data.loc[train_data.Sex == 'female']["Survived"]
rate_women = sum(women)/len(women)
 print("% of women who survived:", rate_women )
lr_mind.fit(train_X,y_mind)
 lr_energy.fit(train_X,y_energy)
 lr_nature.fit(train_X,y_nature)
 lr_tactics.fit(train_X,y_tactics )<predict_on_test>
men = train_data.loc[train_data.Sex == 'male']["Survived"]
rate_men = sum(men)/len(men)
 print("% of men who survived:", rate_men )
submission = pd.DataFrame({'id': df_test.id.values, 'mind': lr_mind.predict(test_X), 'energy': lr_energy.predict(test_X), 'nature': lr_nature.predict(test_X), 'tactics': lr_tactics.predict(test_X)} )<predict_on_test>
for column in train_data.columns:
print("%-15s%3d" %(column, pd.isnull(train_data[column] ).sum()))
mind_prob = lr_mind.predict_proba(test_X)
 mind_t = mind_prob[:,0].mean()
energy_prob = lr_energy.predict_proba(test_X)
 energy_t = energy_prob[:,0].mean()
nature_prob = lr_nature.predict_proba(test_X)
 nature_t = nature_prob[:,0].mean()
tactics_prob = lr_tactics.predict_proba(test_X)
 tactics_t = tactics_prob[:,0].mean()
mind_threshold = np.percentile(mind_prob[:,1], mind_t*100,)
 energy_threshold = np.percentile(energy_prob[:,1], energy_t*100,)
 nature_threshold = np.percentile(nature_prob[:,1], nature_t*100,)
 tactics_threshold = np.percentile(tactics_prob[:,1], tactics_t*100,)
 mind_pred_threshold =(mind_prob[:,1] > mind_threshold ).astype(int)
 energy_pred_threshold =(energy_prob[:,1] > energy_threshold ).astype(int)
 nature_pred_threshold =(nature_prob[:,1] > nature_threshold ).astype(int)
 tactics_pred_threshold =(tactics_prob[:,1] > tactics_threshold ).astype(int )<create_dataframe>
for column in test_data.columns:
print("%-15s%3d" %(column, pd.isnull(test_data[column] ).sum()))
submission = pd.DataFrame({'id': df_test.id.values, 'mind': mind_pred_threshold, 'energy': energy_pred_threshold, 'nature': nature_pred_threshold, 'tactics': tactics_pred_threshold} )<save_to_csv>
print(train_data.drop_duplicates().groupby('Embarked')['PassengerId'].count() )
submission.to_csv('submission.csv', index=False )<import_modules>
train_data['Embarked'] = train_data['Embarked'].fillna("S" )
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from wordcloud import WordCloud
import re
import string
from nltk import word_tokenize
from nltk.stem import WordNetLemmatizer
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.decomposition import TruncatedSVD<set_options>
test_data['Fare'] = test_data['Fare'].fillna(0 )
warnings.filterwarnings("ignore" )<load_from_csv>
train_data['TicketLength'] = train_data['Ticket'].apply(lambda x: len(x))
train = pd.read_csv(".. /input/train.csv")
 test = pd.read_csv(".. /input/test.csv" )<load_from_csv>
test_data['TicketLength'] = test_data['Ticket'].apply(lambda x: len(x))
train = pd.read_csv(".. /input/train.csv")
 test = pd.read_csv(".. /input/test.csv" )<concatenate>
y = train_data["Survived"]
features = ["Pclass", "Sex", "SibSp", "Parch", "Fare", "Embarked", "TicketLength"]
X = pd.get_dummies(train_data[features])
 X_test = pd.get_dummies(test_data[features])
 model = RandomForestClassifier(n_estimators=100, max_depth=5, random_state=1)
 model.fit(X, y)
 predictions = model.predict(X_test)
 output = pd.DataFrame({'PassengerId': test_data.PassengerId, 'Survived': predictions})
 output.to_csv('submission.csv', index=False)
 print("Your submission was successfully saved!" )