Kaggle competition: Santander Customer Transaction Prediction
del X_train, Y_train, X_test
del eval_set
del rmse_guess_train, rmse_base_train, rmse_guess_evalset, rmse_base_evalset
del xgbreg
del features_to_keep_2, features_to_discard_2
del xgb_params_2
loaded.remove('features_to_keep_2')
 loaded.remove('features_to_discard_2')
 loaded.remove('xgb_params_2')
 gc.collect()<define_variables>
import pandas as pd
from pathlib import Path
reset_variable_space<load_pretrained>
root = Path(".. /input" )
def predict_seniority(seniority,ntree=0):
xgbreg = pickle.load(open(os.path.join(DATA_FOLDER,'predictions/models/xgbreg_seniority'+str(seniority)+'/model.pickle'), 'rb'))
X_test = pd.read_pickle(os.path.join(DATA_FOLDER,'predictions/models/xgbreg_seniority'+str(seniority)+'/X_test.pkl'))
Y_pred_test=xgbreg.predict(X_test,ntree_limit=ntree ).clip(0,20)
 Y_pred=pd.read_pickle(os.path.join(DATA_FOLDER,'processed/train_'+str(seniority)+'.pkl'))
Y_pred=Y_pred.loc[Y_pred['month_id']==34,['shop_id','item_id']]
Y_pred['prediction']=Y_pred_test
return Y_pred<load_from_csv>
train = pd.read_csv(root.joinpath("train.csv"))
test = pd.read_csv(root.joinpath("test.csv"))
ntrees=[200,400,2000]
Y_0=predict_seniority(seniority=0,ntree=ntrees[0])
 Y_1=predict_seniority(seniority=1,ntree=ntrees[1])
 Y_2=predict_seniority(seniority=2,ntree=ntrees[2])
 Y_pred=pd.concat([Y_0,Y_1,Y_2],axis=0,sort=False)
 test_df = pd.read_csv(os.path.join(RAW_DATA_FOLDER, 'test.csv'))
test_df=test_df.join(Y_pred.set_index(['shop_id','item_id']),on=['shop_id','item_id'])
 print(test_df.info(null_counts=True))
test_df<save_to_csv>
train_id = train.ID_code
test_id = test.ID_code
target = train.target
train.drop(columns=["ID_code", "target"], inplace=True)
 test.drop(columns=["ID_code"], inplace=True )
submission = pd.DataFrame({
"ID": test_df['ID'],
"item_cnt_month": test_df['prediction']
})
 submission.to_csv('xgb_prediction.csv', index=False )<drop_column>
from sklearn.model_selection import KFold
from sklearn.metrics import roc_auc_score
from catboost import Pool, CatBoostClassifier
del test_df
del Y_0, Y_1, Y_2, Y_pred
del submission
gc.collect()<define_variables>
model = CatBoostClassifier(loss_function="Logloss",
eval_metric="AUC",
task_type="GPU",
learning_rate=0.01,
iterations=10000,
random_seed=42,
od_type="Iter",
depth=10,
early_stopping_rounds=500
)
reset_variable_space<load_from_csv>
n_split = 5
kf = KFold(n_splits=n_split, random_state=42, shuffle=True )
items = pd.read_csv('.. /input/items.csv')
 shops = pd.read_csv('.. /input/shops.csv')
 cats = pd.read_csv('.. /input/item_categories.csv')
 train = pd.read_csv('.. /input/sales_train.csv')
 test = pd.read_csv('.. /input/test.csv' ).set_index('ID' )<filter>
y_valid_pred = 0 * target
y_test_pred = 0
train = train[train.item_price<100000]
train = train[train.item_cnt_day<1001]<feature_engineering>
for idx,(train_index, valid_index)in enumerate(kf.split(train)) :
y_train, y_valid = target.iloc[train_index], target.iloc[valid_index]
X_train, X_valid = train.iloc[train_index,:], train.iloc[valid_index,:]
_train = Pool(X_train, label=y_train)
 _valid = Pool(X_valid, label=y_valid)
 print("
Fold ", idx)
 fit_model = model.fit(_train,
eval_set=_valid,
use_best_model=True,
verbose=200,
plot=True 
)
 pred = fit_model.predict_proba(X_valid)[:,1]
print(" auc = ", roc_auc_score(y_valid, pred))
y_valid_pred.iloc[valid_index] = pred
y_test_pred += fit_model.predict_proba(test)[:,1]
y_test_pred /= n_split
median = train[(train.shop_id==32)&(train.item_id==2973)&(train.date_block_num==4)&(train.item_price>0)].item_price.median()
train.loc[train.item_price<0, 'item_price'] = median<feature_engineering>
submission = pd.read_csv(root.joinpath("sample_submission.csv"))
submission['target'] = y_test_pred
submission.to_csv('submission.csv', index=False )