Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<split>
warnings.filterwarnings('ignore')
train, valid = train_test_split(train_df, stratify=train_df.label, test_size=0.15 )<load_pretrained>
train = pd.read_csv(".. /input/titanic/train.csv")
 test = pd.read_csv(".. /input/titanic/test.csv" )
train_tfms, val_tfms = get_transforms("image", 224, 0.5)
 train_dk = DataKek(df=train, reader_fn=reader_train_fn, transforms=train_tfms)
 val_dk = DataKek(df=valid, reader_fn=reader_train_fn, transforms=val_tfms)
 test_dk = DataKek(df=test_df, reader_fn=reader_test_fn, transforms=val_tfms)
 batch_size = 2
workers = 8
train_dl = DataLoader(train_dk, batch_size=batch_size, num_workers=workers, shuffle=True, drop_last=True)
 val_dl = DataLoader(val_dk, batch_size=batch_size, num_workers=workers, shuffle=False)
 test_dl = DataLoader(test_dk, batch_size=30, num_workers=workers, shuffle=False )<compute_train_metric>
def detect_outliers(dataframe, n, features):
outliers_indices = []
for feature in features:
Quart1 = dataframe[feature].quantile(0.25)
 Quart3 = dataframe[feature].quantile(0.75)
 upper_outlier_threshold = Quart3 +(Quart3 - Quart1)* 1.5
lower_outlier_threshold = Quart1 -(Quart3 - Quart1)* 1.5
feature_outliers_list = dataframe[(dataframe[feature] > upper_outlier_threshold)|(dataframe[feature] < lower_outlier_threshold)].index
outliers_indices.extend(feature_outliers_list)
 return list(a for a, b in Counter(outliers_indices ).items() if b > n )
dataowner = DataOwner(train_dl, val_dl, None)
 BCEmodel = Net(num_classes=1)
 CEmodel = Net(num_classes=2)
 CEcriterion = nn.CrossEntropyLoss()
BCEcriterion = nn.BCEWithLogitsLoss()<compute_train_metric>
outliers = detect_outliers(train, 2, ["Age", "SibSp", "Fare", "Parch"])
 train.loc[outliers]
model = Net(num_classes=1, last_conv_size=1000)
 def bce_accuracy(target: torch.Tensor,
preds: torch.Tensor)-> float:
thresh = 0.5
target = target.cpu().detach().numpy()
preds =(torch.sigmoid(preds ).cpu().detach().numpy() > thresh ).astype(int)
 return accuracy_score(target, preds)
 BCE_keker = Keker(model=model,
dataowner=dataowner,
criterion=BCEcriterion,
step_fn=step_fn,
target_key="label",
metrics={"accuracy": bce_accuracy},
opt=adabound.AdaBound,
opt_params={'final_lr':0.001} )<define_variables>
train = train.drop(outliers, axis = 0 ).reset_index(drop = True )
logdir = 'logs'
lrlogdir = 'lrlogs' 
<set_options>
def fill_age_missing_values(df):
Age_Nan_Indices = list(df[df["Age"].isnull() ].index)
 for index in Age_Nan_Indices:
temp_Pclass = df.iloc[index]["Pclass"]
temp_SibSp = df.iloc[index]["SibSp"]
temp_Parch = df.iloc[index]["Parch"]
age_median = df["Age"][(( df["Pclass"] == temp_Pclass)&(df["SibSp"] == temp_SibSp)&(df["Parch"] == temp_Parch)) ].median()
if df.iloc[index]["Age"]:
df["Age"].iloc[index] = age_median
if np.isnan(age_median):
df["Age"].iloc[index] = df["Age"].median()
return df
BCE_keker.freeze(model_attr='net')
 BCE_keker.kek(lr=0.0001,
epochs=50,
cp_saver_params={
"savedir": "bce_sueeze",
"n_best": 3,
"prefix": "bce_squeeze",
"mode": "min"},
logdir=logdir )<predict_on_test>
df["Age"].isnull().sum()
BCE_keker.load('bce_sueeze/bce_squeeze.best.h5')
 preds = BCE_keker.predict_loader(test_dl)
 test_df['label'] =(torch.sigmoid(torch.Tensor(preds)) > 0.5 ).numpy()<feature_engineering>
