{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.14","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":31254,"databundleVersionId":3103714,"sourceType":"competition"},{"sourceId":195911389,"sourceType":"kernelVersion"}],"dockerImageVersionId":30761,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"%pip install -U lightgbm==3.3.2\n%pip install implicit\n%pip install gdown","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-09-10T02:03:30.686470Z","iopub.execute_input":"2024-09-10T02:03:30.686999Z","iopub.status.idle":"2024-09-10T02:04:14.808130Z","shell.execute_reply.started":"2024-09-10T02:03:30.686941Z","shell.execute_reply":"2024-09-10T02:04:14.806536Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!git clone https://github.com/phandat128/H-M-Fashion-RecSys","metadata":{"execution":{"iopub.status.busy":"2024-09-10T02:32:14.974665Z","iopub.execute_input":"2024-09-10T02:32:14.975622Z","iopub.status.idle":"2024-09-10T02:32:16.103299Z","shell.execute_reply.started":"2024-09-10T02:32:14.975564Z","shell.execute_reply":"2024-09-10T02:32:16.102018Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%cd /kaggle/working/H-M-Fashion-RecSys","metadata":{"execution":{"iopub.status.busy":"2024-09-10T09:18:23.548165Z","iopub.execute_input":"2024-09-10T09:18:23.548752Z","iopub.status.idle":"2024-09-10T09:18:23.557837Z","shell.execute_reply.started":"2024-09-10T09:18:23.548701Z","shell.execute_reply":"2024-09-10T09:18:23.556341Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!cp -r /kaggle/input/notebook439d133a79/H-M-Fashion-RecSys/data ./","metadata":{"execution":{"iopub.status.busy":"2024-09-10T02:04:17.692868Z","iopub.execute_input":"2024-09-10T02:04:17.693426Z","iopub.status.idle":"2024-09-10T02:05:04.996456Z","shell.execute_reply.started":"2024-09-10T02:04:17.693384Z","shell.execute_reply":"2024-09-10T02:05:04.994792Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!gdown 13rGRbevjcd0yZdwuOTPmNyMOIx9WOLb9\n!gdown 13nkDc7Dt6QtXx91i3sjnotQNGX2JpSk_\n!gdown 11Q8nWxOlSTspQwH9OGmR9vGoAqJ2wWbS\n!gdown 11OX9vuHmCrCk8Mcl6XA1TF0l0nBL___j\n!gdown 1-8spKOVtb0jr5xYT8oMKMC5z3BPpCOU-\n!gdown 1-6CAnA2_pHXrhCyplV-WsI9lreSf6Rm-","metadata":{"execution":{"iopub.status.busy":"2024-09-10T02:05:04.998867Z","iopub.execute_input":"2024-09-10T02:05:04.999352Z","iopub.status.idle":"2024-09-10T02:07:18.194467Z","shell.execute_reply.started":"2024-09-10T02:05:04.999301Z","shell.execute_reply":"2024-09-10T02:07:18.192586Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nfrom pandas.api.types import CategoricalDtype\nimport numpy as np\nimport seaborn as sns\nimport matplotlib.pyplot as plt\nimport lightgbm as lgb\n\nimport pickle\nfrom tqdm import tqdm\nimport gc\nfrom pathlib import Path","metadata":{"execution":{"iopub.status.busy":"2024-09-10T09:05:14.159924Z","iopub.execute_input":"2024-09-10T09:05:14.160548Z","iopub.status.idle":"2024-09-10T09:05:14.169767Z","shell.execute_reply.started":"2024-09-10T09:05:14.160493Z","shell.execute_reply":"2024-09-10T09:05:14.167855Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import warnings\nimport sys\nfrom IPython.core.interactiveshell import InteractiveShell\n\nwarnings.filterwarnings(\"ignore\")","metadata":{"execution":{"iopub.status.busy":"2024-09-10T09:05:15.934029Z","iopub.execute_input":"2024-09-10T09:05:15.934621Z","iopub.status.idle":"2024-09-10T09:05:15.942485Z","shell.execute_reply.started":"2024-09-10T09:05:15.934570Z","shell.execute_reply":"2024-09-10T09:05:15.940613Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from src.data import DataHelper\nfrom src.data.metrics import map_at_k, hr_at_k, recall_at_k\n\nfrom src.retrieval.rules import (\n    OrderHistory,\n    OrderHistoryDecay,\n    ItemPair,\n    UserGroupTimeHistory,\n    UserGroupSaleTrend,\n    TimeHistory,\n    TimeHistoryDecay,\n    SaleTrend,\n    OutOfStock,\n)\nfrom src.retrieval.collector import RuleCollector\n\nfrom src.features import full_sale, week_sale, repurchase_ratio, popularity, period_sale\n\nfrom src.utils import (\n    calc_valid_date,\n    merge_week_data,\n    reduce_mem_usage,\n    calc_embd_similarity,\n)","metadata":{"execution":{"iopub.status.busy":"2024-09-10T09:05:17.684610Z","iopub.execute_input":"2024-09-10T09:05:17.685350Z","iopub.status.idle":"2024-09-10T09:05:17.694678Z","shell.execute_reply.started":"2024-09-10T09:05:17.685284Z","shell.execute_reply":"2024-09-10T09:05:17.692515Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_dir = Path(\"./data/\")\nmodel_dir = Path(\"./models/\")","metadata":{"execution":{"iopub.status.busy":"2024-09-10T09:05:20.153063Z","iopub.execute_input":"2024-09-10T09:05:20.153635Z","iopub.status.idle":"2024-09-10T09:05:20.160458Z","shell.execute_reply.started":"2024-09-10T09:05:20.153584Z","shell.execute_reply":"2024-09-10T09:05:20.158847Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TRAIN_WEEK_NUM = 4\nWEEK_NUM = TRAIN_WEEK_NUM + 2\n\nVERSION_NAME = \"Recall 1\"\nTEST = True # * Set as `False` when do local experiments to save time","metadata":{"execution":{"iopub.status.busy":"2024-09-10T09:05:21.362497Z","iopub.execute_input":"2024-09-10T09:05:21.364309Z","iopub.status.idle":"2024-09-10T09:05:21.374003Z","shell.execute_reply.started":"2024-09-10T09:05:21.364233Z","shell.execute_reply":"2024-09-10T09:05:21.371740Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\n# if not os.path.exists(data_dir/\"interim\"/VERSION_NAME):\n#     os.makedirs(data_dir/\"interim\"/VERSION_NAME)\n# if not os.path.exists(data_dir/\"processed\"/VERSION_NAME):\n#     os.makedirs(data_dir/\"processed\"/VERSION_NAME)","metadata":{"execution":{"iopub.status.busy":"2024-09-10T09:05:23.118054Z","iopub.execute_input":"2024-09-10T09:05:23.118669Z","iopub.status.idle":"2024-09-10T09:05:23.129974Z","shell.execute_reply.started":"2024-09-10T09:05:23.118600Z","shell.execute_reply":"2024-09-10T09:05:23.128519Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dh = DataHelper(data_dir)","metadata":{"execution":{"iopub.status.busy":"2024-09-10T09:05:24.893284Z","iopub.execute_input":"2024-09-10T09:05:24.893876Z","iopub.status.idle":"2024-09-10T09:05:24.900407Z","shell.execute_reply.started":"2024-09-10T09:05:24.893825Z","shell.execute_reply":"2024-09-10T09:05:24.898667Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = dh.load_data(name=\"encoded_full\")","metadata":{"execution":{"iopub.status.busy":"2024-09-10T09:05:26.563437Z","iopub.execute_input":"2024-09-10T09:05:26.564016Z","iopub.status.idle":"2024-09-10T09:05:29.903880Z","shell.execute_reply.started":"2024-09-10T09:05:26.563963Z","shell.execute_reply":"2024-09-10T09:05:29.902382Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# uid2idx = pickle.load(open(data_dir/\"index_id_map/user_id2index.pkl\", \"rb\"))\n# submission = pd.read_csv(data_dir/\"raw\"/'sample_submission.csv')\n# submission['customer_id'] = submission['customer_id'].map(uid2idx)","metadata":{"execution":{"iopub.status.busy":"2024-09-10T09:05:29.906813Z","iopub.execute_input":"2024-09-10T09:05:29.907272Z","iopub.status.idle":"2024-09-10T09:05:29.913687Z","shell.execute_reply.started":"2024-09-10T09:05:29.907226Z","shell.execute_reply":"2024-09-10T09:05:29.912158Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Merged_data","metadata":{}},{"cell_type":"code","source":"inter = pd.read_parquet(data_dir / \"processed/processed_inter.pqt\")\ninter = inter[inter['week'] <= WEEK_NUM + 2]","metadata":{"execution":{"iopub.status.busy":"2024-09-10T09:18:34.486274Z","iopub.execute_input":"2024-09-10T09:18:34.486840Z","iopub.status.idle":"2024-09-10T09:18:35.852045Z","shell.execute_reply.started":"2024-09-10T09:18:34.486788Z","shell.execute_reply":"2024-09-10T09:18:35.850725Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#* embeddings from DSSM model\n# dssm_user_embd = np.load(\"/kaggle/working/H-M-Fashion-RecSys/dssm_user_embd.npy\", allow_pickle=True)\n# dssm_item_embd = np.load(\"/kaggle/working/H-M-Fashion-RecSys/dssm_item_embd.npy\", allow_pickle=True)\n# # * embeddings from YouTubeDNN model\n# yt_user_embd = np.load(\"/kaggle/working/H-M-Fashion-RecSys/yt_user_embd.npy\", allow_pickle=True)\n# yt_item_embd = np.load(\"/kaggle/working/H-M-Fashion-RecSys/yt_item_embd.npy\", allow_pickle=True)\n# # * embeddings from Word2Vector model\n# w2v_user_embd = np.load(\"/kaggle/working/H-M-Fashion-RecSys/w2v_user_embd.npy\", allow_pickle=True)\n# w2v_item_embd = np.load(\"/kaggle/working/H-M-Fashion-RecSys/w2v_item_embd.npy\", allow_pickle=True)","metadata":{"execution":{"iopub.status.busy":"2024-09-10T09:05:32.285225Z","iopub.execute_input":"2024-09-10T09:05:32.285748Z","iopub.status.idle":"2024-09-10T09:05:32.291685Z","shell.execute_reply.started":"2024-09-10T09:05:32.285699Z","shell.execute_reply":"2024-09-10T09:05:32.290219Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for col in inter.columns:\n    inter[col] = np.nan_to_num(inter[col])","metadata":{"execution":{"iopub.status.busy":"2024-09-10T09:18:38.376707Z","iopub.execute_input":"2024-09-10T09:18:38.377252Z","iopub.status.idle":"2024-09-10T09:18:39.090354Z","shell.execute_reply.started":"2024-09-10T09:18:38.377200Z","shell.execute_reply":"2024-09-10T09:18:39.088777Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# for i in tqdm(range(WEEK_NUM)):\n#     if i == 0 and not TEST:\n#         continue\n#     candidate = pd.read_parquet(data_dir/\"interim\"/VERSION_NAME/f\"week{i}_candidate.pqt\")\n#     if i == 0:\n#         chunk_size = int(candidate.shape[0] * 0.01)\n#         for chunk,batch in enumerate(range(0, candidate.shape[0], chunk_size)):\n#             sub_candidate = candidate.iloc[batch:batch+chunk_size-1]\n#             # * merge features\n#             sub_candidate = merge_week_data(data, inter, i, sub_candidate)\n#             sub_candidate['article_id'] = sub_candidate['article_id'].astype(int)\n#             sub_candidate['customer_id'] = sub_candidate['customer_id'].astype(int)\n#             # * merge DSSM user and item embeddings\n#             sub_candidate[\"dssm_similarity\"] = calc_embd_similarity(sub_candidate, dssm_user_embd, dssm_item_embd)\n#             # * merge YouTubeDNN user and item embeddings\n#             sub_candidate[\"yt_similarity\"] = calc_embd_similarity(sub_candidate, yt_user_embd, yt_item_embd)\n#             # * merge Word2Vector user and item embeddings\n#             sub_candidate[\"wv_similarity\"] = calc_embd_similarity(sub_candidate, w2v_user_embd, w2v_item_embd, sub=False)\n#             print(f\"Chunk {chunk} done...\")\n#             sub_candidate.to_parquet(data_dir/\"processed\"/VERSION_NAME/f\"week{i}_candidate_{chunk}.pqt\")\n#     else:\n#         # * merge features\n#         candidate = merge_week_data(data, inter, i, candidate)\n#         print(candidate['week'].unique())\n#         # * merge DSSM user and item embeddings\n#         candidate[\"dssm_similarity\"] = calc_embd_similarity(candidate, dssm_user_embd, dssm_item_embd)\n#         # * merge YouTubeDNN user and item embeddings\n#         candidate[\"yt_similarity\"] = calc_embd_similarity(candidate, yt_user_embd, yt_item_embd)\n#         candidate[\"wv_similarity\"] = calc_embd_similarity(candidate, w2v_user_embd, w2v_item_embd, sub=False)\n#     candidate.to_parquet(data_dir/\"processed\"/VERSION_NAME/f\"week{i}_candidate.pqt\")","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2024-09-10T09:05:37.925720Z","iopub.execute_input":"2024-09-10T09:05:37.930211Z","iopub.status.idle":"2024-09-10T09:05:37.947696Z","shell.execute_reply.started":"2024-09-10T09:05:37.930108Z","shell.execute_reply":"2024-09-10T09:05:37.945834Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# del dssm_user_embd, dssm_item_embd, yt_user_embd, yt_item_embd\n# gc.collect()","metadata":{"execution":{"iopub.status.busy":"2024-09-10T09:05:40.277131Z","iopub.execute_input":"2024-09-10T09:05:40.277719Z","iopub.status.idle":"2024-09-10T09:05:40.288033Z","shell.execute_reply.started":"2024-09-10T09:05:40.277659Z","shell.execute_reply":"2024-09-10T09:05:40.286223Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Ranking","metadata":{}},{"cell_type":"code","source":"candidates = {}\nlabels = {}\nfor i in tqdm(range(1, WEEK_NUM)):\n    candidates[i] = pd.read_parquet(data_dir/\"processed\"/VERSION_NAME/f\"week{i}_candidate.pqt\")\n    labels[i] = pd.read_parquet(data_dir/\"processed\"/VERSION_NAME/f\"week{i}_label.pqt\")","metadata":{"execution":{"iopub.status.busy":"2024-09-10T09:18:51.266890Z","iopub.execute_input":"2024-09-10T09:18:51.267475Z","iopub.status.idle":"2024-09-10T09:19:00.245243Z","shell.execute_reply.started":"2024-09-10T09:18:51.267424Z","shell.execute_reply":"2024-09-10T09:19:00.243741Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"feats = [\n    x\n    for x in candidates[1].columns\n    if x\n    not in [\n        \"label\",\n        \"sales_channel_id\",\n        \"t_dat\",\n        \"week\",\n    ]\n]\ncat_features = [\n    \"customer_id\",\n    \"article_id\",\n    \"product_code\",\n    \"FN\",\n    \"Active\",\n    \"club_member_status\",\n    \"fashion_news_frequency\",\n    \"age\",\n    \"product_type_no\",\n    \"product_group_name\",\n    \"graphical_appearance_no\",\n    \"colour_group_code\",\n    \"perceived_colour_value_id\",\n    \"perceived_colour_master_id\",\n\n    \"user_gender\",\n    \"article_gender\",\n    \"season_type\"\n]","metadata":{"execution":{"iopub.status.busy":"2024-09-10T09:19:00.247963Z","iopub.execute_input":"2024-09-10T09:19:00.248547Z","iopub.status.idle":"2024-09-10T09:19:00.256261Z","shell.execute_reply.started":"2024-09-10T09:19:00.248489Z","shell.execute_reply":"2024-09-10T09:19:00.254819Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cate_dict = {}\nfor feat in tqdm(cat_features):\n    if feat in data['user'].columns:\n        value_set = set(data['user'][feat].unique())\n    elif feat in data['item'].columns:\n        value_set = set(data['item'][feat].unique())\n    else:\n        value_set = set(data['inter'][feat].unique())\n    cate_dict[feat] = CategoricalDtype(categories=value_set)","metadata":{"execution":{"iopub.status.busy":"2024-09-10T09:19:15.738669Z","iopub.execute_input":"2024-09-10T09:19:15.739252Z","iopub.status.idle":"2024-09-10T09:19:19.588764Z","shell.execute_reply.started":"2024-09-10T09:19:15.739202Z","shell.execute_reply":"2024-09-10T09:19:19.587178Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"full_data = pd.concat([candidates[i] for i in range(1, WEEK_NUM)], ignore_index=True)","metadata":{"execution":{"iopub.status.busy":"2024-09-10T09:06:15.103439Z","iopub.execute_input":"2024-09-10T09:06:15.104752Z","iopub.status.idle":"2024-09-10T09:06:17.802795Z","shell.execute_reply.started":"2024-09-10T09:06:15.104690Z","shell.execute_reply":"2024-09-10T09:06:17.801383Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"inter = data['inter']\ninter = inter[inter['t_dat']<'2020-08-19'] # * start date of the last valid week\ninter['week'] = (pd.to_datetime('2020-09-29') - pd.to_datetime(inter['t_dat'])).dt.days // 7\ninter = inter.merge(data['item'][[\"article_id\", \"product_code\"]], on=\"article_id\", how=\"left\")","metadata":{"execution":{"iopub.status.busy":"2024-09-10T09:19:19.590786Z","iopub.execute_input":"2024-09-10T09:19:19.591248Z","iopub.status.idle":"2024-09-10T09:19:33.488929Z","shell.execute_reply.started":"2024-09-10T09:19:19.591203Z","shell.execute_reply":"2024-09-10T09:19:33.487339Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tmp = inter.groupby('article_id').week.mean()\nfull_data['article_time_mean'] = full_data['article_id'].map(tmp)\n\ntmp = inter.groupby('customer_id').week.nth(-1)\nfull_data['customer_id_last_time'] = full_data['customer_id'].map(tmp)\n\ntmp = inter.groupby('customer_id').week.nth(0)\nfull_data['customer_id_first_time'] = full_data['customer_id'].map(tmp)\n\ntmp = inter.groupby('customer_id').week.mean()\nfull_data['customer_id_time_mean'] = full_data['customer_id'].map(tmp)\n\nfull_data['customer_id_gap'] = full_data['customer_id_first_time'] - full_data['customer_id_last_time']","metadata":{"execution":{"iopub.status.busy":"2024-09-10T09:07:56.212066Z","iopub.execute_input":"2024-09-10T09:07:56.212593Z","iopub.status.idle":"2024-09-10T09:08:12.279115Z","shell.execute_reply.started":"2024-09-10T09:07:56.212547Z","shell.execute_reply":"2024-09-10T09:08:12.277366Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"feats += [\n    'article_time_mean',\n    'customer_id_last_time',\n    'customer_id_first_time',\n    'customer_id_time_mean',\n    'customer_id_gap'\n]","metadata":{"execution":{"iopub.status.busy":"2024-09-10T09:19:33.491424Z","iopub.execute_input":"2024-09-10T09:19:33.491881Z","iopub.status.idle":"2024-09-10T09:19:33.497845Z","shell.execute_reply.started":"2024-09-10T09:19:33.491837Z","shell.execute_reply":"2024-09-10T09:19:33.496349Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del tmp\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2024-09-10T09:08:22.892575Z","iopub.execute_input":"2024-09-10T09:08:22.893128Z","iopub.status.idle":"2024-09-10T09:08:23.623352Z","shell.execute_reply.started":"2024-09-10T09:08:22.893081Z","shell.execute_reply":"2024-09-10T09:08:23.621672Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for feat in tqdm(cat_features):\n    full_data[feat] = full_data[feat].astype(cate_dict[feat])","metadata":{"execution":{"iopub.status.busy":"2024-09-10T09:08:25.708857Z","iopub.execute_input":"2024-09-10T09:08:25.709850Z","iopub.status.idle":"2024-09-10T09:08:27.649979Z","shell.execute_reply.started":"2024-09-10T09:08:25.709798Z","shell.execute_reply":"2024-09-10T09:08:27.648576Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = full_data.loc[full_data['week']>1]\nvalid = full_data.loc[full_data['week']==1]\n\ndel full_data\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2024-09-10T09:08:47.762876Z","iopub.execute_input":"2024-09-10T09:08:47.763478Z","iopub.status.idle":"2024-09-10T09:08:57.132481Z","shell.execute_reply.started":"2024-09-10T09:08:47.763429Z","shell.execute_reply":"2024-09-10T09:08:57.131011Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del inter","metadata":{"execution":{"iopub.status.busy":"2024-09-10T09:08:57.134574Z","iopub.execute_input":"2024-09-10T09:08:57.135027Z","iopub.status.idle":"2024-09-10T09:08:57.281470Z","shell.execute_reply.started":"2024-09-10T09:08:57.134984Z","shell.execute_reply":"2024-09-10T09:08:57.279912Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def train_rank_model(train, valid, train_group, valid_group):\n\n    train_set = lgb.Dataset(\n        data=train[feats],\n        label=train[\"label\"],\n        group=train_group,\n        feature_name=feats,\n        categorical_feature=cat_features,\n        params=params\n    )\n\n    valid_set = lgb.Dataset(\n        data=valid[feats],\n        label=valid[\"label\"],\n        group=valid_group,\n        feature_name=feats,\n        categorical_feature=cat_features,\n        params=params\n    )\n\n    ranker = lgb.train(\n        params,\n        train_set,\n        num_boost_round=300,\n        valid_sets=[valid_set],\n        early_stopping_rounds=30,\n        verbose_eval=10,\n    )\n    ranker.save_model(\n        model_dir / f\"lgb_small_ranker.model\",\n        num_iteration=ranker.best_iteration,\n    )\n    return ranker","metadata":{"execution":{"iopub.status.busy":"2024-09-10T03:38:18.843957Z","iopub.execute_input":"2024-09-10T03:38:18.844501Z","iopub.status.idle":"2024-09-10T03:38:18.853291Z","shell.execute_reply.started":"2024-09-10T03:38:18.844454Z","shell.execute_reply":"2024-09-10T03:38:18.851493Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def train_binary_model(train, valid):\n\n    train_set = lgb.Dataset(\n        data=train[feats],\n        label=train[\"label\"],\n        feature_name=feats,\n        categorical_feature=cat_features,\n        params=params,\n    )\n\n    valid_set = lgb.Dataset(\n        data=valid[feats],\n        label=valid[\"label\"],\n        feature_name=feats,\n        categorical_feature=cat_features,\n        params=params,\n    )\n\n    ranker = lgb.train(\n        params,\n        train_set,\n        num_boost_round=300,\n        valid_sets=[valid_set],\n        early_stopping_rounds=30,\n        verbose_eval=10,\n    )\n    ranker.save_model(\n        model_dir / f\"lgb_small_binary.model\",\n        num_iteration=ranker.best_iteration,\n    )\n    return ranker","metadata":{"execution":{"iopub.status.busy":"2024-09-10T09:09:01.506705Z","iopub.execute_input":"2024-09-10T09:09:01.507479Z","iopub.status.idle":"2024-09-10T09:09:01.529708Z","shell.execute_reply.started":"2024-09-10T09:09:01.507417Z","shell.execute_reply":"2024-09-10T09:09:01.526801Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"params = {\n    \"objective\": \"binary\",#\"lambdarank\",\n    \"boosting_type\": \"gbdt\",\n    \"metric\": \"auc\",#\"map\",\n    \"max_depth\": 8,\n    \"num_leaves\": 128,\n    \"learning_rate\": 0.03,\n    \"max bin\": 63,\n    \"verbose\": -1,\n    \"eval_at\": 12,\n    \"min_data_in_bin\": 1,\n    \"min_data_in_leaf\": 10,\n    \"bagging_fraction\": 0.8,\n    \"bagging_freq\": 1\n}","metadata":{"execution":{"iopub.status.busy":"2024-09-10T09:09:03.538100Z","iopub.execute_input":"2024-09-10T09:09:03.538684Z","iopub.status.idle":"2024-09-10T09:09:03.546124Z","shell.execute_reply.started":"2024-09-10T09:09:03.538615Z","shell.execute_reply":"2024-09-10T09:09:03.544687Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del candidates\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2024-09-10T09:09:06.066905Z","iopub.execute_input":"2024-09-10T09:09:06.067494Z","iopub.status.idle":"2024-09-10T09:09:06.600287Z","shell.execute_reply.started":"2024-09-10T09:09:06.067439Z","shell.execute_reply":"2024-09-10T09:09:06.599001Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Train positive rate:\", train.label.mean())","metadata":{"execution":{"iopub.status.busy":"2024-09-10T09:09:08.590209Z","iopub.execute_input":"2024-09-10T09:09:08.590805Z","iopub.status.idle":"2024-09-10T09:09:08.608291Z","shell.execute_reply.started":"2024-09-10T09:09:08.590751Z","shell.execute_reply":"2024-09-10T09:09:08.605883Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = train.sort_values(by=[\"week\", \"customer_id\"], ascending=True).reset_index(drop=True)\nvalid = valid.sort_values(by=[\"customer_id\"], ascending=True).reset_index(drop=True)","metadata":{"execution":{"iopub.status.busy":"2024-09-10T09:09:10.855001Z","iopub.execute_input":"2024-09-10T09:09:10.855535Z","iopub.status.idle":"2024-09-10T09:09:19.309569Z","shell.execute_reply.started":"2024-09-10T09:09:10.855486Z","shell.execute_reply":"2024-09-10T09:09:19.308108Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_group = train[[\"customer_id\", \"article_id\", \"week\"]]\ntrain_group = train_group.astype(\"int32\")  # * convert to int to avoid `0` in groupby count result\ntrain_group = (train_group.groupby([\"week\", \"customer_id\"]).size().values)","metadata":{"execution":{"iopub.status.busy":"2024-09-10T07:32:42.140191Z","iopub.execute_input":"2024-09-10T07:32:42.140724Z","iopub.status.idle":"2024-09-10T07:32:42.883513Z","shell.execute_reply.started":"2024-09-10T07:32:42.140679Z","shell.execute_reply":"2024-09-10T07:32:42.882189Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.columns.shape","metadata":{"execution":{"iopub.status.busy":"2024-09-10T08:38:46.001339Z","iopub.execute_input":"2024-09-10T08:38:46.001955Z","iopub.status.idle":"2024-09-10T08:38:46.011724Z","shell.execute_reply.started":"2024-09-10T08:38:46.001899Z","shell.execute_reply":"2024-09-10T08:38:46.010223Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = train[feats+['label']]\nvalid = valid[feats+['label']]","metadata":{"execution":{"iopub.status.busy":"2024-09-10T08:56:23.658905Z","iopub.execute_input":"2024-09-10T08:56:23.659368Z","iopub.status.idle":"2024-09-10T08:56:25.670433Z","shell.execute_reply.started":"2024-09-10T08:56:23.659284Z","shell.execute_reply":"2024-09-10T08:56:25.668855Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# ranker = train_rank_model(train, valid, train_group, valid_group)\nranker = train_binary_model(train, valid)","metadata":{"execution":{"iopub.status.busy":"2024-09-10T08:26:53.041002Z","iopub.execute_input":"2024-09-10T08:26:53.041580Z","iopub.status.idle":"2024-09-10T08:37:31.884759Z","shell.execute_reply.started":"2024-09-10T08:26:53.041522Z","shell.execute_reply":"2024-09-10T08:37:31.883310Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ranker = lgb.Booster(model_file=model_dir / \"lgb_small_binary.model\")\n# ranker = lgb.Booster(model_file=model_dir / \"lgb_small_ranker.model\")","metadata":{"execution":{"iopub.status.busy":"2024-09-10T07:58:44.363851Z","iopub.execute_input":"2024-09-10T07:58:44.365399Z","iopub.status.idle":"2024-09-10T07:58:45.944371Z","shell.execute_reply.started":"2024-09-10T07:58:44.365086Z","shell.execute_reply":"2024-09-10T07:58:45.940061Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"feat_importance = pd.DataFrame(\n    {\"feature\": feats, \"importance\": ranker.feature_importance()}\n).sort_values(by=\"importance\", ascending=False)\nplt.figure(figsize=(8, 22))\nsns.barplot(y=\"feature\", x=\"importance\", data=feat_importance)","metadata":{"execution":{"iopub.status.busy":"2024-09-10T07:58:45.968717Z","iopub.execute_input":"2024-09-10T07:58:45.970476Z","iopub.status.idle":"2024-09-10T07:58:52.691958Z","shell.execute_reply.started":"2024-09-10T07:58:45.970281Z","shell.execute_reply":"2024-09-10T07:58:52.686827Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_candidates = valid.reset_index(drop=True)","metadata":{"execution":{"iopub.status.busy":"2024-09-10T07:59:32.531038Z","iopub.execute_input":"2024-09-10T07:59:32.531672Z","iopub.status.idle":"2024-09-10T07:59:34.431874Z","shell.execute_reply.started":"2024-09-10T07:59:32.531598Z","shell.execute_reply":"2024-09-10T07:59:34.430442Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def predict(ranker, candidates, batch_size = 5_000_000):\n    probs = np.zeros(candidates.shape[0])\n    for batch in range(0, candidates.shape[0], batch_size):\n        outputs = ranker.predict(candidates.loc[batch : batch + batch_size - 1, feats])\n        probs[batch : batch + batch_size] = outputs\n    candidates[\"prob\"] = probs\n    pred_lgb = candidates[['customer_id','article_id','prob']]\n    pred_lgb = pred_lgb.sort_values(by=[\"customer_id\",\"prob\"], ascending=False).reset_index(drop=True)\n    pred_lgb.rename(columns={'article_id':'prediction'}, inplace=True)\n    pred_lgb = pred_lgb.drop_duplicates(['customer_id', 'prediction'], keep='first')\n    pred_lgb['customer_id'] = pred_lgb['customer_id'].astype(int)\n    pred_lgb = pred_lgb.groupby(\"customer_id\")[\"prediction\"].progress_apply(list).reset_index()\n    return pred_lgb","metadata":{"execution":{"iopub.status.busy":"2024-09-10T07:59:49.855163Z","iopub.execute_input":"2024-09-10T07:59:49.855786Z","iopub.status.idle":"2024-09-10T07:59:49.865600Z","shell.execute_reply.started":"2024-09-10T07:59:49.855724Z","shell.execute_reply":"2024-09-10T07:59:49.864024Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tqdm import tqdm\ntqdm.pandas()","metadata":{"execution":{"iopub.status.busy":"2024-09-10T08:00:34.244039Z","iopub.execute_input":"2024-09-10T08:00:34.245289Z","iopub.status.idle":"2024-09-10T08:00:34.253316Z","shell.execute_reply.started":"2024-09-10T08:00:34.245217Z","shell.execute_reply":"2024-09-10T08:00:34.251229Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred = predict(ranker, val_candidates)","metadata":{"execution":{"iopub.status.busy":"2024-09-10T08:00:36.415525Z","iopub.execute_input":"2024-09-10T08:00:36.416128Z","iopub.status.idle":"2024-09-10T08:01:13.464541Z","shell.execute_reply.started":"2024-09-10T08:00:36.416065Z","shell.execute_reply":"2024-09-10T08:01:13.463146Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label = labels[1]\nlabel = pd.merge(label, pred, on=\"customer_id\", how=\"left\")","metadata":{"execution":{"iopub.status.busy":"2024-09-10T08:01:14.403484Z","iopub.execute_input":"2024-09-10T08:01:14.404609Z","iopub.status.idle":"2024-09-10T08:01:14.435054Z","shell.execute_reply.started":"2024-09-10T08:01:14.404545Z","shell.execute_reply":"2024-09-10T08:01:14.433603Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"map_at_k(label[\"article_id\"], label[\"prediction\"], k=12)\n","metadata":{"execution":{"iopub.status.busy":"2024-09-10T08:01:16.086621Z","iopub.execute_input":"2024-09-10T08:01:16.087246Z","iopub.status.idle":"2024-09-10T08:01:19.287320Z","shell.execute_reply.started":"2024-09-10T08:01:16.087188Z","shell.execute_reply":"2024-09-10T08:01:19.285941Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"batch_size = 5_000_000\nprobs = np.zeros(val_candidates.shape[0])\nfor batch in range(0, val_candidates.shape[0], batch_size):\n    outputs = ranker.predict(val_candidates.loc[batch : batch + batch_size - 1, feats])\n    probs[batch : batch + batch_size] = outputs\nval_candidates[\"prob\"] = probs\npred_lgb = val_candidates[['customer_id','article_id','prob']]\npred_lgb = pred_lgb.sort_values(by=[\"customer_id\",\"prob\"], ascending=False).reset_index(drop=True)\npred_lgb.rename(columns={'article_id':'prediction'}, inplace=True)\npred_lgb = pred_lgb.drop_duplicates(['customer_id', 'prediction'], keep='first')\npred_lgb['customer_id'] = pred_lgb['customer_id'].astype(int)","metadata":{"execution":{"iopub.status.busy":"2024-09-10T08:01:43.400790Z","iopub.execute_input":"2024-09-10T08:01:43.401797Z","iopub.status.idle":"2024-09-10T08:02:19.724461Z","shell.execute_reply.started":"2024-09-10T08:01:43.401733Z","shell.execute_reply":"2024-09-10T08:02:19.722853Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred_lgb.to_parquet(data_dir/\"processed\"/\"small_binary_valid.pqt\")","metadata":{"execution":{"iopub.status.busy":"2024-09-10T08:02:19.726746Z","iopub.execute_input":"2024-09-10T08:02:19.727266Z","iopub.status.idle":"2024-09-10T08:02:20.388750Z","shell.execute_reply.started":"2024-09-10T08:02:19.727215Z","shell.execute_reply":"2024-09-10T08:02:20.387412Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(feats)","metadata":{"execution":{"iopub.status.busy":"2024-09-10T09:19:48.315732Z","iopub.execute_input":"2024-09-10T09:19:48.316285Z","iopub.status.idle":"2024-09-10T09:19:48.325932Z","shell.execute_reply.started":"2024-09-10T09:19:48.316238Z","shell.execute_reply":"2024-09-10T09:19:48.324145Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_pred = []\nfor chunk in range(100):\n    print(f\"Chunk {chunk}\")\n    test_candidates = pd.read_parquet(data_dir/\"processed\"/VERSION_NAME/f\"week0_candidate_{chunk}.pqt\")\n    for feat in cat_features:\n        test_candidates[feat] = test_candidates[feat].astype(cate_dict[feat])\n\n    # * Extra Features ===================================\n\n    tmp = inter.groupby('article_id').week.mean()\n    test_candidates['article_time_mean'] = test_candidates['article_id'].map(tmp)\n\n    tmp = inter.groupby('customer_id').week.nth(-1)\n    test_candidates['customer_id_last_time'] = test_candidates['customer_id'].map(tmp)\n\n    tmp = inter.groupby('customer_id').week.nth(0)\n    test_candidates['customer_id_first_time'] = test_candidates['customer_id'].map(tmp)\n\n    tmp = inter.groupby('customer_id').week.mean()\n    test_candidates['customer_id_time_mean'] = test_candidates['customer_id'].map(tmp)\n\n    test_candidates['customer_id_gap'] = test_candidates['customer_id_first_time'] - test_candidates['customer_id_last_time']\n\n    gc.collect()\n    # * ==================================================\n\n    batch_size = 5_000_000\n    probs = np.zeros(test_candidates.shape[0])\n    for batch in tqdm(range(0, test_candidates.shape[0], batch_size)):\n        outputs = ranker.predict(test_candidates.loc[batch : batch + batch_size - 1, feats])\n        probs[batch : batch + batch_size] = outputs\n    test_candidates[\"prob\"] = probs\n    pred_lgb = test_candidates[['customer_id','article_id','prob']]\n    pred_lgb = pred_lgb.sort_values(by=[\"customer_id\",\"prob\"], ascending=False).reset_index(drop=True)\n    pred_lgb.rename(columns={'article_id':'prediction'}, inplace=True)\n    pred_lgb = pred_lgb.drop_duplicates(['customer_id', 'prediction'], keep='first')\n    pred_lgb['customer_id'] = pred_lgb['customer_id'].astype(int)\n    test_pred.append(pred_lgb)\n    del test_candidates\n    gc.collect()","metadata":{"execution":{"iopub.status.busy":"2024-09-10T09:19:59.295323Z","iopub.execute_input":"2024-09-10T09:19:59.295877Z","iopub.status.idle":"2024-09-10T09:52:54.051624Z","shell.execute_reply.started":"2024-09-10T09:19:59.295827Z","shell.execute_reply":"2024-09-10T09:52:54.050172Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred_lgb = pd.concat(test_pred, ignore_index=True)","metadata":{"execution":{"iopub.status.busy":"2024-09-10T09:52:54.053954Z","iopub.execute_input":"2024-09-10T09:52:54.054392Z","iopub.status.idle":"2024-09-10T09:52:54.608926Z","shell.execute_reply.started":"2024-09-10T09:52:54.054347Z","shell.execute_reply":"2024-09-10T09:52:54.607721Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred_lgb.to_parquet(data_dir/\"processed\"/\"small_binary_test.pqt\")","metadata":{"execution":{"iopub.status.busy":"2024-09-10T09:53:48.011839Z","iopub.execute_input":"2024-09-10T09:53:48.012474Z","iopub.status.idle":"2024-09-10T09:53:56.791885Z","shell.execute_reply.started":"2024-09-10T09:53:48.012422Z","shell.execute_reply":"2024-09-10T09:53:56.789965Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred2_lgb_rank = pd.read_parquet(data_dir/\"processed\"/\"small_binary_test.pqt\")\n","metadata":{"execution":{"iopub.status.busy":"2024-09-10T09:53:58.286219Z","iopub.execute_input":"2024-09-10T09:53:58.287566Z","iopub.status.idle":"2024-09-10T09:54:00.960386Z","shell.execute_reply.started":"2024-09-10T09:53:58.287499Z","shell.execute_reply":"2024-09-10T09:54:00.959119Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred2_lgb_rank   = pred2_lgb_rank.sort_values(by='prob', ascending=False).reset_index(drop=True)","metadata":{"execution":{"iopub.status.busy":"2024-09-10T09:54:00.962540Z","iopub.execute_input":"2024-09-10T09:54:00.963039Z","iopub.status.idle":"2024-09-10T09:54:28.879996Z","shell.execute_reply.started":"2024-09-10T09:54:00.962985Z","shell.execute_reply":"2024-09-10T09:54:28.878570Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred2_lgb_rank   = pred2_lgb_rank.groupby('customer_id')['prediction'].apply(list).reset_index()","metadata":{"execution":{"iopub.status.busy":"2024-09-10T09:54:28.886883Z","iopub.execute_input":"2024-09-10T09:54:28.888088Z","iopub.status.idle":"2024-09-10T09:55:30.584657Z","shell.execute_reply.started":"2024-09-10T09:54:28.888017Z","shell.execute_reply":"2024-09-10T09:55:30.583484Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred2_lgb_rank","metadata":{"execution":{"iopub.status.busy":"2024-09-10T09:55:30.585961Z","iopub.execute_input":"2024-09-10T09:55:30.586365Z","iopub.status.idle":"2024-09-10T09:55:30.611136Z","shell.execute_reply.started":"2024-09-10T09:55:30.586324Z","shell.execute_reply":"2024-09-10T09:55:30.609756Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label = pd.read_parquet(data_dir/\"processed\"/\"Recall 1\"/\"week1_label.pqt\")","metadata":{"execution":{"iopub.status.busy":"2024-09-10T09:55:30.613618Z","iopub.execute_input":"2024-09-10T09:55:30.614069Z","iopub.status.idle":"2024-09-10T09:55:30.668463Z","shell.execute_reply.started":"2024-09-10T09:55:30.614025Z","shell.execute_reply":"2024-09-10T09:55:30.667118Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"idx2uid = pickle.load(open(data_dir/\"index_id_map/user_index2id.pkl\", \"rb\"))\nidx2iid = pickle.load(open(data_dir/\"index_id_map/item_index2id.pkl\", \"rb\"))","metadata":{"execution":{"iopub.status.busy":"2024-09-10T09:55:30.670277Z","iopub.execute_input":"2024-09-10T09:55:30.670763Z","iopub.status.idle":"2024-09-10T09:55:31.341657Z","shell.execute_reply.started":"2024-09-10T09:55:30.670712Z","shell.execute_reply":"2024-09-10T09:55:31.340495Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def parse(x):\n    l = ['0'+str(idx2iid[i]) for i in x]\n    l = ' '.join(l[:12])\n    return l","metadata":{"execution":{"iopub.status.busy":"2024-09-10T09:55:31.342963Z","iopub.execute_input":"2024-09-10T09:55:31.343343Z","iopub.status.idle":"2024-09-10T09:55:31.352277Z","shell.execute_reply.started":"2024-09-10T09:55:31.343304Z","shell.execute_reply":"2024-09-10T09:55:31.351079Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred2_lgb_rank","metadata":{"execution":{"iopub.status.busy":"2024-09-10T09:55:31.353861Z","iopub.execute_input":"2024-09-10T09:55:31.354349Z","iopub.status.idle":"2024-09-10T09:55:31.381293Z","shell.execute_reply.started":"2024-09-10T09:55:31.354307Z","shell.execute_reply":"2024-09-10T09:55:31.379968Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tqdm import tqdm\ntqdm.pandas()","metadata":{"execution":{"iopub.status.busy":"2024-09-10T09:56:30.779284Z","iopub.execute_input":"2024-09-10T09:56:30.779870Z","iopub.status.idle":"2024-09-10T09:56:30.787249Z","shell.execute_reply.started":"2024-09-10T09:56:30.779818Z","shell.execute_reply":"2024-09-10T09:56:30.785889Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred2_lgb_rank['prediction'] = pred2_lgb_rank['prediction'].progress_apply(lambda x: parse(x))","metadata":{"execution":{"iopub.status.busy":"2024-09-10T09:56:33.229906Z","iopub.execute_input":"2024-09-10T09:56:33.230571Z","iopub.status.idle":"2024-09-10T09:56:58.618316Z","shell.execute_reply.started":"2024-09-10T09:56:33.230515Z","shell.execute_reply":"2024-09-10T09:56:58.616836Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"uid2idx = pickle.load(open(data_dir/\"index_id_map/user_id2index.pkl\", \"rb\"))\nsubmission = pd.read_csv(data_dir/\"raw\"/'sample_submission.csv')\nsubmission['customer_id'] = submission['customer_id'].map(uid2idx)","metadata":{"execution":{"iopub.status.busy":"2024-09-10T09:56:58.620579Z","iopub.execute_input":"2024-09-10T09:56:58.621755Z","iopub.status.idle":"2024-09-10T09:57:04.794554Z","shell.execute_reply.started":"2024-09-10T09:56:58.621703Z","shell.execute_reply":"2024-09-10T09:57:04.793233Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nsubmission = submission.merge(pred2_lgb_rank, on='customer_id', how='left')\nsubmission['customer_id'] = submission['customer_id'].map(idx2uid)","metadata":{"execution":{"iopub.status.busy":"2024-09-10T09:57:04.796174Z","iopub.execute_input":"2024-09-10T09:57:04.796612Z","iopub.status.idle":"2024-09-10T09:57:05.592919Z","shell.execute_reply.started":"2024-09-10T09:57:04.796568Z","shell.execute_reply":"2024-09-10T09:57:05.591725Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred2_lgb_rank","metadata":{"execution":{"iopub.status.busy":"2024-09-10T09:57:05.596118Z","iopub.execute_input":"2024-09-10T09:57:05.597025Z","iopub.status.idle":"2024-09-10T09:57:05.612663Z","shell.execute_reply.started":"2024-09-10T09:57:05.596959Z","shell.execute_reply":"2024-09-10T09:57:05.611228Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission","metadata":{"execution":{"iopub.status.busy":"2024-09-10T09:57:05.614708Z","iopub.execute_input":"2024-09-10T09:57:05.615128Z","iopub.status.idle":"2024-09-10T09:57:05.632699Z","shell.execute_reply.started":"2024-09-10T09:57:05.615085Z","shell.execute_reply":"2024-09-10T09:57:05.631374Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = submission[['customer_id', 'prediction_y']]\nsubmission.columns = ['customer_id', 'prediction']","metadata":{"execution":{"iopub.status.busy":"2024-09-10T09:57:05.634111Z","iopub.execute_input":"2024-09-10T09:57:05.634558Z","iopub.status.idle":"2024-09-10T09:57:05.725762Z","shell.execute_reply.started":"2024-09-10T09:57:05.634515Z","shell.execute_reply":"2024-09-10T09:57:05.724465Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('large_recall_binary.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2024-09-10T09:57:05.727290Z","iopub.execute_input":"2024-09-10T09:57:05.727759Z","iopub.status.idle":"2024-09-10T09:57:13.966455Z","shell.execute_reply.started":"2024-09-10T09:57:05.727630Z","shell.execute_reply":"2024-09-10T09:57:13.964858Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.head()","metadata":{"execution":{"iopub.status.busy":"2024-09-10T09:57:13.969503Z","iopub.execute_input":"2024-09-10T09:57:13.969996Z","iopub.status.idle":"2024-09-10T09:57:13.984696Z","shell.execute_reply.started":"2024-09-10T09:57:13.969925Z","shell.execute_reply":"2024-09-10T09:57:13.983184Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}