Kaggle competition: Titanic - Machine Learning from Disaster
def compute_roc_auc(clf,index):
y_predict = clf.predict_proba(train_data.iloc[index])[:,1]
fpr, tpr, thresholds = roc_curve(target_data.iloc[index], y_predict)
 auc_score = auc(fpr, tpr)
 return fpr, tpr, auc_score<init_hyperparams>
X_full = pd.read_csv('.. /input/titanic/train.csv', index_col='PassengerId' )
params = {'bagging_fraction': 0.7982116702024386,
'feature_fraction': 0.1785051643813966,
'max_depth': int(49.17611603427576),
'min_child_weight': 3.2852905549011155,
'min_data_in_leaf': int(31.03480802715621),
'n_estimators': int(1491.3676131788188),
'num_leaves': int(52.851307790411965),
'reg_alpha': 0.45963319421692145,
'reg_lambda': 0.6591286807489907,
'metric':'auc',
'boosting_type': 'dart',
'colsample_bytree':.8,
'subsample':.9,
'min_split_gain':.01,
'max_bin':127,
'bagging_freq':5,
'learning_rate':0.1 ,
'early_stopping_rounds':500 }<split>
np.random.seed(0)
 missing_values_count = X_full.isnull().sum()
total_cells = np.product(X_full.shape)
 total_missing = missing_values_count.sum()
percent_missing =(total_missing/total_cells)
 print(percent_missing)
 print(total_cells)
 print(total_missing )
fprs_lgb, tprs_lgb, scores_lgb = [], [], []
feature_importances = pd.DataFrame()
feature_importances['feature'] = train_data.columns
predictions = np.zeros(len(test_data))
for(train, test), i in zip(cv.split(train_data, target_data), range(5)) :
lgb_best = LGBMClassifier(boosting = params['boosting_type'],n_estimators = params['n_estimators'],
learning_rate = params['learning_rate'],num_leaves = params['num_leaves'],
colsample_bytree = params['colsample_bytree'],subsample = params['subsample'],
max_depth = params['max_depth'],reg_alpha = params['reg_alpha'],
reg_lambda = params['reg_lambda'],min_split_gain = params['min_split_gain'],
min_child_weight = params['min_child_weight'],max_bin = params['max_bin'],
bagging_freq = params['bagging_freq'],feature_fraction = params['feature_fraction'],
bagging_fraction = params['bagging_fraction'],min_data_in_leaf = params['min_data_in_leaf'],
early_stopping_rounds = params['early_stopping_rounds'])
 lgb_best.fit(train_data.iloc[train,:], target_data.iloc[train],
eval_set = [(train_data.iloc[train,:], target_data.iloc[train]), 
(train_data.iloc[test,:], target_data.iloc[test])],eval_metric='auc',verbose = 200)
 feature_importances['fold_{}'.format(i + 1)] = lgb_best.feature_importances_
_, _, auc_score_train = compute_roc_auc(lgb_best,train)
 fpr, tpr, auc_score = compute_roc_auc(lgb_best,test)
 scores_lgb.append(( auc_score_train, auc_score))
fprs_lgb.append(fpr)
 tprs_lgb.append(tpr)
 predictions += lgb_best.predict_proba(test_data, num_iteration=lgb_best.best_iteration_)[:,1]/cv.n_splits<compute_test_metric>
X_test_full = pd.read_csv('.. /input/titanic/test.csv', index_col='PassengerId' )
print('Mean AUC:', np.mean(scores_lgb,axis = 1))<save_to_csv>
X_full.dropna(axis=0, subset=['Survived'], inplace=True)
 y = X_full.Survived
X_full.drop(['Survived'], axis=1, inplace=True)
 X_full.drop(['Name', 'Ticket', 'Fare', 'SibSp'], axis=1, inplace=True)
 X_train_full, X_valid_full, y_train, y_valid = train_test_split(X_full, y, train_size=0.8, test_size=0.2, random_state=0)
 categorical_cols = [cname for cname in X_train_full.columns if
X_train_full[cname].dtype == 'object']
numerical_cols = [cname for cname in X_train_full.columns if
X_train_full[cname].dtype in ['int64', 'float64']]
my_cols = categorical_cols + numerical_cols
X_train = X_train_full[my_cols].copy()
X_valid = X_valid_full[my_cols].copy()
X_test = X_test_full[my_cols].copy()
X_train.head()
data = {'TransactionID':transactionID,'isFraud':predictions}
submissionDF = pd.DataFrame(data)
 submissionDF.to_csv('sample_submission2.csv',index=False )<set_options>
numerical_transformer = SimpleImputer(strategy='most_frequent')
 categorical_transformer = Pipeline(steps=[ 
('imputer', SimpleImputer(strategy='most_frequent')) , 
('onehot', OneHotEncoder(handle_unknown='ignore'))
])
 preprocessor = ColumnTransformer(
 transformers=[ 
('num', numerical_transformer, numerical_cols), 
('cat', categorical_transformer, categorical_cols)
 ])
 n_estimators = [24, 25, 26]
max_depth = [8, 9, 10]
min_samples_split = [11, 12, 13]
min_samples_leaf = [1, 2, 3]
criterion = ['gini', 'entropy']
model = RandomForestClassifier(n_estimators=100, max_depth=3, random_state=0)
 model = RandomForestClassifier(n_estimators=25, max_depth=9, min_samples_leaf=1, min_samples_split=12, random_state=0)
 hyperF = dict(n_estimators = n_estimators, max_depth = max_depth, min_samples_split = min_samples_split, min_samples_leaf = min_samples_leaf, criterion = criterion)
 gridF = GridSearchCV(model, hyperF, cv = 3, verbose = 1, n_jobs = -1)
 clf = Pipeline(steps=[('preprocessor', preprocessor), 
('model', model)
 ])
 grid_test = Pipeline(steps=[('preprocessor', preprocessor), 
('model', gridF)
 ])
 grid_test.fit(X_train, y_train)
 clf.fit(X_train, y_train)
 preds = clf.predict(X_valid)
 Fpreds = grid_test.predict(X_valid)
 print('acc:', accuracy_score(y_valid, preds))
print('acc:', accuracy_score(y_valid, Fpreds))
print('loading libs...')
 warnings.filterwarnings("ignore")
 print('done' )<load_from_csv>
print(gridF.best_params_ )
<define_variables><EOS> 
preds_test = clf.predict(X_test)
 output = pd.DataFrame({'PassengerId': X_test.index, 'Survived': preds_test})
 output.to_csv('my_submission.csv', index=False)
 print("Your submission was successfully saved!" )