df["Title"] = pd.Series([name.split(",")[1].split(".")[0].strip() for name in df["Name"]])
 df.head()
test_df['id'] = test_df['id'].apply(lambda x: x.split('.')[0])
 test_df['label'] = test_df['label'].apply(lambda x: 'cleaned' if x==1 else 'dirty' )<save_to_csv>
df["Title"] = df["Title"].replace(['Lady', 'the Countess', 'Countess', 'Don', 'Jonkheer', 'Dona', 'Sir'], 'Royals')
 df["Title"] = df["Title"].replace(['Col', 'Dr', 'Major', 'Capt'], 'Professionals')
 df["Title"] = df["Title"].replace(["Ms", "Mme", "Mlle", "Mrs"], 'Miss')
 df["Title"] = df["Title"].replace(['Master', 'Rev'], 'Mas/Rev')
 df["Title"] = df["Title"].map({"Mas/Rev": 0, "Miss": 1, "Mr": 2, "Royals": 3, "Professionals": 4} )
test_df.to_csv('sub.csv', index=False )<import_modules>
df["Ftotal"] = 1 + df["SibSp"] + df["Parch"]
import numpy as np
import pandas as pd
import math
import xgboost as xgb
from xgboost import XGBClassifier
from sklearn import preprocessing
from sklearn.model_selection import KFold
from sklearn.metrics import accuracy_score<load_from_csv>
df["Age"] = df["Age"].astype(int)
 df.loc[(df['Age'] <= 2), 'Age Group'] = 'Baby'
df.loc[(( df["Age"] > 2)&(df['Age'] <= 10)) , 'Age Group'] = 'Child'
df.loc[(( df["Age"] > 10)&(df['Age'] <= 19)) , 'Age Group'] = 'Young Adult'
df.loc[(( df["Age"] > 19)&(df['Age'] <= 60)) , 'Age Group'] = 'Adult'
df.loc[(df["Age"] > 60), 'Age Group'] = 'Senior'
df["Age Group"] = df["Age Group"].map({"Baby": 0, "Child": 1, "Young Adult": 2, "Adult": 3, "Senior": 4} )
df_train = pd.read_csv('.. /input/train.csv')
 df_test = pd.read_csv('.. /input/test.csv' )<define_variables>
df["Sex"] = df["Sex"].map({"male": 0, "female": 1})
 df.sample(5 )
labels = df_train['diabetes']<drop_column>
df["Embarked"] = df["Embarked"].map({"C": 0, "Q": 1, "S": 2})
 df.sample(5 )
df_train.drop(columns=['p_id', 'diabetes'], axis=1, inplace=True)
 df_test.drop(columns=['p_id'], axis=1, inplace=True )<normalization>
passenger_ID = pd.Series(df["PassengerId"], name = "PassengerId")
 df = df.drop(["Name", "PassengerId", "SibSp", "Parch", "Age", "Ticket"], axis=1)
 df.sample(5 )
min_max_scaler = preprocessing.MinMaxScaler()<normalization>
df = pd.get_dummies(df, columns = ["Title"])
 df = pd.get_dummies(df, columns = ["Embarked"])
 df = pd.get_dummies(df, columns = ["Pclass"])
 df = pd.get_dummies(df, columns = ["Age Group"] )
scaler = min_max_scaler.fit(df_train)
 X_train_scaled = min_max_scaler.transform(df_train)
 train = pd.DataFrame(X_train_scaled, columns=df_train.columns)
 X_test_scaled = min_max_scaler.transform(df_test)
 test = pd.DataFrame(X_test_scaled, columns=df_train.columns )<compute_train_metric>
train = df[:train.shape[0]]
test = df[train.shape[0]:].drop(["Survived"], axis = 1 )
def xgb_CV(X, y, X_test, folds, params):
prediction = np.zeros(len(X_test))
scores = []
for fold_n,(train_index, valid_index)in enumerate(folds.split(X)) :
X_train, X_valid = X.iloc[train_index], X.iloc[valid_index]
y_train, y_valid = y.iloc[train_index], y.iloc[valid_index]
train_data = xgb.DMatrix(data=X_train, label=y_train, feature_names=X.columns)
 valid_data = xgb.DMatrix(data=X_valid, label=y_valid, feature_names=X.columns)
 watchlist = [(train_data, 'train'),(valid_data, 'valid_data')]
