Kaggle competition: Titanic - Machine Learning from Disaster
rf_model = RandomForestClassifier(**rf_params, random_state=29,n_jobs = -1 )<compute_test_metric>
%matplotlib inline
train= pd.read_csv('/kaggle/input/titanic/train.csv')
 def impute_age(cols):
Age = cols[0]
Pclass = cols[1]
if pd.isnull(Age):
if Pclass==1:
return 37
elif Pclass == 2:
return 29
else :
return 24
else:
return Age
cross_val_model(X_train, y_train['target'], rf_model )<init_hyperparams>
train['Age']=train[['Age','Pclass']].apply(impute_age,axis = 1 )
xgb_params = {}
xgb_params['learning_rate'] = 0.02
xgb_params['n_estimators'] = 1000
xgb_params['max_depth'] = 4
xgb_params['subsample'] = 0.9
xgb_params['colsample_bytree'] = 0.9<choose_model_class>
train.drop('Cabin',inplace = True,axis =1 )
XGB_model = XGBClassifier(**rf_params, random_state=29,n_jobs=-1 )<compute_test_metric>
sex = pd.get_dummies(train['Sex'],drop_first=True)
 embark = pd.get_dummies(train['Embarked'],drop_first=True )
cross_val_model(X_train, y_train['target'], XGB_model )<categorify>
train = pd.concat([train,sex,embark],axis=1)
train_ext = train.copy()
test_ext = test.copy()
missing_values_table(train_ext )<feature_engineering>
train.drop(['Sex','Embarked','PassengerId','Name','Ticket'],axis=1,inplace=True )
def create_extra_features(train_ext):
train_ext['null_sum'] = train_ext[train_ext==-1].count(axis=1)
 train_ext['ord_sum'] = train_ext[col_ordinal].sum(axis=1)
 train_ext['interval_median'] = train_ext[col_interval].sum(axis=1)
 train_ext['new_amount_borrowed_by_income'] = train_ext['amount_borrowed']/train_ext['income']
train_ext['new_amount_borrowed_by_months'] = train_ext['amount_borrowed']/train_ext['borrowed_in_months']
return train_ext<define_variables>
st = StandardScaler()
feature_scale = ['Age','Fare']
train[feature_scale] = st.fit_transform(train[feature_scale] )
train_ext = create_extra_features(train_ext)
 test_ext = create_extra_features(test_ext)
 train_backup = train_ext.copy()
test_backup = test_ext.copy()<filter>
feature_scale = ['Age','Fare']
train[feature_scale] = st.fit_transform(train[feature_scale])
meta_data = get_meta(train_ext)
 col_ordinal = meta_data[(meta_data.level == 'ordinal')&(meta_data.keep)&(meta_data.role != 'target')].index
col_nominal = meta_data[(meta_data.level == 'nominal')&(meta_data.keep)&(meta_data.role != 'target')].index
col_interval = meta_data[(meta_data.level == 'interval')&(meta_data.keep)&(meta_data.role != 'target')].index
col_binary = meta_data[(meta_data.level == 'binary')&(meta_data.keep)&(meta_data.role != 'target')].index 
<groupby>
x = train.drop(['Survived'],axis=1)
 y = train['Survived']
meta_counts = meta_data.groupby(['role', 'level'] ).agg({'dtype': lambda x: x.count() } ).reset_index()
meta_counts<define_variables>
from sklearn.model_selection import GridSearchCV
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import GradientBoostingClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.svm import SVC
from sklearn.neighbors import KNeighborsClassifier
ids_targets = meta_data[meta_data['role'] != 'input'].index<categorify>
tree = DecisionTreeClassifier()
tree.fit(x,y)
 tree.score(x,y )
train_ext.fillna(-1, inplace = True)
 X_ext = pd.concat([train_ext[col_interval],train_ext[col_ordinal], pd.get_dummies(train_ext[col_binary])], axis=1)
 X_ext.head()<drop_column>
