Kaggle competition: Santander Customer Transaction Prediction
<SOS> metric: AUC Kaggle data source: santander-customer-transaction-prediction<choose_model_class>
pd.set_option('display.max_columns', 200 )
num_round = 1000000
folds = StratifiedKFold(n_splits=5, shuffle=True, random_state=42)
 oof = np.zeros(len(train))
predictions = np.zeros(len(test))
model = CatBoostClassifier(loss_function="Logloss",
eval_metric="AUC",
task_type="GPU",
learning_rate=0.01,
iterations=70000,
l2_leaf_reg=50,
random_seed=42,
od_type="Iter",
depth=5,
early_stopping_rounds=15000,
border_count=64 
)
 for fold_,(trn_idx, val_idx)in enumerate(folds.split(train.values, train.target.values)) :
print("Fold {}".format(fold_))
X_train, y_train = train.iloc[trn_idx][features], train.iloc[trn_idx]['target']
X_valid, y_valid = train.iloc[val_idx][features], train.iloc[val_idx]['target']
for col in tqdm(features):
gr = data[col].value_counts()
gr_bin = data.groupby(col)[col].count() >1
X_train[col + '_un'] = X_train[col].map(gr ).astype('category' ).cat.codes
X_valid[col + '_un'] = X_valid[col].map(gr ).astype('category' ).cat.codes
test[col + '_un'] = test[col].map(gr ).astype('category' ).cat.codes
X_train[col + '_un_bin'] = X_train[col].map(gr_bin ).astype('category' ).cat.codes
X_valid[col + '_un_bin'] = X_valid[col].map(gr_bin ).astype('category' ).cat.codes
test[col + '_un_bin'] = test[col].map(gr_bin ).astype('category' ).cat.codes
X_train[col + '_raw_mul'] = X_train[col] * X_train[col + '_un_bin']
X_valid[col + '_raw_mul'] = X_valid[col] * X_valid[col + '_un_bin']
test[col + '_raw_mul'] = test[col] * test[col + '_un_bin']
X_train[col + '_raw_mul_2'] = X_train[col] * X_train[col + '_un']
X_valid[col + '_raw_mul_2'] = X_valid[col] * X_valid[col + '_un']
test[col + '_raw_mul_2'] = test[col] * test[col + '_un']
X_train[col + '_raw_mul_3'] = X_train[col + '_un_bin'] * X_train[col + '_un']
X_valid[col + '_raw_mul_3'] = X_valid[col + '_un_bin'] * X_valid[col + '_un']
test[col + '_raw_mul_3'] = test[col + '_un_bin'] * test[col + '_un']
_train = Pool(X_train, label=y_train)
 _valid = Pool(X_valid, label=y_valid)
 clf = model.fit(_train,
eval_set=_valid,
use_best_model=True,
verbose=5000)
 pred = clf.predict_proba(X_valid)[:,1]
oof[val_idx] = pred
print(" auc = ", roc_auc_score(y_valid, pred))
predictions += clf.predict_proba(test.drop('ID_code', axis=1)) [:,1] / folds.n_splits
print("CV score: {:<8.5f}".format(roc_auc_score(train.target, oof)) )<save_to_csv>
train_df = pd.read_csv('.. /input/train.csv')
 test_df = pd.read_csv('.. /input/test.csv' )
sub = pd.DataFrame({"ID_code": test.ID_code.values})
 sub["target"] = predictions
sub.to_csv("Range_bins_sub_3.csv", index=False )<set_options>
train_df.target.value_counts()
!pip install --upgrade seaborn
np.random.seed(42)
 plt.rcParams.update({'figure.max_open_warning': 0})
 pd.options.mode.chained_assignment = None
pd.set_option('display.max_rows', 250)
 pd.set_option('display.max_columns', 250 )<import_modules>
param = {
'num_leaves': 18,
'max_bin': 63,
'min_data_in_leaf': 5,
'learning_rate': 0.010614430970330217,
'min_sum_hessian_in_leaf': 0.0093586657313989123,
'feature_fraction': 0.056701788569420042,
'lambda_l1': 0.060222413158420585,
'lambda_l2': 4.6580550589317573,
'min_gain_to_split': 0.29588543202055562,
'max_depth': 49,
'save_binary': True,
'seed': 1337,
'feature_fraction_seed': 1337,
'bagging_seed': 1337,
'drop_seed': 1337,
'data_random_seed': 1337,
'objective': 'binary',
'boosting_type': 'gbdt',
'verbose': 1,
'metric': 'auc',
'is_unbalance': True,
'boost_from_average': False
}
!pip install --upgrade lightgbm 
<load_from_csv>
nfold = 10
train = pd.read_csv(".. /input/house-prices-advanced-regression-techniques/train.csv")
 test = pd.read_csv(".. /input/house-prices-advanced-regression-techniques/test.csv")
 train_id = train.Id
test_id = test.Id
print("Train set: ", train.shape)
 print("Test set: ", test.shape)
 train.info()<concatenate>
target = 'target'
predictors = train_df.columns.values.tolist() [2:]
df = pd.concat([train, test])
 df<count_values>
gc.collect()
def col_types(df):
num_cols = df.loc[:, df.dtypes != "object"].columns.tolist()
cat_cols = df.loc[:, df.dtypes == "object"].columns.tolist()
ord_cols = []
for col in num_cols:
if df[col].value_counts().size < 20:
ord_cols.append(col)
 num_cols = [x for x in num_cols if x not in ord_cols + ["Id", "SalePrice"]]
return num_cols, cat_cols, ord_cols<count_missing_values>
skf = StratifiedKFold(n_splits=nfold, shuffle=True, random_state=2019)
 oof = np.zeros(len(train_df))
predictions = np.zeros(len(test_df))
i = 1
for train_index, valid_index in skf.split(train_df, train_df.target.values):
print("
fold {}".format(i))
xg_train = lgb.Dataset(train_df.iloc[train_index][predictors].values,
label=train_df.iloc[train_index][target].values,
feature_name=predictors,
free_raw_data = False 
)
 xg_valid = lgb.Dataset(train_df.iloc[valid_index][predictors].values,
label=train_df.iloc[valid_index][target].values,
feature_name=predictors,
free_raw_data = False 
)
 nround = 8523
clf = lgb.train(param, xg_train, nround, valid_sets = [xg_valid], verbose_eval=250)
 oof[valid_index] = clf.predict(train_df.iloc[valid_index][predictors].values, num_iteration=nround)
 predictions += clf.predict(test_df[predictors], num_iteration=nround)/ nfold
i = i + 1
print("
CV AUC: {:<0.4f}".format(metrics.roc_auc_score(train_df.target.values, oof)) )
def missing(df):
miss = pd.DataFrame({"no_missing_values": df.isnull().sum() ,
"missing_value_ratio":(df.isnull().sum() / df.shape[0] ).round(4),
"missing_in_train": df[df.SalePrice.notnull() ].isnull().sum() ,
"missing_in_test": df[df.SalePrice.isnull() ].isnull().sum() })
 return miss[miss.no_missing_values > 0].sort_values("no_missing_values", ascending=False)
 missing(df )<filter>
sub_df = pd.DataFrame({"ID_code": test_df.ID_code.values})
 sub_df["target"] = predictions
sub_df[:10]
df[(df.GarageFinish.isnull())&(df.GarageType.notnull())]<feature_engineering>
sub_df.to_csv("Customer_Transaction.csv", index=False )