model = xgb.train(dtrain=train_data, num_boost_round=20000, evals=watchlist, \
early_stopping_rounds=200, verbose_eval=150, params=params)
 y_pred_valid = model.predict(xgb.DMatrix(X_valid, feature_names=X.columns), ntree_limit=model.best_ntree_limit)
 y_pred = model.predict(xgb.DMatrix(X_test, feature_names=X.columns), ntree_limit=model.best_ntree_limit)
 scores.append(accuracy_score(y_valid, y_pred_valid))
prediction += y_pred
print('CV mean score: {0:.4f}, std: {1:.4f}.'.format(np.mean(scores), np.std(scores)))
 prediction /= n_fold
return prediction<compute_train_metric>
k_fold = StratifiedKFold(n_splits=5)
 X_train = train.drop(labels="Survived", axis=1)
 y_train = train["Survived"]
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
 LG_classifier = LogisticRegression(random_state=0)
 SVC_classifier = SVC(kernel="rbf", random_state=0)
 KNN_classifier = KNeighborsClassifier()
NB_classifier = GaussianNB()
DT_classifier = DecisionTreeClassifier(criterion="entropy", random_state=0)
 RF_classifier = RandomForestClassifier(n_estimators=200, criterion="entropy", random_state=0)
 titanic_classifiers = [LG_classifier, SVC_classifier, KNN_classifier, NB_classifier, DT_classifier, RF_classifier]
classifier_dict = {
0: "Logistic Regression",
1: "Support Vector Classfication",
2: "K Nearest Neighbor Classification",
3: "Naive bayes Classifier",
4: "Decision Trees Classifier",
5: "Random Forest Classifier",
}
titanic_results = pd.DataFrame({'Model': [],'Mean Accuracy': [], "Standard Deviation": []})
 for i, classifier in enumerate(titanic_classifiers):
classifier_scores = cross_val_score(classifier, X_train, y_train, cv=k_fold, n_jobs=2, scoring="accuracy")
 titanic_results = titanic_results.append(pd.DataFrame({"Model":[classifier_dict[i]],
"Mean Accuracy": [classifier_scores.mean() ],
"Standard Deviation": [classifier_scores.std() ]}))
params = {'eta': 0.01,
'max_depth': 4,
'subsample': 0.05,
'colsample_bytree': 0.05,
'min_child_weight' : 1,
'objective': 'binary:hinge',
'eval_metric': 'error',
'silent': True,
'nthread': 4}
n_fold = 10
folds = KFold(n_splits=n_fold, shuffle=True, random_state=11)
 predictions = xgb_CV(train, labels, test, folds, params )<prepare_output>
print(titanic_results.to_string(index=False))
preds = np.where(predictions > 0.5, 1, 0)
 preds<save_to_csv>
RF_classifier = RandomForestClassifier()
RF_paramgrid = {"max_depth": [None],
"max_features": [1, 3, 10],
"min_samples_split": [2, 3, 10],
"min_samples_leaf": [1, 3, 10],
"bootstrap": [False],
"n_estimators" :[100,200,300],
"criterion": ["entropy"]}
RF_classifiergrid = GridSearchCV(RF_classifier, param_grid = RF_paramgrid, cv=k_fold, scoring="accuracy", n_jobs= -1, verbose=1)
 RF_classifiergrid.fit(X_train,y_train)
 RFC_optimum = RF_classifiergrid.best_estimator_
RF_classifiergrid.best_score_
df_submission = pd.read_csv('.. /input/sample_submission.csv')
 df_submission['diabetes'] = preds
df_submission.to_csv('my_submission.csv', index=False )<compute_train_metric>
IDtest = passenger_ID[train.shape[0]:].reset_index(drop = True )
def get_metrics(x, y, e):
z = e.predict(x)
 print(classification_report(z, y, digits=4))<set_options>
X_train = train.drop(labels="Survived", axis=1)
 y_train = train["Survived"]
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
 X_test = sc.fit_transform(test)
 RFC_optimum.fit(X_train, y_train)
 test_predictions = pd.Series(RFC_optimum.predict(X_test ).astype(int), name="Survived")
 titanic_results = pd.concat([IDtest, test_predictions], axis = 1)
 titanic_results.to_csv('submission.csv', index=False )