test = pd.read_csv('/kaggle/input/titanic/test.csv' )
X_ext = X_ext.drop(columns = ['facebook_profile_False','gender_f'], axis=1 )<prepare_x_and_y>
test2 = test.copy()
test_ext = pd.concat([test_ext[col_interval],test_ext[col_ordinal], pd.get_dummies(test_ext[col_binary])], axis=1)
 test_ext.fillna(-1, inplace = True)
 y_ext = pd.DataFrame(train_ext.target)
 y_ext=y_ext.astype('bool')
 y_ext = y_ext.values
y_ext = y_ext.reshape(-1 )<drop_column>
test['Age']=test[['Age','Pclass']].apply(impute_age,axis = 1 )
test_ext['gender_-1'] = 0
test_ext['facebook_profile_-1'] = 0
test_ext=test_ext.drop(columns = ['facebook_profile_False', 'gender_f'], axis = 1)
 test_ext.head()<drop_column>
test.drop('Cabin',inplace = True,axis =1 )
cols = list(X_ext)
 test_ext = test_ext[cols]<split>
sex = pd.get_dummies(test['Sex'],drop_first=True)
 embark = pd.get_dummies(test['Embarked'],drop_first=True )
X_train, X_test, y_train, y_test = train_test_split(X_ext, y_ext, test_size=0.2, random_state=42 )<compute_test_metric>
test = pd.concat([test,sex,embark],axis=1 )
cross_val_model(X_ext, y_ext, rf_model )<compute_test_metric>
test.drop(['Sex','Embarked','PassengerId','Name','Ticket'],axis=1,inplace=True )
cross_val_model(X_ext, y_ext, XGB_model )<set_options>
test['Fare'].fillna(test['Fare'].mean() ,inplace=True )
gc.collect<import_modules>
feature_scale = ['Age','Fare']
test[feature_scale] = st.fit_transform(test[feature_scale] )
from sklearn.model_selection import GridSearchCV<import_modules>
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import GradientBoostingClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.svm import SVC
from sklearn.neighbors import KNeighborsClassifier
from sklearn.model_selection import GridSearchCV<train_on_grid>
level1 = LogisticRegression()
model = StackingClassifier(estimators=level0,final_estimator=level1,cv=5 )
tuned_parameters = [{'max_depth': [4,5,6,7,8,9,10],
'max_features': [4,5,6,7,8,9,10],
'n_estimators':[10,25,50,75]}]
clf = GridSearchCV(RandomForestClassifier(random_state=29), tuned_parameters, cv=3, scoring='roc_auc')
 clf.fit(X_train, y_train )<find_best_params>
model.fit(x,y )
print("Best parameters set found on development set:")
 print()
print(clf.best_params_)
 print()
print("Grid scores on development set:")
 print()
means = clf.cv_results_['mean_test_score']
stds = clf.cv_results_['std_test_score']
for mean, std, params in zip(means, stds, clf.cv_results_['params']):
print("%0.3f(+/-%0.03f)for %r"
%(mean, std * 2, params))
print()
print("Detailed classification report:")
 print()
print("The model is trained on the full development set.")
 print("The scores are computed on the full evaluation set.")
 print()
acc = accuracy_score(y_test, clf.predict(X_test))
auc = roc_auc_score(y_test, clf.predict(X_test))
print("Accuracy: %.4f" % acc)
 print()
print("AUC: %.4f" % auc)
 print()<import_modules>
model.score(x,y )
from hyperopt.pyll.base import scope
from hyperopt.pyll.stochastic import sample
from hyperopt import STATUS_OK, Trials, fmin, hp, tpe<define_variables>
y_predicted = model.predict(test )
N_HYPEROPT_PROBES = 10
EARLY_STOPPING = 80
HOLDOUT_SEED = 123456
HOLDOUT_SIZE = 0.10
HYPEROPT_ALGO = tpe.suggest
DATASET = 'clean'
SEED0 = random.randint(1,1000000000)
 NB_CV_FOLDS = 5<define_variables>
submission = pd.DataFrame({
"PassengerId":test2['PassengerId'],
"Survived":y_predicted
} )
obj_call_count = 0
cur_best_score = 0<categorify>
submission.to_csv('first_kaggale_titanic_submission.csv',index=False )