Kaggle competition: Titanic - Machine Learning from Disaster
<set_options><EOS> 
import pandas as pd
import numpy as np
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<compute_test_metric>
import pandas as pd
import numpy as np
class OptimizedRounder(object):
def __init__(self):
self.coef_ = 0
def _kappa_loss(self, coef, X, y):
X_p = np.copy(X)
 for i, pred in enumerate(X_p):
if pred < coef[0]:
X_p[i] = 0
elif pred >= coef[0] and pred < coef[1]:
X_p[i] = 1
elif pred >= coef[1] and pred < coef[2]:
X_p[i] = 2
elif pred >= coef[2] and pred < coef[3]:
X_p[i] = 3
else:
X_p[i] = 4
ll = metrics.cohen_kappa_score(y, X_p, weights='quadratic')
 return -ll
def fit(self, X, y):
loss_partial = partial(self._kappa_loss, X=X, y=y)
 initial_coef = [0.5, 1.5, 2.5, 3.5]
self.coef_ = sp.optimize.minimize(loss_partial, initial_coef, method='nelder-mead')
 print(-loss_partial(self.coef_['x']))
def predict(self, X, coef):
X_p = np.copy(X)
 for i, pred in enumerate(X_p):
if pred < coef[0]:
X_p[i] = 0
elif pred >= coef[0] and pred < coef[1]:
X_p[i] = 1
elif pred >= coef[1] and pred < coef[2]:
X_p[i] = 2
elif pred >= coef[2] and pred < coef[3]:
X_p[i] = 3
else:
X_p[i] = 4
return X_p
def coefficients(self):
return self.coef_['x']
def run_subm(learn=learn, coefficients=[0.5, 1.5, 2.5, 3.5]):
opt = OptimizedRounder()
preds,y = learn.TTA(scale=1,ds_type=DatasetType.Test)
 tst_pred = opt.predict(preds, coefficients)
 test_df.diagnosis = tst_pred.astype(int)
 test_df.to_csv('submission.csv',index=False)
 return test_df 
<prepare_output>
test = pd.read_csv('.. /input/titanic/test.csv')
 train = pd.read_csv('.. /input/titanic/train.csv' )
learn.data = get_data(400 )<load_pretrained>
testPassengerIds = test['PassengerId']
testPassengerIds.head()
learn.load('ef5_b_400_1');<feature_engineering>
train.drop(['PassengerId', 'Name', 'Ticket'], inplace = True, axis = 1)
 test.drop(['PassengerId', 'Name', 'Ticket'], inplace = True, axis = 1 )
%%time
preds1 = learn.TTA(beta =.2, scale=1,ds_type=DatasetType.Test )<define_search_space>
train.isnull().any()
v1 = torch.tensor([1.0,1.0,.7,.7,1.0] )<load_pretrained>
test.isnull().any()
learn.load('ef5_b_400_2');<set_options>
train['Cabin'].fillna(0, inplace = True)
 def getCabin(value):
val_dict = {
'A' : 6,
'B' : 5,
'C' : 4,
'D' : 3,
'E' : 2,
'F' : 1,
'T' : 1
}
return val_dict.get(str(value)[0], 0)
 train['Cabin'] = train["Cabin"].apply(getCabin)
 test['Cabin'] = test['Cabin'].apply(getCabin )
gc.collect()<feature_engineering>
train['Embarked'].fillna(train['Embarked'].mode().item() , inplace = True)
 test['Embarked'].fillna(train['Embarked'].mode().item() , inplace = True )
%%time
preds2 = learn.TTA(beta =.2, scale=1,ds_type=DatasetType.Test )<define_search_space>
train = pd.get_dummies(train, drop_first=True)
 test = pd.get_dummies(test, drop_first=True )
v2 = torch.tensor([1.0,.7,1.0,.7,1.0] )<prepare_output>
train.isnull().any() | test.isnull().any()
learn.data = get_data(400 )<set_options>
def correctedLog(value):
return np.log(1 + value)
 train['Status'] = train['Pclass'] + train['Fare'].apply(correctedLog)
 test['Status'] = test['Pclass'] + test['Fare'].apply(correctedLog )
gc.collect()<set_options>
train['RootAgeTimesClass'] = train['Age'].apply(np.sqrt)* train['Pclass']
test['RootAgeTimesClass'] = test['Age'].apply(np.sqrt)* test['Pclass']
learn.load('ef5_b15_400_1');
gc.collect()<feature_engineering>
train['FamilySize'] = train['SibSp'] + train['Parch'] + 1
test['FamilySize'] = train['SibSp'] + train['Parch'] + 1
%%time
preds3 = learn.TTA(beta =.2, scale=1,ds_type=DatasetType.Test )<define_search_space>
train['Young'] = train['Age'] <= train['Age'].mean()
test['Young'] = test["Age"] <= train['Age'].mean()
v3 = torch.tensor([1.0,1.0,.5,1.0,1.0] )<prepare_x_and_y>
train['YoungMale'] = train['Young'] & train['Sex_male']
test['YoungMale'] = test["Young"] & test['Sex_male']
learn.data = get_data(320)
<load_pretrained>
X = train.iloc[:, 1: ]
y = train.iloc[:, 0]
y.shape, X.shape
learn.load('ef5_b15_320_1');<set_options>
train['Fare'], maxlog = boxcox(train['Fare'] + 1)
 test['Fare'] = boxcox(test['Fare'] + 1, lmbda = maxlog )
gc.collect()<feature_engineering>
from sklearn.model_selection import train_test_split
from sklearn.metrics import f1_score
%%time
preds4 = learn.TTA(beta =.2, scale=1,ds_type=DatasetType.Test )<define_search_space>
X_train, X_val, y_train, y_val = train_test_split(X, y, random_state =55, test_size = 0.2, shuffle = True)
 y_train.shape, y_val.shape
v4 = torch.tensor([1.0,1.0,.5,1.0,1.0] )<find_best_params>
np.random.seed(0 )
image_type(3)
 learn.data = get_data(400 )<set_options>
paramDict = {
'n_estimators' : [5, 10, 25, 50, 75, 100, 200, 500],
'max_depth' : [4, 8, 10, 15, 20, 50],
}
model = RandomForestClassifier(n_jobs = 8)
 clf = GridSearchCV(estimator=model, param_grid=paramDict, n_jobs=10 )
learn.load('ef5_ah_400_1')
 gc.collect()<feature_engineering>
clf.fit(X_train, y_train )
%%time
preds5 = learn.TTA(beta =.2, scale=1,ds_type=DatasetType.Test )<define_search_space>
clf.best_params_, clf.best_score_
v5 = torch.tensor([1.0,.7,.7,1.0,.7] )<set_options>
f1_score(clf.predict(X_val), y_val )
gc.collect()<set_options>
finalModel = RandomForestClassifier(**clf.best_params_)
 finalModel.fit(X, y)
 y_preds = finalModel.predict(test)
 file_name = "Submission_16_08_6.csv"
y_pred_series = pd.Series(y_preds.flatten() , name = 'Survived')
 file = pd.concat([testPassengerIds, y_pred_series], axis = 1)
 file.to_csv(file_name, index = False);