{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Import","metadata":{}},{"cell_type":"code","source":"from implicit.als import AlternatingLeastSquares\nfrom implicit.bpr import BayesianPersonalizedRanking\nfrom implicit.evaluation import mean_average_precision_at_k\nfrom scipy.sparse import csr_matrix\nimport tensorflow as tf\nimport pandas as pd\nimport numpy as np\nimport os, gc, time\nimport itertools","metadata":{"execution":{"iopub.status.busy":"2022-11-16T17:40:16.483357Z","iopub.execute_input":"2022-11-16T17:40:16.483932Z","iopub.status.idle":"2022-11-16T17:40:22.494836Z","shell.execute_reply.started":"2022-11-16T17:40:16.483776Z","shell.execute_reply":"2022-11-16T17:40:22.493584Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Load Data","metadata":{}},{"cell_type":"code","source":"data = pd.read_parquet('../input/preprocess-cart-to-order/cart_to_order.parquet')\ndata.interest = data.interest.astype(int)\ndata = data.sort_values(by=['session', 'ts'])\ndata = data.reset_index(drop=True)\ndata = data[['ts', 'interest', 'session_id', 'aid_id']]\ndisplay(data)","metadata":{"execution":{"iopub.status.busy":"2022-11-16T17:40:22.497677Z","iopub.execute_input":"2022-11-16T17:40:22.499230Z","iopub.status.idle":"2022-11-16T17:40:59.325584Z","shell.execute_reply.started":"2022-11-16T17:40:22.499163Z","shell.execute_reply":"2022-11-16T17:40:59.324262Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data['session_id'] = data['session_id'] - 1\ndata['aid_id'] = data['aid_id'] - 1\ndata","metadata":{"execution":{"iopub.status.busy":"2022-11-16T17:40:59.327124Z","iopub.execute_input":"2022-11-16T17:40:59.327454Z","iopub.status.idle":"2022-11-16T17:40:59.612248Z","shell.execute_reply.started":"2022-11-16T17:40:59.327426Z","shell.execute_reply":"2022-11-16T17:40:59.611046Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# MODEL","metadata":{"papermill":{"duration":0.030696,"end_time":"2022-02-18T04:54:01.064352","exception":false,"start_time":"2022-02-18T04:54:01.033656","status":"completed"},"tags":[]}},{"cell_type":"code","source":"num_session = data['session_id'].nunique()\nnum_article = data['aid_id'].nunique()\n\ncsr_data = csr_matrix((data['interest'], (data.session_id, data.aid_id)), shape= (num_session, num_article))\ncsr_data","metadata":{"_cell_guid":"564d30cf-3938-4b8a-841a-05398fe8810d","_uuid":"2b9946d0-e77f-4d6a-bf11-fc898d587767","collapsed":false,"jupyter":{"outputs_hidden":false},"papermill":{"duration":1.947413,"end_time":"2022-02-18T04:54:03.042443","exception":false,"start_time":"2022-02-18T04:54:01.095030","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-11-16T17:40:59.622470Z","iopub.execute_input":"2022-11-16T17:40:59.622906Z","iopub.status.idle":"2022-11-16T17:41:01.828429Z","shell.execute_reply.started":"2022-11-16T17:40:59.622873Z","shell.execute_reply":"2022-11-16T17:41:01.827411Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.environ['OPENBLAS_NUM_THREADS']='1'\nos.environ['KMP_DUPLICATE_LIB_OK']='True'\nos.environ['MKL_NUM_THREADS']='1'","metadata":{"_cell_guid":"0a85e591-23de-46c5-a722-b9beaf9932d9","_uuid":"36d4e0b2-5b84-42c5-a3a7-a8a389c9a7d8","collapsed":false,"jupyter":{"outputs_hidden":false},"papermill":{"duration":0.040709,"end_time":"2022-02-18T04:54:03.441269","exception":false,"start_time":"2022-02-18T04:54:03.400560","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-11-16T17:41:01.829529Z","iopub.execute_input":"2022-11-16T17:41:01.830251Z","iopub.status.idle":"2022-11-16T17:41:01.835675Z","shell.execute_reply.started":"2022-11-16T17:41:01.830220Z","shell.execute_reply":"2022-11-16T17:41:01.834656Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"als_model = BayesianPersonalizedRanking(factors=100, regularization=0.01, use_gpu=False, iterations=5)\n#als_model = AlternatingLeastSquares(factors=100, regularization=0.01, use_gpu=False, iterations=5)\nals_model.fit(csr_data.T, show_progress = True)","metadata":{"execution":{"iopub.status.busy":"2022-11-16T17:41:01.837549Z","iopub.execute_input":"2022-11-16T17:41:01.838149Z","iopub.status.idle":"2022-11-16T17:41:32.146099Z","shell.execute_reply.started":"2022-11-16T17:41:01.838117Z","shell.execute_reply":"2022-11-16T17:41:32.144860Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# def train(search_mode, **kwargs):\n#     als_model = None\n#     if search_mode == False:\n#         als_model = AlternatingLeastSquares(factors=120, regularization=0.01, use_gpu=False, iterations=5, dtype=np.float32, calculate_training_loss=True)\n#         als_model.fit(csr_data.T)\n#     else:\n#         for i in range(kwargs['start'], kwargs['end'], kwargs['step']):\n#             als_model = AlternatingLeastSquares(factors = i, regularization=0.01, use_gpu=False, iterations=5, dtype=np.float32, calculate_training_loss=True)\n#             als_model.fit(csr_data.T)\n#     return als_model\n# train(False, start = 40, end = 400, step = 40)","metadata":{"execution":{"iopub.status.busy":"2022-11-16T17:41:32.147477Z","iopub.execute_input":"2022-11-16T17:41:32.147816Z","iopub.status.idle":"2022-11-16T17:41:32.152809Z","shell.execute_reply.started":"2022-11-16T17:41:32.147786Z","shell.execute_reply":"2022-11-16T17:41:32.151963Z"},"jupyter":{"source_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Check Model performance","metadata":{}},{"cell_type":"code","source":"data = pd.read_parquet('../input/preprocess-cart-to-order/cart_to_order.parquet')\ndata.interest = data.interest.astype(int)\ndata = data.sort_values(by=['session', 'ts'])\ndata = data.reset_index(drop=True)\nsession_unique = data['session'].unique()\narticle_unique = data['aid'].unique()\nsession_to_idx = {v:k for k,v  in enumerate(session_unique)}\narticle_to_idx = {v:k for k,v in enumerate(article_unique)}\n\ndel data\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2022-11-16T17:41:32.174194Z","iopub.execute_input":"2022-11-16T17:41:32.174552Z","iopub.status.idle":"2022-11-16T17:42:08.017559Z","shell.execute_reply.started":"2022-11-16T17:41:32.174521Z","shell.execute_reply":"2022-11-16T17:42:08.016120Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"a_session, a_article = session_to_idx[1], article_to_idx[7563]\na_session_vector, a_article_vector = als_model.user_factors[a_session], als_model.item_factors[a_article]","metadata":{"_cell_guid":"b19cdf0c-e15e-45ab-a65f-5e0c8e34a37d","_uuid":"89e714c6-a060-43b4-bc86-802c2d6c1d86","collapsed":false,"jupyter":{"outputs_hidden":false},"papermill":{"duration":0.038723,"end_time":"2022-02-18T04:54:44.264358","exception":false,"start_time":"2022-02-18T04:54:44.225635","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-11-16T17:42:08.018993Z","iopub.execute_input":"2022-11-16T17:42:08.019480Z","iopub.status.idle":"2022-11-16T17:42:08.025290Z","shell.execute_reply.started":"2022-11-16T17:42:08.019448Z","shell.execute_reply":"2022-11-16T17:42:08.024002Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_article = article_to_idx[7563]\ntest_article_vector = als_model.item_factors[test_article]\nnp.dot(a_session, test_article_vector)","metadata":{"_cell_guid":"3c42052b-5b3e-4180-af10-2b3884afce31","_uuid":"a938fb51-d229-4c1c-9a50-96deebccec7f","collapsed":false,"jupyter":{"outputs_hidden":false},"papermill":{"duration":0.045804,"end_time":"2022-02-18T04:54:44.342226","exception":false,"start_time":"2022-02-18T04:54:44.296422","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-11-16T17:42:08.026835Z","iopub.execute_input":"2022-11-16T17:42:08.027228Z","iopub.status.idle":"2022-11-16T17:42:08.042131Z","shell.execute_reply.started":"2022-11-16T17:42:08.027159Z","shell.execute_reply":"2022-11-16T17:42:08.040883Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"a_test_article = 7563\narticle_id = article_to_idx[a_test_article]\nsimilar_article = als_model.similar_items(article_id, N=20)\nsimilar_article","metadata":{"_cell_guid":"cdd9b5c0-13a2-4367-bb25-218379a4bc41","_uuid":"e41cdc01-565b-463f-a9e8-75179f96c9ed","collapsed":false,"jupyter":{"outputs_hidden":false},"papermill":{"duration":0.128803,"end_time":"2022-02-18T04:54:44.503250","exception":false,"start_time":"2022-02-18T04:54:44.374447","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-11-16T17:42:08.043721Z","iopub.execute_input":"2022-11-16T17:42:08.044088Z","iopub.status.idle":"2022-11-16T17:42:08.384613Z","shell.execute_reply.started":"2022-11-16T17:42:08.044036Z","shell.execute_reply":"2022-11-16T17:42:08.383217Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"idx_to_article = {v:k for k,v in article_to_idx.items()}\nj = [idx_to_article[i[0]] for i in similar_article]\nprint(j)","metadata":{"_cell_guid":"b3d82aee-e389-4cc1-871f-1e8136dfa830","_uuid":"803a224e-7af6-48f6-b60f-27edb2f0d812","collapsed":false,"jupyter":{"outputs_hidden":false},"papermill":{"duration":0.077125,"end_time":"2022-02-18T04:54:44.640529","exception":false,"start_time":"2022-02-18T04:54:44.563404","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-11-16T17:42:08.386283Z","iopub.execute_input":"2022-11-16T17:42:08.387515Z","iopub.status.idle":"2022-11-16T17:42:08.580142Z","shell.execute_reply.started":"2022-11-16T17:42:08.387467Z","shell.execute_reply":"2022-11-16T17:42:08.578851Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"user = session_to_idx[3]\narticle_recommended = als_model.recommend(user, csr_data, N=20, filter_already_liked_items=False)\narticle_recommended","metadata":{"_cell_guid":"27d9fa4b-bc68-4d7c-919d-33f2e77ebcf9","_uuid":"b19ff3e5-6e17-4ec1-9e7c-f80da1f58045","collapsed":false,"jupyter":{"outputs_hidden":false},"papermill":{"duration":0.0642,"end_time":"2022-02-18T04:54:44.832518","exception":false,"start_time":"2022-02-18T04:54:44.768318","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-11-16T17:42:08.582425Z","iopub.execute_input":"2022-11-16T17:42:08.583228Z","iopub.status.idle":"2022-11-16T17:42:08.668461Z","shell.execute_reply.started":"2022-11-16T17:42:08.583190Z","shell.execute_reply":"2022-11-16T17:42:08.666709Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"k = [str(idx_to_article[i[0]]) for i in article_recommended]\nprint(' '.join(k))","metadata":{"_cell_guid":"2a11ce42-4a0e-4957-a061-f53b2779dd5c","_uuid":"a00fc248-a93f-4350-94c3-70ab3b7b3149","collapsed":false,"jupyter":{"outputs_hidden":false},"papermill":{"duration":0.067564,"end_time":"2022-02-18T04:54:44.967814","exception":false,"start_time":"2022-02-18T04:54:44.900250","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-11-16T17:42:08.670465Z","iopub.execute_input":"2022-11-16T17:42:08.671289Z","iopub.status.idle":"2022-11-16T17:42:08.678463Z","shell.execute_reply.started":"2022-11-16T17:42:08.671243Z","shell.execute_reply":"2022-11-16T17:42:08.677250Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **SUBMISSION**","metadata":{"_cell_guid":"706cae9c-ea33-41be-a2c3-e24149c14555","_uuid":"a838615d-ca28-4579-8ebc-b2ca8b5ddfa9","papermill":{"duration":0.042207,"end_time":"2022-02-18T04:54:45.159015","exception":false,"start_time":"2022-02-18T04:54:45.116808","status":"completed"},"tags":[]}},{"cell_type":"code","source":"from tqdm.notebook import tqdm\nimport glob\nimport pickle\nfrom collections import Counter","metadata":{"execution":{"iopub.status.busy":"2022-11-16T17:42:08.680415Z","iopub.execute_input":"2022-11-16T17:42:08.681226Z","iopub.status.idle":"2022-11-16T17:42:08.689016Z","shell.execute_reply.started":"2022-11-16T17:42:08.681179Z","shell.execute_reply":"2022-11-16T17:42:08.687355Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_test():    \n    dfs = []\n    for e, chunk_file in enumerate(glob.glob('../input/otto-chunk-data-inparquet-format/test_parquet/*')):\n        chunk = pd.read_parquet(chunk_file)\n        dfs.append(chunk)\n    return pd.concat(dfs).reset_index(drop=True).astype({\"ts\": \"datetime64[ms]\"})\n\ntest_df = load_test()\nprint('Test data has shape',test_df.shape)\ntest_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-11-16T17:42:08.701285Z","iopub.execute_input":"2022-11-16T17:42:08.702113Z","iopub.status.idle":"2022-11-16T17:42:12.270590Z","shell.execute_reply.started":"2022-11-16T17:42:08.702069Z","shell.execute_reply":"2022-11-16T17:42:12.269445Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# THREE CO-VISITATION MATRICES\nDISK_PIECES = 4\nVER = 4 \n\ntop_20_buys = pickle.load(open(f'../input/candidate-rerank-model-lb-0-574/top_15_carts_orders_v{VER}_0.pkl', 'rb'))\nfor k in range(1,DISK_PIECES): \n    top_20_buys.update( pickle.load(open(f'../input/candidate-rerank-model-lb-0-574/top_15_carts_orders_v{VER}_{k}.pkl', 'rb')) )\n\ntop_20_buy2buy = pickle.load(open(f'../input/candidate-rerank-model-lb-0-574/top_15_buy2buy_v{VER}_0.pkl', 'rb'))\n\n# TOP CLICKS AND ORDERS IN TEST\ntop_clicks = test_df.loc[test_df['type']=='clicks','aid'].value_counts().index.values[:20]\ntop_orders = test_df.loc[test_df['type']=='orders','aid'].value_counts().index.values[:20]\n\nprint('Here are size of our 3 co-visitation matrices:')\nlen( top_20_buy2buy ), len( top_20_buys )","metadata":{"execution":{"iopub.status.busy":"2022-11-16T17:42:12.449959Z","iopub.execute_input":"2022-11-16T17:42:12.450486Z","iopub.status.idle":"2022-11-16T17:42:24.819388Z","shell.execute_reply.started":"2022-11-16T17:42:12.450438Z","shell.execute_reply":"2022-11-16T17:42:24.818147Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"type_weight_multipliers = {'clicks': 1, 'carts': 6, 'orders': 3}\n\ndef suggest_buys(df):\n    # USER HISTORY AIDS AND TYPES\n    aids=df.aid.tolist()\n    types = df.type.tolist()\n    # UNIQUE AIDS AND UNIQUE BUYS\n    unique_aids = list(dict.fromkeys(aids[::-1] ))\n    df = df.loc[(df['type']=='carts')|(df['type']=='orders')]\n    unique_buys = list(dict.fromkeys( df.aid.tolist()[::-1] ))\n    # RERANK CANDIDATES USING WEIGHTS\n    if len(unique_aids)>=20:\n        weights=np.logspace(0.5,1,len(aids),base=2, endpoint=True)-1\n        aids_temp = Counter() \n        # RERANK BASED ON REPEAT ITEMS AND TYPE OF ITEMS\n        for aid,w,t in zip(aids,weights,types): \n            aids_temp[aid] += w * type_weight_multipliers[t]\n        # RERANK CANDIDATES USING \"BUY2BUY\" CO-VISITATION MATRIX\n        aids3 = list(itertools.chain(*[top_20_buy2buy[aid] for aid in unique_buys if aid in top_20_buy2buy]))\n        for aid in aids3: aids_temp[aid] += 0.1\n        sorted_aids = [k for k,v in aids_temp.most_common(20)]\n        return sorted_aids\n    # USE \"CART ORDER\" CO-VISITATION MATRIX\n    aids2 = list(itertools.chain(*[top_20_buys[aid] for aid in unique_aids if aid in top_20_buys]))\n    # USE \"BUY2BUY\" CO-VISITATION MATRIX\n    aids3 = list(itertools.chain(*[top_20_buy2buy[aid] for aid in unique_buys if aid in top_20_buy2buy]))\n    # RERANK CANDIDATES\n    top_aids2 = [aid2 for aid2, cnt in Counter(aids2+aids3).most_common(20) if aid2 not in unique_aids] \n    result = unique_aids + top_aids2[:20 - len(unique_aids)]\n    # USE TOP20 TEST ORDERS\n    return result + list(top_orders)[:20-len(result)]\n\ndef suggest_order(df):\n    user = df['session'].iloc[0]\n    if user not in session_to_idx:\n        return suggest_buys(df)\n    else:\n        session = session_to_idx[user]\n        article_recommended = als_model.recommend(session, csr_data, N=20, filter_already_liked_items=False)\n        lists = [idx_to_article[i[0]] for i in article_recommended]\n        return lists","metadata":{"execution":{"iopub.status.busy":"2022-11-16T17:42:24.821303Z","iopub.execute_input":"2022-11-16T17:42:24.822424Z","iopub.status.idle":"2022-11-16T17:42:24.836689Z","shell.execute_reply.started":"2022-11-16T17:42:24.822382Z","shell.execute_reply":"2022-11-16T17:42:24.835373Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\nbuy_list = pd.DataFrame()\nsplit = 1000\nprint(\"prediction start\")\nfor i in tqdm(range(split)):\n    \n    if i>0 and i%10==0:\n        print(i/10, end=' ')\n\n    start = int(i*(len(test_df)/split))\n    end = min( int((i+1)*(len(test_df)/split)), len(test_df) )\n    \n    buy = test_df.loc[start : end].sort_values([\"session\", \"ts\"]).groupby([\"session\"]).apply(\n        lambda x: suggest_order(x)\n    )\n    buy = pd.DataFrame(buy.add_suffix(\"_orders\"), columns=[\"labels\"]).reset_index()\n    buy_list=buy_list.append(buy)\norders_pred_df = buy_list\ndisplay(orders_pred_df)","metadata":{"execution":{"iopub.status.busy":"2022-11-16T17:42:24.886485Z","iopub.execute_input":"2022-11-16T17:42:24.887235Z","iopub.status.idle":"2022-11-16T17:43:09.126639Z","shell.execute_reply.started":"2022-11-16T17:42:24.887124Z","shell.execute_reply":"2022-11-16T17:43:09.125407Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(len(orders_pred_df))\norders_pred_df = orders_pred_df.drop_duplicates(subset='session')\norders_pred_df = orders_pred_df.sort_values(by=['session'])\nprint(len(orders_pred_df))","metadata":{"execution":{"iopub.status.busy":"2022-11-16T17:43:09.128373Z","iopub.execute_input":"2022-11-16T17:43:09.129522Z","iopub.status.idle":"2022-11-16T17:43:09.143081Z","shell.execute_reply.started":"2022-11-16T17:43:09.129475Z","shell.execute_reply":"2022-11-16T17:43:09.141839Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def toList(df):\n    lists = df[1:-1].split(',')\n    lists = [int(i) for i in lists]\n    return lists\nother_pred_df = pd.read_csv('../input/candidate-rerank-model-lb-0-574/intermediate.csv')\nother_pred_df = other_pred_df[['session', 'labels']]\nother_pred_df.labels = other_pred_df.labels.apply(toList)\nother_pred_df","metadata":{"execution":{"iopub.status.busy":"2022-11-16T17:48:20.436671Z","iopub.execute_input":"2022-11-16T17:48:20.437929Z","iopub.status.idle":"2022-11-16T17:48:59.631662Z","shell.execute_reply.started":"2022-11-16T17:48:20.437884Z","shell.execute_reply":"2022-11-16T17:48:59.630474Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred_df = pd.concat([orders_pred_df, other_pred_df])\npred_df[\"labels\"] = pred_df.labels.apply(lambda x: \" \".join(map(str,x)))\ndisplay(pred_df)","metadata":{"execution":{"iopub.status.busy":"2022-11-16T17:49:14.212768Z","iopub.execute_input":"2022-11-16T17:49:14.213187Z","iopub.status.idle":"2022-11-16T17:49:28.943394Z","shell.execute_reply.started":"2022-11-16T17:49:14.213152Z","shell.execute_reply":"2022-11-16T17:49:28.942127Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred_df.columns = [\"session_type\", \"labels\"]\npred_df.to_csv(\"submission.csv\", index=False)\npred_df","metadata":{"execution":{"iopub.status.busy":"2022-11-16T17:43:56.376884Z","iopub.status.idle":"2022-11-16T17:43:56.378805Z","shell.execute_reply.started":"2022-11-16T17:43:56.378482Z","shell.execute_reply":"2022-11-16T17:43:56.378512Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# %%time\n# # COMPUTE METRIC\n# score = 0\n# weights = {'clicks': 0.10, 'carts': 0.30, 'orders': 0.60}\n# for t in ['clicks','carts','orders']:\n#     sub = pred_df.loc[pred_df.session_type.str.contains(t)].copy()\n#     sub['session'] = sub.session_type.apply(lambda x: int(x.split('_')[0]))\n#     sub.labels = sub.labels.apply(lambda x: [int(i) for i in x.split(' ')[:20]])\n#     test_labels = pd.read_parquet('../input/otto-validation/test_labels.parquet')\n#     test_labels = test_labels.loc[test_labels['type']==t]\n#     test_labels = test_labels.merge(sub, how='left', on=['session'])\n#     test_labels['hits'] = test_labels.apply(lambda df: len(set(df.ground_truth).intersection(set(df.labels))), axis=1)\n#     test_labels['gt_count'] = test_labels.ground_truth.str.len().clip(0,20)\n#     recall = test_labels['hits'].sum() / test_labels['gt_count'].sum()\n#     score += weights[t]*recall\n#     print(f'{t} recall =',recall)\n    \n# print('=============')\n# print('Overall Recall =',score)\n# print('=============')","metadata":{"execution":{"iopub.status.busy":"2022-11-16T17:43:56.380627Z","iopub.status.idle":"2022-11-16T17:43:56.381611Z","shell.execute_reply.started":"2022-11-16T17:43:56.381276Z","shell.execute_reply":"2022-11-16T17:43:56.381305Z"},"trusted":true},"execution_count":null,"outputs":[]}]}