Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<train_model>
traindf = pd.read_csv('.. /input/titanic/train.csv' ).set_index('PassengerId')
 testdf = pd.read_csv('.. /input/titanic/test.csv' ).set_index('PassengerId')
 df = pd.concat([traindf, testdf], axis=0, sort=False)
 df['Title'] = df.Name.str.split(',' ).str[1].str.split('.' ).str[0].str.strip()
df['IsWomanOrBoy'] =(( df.Title == 'Master')|(df.Sex == 'female'))
df['LastName'] = df.Name.str.split(',' ).str[0]
family = df.groupby(df.LastName ).Survived
df['WomanOrBoyCount'] = family.transform(lambda s: s[df.IsWomanOrBoy].fillna(0 ).count())
 df['WomanOrBoyCount'] = df.mask(df.IsWomanOrBoy, df.WomanOrBoyCount - 1, axis=0)
 df['FamilySurvivedCount'] = family.transform(lambda s: s[df.IsWomanOrBoy].fillna(0 ).sum())
 df['FamilySurvivedCount'] = df.mask(df.IsWomanOrBoy, df.FamilySurvivedCount - df.Survived.fillna(0), axis=0)
 df['WomanOrBoySurvived'] = df.FamilySurvivedCount / df.WomanOrBoyCount.replace(0, np.nan)
 df['Alone'] =(df.WomanOrBoyCount == 0)
 train_y = df.Survived.loc[traindf.index]
df = pd.concat([df.WomanOrBoySurvived.fillna(0), df.Alone, df.Sex.replace({'male': 0, 'female': 1})], axis=1)
 test_x = df.loc[testdf.index]
rf = RandomForestRegressor(
 n_estimators = 1,
bootstrap = False,
n_jobs = -1,
random_state = 42 
)
 rf.fit(X_train, y_train)
 print_rf_score(rf )<train_model>
test_x['Survived'] =(((test_x.WomanOrBoySurvived <= 0.238)&(test_x.Sex > 0.5)&(test_x.Alone > 0.5)) | \ 
(( test_x.WomanOrBoySurvived > 0.238)& \
~(( test_x.WomanOrBoySurvived > 0.55)&(test_x.WomanOrBoySurvived <= 0.633))))
rf = RandomForestRegressor(n_estimators = 10, n_jobs = -1, random_state = 42)
 rf.fit(X_train, y_train)
 print_rf_score(rf )<predict_on_test>
pd.DataFrame({'Survived': test_x['Survived'].astype(int)}, \
index=testdf.index ).reset_index().to_csv('survived.csv', index=False )
tree_preds = np.stack([tree.predict(X_valid)for tree in rf.estimators_])
 print(f' Individual Tree Predictions: {[np.around(tree_preds[:,0], 1)]}')
 print(f' Mean of Tree Predictions: {np.mean(tree_preds[:,0])}')
 print(f' Ground truth for sample: {y_valid[0]}' )<find_best_params>
warnings.filterwarnings("ignore" )
%%time
train_scores_r2 = []
valid_scores_r2 = []
train_scores_rmse = []
valid_scores_rmse = []
for trees in tqdm_notebook(range(1, 100)) :
rf = RandomForestRegressor(n_estimators = trees, n_jobs = -1, random_state = 42)
 rf.fit(X_train, y_train)
 train_scores_r2.append(rf.score(X_train, y_train))
valid_scores_r2.append(rf.score(X_valid, y_valid))
train_scores_rmse.append(rmse(rf.predict(X_train), y_train))
valid_scores_rmse.append(rmse(rf.predict(X_valid), y_valid)) 
<train_model>
traindf = pd.read_csv('.. /input/titanic/train.csv' ).set_index('PassengerId')
 testdf = pd.read_csv('.. /input/titanic/test.csv' ).set_index('PassengerId')
 df = pd.concat([traindf, testdf], axis=0, sort=False )
rf = RandomForestRegressor(
 n_estimators = 20,
n_jobs = -1,
oob_score = True,
random_state = 42 
)
 rf.fit(X_train, y_train)
 print_rf_score(rf)
 print(f'OOB Score: {rf.oob_score_}' )<train_model>
df['Title'] = df.Name.str.split(',' ).str[1].str.split('.' ).str[0].str.strip()
df['IsWomanOrBoy'] =(( df.Title == 'Master')|(df.Sex == 'female'))
df['LastName'] = df.Name.str.split(',' ).str[0]
family = df.groupby(df.LastName ).Survived
df['WomanOrBoyCount'] = family.transform(lambda s: s[df.IsWomanOrBoy].fillna(0 ).count())
 df['WomanOrBoyCount'] = df.mask(df.IsWomanOrBoy, df.WomanOrBoyCount - 1, axis=0)
 df['FamilySurvivedCount'] = family.transform(lambda s: s[df.IsWomanOrBoy].fillna(0 ).sum())
 df['FamilySurvivedCount'] = df.mask(df.IsWomanOrBoy, df.FamilySurvivedCount - df.Survived.fillna(0), axis=0)
 df['WomanOrBoySurvived'] = df.FamilySurvivedCount / df.WomanOrBoyCount.replace(0, np.nan)
 df['Alone'] =(df.WomanOrBoyCount == 0 )
