Kaggle competition: Santander Customer Transaction Prediction
<SOS> metric: AUC Kaggle data source: santander-customer-transaction-prediction<save_to_csv>
warnings.filterwarnings('ignore')
preds =(4 * stack_preds +
predictions_df["LGBMRegressor"] +
predictions_df["XGBRegressor"] +
2 * predictions_df["GradientBoostingRegressor"] +
predictions_df["SVR"] +
predictions_df["Lasso"])/ 10
sub = pd.DataFrame({"Id": test_id.Id, "SalePrice": np.expm1(preds)})
 sub.to_csv("BlendedModel120121.csv", index = False)
 sub<install_modules>
train_df = pd.read_csv('.. /input/train.csv')
 test_df = pd.read_csv('.. /input/test.csv')
 features = [c for c in train_df.columns if c not in ['ID_code', 'target']]
target = train_df.pop('target')
 train_df.drop('ID_code',axis=1,inplace=True)
 test_df.drop('ID_code',axis=1,inplace=True )
!pip install.. /input/python-datatable/datatable-0.11.0-cp37-cp37m-manylinux2010_x86_64.whl<import_modules>
param = {
'bagging_freq': 5,
'bagging_fraction': 0.335,
'boost_from_average':'false',
'boost': 'gbdt',
'feature_fraction': 0.041,
'learning_rate': 0.0083,
'max_depth': -1,
'metric':'auc',
'min_data_in_leaf': 80,
'min_sum_hessian_in_leaf': 10.0,
'num_leaves': 13,
'num_threads': 8,
'tree_learner': 'serial',
'objective': 'binary',
'verbosity': 1}
import pandas as pd
import datatable as dt
import lightgbm as lgb
from matplotlib import pyplot as plt<load_from_csv>
test_df['target'] = -1
train_df['i_am_train'] = 1
test_df['i_am_train'] = 0
full_df = pd.concat([train_df, test_df], axis=0 )
features = ['user_id', 'content_id', 'answered_correctly', 'prior_question_elapsed_time', 'prior_question_had_explanation']
train_df = dt.fread('.. /input/riiid-test-answer-prediction/train.csv' ).to_pandas()
train_df = train_df[features]<data_type_conversions>
for f in features:
full_df[f+'_counts'] = full_df[f].map(full_df[f].value_counts().to_dict() )
train_df = train_df[train_df['answered_correctly'] != -1].reset_index(drop=True)
 train_df.fillna(False, inplace=True)
 train_df['user_id'] = train_df['user_id'].astype('int32')
 train_df['content_id'] = train_df['content_id'].astype('int16')
 train_df['answered_correctly'] = train_df['answered_correctly'].astype('int8')
 train_df['prior_question_elapsed_time'] = train_df['prior_question_elapsed_time'].astype('float32')
 train_df['prior_question_had_explanation'] = train_df['prior_question_had_explanation'].astype('bool' )<groupby>
train_df = full_df.loc[full_df['i_am_train']==1]
test_df = full_df.loc[full_df['i_am_train']==0]
del train_df['i_am_train'], test_df['i_am_train'], test_df['target'], full_df
train_df['user'] = train_df.groupby('user_id')['answered_correctly'].shift()
cumulated = train_df.groupby('user_id')['user'].agg(['cumsum', 'cumcount'])
 train_df['user_correctness'] = cumulated['cumsum'] / cumulated['cumcount']
train_df.drop(columns=['user'], inplace=True )<groupby>
num_folds = 5
folds = StratifiedKFold(n_splits=num_folds, shuffle=True, random_state=42)
 oof = np.zeros(len(train_df))
predictions = np.zeros(len(test_df))
user_agg = train_df.groupby('user_id')['answered_correctly'].agg(['sum', 'count'])
 content_agg = train_df.groupby('content_id')['answered_correctly'].agg(['sum', 'count'] )<load_from_csv>
features = [c for c in train_df.columns if c not in ['ID_code', 'target']]
questions_df = pd.read_csv('.. /input/riiid-test-answer-prediction/questions.csv')[['question_id', 'part']]
questions_df['question_id'] = questions_df['question_id'].astype('int16')
 questions_df['part'] = questions_df['part'].astype('int8')
 train_df = pd.merge(train_df, questions_df, left_on='content_id', right_on='question_id', how='left')
 train_df.drop(columns=['question_id'], inplace=True )<categorify>
print('Training the Model:')
 for fold_,(trn_idx, val_idx)in enumerate(folds.split(train_df.values, target.values)) :
print("Fold idx:{}".format(fold_ + 1))
to_appends = []
for i in range(9):
np.random.seed(i)
 to_append_train = train_df.iloc[trn_idx].loc[target==1].copy().apply(np.random.permutation)
 to_appends.append(to_append_train)
 full_append = pd.concat(to_appends,axis=0)
 full_append['target'] = 1
trn_data = lgb.Dataset(pd.concat([train_df.iloc[trn_idx][features],full_append[features]], axis=0), label=pd.concat([target.iloc[trn_idx],full_append['target']],axis=0))
val_data = lgb.Dataset(train_df.iloc[val_idx][features], label=target.iloc[val_idx])
 clf = lgb.train(param, trn_data, 1000000, valid_sets = [trn_data, val_data], verbose_eval=100, early_stopping_rounds = 4000)
 oof[val_idx] = clf.predict(train_df.iloc[val_idx][features], num_iteration=clf.best_iteration)
 predictions += clf.predict(test_df[features], num_iteration=clf.best_iteration)/ folds.n_splits
print("CV score: {:<8.5f}".format(roc_auc_score(target, oof)) )
train_df['content_count'] = train_df['content_id'].map(content_agg['count'] ).astype('int32')
 train_df['content_id'] = train_df['content_id'].map(content_agg['sum'] / content_agg['count'] )<drop_column>
train_df = pd.read_csv('.. /input/train.csv', usecols=['ID_code','target'])
 test_df = pd.read_csv('.. /input/test.csv', usecols=['ID_code','var_0'] )
valid_df = train_df.groupby('user_id' ).tail(15)
 train_df.drop(valid_df.index, inplace=True )<define_variables>
sub = pd.DataFrame({"ID_code": test_df.ID_code.values})
 sub["target"] = predictions
sub.to_csv('submission_upsampled.csv', index=False )
<init_hyperparams><EOS> 
oofs = pd.DataFrame({"ID_code": train_df.ID_code.values})
 oofs["target"] = oof
oofs.to_csv('oof_upsampled.csv', index=False )