{"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":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport gc","metadata":{"execution":{"iopub.status.busy":"2022-11-19T15:57:45.231718Z","iopub.execute_input":"2022-11-19T15:57:45.232135Z","iopub.status.idle":"2022-11-19T15:57:45.237613Z","shell.execute_reply.started":"2022-11-19T15:57:45.232106Z","shell.execute_reply":"2022-11-19T15:57:45.236840Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_path = \"../input/ottorecommendationgraphembedding/new_test.csv\"\ntest_df = pd.read_csv(test_path)\n","metadata":{"execution":{"iopub.status.busy":"2022-11-19T15:57:45.401669Z","iopub.execute_input":"2022-11-19T15:57:45.402779Z","iopub.status.idle":"2022-11-19T15:57:54.755760Z","shell.execute_reply.started":"2022-11-19T15:57:45.402748Z","shell.execute_reply":"2022-11-19T15:57:54.754667Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df[[\"knn_{}\" for i in range(9)]] = pd.DataFrame(test_df['nearest_neighbors'].values)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nearest_neighbors = test_df['nearest_neighbors'].str.replace(\"]\", \"\").str.replace(\"[\",\"\").str.split(',', expand=True)\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for col in nearest_neighbors.columns.tolist():\n    nearest_neighbors[col] = nearest_neighbors[col].astype(\"int32\").map(reverse_mapping)\n   # nearest_neighbors[col] = nearest_neighbors[col].astype(\"str\")\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nearest_neighbors.columns = ['knn_0','knn_1','knn_2','knn_3','knn_4','knn_5',\n                             'knn_6','knn_7','knn_8']","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df['true_neighbors'] = (nearest_neighbors['knn_0'].astype(\"str\") + \" \" +  nearest_neighbors['knn_1'].astype(\"str\")  \\\n                            + \" \" +  nearest_neighbors['knn_2'].astype(\"str\") + \" \" +  nearest_neighbors['knn_3'].astype(\"str\")\n                            + \" \" +  nearest_neighbors['knn_4'].astype(\"str\") + \" \" +  nearest_neighbors['knn_5'].astype(\"str\") )                         \n                           # + \" \" +  nearest_neighbors['knn_6'].astype(\"str\") + \" \" +  nearest_neighbors['knn_7'].astype(\"str\") \n                           # + \" \" +  nearest_neighbors['knn_8'].astype(\"str\") )","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df = test_df.drop([\"ts\",\"type\",\"new_aid\",\"precedent_aid\",\"aid_graph\",\"nearest_neighbors\"], axis=1)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2022-11-19T16:00:09.307045Z","iopub.execute_input":"2022-11-19T16:00:09.307409Z","iopub.status.idle":"2022-11-19T16:00:13.411770Z","shell.execute_reply.started":"2022-11-19T16:00:09.307382Z","shell.execute_reply":"2022-11-19T16:00:13.410710Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# General popularity\nproduct_ids =  test_df.drop_duplicates(subset=['session','aid'])['aid'].tolist()\nmy_dict = pd.Series(product_ids).value_counts().to_dict()\ntest_df['general_popularity'] =  test_df['aid'].map(my_dict)\n# Recency weights\ntest_df['recency'] = test_df.groupby(['session']).cumcount()+1\n#  popularity per session\ntest_df['popularity_per_session'] = test_df.groupby(['session', 'aid']).transform('count').iloc[:,-1]","metadata":{"execution":{"iopub.status.busy":"2022-11-19T15:58:12.106764Z","iopub.execute_input":"2022-11-19T15:58:12.107163Z","iopub.status.idle":"2022-11-19T15:58:15.925413Z","shell.execute_reply.started":"2022-11-19T15:58:12.107131Z","shell.execute_reply":"2022-11-19T15:58:15.924040Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"group_label = test_df.sort_values(\"general_popularity\",ascending=False).groupby(\"session\")\npp = []\nfor name,group in group_label:\n    res = {}\n    res['session_id'] = name\n    res['labels'] = \" \".join(group['aid'].astype(\"str\").values[:20])\n    res['neighbors'] = \" \".join(group['true_neighbors'].values[:1])\n\n    pp.append(res)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_list = []\nfor row in pp:\n    session_id = row['session_id']\n    sorted_aids = row['labels']\n\n\n    data_list.append([f\"{session_id}_clicks\", sorted_aids,row['neighbors']])\n    data_list.append([f\"{session_id}_carts\", sorted_aids,row['neighbors']])\n    data_list.append([f\"{session_id}_orders\", sorted_aids,row['neighbors']])\n    \nsub = pd.DataFrame(\n    data_list, columns=[\"session_type\", \"aid\",'neighbors'])\nsub.head()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#mask = sub.aid.apply(lambda x: len(str(x))<=7).values\n#sub['labels'] = sub.loc[mask,'aid'] + \" \" + sub.loc[mask,'neighbors']\n#sub['labels'] = sub['labels'].fillna(sub['aid'])\nsub['labels'] = sub['aid'].astype(\"str\")  + \" \" + sub['neighbors'].astype(\"str\")","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub2 = sub[[\"session_type\",\"labels\"]]","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub2.head()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub2.loc[4,'labels']","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub2.to_csv(\"submission_graph8.csv\",index=False)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub2","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}