Kaggle competition: Market Basket - ID NDSC 2020
1/0<load_pretrained>
assoc_df = pd.read_csv('.. /input/market-basket-id-ndsc-2020/association_order.csv')
 rules_df = pd.read_csv('.. /input/market-basket-id-ndsc-2020/rules.csv' )
data = pd.read_pickle('data.pkl' )<prepare_x_and_y>
def get_assoc(*args):
orderid = [int(id)for id in args]
assoc_conds = [assoc_df.loc[(assoc_df['itemid'] == cond)]['orderid'] for cond in orderid]
assoc_conds = [set(assoc_cond.values.tolist())for assoc_cond in assoc_conds]
intersection_val = assoc_conds[0].intersection(*assoc_conds[1:])
 return len(intersection_val)
X_train = data[data.date_block_num < 33].drop(['item_cnt_month'], axis=1)
 Y_train = data[data.date_block_num < 33]['item_cnt_month']
X_valid = data[data.date_block_num == 33].drop(['item_cnt_month'], axis=1)
 Y_valid = data[data.date_block_num == 33]['item_cnt_month']
X_test = data[data.date_block_num == 34].drop(['item_cnt_month'], axis=1 )<set_options>
def split_rule(x):
return x.split(">")
 def calculate_rule(rules):

 condA, condB = split_rule(rules)
 condB += "&" + condA
condA = condA.split("&")
 condB = condB.split("&")
 totalA = get_assoc(*condA)
 totalB = get_assoc(*condB)
 return totalB / totalA
del data
gc.collect()<train_model>
rules_df['confidence'] = [floor(confidence * 1000)for confidence in confidences]
%%time
model = XGBRegressor(
 max_depth=8,
n_estimators=1000,
min_child_weight=300,
colsample_bytree=0.8,
subsample=0.8,
eta=0.3,
seed=10086)
 model.fit(
 X_train,
Y_train,
eval_metric="rmse",
eval_set=[(X_train, Y_train),(X_valid, Y_valid)],
verbose=True,
early_stopping_rounds = 10 )<compute_train_metric>
submission = rules_df[['rule', 'confidence']]
submission
<compute_test_metric><EOS> 
submission.to_csv("submission.csv", index= False )