rf = RandomForestRegressor(
 n_estimators = 20,
min_samples_leaf = 4,
n_jobs = -1,
oob_score = True,
random_state = 42 
)
 rf.fit(X_train, y_train)
 print_rf_score(rf)
 print(f'OOB Score: {rf.oob_score_}' )<train_model>
train_y = df.Survived.loc[traindf.index]
data = pd.concat([df.WomanOrBoySurvived.fillna(0), df.Alone, \
df.Sex.replace({'male': 0, 'female': 1})], axis=1)
 train_x, test_x = data.loc[traindf.index], data.loc[testdf.index]
train_x.head(5 )
rf = RandomForestRegressor(
 n_estimators = 20,
min_samples_leaf = 4,
max_features = 0.3,
n_jobs = -1,
oob_score = True,
random_state = 42 
)
 rf.fit(X_train, y_train)
 print_rf_score(rf)
 print(f'OOB Score: {rf.oob_score_}' )<train_model>
parameters = {'max_depth' : np.arange(2, 5, dtype=int),
'min_samples_leaf' : np.arange(2, 5, dtype=int)}
classifier = DecisionTreeClassifier(random_state=1000)
 model = GridSearchCV(estimator=classifier, param_grid=parameters, scoring='accuracy', cv=2, n_jobs=-1)
 model.fit(train_x, train_y)
 best_parameters = model.best_params_
print(best_parameters )
rf = RandomForestRegressor(
 n_estimators = 20,
min_samples_leaf = 4,
max_features = 0.3,
n_jobs = -1,
oob_score = True,
random_state = 42, 
)
 rf.fit(X_train, y_train)
 print_rf_score(rf)
 print(f'OOB Score: {rf.oob_score_}' )<save_to_csv>
model=DecisionTreeClassifier(max_depth = best_parameters['max_depth'],
random_state = 1000)
 model.fit(train_x, train_y )
predictions = np.expm1(rf.predict(rf_test))
submission['revenue'] = np.round(predictions)
 submission.to_csv('submission_simple_rf.csv', index = False )<train_model>
dot_data = export_graphviz(model, out_file=None, feature_names=train_x.columns, class_names=['0', '1'],
filled=True, rounded=False,special_characters=True, precision=3)
 graph = graphviz.Source(dot_data)
 graph
def xgtrain(X_train, X_valid, y_train, y_valid):
regressor = XGBRegressor(
 n_estimators = 50000,
learning_rate = 0.001,
max_depth = 6,
subsample = 0.3,
colsample_bytree = 0.2 
)
 regressor_ = regressor.fit(
 X_train.values, y_train.values,
eval_metric = 'rmse',
eval_set = [ 
(X_train.values, y_train.values), 
(X_valid.values, y_valid.values)
 ],
verbose = 1000,
early_stopping_rounds = 500, 
)
 return regressor_<train_model>
y_pred = model.predict(test_x ).astype(int)
 print('Mean =', y_pred.mean() , ' Std =', y_pred.std() )
%%time
regressor_ = xgtrain(X_train, X_valid, y_train, y_valid )<train_model>
test_x['Survived'] =(((test_x.WomanOrBoySurvived <= 0.238)&(test_x.Sex > 0.5)&(test_x.Alone > 0.5)) | \ 
(( test_x.WomanOrBoySurvived > 0.238)& \
~(( test_x.WomanOrBoySurvived > 0.55)&(test_x.WomanOrBoySurvived <= 0.633))))
y_pred = test_x['Survived'].astype(int)
 print('Mean =', y_pred.mean() , ' Std =', y_pred.std() )
def lgbtrain(X_train, y_train, X_valid, y_valid):
lgb_train = lgb.Dataset(X_train, y_train)
 lgb_eval = lgb.Dataset(X_valid, y_valid, reference=lgb_train)
 params = {
'boosting_type': 'gbdt',
'objective': 'regression',
'metric': {'l2_root'},
'max_depth': 4,
'learning_rate': 0.001,
'feature_fraction': 0.3,
'bagging_fraction': 1,
'bagging_freq': 1,
}
gbm = lgb.train(
 params,
lgb_train,
valid_sets = lgb_eval,
num_boost_round=50000,
early_stopping_rounds=500,
verbose_eval = 1000 
)
 return gbm<train_model>
pd.DataFrame({'Survived': y_pred}, index=testdf.index ).reset_index().to_csv('submission.csv', index=False )