{"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":30664,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"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-06-08T07:29:37.808662Z","iopub.execute_input":"2024-06-08T07:29:37.809049Z","iopub.status.idle":"2024-06-08T07:29:39.196979Z","shell.execute_reply.started":"2024-06-08T07:29:37.809018Z","shell.execute_reply":"2024-06-08T07:29:39.195330Z"},"trusted":true},"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-06-08T07:29:39.199508Z","iopub.execute_input":"2024-06-08T07:29:39.200026Z","iopub.status.idle":"2024-06-08T07:29:39.211481Z","shell.execute_reply.started":"2024-06-08T07:29:39.199993Z","shell.execute_reply":"2024-06-08T07:29:39.209901Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = load('train')","metadata":{"execution":{"iopub.status.busy":"2024-06-08T07:29:39.213458Z","iopub.execute_input":"2024-06-08T07:29:39.213921Z","iopub.status.idle":"2024-06-08T07:31:01.774156Z","shell.execute_reply.started":"2024-06-08T07:29:39.213880Z","shell.execute_reply":"2024-06-08T07:31:01.772168Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df","metadata":{"execution":{"iopub.status.busy":"2024-06-08T07:31:01.776067Z","iopub.execute_input":"2024-06-08T07:31:01.776485Z","iopub.status.idle":"2024-06-08T07:31:01.803101Z","shell.execute_reply.started":"2024-06-08T07:31:01.776452Z","shell.execute_reply":"2024-06-08T07:31:01.801913Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# To reduce runtime, only consider data from the last three days as train_df.","metadata":{}},{"cell_type":"code","source":"datetime.strptime(\"2022-08-25 00:00:00\", \"%Y-%m-%d %H:%M:%S\").timestamp()","metadata":{"execution":{"iopub.status.busy":"2024-06-08T07:31:01.806613Z","iopub.execute_input":"2024-06-08T07:31:01.807109Z","iopub.status.idle":"2024-06-08T07:31:01.817238Z","shell.execute_reply.started":"2024-06-08T07:31:01.807066Z","shell.execute_reply":"2024-06-08T07:31:01.815182Z"},"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-06-08T07:31:01.819013Z","iopub.execute_input":"2024-06-08T07:31:01.819463Z","iopub.status.idle":"2024-06-08T07:31:03.996957Z","shell.execute_reply.started":"2024-06-08T07:31:01.819430Z","shell.execute_reply":"2024-06-08T07:31:03.994992Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df","metadata":{"execution":{"iopub.status.busy":"2024-06-08T07:31:03.999039Z","iopub.execute_input":"2024-06-08T07:31:03.999475Z","iopub.status.idle":"2024-06-08T07:31:04.030162Z","shell.execute_reply.started":"2024-06-08T07:31:03.999440Z","shell.execute_reply":"2024-06-08T07:31:04.028497Z"},"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-06-08T07:31:04.032033Z","iopub.execute_input":"2024-06-08T07:31:04.032528Z","iopub.status.idle":"2024-06-08T07:32:22.315218Z","shell.execute_reply.started":"2024-06-08T07:31:04.032494Z","shell.execute_reply":"2024-06-08T07:32:22.313681Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df","metadata":{"execution":{"iopub.status.busy":"2024-06-08T07:32:22.317339Z","iopub.execute_input":"2024-06-08T07:32:22.317910Z","iopub.status.idle":"2024-06-08T07:32:22.337455Z","shell.execute_reply.started":"2024-06-08T07:32:22.317865Z","shell.execute_reply":"2024-06-08T07:32:22.335868Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Choose the last day as the validation set.","metadata":{}},{"cell_type":"code","source":"train_df['datetime'].dt.day.unique()","metadata":{"execution":{"iopub.status.busy":"2024-06-08T07:32:22.339301Z","iopub.execute_input":"2024-06-08T07:32:22.339767Z","iopub.status.idle":"2024-06-08T07:32:23.433452Z","shell.execute_reply.started":"2024-06-08T07:32:22.339724Z","shell.execute_reply":"2024-06-08T07:32:23.432088Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df","metadata":{"execution":{"iopub.status.busy":"2024-06-08T07:32:23.435021Z","iopub.execute_input":"2024-06-08T07:32:23.436340Z","iopub.status.idle":"2024-06-08T07:32:23.453380Z","shell.execute_reply.started":"2024-06-08T07:32:23.436304Z","shell.execute_reply":"2024-06-08T07:32:23.451652Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df['day'] = train_df['datetime'].dt.day","metadata":{"execution":{"iopub.status.busy":"2024-06-08T07:32:23.455491Z","iopub.execute_input":"2024-06-08T07:32:23.456005Z","iopub.status.idle":"2024-06-08T07:32:24.368185Z","shell.execute_reply.started":"2024-06-08T07:32:23.455960Z","shell.execute_reply":"2024-06-08T07:32:24.366759Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df","metadata":{"execution":{"iopub.status.busy":"2024-06-08T07:32:24.369999Z","iopub.execute_input":"2024-06-08T07:32:24.370400Z","iopub.status.idle":"2024-06-08T07:32:24.387792Z","shell.execute_reply.started":"2024-06-08T07:32:24.370349Z","shell.execute_reply":"2024-06-08T07:32:24.386115Z"},"trusted":true},"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-06-08T07:32:24.395771Z","iopub.execute_input":"2024-06-08T07:32:24.396241Z","iopub.status.idle":"2024-06-08T07:32:27.255149Z","shell.execute_reply.started":"2024-06-08T07:32:24.396204Z","shell.execute_reply":"2024-06-08T07:32:27.253932Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_val_offline  #28号","metadata":{"execution":{"iopub.status.busy":"2024-06-08T07:32:27.256732Z","iopub.execute_input":"2024-06-08T07:32:27.257058Z","iopub.status.idle":"2024-06-08T07:32:27.273119Z","shell.execute_reply.started":"2024-06-08T07:32:27.257030Z","shell.execute_reply":"2024-06-08T07:32:27.271693Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train_offline  #25-27号","metadata":{"execution":{"iopub.status.busy":"2024-06-08T07:32:27.274859Z","iopub.execute_input":"2024-06-08T07:32:27.275246Z","iopub.status.idle":"2024-06-08T07:32:27.294848Z","shell.execute_reply.started":"2024-06-08T07:32:27.275214Z","shell.execute_reply":"2024-06-08T07:32:27.293478Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# A very simple itemCF for now; later, improvements and optimizations will be considered, such as time factors, click order, and user activity.","metadata":{}},{"cell_type":"code","source":"df_train_offline","metadata":{"execution":{"iopub.status.busy":"2024-06-08T07:32:27.296761Z","iopub.execute_input":"2024-06-08T07:32:27.297192Z","iopub.status.idle":"2024-06-08T07:32:27.317577Z","shell.execute_reply.started":"2024-06-08T07:32:27.297158Z","shell.execute_reply":"2024-06-08T07:32:27.315939Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train_offline['aid'].nunique()","metadata":{"execution":{"iopub.status.busy":"2024-06-08T07:32:27.319280Z","iopub.execute_input":"2024-06-08T07:32:27.319755Z","iopub.status.idle":"2024-06-08T07:32:27.834605Z","shell.execute_reply.started":"2024-06-08T07:32:27.319711Z","shell.execute_reply":"2024-06-08T07:32:27.833287Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"session_aids_df = df_train_offline[['session','aid']].groupby('session',as_index=False).agg(list)","metadata":{"execution":{"iopub.status.busy":"2024-06-08T07:32:27.836154Z","iopub.execute_input":"2024-06-08T07:32:27.836603Z","iopub.status.idle":"2024-06-08T07:34:00.446375Z","shell.execute_reply.started":"2024-06-08T07:32:27.836569Z","shell.execute_reply":"2024-06-08T07:34:00.445140Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"session_aids_df","metadata":{"execution":{"iopub.status.busy":"2024-06-08T07:34:00.448909Z","iopub.execute_input":"2024-06-08T07:34:00.449510Z","iopub.status.idle":"2024-06-08T07:34:00.473014Z","shell.execute_reply.started":"2024-06-08T07:34:00.449455Z","shell.execute_reply":"2024-06-08T07:34:00.471681Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from collections import defaultdict","metadata":{"execution":{"iopub.status.busy":"2024-06-08T07:34:00.475763Z","iopub.execute_input":"2024-06-08T07:34:00.476297Z","iopub.status.idle":"2024-06-08T07:34:00.483543Z","shell.execute_reply.started":"2024-06-08T07:34:00.476253Z","shell.execute_reply":"2024-06-08T07:34:00.481964Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"item_Sim = defaultdict()","metadata":{"execution":{"iopub.status.busy":"2024-06-08T07:34:00.485310Z","iopub.execute_input":"2024-06-08T07:34:00.485783Z","iopub.status.idle":"2024-06-08T07:34:00.497707Z","shell.execute_reply.started":"2024-06-08T07:34:00.485750Z","shell.execute_reply":"2024-06-08T07:34:00.496138Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"item_Sim","metadata":{"execution":{"iopub.status.busy":"2024-06-08T07:34:00.500293Z","iopub.execute_input":"2024-06-08T07:34:00.501692Z","iopub.status.idle":"2024-06-08T07:34:00.514735Z","shell.execute_reply.started":"2024-06-08T07:34:00.501643Z","shell.execute_reply":"2024-06-08T07:34:00.512889Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i,row in tqdm( session_aids_df.iterrows(), total=len(session_aids_df) ):\n    \n    aid_list = row['aid']\n    \n    for item in aid_list:\n        \n        item_Sim.setdefault(item,dict())\n        \n        for related_item in aid_list:\n            if item == related_item:\n                continue\n        \n            item_Sim[item].setdefault(related_item,0)\n            \n            item_Sim[item][related_item] += 1\n    ","metadata":{"execution":{"iopub.status.busy":"2024-06-08T07:34:00.516567Z","iopub.execute_input":"2024-06-08T07:34:00.517030Z","iopub.status.idle":"2024-06-08T07:47:24.474573Z","shell.execute_reply.started":"2024-06-08T07:34:00.516985Z","shell.execute_reply":"2024-06-08T07:47:24.472228Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"找到热度前20的商品，方便为没有历史记录的商品做推荐","metadata":{"execution":{"iopub.status.busy":"2024-06-08T07:47:24.477029Z","iopub.execute_input":"2024-06-08T07:47:24.477591Z","iopub.status.idle":"2024-06-08T07:47:24.489496Z","shell.execute_reply.started":"2024-06-08T07:47:24.477545Z","shell.execute_reply":"2024-06-08T07:47:24.486866Z"}}},{"cell_type":"code","source":"hot_items = df_train_offline.groupby(['aid'],as_index=False)['session'].count()\nhot_items.columns = ['aid','pop']\nhot_items","metadata":{"execution":{"iopub.status.busy":"2024-06-08T07:54:40.672448Z","iopub.execute_input":"2024-06-08T07:54:40.674066Z","iopub.status.idle":"2024-06-08T07:54:43.042466Z","shell.execute_reply.started":"2024-06-08T07:54:40.674009Z","shell.execute_reply":"2024-06-08T07:54:43.040803Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"hot_items = hot_items.sort_values(['pop'],ascending=False).reset_index(drop=True)","metadata":{"execution":{"iopub.status.busy":"2024-06-08T07:54:49.209458Z","iopub.execute_input":"2024-06-08T07:54:49.209937Z","iopub.status.idle":"2024-06-08T07:54:49.337663Z","shell.execute_reply.started":"2024-06-08T07:54:49.209902Z","shell.execute_reply":"2024-06-08T07:54:49.336158Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"hot_items","metadata":{"execution":{"iopub.status.busy":"2024-06-08T07:54:53.664298Z","iopub.execute_input":"2024-06-08T07:54:53.664752Z","iopub.status.idle":"2024-06-08T07:54:53.679537Z","shell.execute_reply.started":"2024-06-08T07:54:53.664719Z","shell.execute_reply":"2024-06-08T07:54:53.677716Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2024-06-08T07:48:55.066254Z","iopub.execute_input":"2024-06-08T07:48:55.066711Z","iopub.status.idle":"2024-06-08T07:48:55.073062Z","shell.execute_reply.started":"2024-06-08T07:48:55.066676Z","shell.execute_reply":"2024-06-08T07:48:55.071454Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2024-06-08T07:48:56.260477Z","iopub.execute_input":"2024-06-08T07:48:56.260963Z","iopub.status.idle":"2024-06-08T07:48:56.272915Z","shell.execute_reply.started":"2024-06-08T07:48:56.260930Z","shell.execute_reply":"2024-06-08T07:48:56.271229Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# To be continued","metadata":{}},{"cell_type":"code","source":"session_items_val = df_val_offline.groupby(['session'], as_index = False)['aid'].agg(set)","metadata":{"execution":{"iopub.status.busy":"2024-06-08T07:48:59.614718Z","iopub.execute_input":"2024-06-08T07:48:59.615221Z","iopub.status.idle":"2024-06-08T07:49:46.500448Z","shell.execute_reply.started":"2024-06-08T07:48:59.615187Z","shell.execute_reply":"2024-06-08T07:49:46.498872Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"session_items_val","metadata":{"execution":{"iopub.status.busy":"2024-06-08T07:49:53.080626Z","iopub.execute_input":"2024-06-08T07:49:53.081098Z","iopub.status.idle":"2024-06-08T07:49:53.102716Z","shell.execute_reply.started":"2024-06-08T07:49:53.081064Z","shell.execute_reply":"2024-06-08T07:49:53.100684Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"此时不再区分有哪些行为\n只要是有交互的商品都当作历史商品","metadata":{}},{"cell_type":"code","source":"session_items_train = df_train_offline.groupby(['session'], as_index = False)['aid'].agg(set)","metadata":{"execution":{"iopub.status.busy":"2024-06-08T07:49:56.875791Z","iopub.execute_input":"2024-06-08T07:49:56.876187Z","iopub.status.idle":"2024-06-08T07:51:33.088623Z","shell.execute_reply.started":"2024-06-08T07:49:56.876157Z","shell.execute_reply":"2024-06-08T07:51:33.086664Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"session_items_train","metadata":{"execution":{"iopub.status.busy":"2024-06-08T07:51:33.091452Z","iopub.execute_input":"2024-06-08T07:51:33.091921Z","iopub.status.idle":"2024-06-08T07:51:33.113074Z","shell.execute_reply.started":"2024-06-08T07:51:33.091885Z","shell.execute_reply":"2024-06-08T07:51:33.111403Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#key -> value   广播，进行向量化\ndict_session_item = dict(zip(session_items_train['session'], session_items_train['aid']))","metadata":{"execution":{"iopub.status.busy":"2024-06-08T07:51:33.114817Z","iopub.execute_input":"2024-06-08T07:51:33.115275Z","iopub.status.idle":"2024-06-08T07:51:35.101835Z","shell.execute_reply.started":"2024-06-08T07:51:33.115242Z","shell.execute_reply":"2024-06-08T07:51:35.100411Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dict_val = dict(zip(session_items_val['session'], session_items_val['aid']))","metadata":{"execution":{"iopub.status.busy":"2024-06-08T07:51:35.105777Z","iopub.execute_input":"2024-06-08T07:51:35.106400Z","iopub.status.idle":"2024-06-08T07:51:35.918314Z","shell.execute_reply.started":"2024-06-08T07:51:35.106329Z","shell.execute_reply":"2024-06-08T07:51:35.916672Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(dict_session_item)","metadata":{"execution":{"iopub.status.busy":"2024-06-08T07:51:35.920168Z","iopub.execute_input":"2024-06-08T07:51:35.920611Z","iopub.status.idle":"2024-06-08T07:51:35.935150Z","shell.execute_reply.started":"2024-06-08T07:51:35.920576Z","shell.execute_reply":"2024-06-08T07:51:35.933438Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(dict_val)","metadata":{"execution":{"iopub.status.busy":"2024-06-08T07:51:35.937193Z","iopub.execute_input":"2024-06-08T07:51:35.937744Z","iopub.status.idle":"2024-06-08T07:51:35.950345Z","shell.execute_reply.started":"2024-06-08T07:51:35.937680Z","shell.execute_reply":"2024-06-08T07:51:35.948846Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"type(dict_val[1717837])","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"遍历验证集里每个用户\n查表：查贡献次数高的打分，然后完成推荐\n排序\n","metadata":{}},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def evaluate_recommendation(recomendation, val):\n    score = 0\n        \n    \n    recomended_items = [item[0] for item in recomendation]\n    num_hits = len(val.intersection(set(recomended_items)))  #计算命中商品数\n    score = num_hits / min(20, len(val))\n\n    return score","metadata":{"execution":{"iopub.status.busy":"2024-06-08T07:54:57.660486Z","iopub.execute_input":"2024-06-08T07:54:57.661016Z","iopub.status.idle":"2024-06-08T07:54:57.669475Z","shell.execute_reply.started":"2024-06-08T07:54:57.660979Z","shell.execute_reply":"2024-06-08T07:54:57.667756Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#集合类型\ntop_k = 100  # 每一个历史商品，如果在item_Sim中出现，取出100个关联性最高的产品\nscores = 0\n\nfor each_session, val in tqdm(dict_val.items()):\n    rank_score = {}  # 针对每个用户进行设置\n    \n    if each_session in dict_session_item:\n        prev_aid = dict_session_item[each_session]\n    \n        # 对于每一个历史产品，和哪个产品相似性较高\n        for each_aid in prev_aid:\n            if each_aid in item_Sim:\n                related_items = item_Sim[each_aid]\n                related_items = sorted(related_items.items(), key=lambda x: x[1], reverse=True)[:top_k]\n            \n                for target_aid, co_occurrence_count in related_items:\n                    # 对于用户来说第一次出现\n                    if target_aid not in rank_score:\n                        rank_score[target_aid] = 1  # 考虑商品间的共现次数\n                    else:\n                        rank_score[target_aid] += 1\n            \n        # 进行推荐，对rank_score进行排序\n        recomendation = sorted(rank_score.items(), key=lambda x: x[1], reverse=True)[:20]\n    else:\n        # 如果不在dict_session_item里的session也要推荐，可以用热度最高的20件商品推荐\n        # 假设hot_items是根据商品热度排序的列表\n        recomendation = hot_items[:20]\n\n    scores += evaluate_recommendation(recomendation, val)","metadata":{"execution":{"iopub.status.busy":"2024-06-08T07:54:58.634513Z","iopub.execute_input":"2024-06-08T07:54:58.634979Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"scores / len(session_items_val)","metadata":{"execution":{"iopub.status.busy":"2024-06-08T07:52:09.334242Z","iopub.status.idle":"2024-06-08T07:52:09.334724Z","shell.execute_reply.started":"2024-06-08T07:52:09.334507Z","shell.execute_reply":"2024-06-08T07:52:09.334526Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"scores","metadata":{"execution":{"iopub.status.busy":"2024-06-08T07:52:09.337516Z","iopub.status.idle":"2024-06-08T07:52:09.337976Z","shell.execute_reply.started":"2024-06-08T07:52:09.337758Z","shell.execute_reply":"2024-06-08T07:52:09.337775Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}