{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":38760,"databundleVersionId":4493939,"sourceType":"competition"},{"sourceId":4436180,"sourceType":"datasetVersion","datasetId":2597726}],"dockerImageVersionId":30684,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd, numpy as np\n\nimport glob\n\n\nimport warnings\nwarnings.filterwarnings(\"ignore\")\n\nfrom datetime import datetime\n\nfrom tqdm import tqdm\n\ntype_labels = {'clicks':1, 'carts':2, 'orders':3}","metadata":{"execution":{"iopub.status.busy":"2024-04-18T08:15:37.053857Z","iopub.execute_input":"2024-04-18T08:15:37.054236Z","iopub.status.idle":"2024-04-18T08:15:38.334873Z","shell.execute_reply.started":"2024-04-18T08:15:37.054206Z","shell.execute_reply":"2024-04-18T08:15:38.333654Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load(which):    \n    dfs = []\n\n    test_files = glob.glob('/kaggle/input/otto-chunk-data-inparquet-format/'+which+'_parquet/*')\n    \n    for e, chunk_file in enumerate(test_files):\n        chunk = pd.read_parquet(chunk_file)\n        chunk.ts = (chunk.ts/1000).astype('int32')\n        chunk['type'] = chunk['type'].map(type_labels).astype('int8')\n        dfs.append(chunk)\n        \n    return pd.concat(dfs).reset_index(drop=True) #.astype({\"ts\": \"datetime64[ms]\"})","metadata":{"execution":{"iopub.status.busy":"2024-04-18T08:15:38.337337Z","iopub.execute_input":"2024-04-18T08:15:38.338016Z","iopub.status.idle":"2024-04-18T08:15:38.345661Z","shell.execute_reply.started":"2024-04-18T08:15:38.337963Z","shell.execute_reply":"2024-04-18T08:15:38.344634Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = load('train')","metadata":{"execution":{"iopub.status.busy":"2024-04-18T08:15:38.347171Z","iopub.execute_input":"2024-04-18T08:15:38.348218Z","iopub.status.idle":"2024-04-18T08:16:46.371570Z","shell.execute_reply.started":"2024-04-18T08:15:38.348176Z","shell.execute_reply":"2024-04-18T08:16:46.370273Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = train_df[train_df['ts']>=1661385600].reset_index(drop=True)","metadata":{"execution":{"iopub.status.busy":"2024-04-18T08:16:46.375858Z","iopub.execute_input":"2024-04-18T08:16:46.376875Z","iopub.status.idle":"2024-04-18T08:16:48.234205Z","shell.execute_reply.started":"2024-04-18T08:16:46.376839Z","shell.execute_reply":"2024-04-18T08:16:48.233268Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df['datetime'] = train_df['ts'].apply(lambda x: datetime.fromtimestamp(x))","metadata":{"execution":{"iopub.status.busy":"2024-04-18T08:16:48.235927Z","iopub.execute_input":"2024-04-18T08:16:48.236356Z","iopub.status.idle":"2024-04-18T08:18:01.275152Z","shell.execute_reply.started":"2024-04-18T08:16:48.236315Z","shell.execute_reply":"2024-04-18T08:18:01.273532Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df['day'] = train_df['datetime'].dt.day","metadata":{"execution":{"iopub.status.busy":"2024-04-18T08:18:01.277225Z","iopub.execute_input":"2024-04-18T08:18:01.278011Z","iopub.status.idle":"2024-04-18T08:18:02.160301Z","shell.execute_reply.started":"2024-04-18T08:18:01.277965Z","shell.execute_reply":"2024-04-18T08:18:02.159055Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_val_offline = train_df[train_df['day']==28].reset_index(drop=True)\ndf_train_offline = train_df[train_df['day']<28].reset_index(drop=True)","metadata":{"execution":{"iopub.status.busy":"2024-04-18T08:18:02.161863Z","iopub.execute_input":"2024-04-18T08:18:02.162781Z","iopub.status.idle":"2024-04-18T08:18:04.147383Z","shell.execute_reply.started":"2024-04-18T08:18:02.162738Z","shell.execute_reply":"2024-04-18T08:18:04.146260Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train_offline","metadata":{"execution":{"iopub.status.busy":"2024-04-18T08:18:04.149484Z","iopub.execute_input":"2024-04-18T08:18:04.150416Z","iopub.status.idle":"2024-04-18T08:18:04.174387Z","shell.execute_reply.started":"2024-04-18T08:18:04.150372Z","shell.execute_reply":"2024-04-18T08:18:04.173275Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train_offline = df_train_offline.sort_values('ts',ascending=True).reset_index(drop=True)","metadata":{"execution":{"iopub.status.busy":"2024-04-18T08:19:11.050830Z","iopub.execute_input":"2024-04-18T08:19:11.051215Z","iopub.status.idle":"2024-04-18T08:19:15.993824Z","shell.execute_reply.started":"2024-04-18T08:19:11.051180Z","shell.execute_reply":"2024-04-18T08:19:15.992764Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nsession_aids_df = df_train_offline[['session','aid']].groupby('session',as_index=False).agg(list)","metadata":{"execution":{"iopub.status.busy":"2024-04-18T08:19:15.995727Z","iopub.execute_input":"2024-04-18T08:19:15.996071Z","iopub.status.idle":"2024-04-18T08:20:53.349878Z","shell.execute_reply.started":"2024-04-18T08:19:15.996044Z","shell.execute_reply":"2024-04-18T08:20:53.348496Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from collections import defaultdict","metadata":{"execution":{"iopub.status.busy":"2024-04-18T08:20:53.352554Z","iopub.execute_input":"2024-04-18T08:20:53.353039Z","iopub.status.idle":"2024-04-18T08:20:53.357794Z","shell.execute_reply.started":"2024-04-18T08:20:53.352999Z","shell.execute_reply":"2024-04-18T08:20:53.356960Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#drop_duplicated(keep='last')\nsession_aids_df['session_activity'] = session_aids_df['aid'].apply(lambda x: len(x))","metadata":{"execution":{"iopub.status.busy":"2024-04-18T08:20:53.358963Z","iopub.execute_input":"2024-04-18T08:20:53.359859Z","iopub.status.idle":"2024-04-18T08:20:55.340394Z","shell.execute_reply.started":"2024-04-18T08:20:53.359829Z","shell.execute_reply":"2024-04-18T08:20:55.339285Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"session_dict = dict(zip(session_aids_df['session'],session_aids_df['session_activity']))","metadata":{"execution":{"iopub.status.busy":"2024-04-18T08:20:55.342305Z","iopub.execute_input":"2024-04-18T08:20:55.342650Z","iopub.status.idle":"2024-04-18T08:20:56.800487Z","shell.execute_reply.started":"2024-04-18T08:20:55.342623Z","shell.execute_reply":"2024-04-18T08:20:56.799604Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import math","metadata":{"execution":{"iopub.status.busy":"2024-04-18T08:22:04.059068Z","iopub.execute_input":"2024-04-18T08:22:04.059470Z","iopub.status.idle":"2024-04-18T08:22:04.064936Z","shell.execute_reply.started":"2024-04-18T08:22:04.059439Z","shell.execute_reply":"2024-04-18T08:22:04.063601Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"item_Sim = defaultdict()\n\nfor i,row in tqdm( session_aids_df.iterrows(), total=len(session_aids_df) ):\n    \n    aid_list = row['aid']\n    \n    for loc1, item in enumerate(aid_list):\n        \n        item_Sim.setdefault(item,dict())\n        \n        for loc2, related_item in enumerate(aid_list):\n            if item == related_item:\n                continue\n        \n            item_Sim[item].setdefault(related_item,0)\n            \n            if loc1 < loc2:\n                item_Sim[item][related_item] += 1 * 0.7 ** (loc2-loc1-1) * 1/math.log(session_dict[row['session']]+1)\n            else:\n                item_Sim[item][related_item] += 0.7 * 0.7 ** (loc1-loc2-1) * 1/math.log(session_dict[row['session']]+1)","metadata":{"execution":{"iopub.status.busy":"2024-04-18T08:22:11.009322Z","iopub.execute_input":"2024-04-18T08:22:11.009734Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# to be continue","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}