Kaggle competition: Santander Customer Transaction Prediction
<SOS> metric: AUC Kaggle data source: santander-customer-transaction-prediction<train_model>
import numpy as np
import pandas as pd
import gc
import lightgbm as lgb
from sklearn.model_selection import StratifiedKFold
from scipy.stats import rankdata
from sklearn.metrics import roc_auc_score
tr_data = lgb.Dataset(train_df[features], label=train_df[target])
 va_data = lgb.Dataset(valid_df[features], label=valid_df[target])
 model = lgb.train(
 params,
tr_data,
num_boost_round=10000,
valid_sets=[tr_data, va_data],
early_stopping_rounds=50,
verbose_eval=50 
)
<import_modules>
train_df = pd.read_csv(".. /input/train.csv")
 test_df = pd.read_csv(".. /input/test.csv")
 features = [x for x in train_df.columns if x.startswith("var")]
import riiideducation<split>
for var in features:
if np.corrcoef(train_df['target'], train_df[var])[1][0] < 0:
train_df[var] = train_df[var] * -1
test_df[var] = test_df[var] * -1
env = riiideducation.make_env()
iter_test = env.iter_test()
prior_test_df = None<feature_engineering>
var_stats = {}
for var in features:
var_stats[var] = train_df[var].value_counts()
for(test_df, sample_prediction_df)in iter_test:
if prior_test_df is not None:
prior_test_df[target] = eval(test_df['prior_group_answers_correct'].iloc[0])
 prior_test_df = prior_test_df[prior_test_df[target] != -1].reset_index(drop=True)
 user_ids = prior_test_df['user_id'].values
content_ids = prior_test_df['content_id'].values
targets = prior_test_df[target].values
for user_id, content_id, answered_correctly in zip(user_ids, content_ids, targets):
if user_id in user_agg.index:
user_agg.loc[user_id, 'sum'] += answered_correctly
user_agg.loc[user_id, 'count'] += 1
else:
user_agg.loc[user_id] = [answered_correctly, 1]
if content_id in content_agg.index:
content_agg.loc[content_id, 'sum'] += answered_correctly
content_agg.loc[content_id, 'count'] += 1
else:
content_agg.loc[content_id] = [answered_correctly, 1]
prior_test_df = test_df.copy()
test_df = pd.merge(test_df, questions_df, left_on='content_id', right_on='question_id', how='left')
 test_df['prior_question_had_explanation'] = test_df['prior_question_had_explanation'].fillna(False ).astype('bool')
 test_df['user_correctness'] = test_df['user_id'].map(user_agg['sum'] / user_agg['count'])
 test_df['content_count'] = test_df['content_id'].map(content_agg['count'] ).fillna(1)
 test_df['content_id'] = test_df['content_id'].map(content_agg['sum'] / content_agg['count'] ).fillna(0.7)
 test_df['answered_correctly'] = model.predict(test_df[features])
 env.predict(test_df.loc[test_df['content_type_id'] == 0, ['row_id', 'answered_correctly']] )<import_modules>
def logit(p):
return np.log(p)- np.log(1 - p)
 def var_to_feat(vr, var_stats, feat_id):
new_df = pd.DataFrame()
new_df["var"] = vr.values
new_df["hist"] = pd.Series(vr ).map(var_stats)
 new_df["feature_id"] = feat_id
new_df["var_rank"] = new_df["var"].rank() /200000.
 return new_df.values
import riiideducation
import pandas as pd
import numpy as np
from sklearn.metrics import roc_auc_score
import tensorflow as tf
from sklearn.model_selection import train_test_split
from sklearn.metrics import roc_auc_score
from sklearn import preprocessing
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import cross_validate, cross_val_predict, cross_val_score, train_test_split, validation_curve, ShuffleSplit, RandomizedSearchCV
from sklearn.metrics import recall_score, precision_score, accuracy_score, confusion_matrix, classification_report
import matplotlib.pyplot as plt<load_from_csv>
TARGET = np.array(list(train_df['target'].values)* 200)
 TRAIN = []
var_mean = {}
var_var = {}
for var in features:
tmp = var_to_feat(train_df[var], var_stats[var], int(var[4:]))
var_mean[var] = np.mean(tmp[:,0])
 var_var[var] = np.var(tmp[:,0])
 tmp[:,0] =(tmp[:,0]-var_mean[var])/var_var[var]
TRAIN.append(tmp)
 TRAIN = np.vstack(TRAIN)
 target = train_df['target'].values
del train_df
_=gc.collect()
print(TRAIN.shape, len(TARGET))
train = pd.read_csv('/kaggle/input/riiid-test-answer-prediction/train.csv',
usecols=[1, 2, 3, 4, 5, 7, 8, 9],
nrows=5000000,
dtype={'timestamp': 'int64',
'user_id': 'int32',
'content_id': 'int16',
'content_type_id': 'int8',
'task_container_id': 'int16',
'answered_correctly':'int8',
'prior_question_elapsed_time': 'float32',
'prior_question_had_explanation': 'boolean'}
)<define_variables>
model = lgb.LGBMClassifier(**{
'learning_rate': 0.03,
'num_leaves': 31,
'max_bin': 1023,
'min_child_samples': 1000,
'feature_fraction': 1.0,
'bagging_freq': 1,
'bagging_fraction': 0.85,
'objective': 'binary',
'n_jobs': -1,
'n_estimators':200,})
 NFOLDS = 10
predtrain = np.zeros(len(TARGET))
MODELS = []
skf = StratifiedKFold(n_splits=NFOLDS, shuffle=True, random_state=11111)
 for fold_,(train_indexes, valid_indexes)in enumerate(skf.split(TRAIN, TARGET)) :
print('Fold:', fold_)
 model = model.fit(TRAIN[train_indexes], TARGET[train_indexes],
eval_set =(TRAIN[valid_indexes], TARGET[valid_indexes]),
verbose = 100,
eval_metric='auc',
early_stopping_rounds=20,
categorical_feature = [2])
 MODELS.append(model)
 predtrain[valid_indexes] = model.predict_proba(TRAIN[valid_indexes])[:,1]
pred = np.reshape(predtrain ,(200000,200), order='F')
 print(NFOLDS,'-Fold CV AUC:',roc_auc_score(target, np.mean(logit(pred),axis=1)))
 _=gc.collect()
<load_from_csv><EOS> 
ypred = np.zeros(( 200000,200))
for feat,var in enumerate(features):
tmp = var_to_feat(test_df[var], var_stats[var], int(var[4:]))
tmp[:,0] =(tmp[:,0]-var_mean[var])/var_var[var]
tmp[:,1] = tmp[:,1] + 1
tmp[ np.isnan(tmp)] = 1
for model_id in range(NFOLDS):
model = MODELS[model_id]
ypred[:,feat] += model.predict_proba(tmp)[:,1] / NFOLDS
ypred = np.mean(logit(ypred), axis=1)
 sub = test_df[['ID_code']]
sub['target'] = ypred
sub['target'] = sub['target'].rank() / 200000.
 sub.to_csv('golden_sub.csv', index=False)
 print(sub.head(20))