Kaggle competition: Titanic - Machine Learning from Disaster
%%time
trn_data = train_df[all_cols + ['fullVisitorId']].groupby('fullVisitorId')\ 
.agg({c: np.mean if c in num_cols else find_mode for c in all_cols})
 trn_pred_list = train_df[['fullVisitorId', 'predictions']].groupby('fullVisitorId' ).apply(parse_predictions)
 trn_all_predictions = pd.DataFrame(list(trn_pred_list.values), index=trn_data.index )<feature_engineering>
train_dataset = pd.read_csv('/kaggle/input/titanic/train.csv')
 test_dataset = pd.read_csv('/kaggle/input/titanic/test.csv')
 X_train = train_dataset.iloc[:, [False,False,True,False,True,True,True,True,False,True,False,False]].values
y_train = train_dataset.iloc[:, 1].values
X_test = test_dataset.iloc[:, [False,True,False,True,True,True,True,False,True,False,False]].values
session_pred_cols = trn_all_predictions.columns[:-2]
trn_all_predictions['t_log_mean'] = np.log1p(trn_all_predictions[session_pred_cols].mean(axis=1))
trn_all_predictions['t_log_median'] = np.log1p(trn_all_predictions[session_pred_cols].median(axis=1))
trn_all_predictions['t_sum_log'] = np.log1p(trn_all_predictions[session_pred_cols] ).sum(axis=1)
 trn_all_predictions['t_sum_act'] = np.log1p(trn_all_predictions[session_pred_cols].fillna(0 ).sum(axis=1))
trn_all_predictions['t_nb_sess'] = trn_all_predictions[session_pred_cols].isnull().sum(axis=1)
 full_data = pd.concat([trn_data, trn_all_predictions], axis=1)
 del trn_data, trn_all_predictions
gc.collect()
full_data.shape<prepare_output>
ct = ColumnTransformer(transformers = [('encoder',OneHotEncoder() ,[1])],remainder = 'passthrough')
%%time
sub_data = test_df[all_cols + ['fullVisitorId']].groupby('fullVisitorId')\ 
.agg({c: np.mean if c in num_cols else find_mode for c in all_cols})
 sub_pred_list = test_df[['fullVisitorId', 'predictions']].groupby('fullVisitorId' ).apply(parse_predictions)
 sub_all_predictions = pd.DataFrame(list(sub_pred_list.values), index=sub_data.index)
 for f in session_pred_cols:
if f not in sub_all_predictions.columns:
sub_all_predictions[f] = np.nan
sub_all_predictions['t_log_mean'] = np.log1p(sub_all_predictions[session_pred_cols].mean(axis=1))
sub_all_predictions['t_log_median'] = np.log1p(sub_all_predictions[session_pred_cols].median(axis=1))
sub_all_predictions['t_sum_log'] = np.log1p(sub_all_predictions[session_pred_cols] ).sum(axis=1)
 sub_all_predictions['t_sum_act'] = np.log1p(sub_all_predictions[session_pred_cols].fillna(0 ).sum(axis=1))
sub_all_predictions['t_nb_sess'] = sub_all_predictions[session_pred_cols].isnull().sum(axis=1)
 sub_full_data = pd.concat([sub_data, sub_all_predictions], axis=1)
 del sub_data, sub_all_predictions
gc.collect()
sub_full_data.shape<groupby>
X_train = np.array(ct.fit_transform(X_train))
X_test = np.array(ct.fit_transform(X_test))
target = train_df[['fullVisitorId', 'totals.transactionRevenue']].groupby('fullVisitorId' ).sum()<init_hyperparams>
imputer = SimpleImputer(missing_values = np.nan, strategy = 'mean')
 imputer.fit(X_train)
 X_train = imputer.transform(X_train)
 imputer.fit(X_test)
 X_test = imputer.transform(X_test )
params = {'learning_rate': 0.01,
'objective': 'regression',
'metric': 'rmse',
'num_leaves': 31,
'verbose': 1,
'bagging_fraction': 0.93,
'feature_fraction': 0.57,
'random_state': 42,
'max_depth': 14,
'random_seed': 42,
'bagging_frequency': 5,
'lambda_l2': 0.62,
'lambda_l1': 0.07,
'min_child_samples': 179
}<prepare_x_and_y>
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
 X_test = sc.transform(X_test )
folds = get_folds(df=full_data[['totals.pageviews']].reset_index() , n_splits=5)
 oof_reg_preds = np.zeros(full_data.shape[0])
 sub_preds = np.zeros(sub_full_data.shape[0])
 vis_importances = pd.DataFrame()
for fold_,(trn_, val_)in enumerate(folds):
trn_x, trn_y = full_data.iloc[trn_], target['totals.transactionRevenue'].iloc[trn_]
val_x, val_y = full_data.iloc[val_], target['totals.transactionRevenue'].iloc[val_]
reg = lgb.LGBMRegressor(**params,n_estimators=2000)
 reg.fit(trn_x, np.log1p(trn_y), eval_set=[(val_x, np.log1p(val_y)) ], early_stopping_rounds=100,
verbose=100, eval_metric='rmse')
 imp_df = pd.DataFrame()
imp_df['feature'] = trn_x.columns
imp_df['gain'] = reg.booster_.feature_importance(importance_type='gain')
 imp_df['fold'] = fold_ + 1
vis_importances = pd.concat([vis_importances, imp_df], axis=0, sort=False)
 oof_reg_preds[val_] = reg.predict(val_x, num_iteration=reg.best_iteration_)
 oof_reg_preds[oof_reg_preds < 0] = 0
lgb_preds = reg.predict(sub_full_data[full_data.columns], num_iteration=reg.best_iteration_)
 lgb_preds[lgb_preds < 0] = 0
sub_preds += np.expm1(lgb_preds)/ len(folds)
 print("LGBM Result: ", mean_squared_error(np.log1p(target['totals.transactionRevenue']), oof_reg_preds)**.5 )<save_to_csv>
for i in range(len(X_train)) :
if X_train[i][0]>X_train[i][1]:
X_train[i][0] = 1
X_train[i][1] = 0
else:
X_train[i][0] = 0
X_train[i][1] = 1
print(X_train )
sub_full_data['PredictedLogRevenue'] = np.log1p(sub_preds)
 sub_full_data[['PredictedLogRevenue']].to_csv('submission.csv', index=True )<load_from_csv>
for i in range(len(X_test)) :
if X_test[i][0]>X_test[i][1]:
X_test[i][0] = 1
X_test[i][1] = 0
else:
X_test[i][0] = 0
X_test[i][1] = 1
print(X_test )
pass<set_options>
classifier = KNeighborsClassifier(n_neighbors=5, metric='minkowski',p=2)
 classifier.fit(X_train, y_train )
warnings.simplefilter('error', SettingWithCopyWarning)
 gc.enable()
%matplotlib inline<load_from_csv>
y_test = classifier.predict(X_test)
 print(y_test )
train = pd.read_csv('.. /input/create-extracted-json-fields-dataset/extracted_fields_train.gz',
dtype={'date': str, 'fullVisitorId': str, 'sessionId':str}, nrows=None)
 test = pd.read_csv('.. /input/create-extracted-json-fields-dataset/extracted_fields_test.gz',
dtype={'date': str, 'fullVisitorId': str, 'sessionId':str}, nrows=None)
 train.shape, test.shape<prepare_x_and_y>
output = pd.DataFrame({'PassengerId': test_dataset.PassengerId, 'Survived': y_test})
 output.to_csv('my_submission.csv', index=False)
 print("Your submission was successfully saved!" )