{"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":"raw","source":"","metadata":{"papermill":{"duration":0.005854,"end_time":"2023-07-27T03:39:10.985722","exception":false,"start_time":"2023-07-27T03:39:10.979868","status":"completed"},"tags":[]}},{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport gc\nfrom tqdm import tqdm\nfrom collections import defaultdict\nimport heapq\ntqdm.pandas()\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","papermill":{"duration":0.022116,"end_time":"2023-07-27T03:39:11.012856","exception":false,"start_time":"2023-07-27T03:39:10.99074","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-07-31T04:14:49.421793Z","iopub.execute_input":"2023-07-31T04:14:49.4222Z","iopub.status.idle":"2023-07-31T04:14:49.437309Z","shell.execute_reply.started":"2023-07-31T04:14:49.422158Z","shell.execute_reply":"2023-07-31T04:14:49.436038Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train=pd.read_csv(\"/kaggle/input/datathon-alicorp-2023-b/train.csv\",sep=\"|\")\ndf_sub=pd.read_csv(\"/kaggle/input/datathon-alicorp-2023-b/submit_example.csv\")","metadata":{"papermill":{"duration":4.514134,"end_time":"2023-07-27T03:39:15.533385","exception":false,"start_time":"2023-07-27T03:39:11.019251","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-07-31T04:14:49.439344Z","iopub.execute_input":"2023-07-31T04:14:49.439729Z","iopub.status.idle":"2023-07-31T04:14:52.023304Z","shell.execute_reply.started":"2023-07-31T04:14:49.439695Z","shell.execute_reply":"2023-07-31T04:14:52.022285Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train","metadata":{"papermill":{"duration":0.039523,"end_time":"2023-07-27T03:39:15.578333","exception":false,"start_time":"2023-07-27T03:39:15.53881","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-07-31T04:14:52.024463Z","iopub.execute_input":"2023-07-31T04:14:52.024773Z","iopub.status.idle":"2023-07-31T04:14:52.045257Z","shell.execute_reply.started":"2023-07-31T04:14:52.024746Z","shell.execute_reply":"2023-07-31T04:14:52.044087Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train[\"product_id\"].nunique()","metadata":{"papermill":{"duration":0.031133,"end_time":"2023-07-27T03:39:15.615023","exception":false,"start_time":"2023-07-27T03:39:15.58389","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-07-31T04:14:52.046579Z","iopub.execute_input":"2023-07-31T04:14:52.047066Z","iopub.status.idle":"2023-07-31T04:14:52.072808Z","shell.execute_reply.started":"2023-07-31T04:14:52.047022Z","shell.execute_reply":"2023-07-31T04:14:52.07136Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for type_id in [\"BO\",\"GA\",\"PA\"]:\n    \n    print(type_id)\n    print(df_train.loc[df_train[\"type_id\"]==type_id,\"product_id\"].nunique())\n    print(\"\")\n    \n    ","metadata":{"execution":{"iopub.status.busy":"2023-07-31T04:14:52.076516Z","iopub.execute_input":"2023-07-31T04:14:52.076998Z","iopub.status.idle":"2023-07-31T04:14:52.609863Z","shell.execute_reply.started":"2023-07-31T04:14:52.07696Z","shell.execute_reply":"2023-07-31T04:14:52.608502Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train[\"customer_id\"].nunique()","metadata":{"execution":{"iopub.status.busy":"2023-07-31T04:14:52.611534Z","iopub.execute_input":"2023-07-31T04:14:52.612037Z","iopub.status.idle":"2023-07-31T04:14:52.634037Z","shell.execute_reply.started":"2023-07-31T04:14:52.611989Z","shell.execute_reply":"2023-07-31T04:14:52.633094Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train[\"product_id\"].value_counts()","metadata":{"papermill":{"duration":0.039459,"end_time":"2023-07-27T03:39:15.659907","exception":false,"start_time":"2023-07-27T03:39:15.620448","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-07-31T04:14:52.635474Z","iopub.execute_input":"2023-07-31T04:14:52.635954Z","iopub.status.idle":"2023-07-31T04:14:52.664977Z","shell.execute_reply.started":"2023-07-31T04:14:52.635921Z","shell.execute_reply":"2023-07-31T04:14:52.66387Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train[\"customer_id\"].nunique()","metadata":{"papermill":{"duration":0.030351,"end_time":"2023-07-27T03:39:15.69749","exception":false,"start_time":"2023-07-27T03:39:15.667139","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-07-31T04:14:52.667009Z","iopub.execute_input":"2023-07-31T04:14:52.667402Z","iopub.status.idle":"2023-07-31T04:14:52.690519Z","shell.execute_reply.started":"2023-07-31T04:14:52.667368Z","shell.execute_reply":"2023-07-31T04:14:52.689568Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.groupby(\"customer_id\")[\"product_id\"].nunique()","metadata":{"papermill":{"duration":0.58333,"end_time":"2023-07-27T03:39:16.286403","exception":false,"start_time":"2023-07-27T03:39:15.703073","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-07-31T04:14:52.691985Z","iopub.execute_input":"2023-07-31T04:14:52.692617Z","iopub.status.idle":"2023-07-31T04:14:53.359124Z","shell.execute_reply.started":"2023-07-31T04:14:52.692568Z","shell.execute_reply":"2023-07-31T04:14:53.357863Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.groupby(\"customer_id\")[\"product_id\"].nunique().describe()","metadata":{"papermill":{"duration":0.518701,"end_time":"2023-07-27T03:39:16.811411","exception":false,"start_time":"2023-07-27T03:39:16.29271","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-07-31T04:14:53.360805Z","iopub.execute_input":"2023-07-31T04:14:53.361465Z","iopub.status.idle":"2023-07-31T04:14:54.020412Z","shell.execute_reply.started":"2023-07-31T04:14:53.36142Z","shell.execute_reply":"2023-07-31T04:14:54.019132Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.query(\"customer_id==13758\")","metadata":{"papermill":{"duration":0.049443,"end_time":"2023-07-27T03:39:16.867077","exception":false,"start_time":"2023-07-27T03:39:16.817634","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-07-31T04:14:54.022402Z","iopub.execute_input":"2023-07-31T04:14:54.022909Z","iopub.status.idle":"2023-07-31T04:14:54.058705Z","shell.execute_reply.started":"2023-07-31T04:14:54.022862Z","shell.execute_reply":"2023-07-31T04:14:54.057464Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(pd.to_datetime(df_train[\"fecha_compra\"]).max()-pd.to_datetime(df_train[\"fecha_compra\"]).min())\nprint(df_train[\"fecha_compra\"].nunique())","metadata":{"papermill":{"duration":0.939274,"end_time":"2023-07-27T03:39:17.812408","exception":false,"start_time":"2023-07-27T03:39:16.873134","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-07-31T04:14:54.060262Z","iopub.execute_input":"2023-07-31T04:14:54.060716Z","iopub.status.idle":"2023-07-31T04:14:54.682196Z","shell.execute_reply.started":"2023-07-31T04:14:54.060685Z","shell.execute_reply":"2023-07-31T04:14:54.680898Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train[\"fecha_compra\"]=pd.to_datetime(df_train[\"fecha_compra\"])\n\n\ndf_temp=df_train[[\"fecha_compra\"]].drop_duplicates()\n\ndf_temp[\"max_fecha\"]=df_train[\"fecha_compra\"].max()\n\ndf_temp[\"order_day\"]=(df_temp[\"max_fecha\"]-df_temp[\"fecha_compra\"]).dt.days\n\ndf_temp[\"order_week\"]=df_temp[\"order_day\"]//7\n\ndf_train=df_train.merge(df_temp,on=\"fecha_compra\",how=\"left\")\n\ndel df_temp; gc.collect()","metadata":{"papermill":{"duration":1.072783,"end_time":"2023-07-27T03:39:18.891937","exception":false,"start_time":"2023-07-27T03:39:17.819154","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-07-31T04:14:54.683681Z","iopub.execute_input":"2023-07-31T04:14:54.684022Z","iopub.status.idle":"2023-07-31T04:14:55.783402Z","shell.execute_reply.started":"2023-07-31T04:14:54.683994Z","shell.execute_reply":"2023-07-31T04:14:55.782156Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(df_train.loc[df_train[\"customer_id\"].isin(df_sub[\"customer_id\"]),\"customer_id\"].nunique())\nprint(df_train.loc[~df_train[\"customer_id\"].isin(df_sub[\"customer_id\"]),\"customer_id\"].nunique())\nprint(df_sub[\"customer_id\"].nunique())\n","metadata":{"papermill":{"duration":0.087385,"end_time":"2023-07-27T03:39:18.986563","exception":false,"start_time":"2023-07-27T03:39:18.899178","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-07-31T04:14:55.788385Z","iopub.execute_input":"2023-07-31T04:14:55.788791Z","iopub.status.idle":"2023-07-31T04:14:55.863766Z","shell.execute_reply.started":"2023-07-31T04:14:55.788756Z","shell.execute_reply":"2023-07-31T04:14:55.862539Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(df_train.query(\"order_week==0\").groupby(\"customer_id\")[\"product_id\"].nunique().describe())\n\nprint(df_train.query(\"order_week==1\").groupby(\"customer_id\")[\"product_id\"].nunique().describe())\n\nprint(df_train.query(\"order_week==2\").groupby(\"customer_id\")[\"product_id\"].nunique().describe())\n\n\n      \n      ","metadata":{"papermill":{"duration":0.239026,"end_time":"2023-07-27T03:39:19.233063","exception":false,"start_time":"2023-07-27T03:39:18.994037","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-07-31T04:14:55.865305Z","iopub.execute_input":"2023-07-31T04:14:55.865721Z","iopub.status.idle":"2023-07-31T04:14:56.124755Z","shell.execute_reply.started":"2023-07-31T04:14:55.865687Z","shell.execute_reply":"2023-07-31T04:14:56.123661Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test=df_train.loc[df_train[\"order_week\"]==0].reset_index(drop=True)\n\ndf_train=df_train.loc[df_train[\"order_week\"]>0].reset_index(drop=True)\n\ndf_train[\"order_week\"]=df_train[\"order_week\"]-1","metadata":{"execution":{"iopub.status.busy":"2023-07-31T04:14:56.126122Z","iopub.execute_input":"2023-07-31T04:14:56.126432Z","iopub.status.idle":"2023-07-31T04:14:56.512694Z","shell.execute_reply.started":"2023-07-31T04:14:56.126405Z","shell.execute_reply":"2023-07-31T04:14:56.51167Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def chunkify(lst,n):\n    return [lst[i::n] for i in range(n)]\n\n\ndef create_dict_next_buy(df_train):\n    \n    customers_list=df_train[\"customer_id\"].unique().tolist()\n\n    customer_chunks=chunkify(customers_list,10)\n\n    df_next_week_buy=[]\n\n    for customer_chunk in tqdm(customer_chunks):\n\n        df_temp=df_train.loc[df_train[\"customer_id\"].isin(customer_chunk),[\"customer_id\",\"product_id\",\"order_week_customer\"]].reset_index(drop=True)\n\n\n        df_temp=df_temp.merge(df_temp,on=\"customer_id\",how=\"left\")\n\n        df_temp=df_temp.loc[(df_temp[\"order_week_customer_y\"]-df_temp[\"order_week_customer_x\"])==1].reset_index(drop=True)\n\n        df_temp[\"unos\"]=1\n\n        df_temp=df_temp.groupby([\"product_id_x\",\"product_id_y\"])[\"unos\"].sum().reset_index()\n\n        df_temp=df_temp.rename(columns={\"unos\":\"venta_unitaria\"})\n\n        df_next_week_buy.append(df_temp)\n\n        del df_temp ; gc.collect()\n\n    df_next_week_buy=pd.concat(df_next_week_buy,ignore_index=True)\n\n    df_next_week_buy=df_next_week_buy.groupby([\"product_id_x\",\"product_id_y\"])[\"venta_unitaria\"].sum().reset_index()\n\n    df_next_week_buy[\"venta_unitaria_total\"]=df_next_week_buy.groupby(\"product_id_x\")[\"venta_unitaria\"].transform(\"sum\")\n\n    df_next_week_buy[\"score_next_buy\"]=df_next_week_buy[\"venta_unitaria\"]/df_next_week_buy.reset_index(drop=True)[\"venta_unitaria_total\"]\n\n    df_next_week_buy=df_next_week_buy.sort_values([\"product_id_x\",\"score_next_buy\"],ascending=[True,False]).reset_index(drop=True)\n    \n    df_next_week_buy=df_next_week_buy.groupby([\"product_id_x\"]).head(30).reset_index(drop=True)\n    \n\n\n    dict_next_buy=df_next_week_buy.set_index([\"product_id_x\",\"product_id_y\"])[\"score_next_buy\"].to_dict()\n    \n    return dict_next_buy\n\n\ndef prepare_dicts_rating(df_train):\n    \n\n    \n    #orden de semana de compra segun cliente\n    df_temp=df_train.drop_duplicates([\"customer_id\",\"order_week\"]).drop_duplicates()\n    df_temp=df_temp.sort_values([\"customer_id\",\"order_week\"],ascending=[True,True]).reset_index(drop=True)\n    df_temp[\"unos\"]=1\n    df_temp[\"order_week_customer\"]=df_temp.groupby(\"customer_id\")[\"unos\"].cumsum()\n    df_temp[\"order_week_customer\"]=df_temp[\"order_week_customer\"]-1\n    df_train=df_train.merge(df_temp[[\"customer_id\",\"order_week\",\"order_week_customer\"]],on=[\"customer_id\",\"order_week\"],how=\"left\")\n    \n    #dictionary next buy\n    dict_next_buy=create_dict_next_buy(df_train)\n    \n    df_train[\"venta_unitaria\"]=1\n\n\n    df_ven_cus_prod=df_train.groupby([\"customer_id\",\"product_id\"])[\"venta_unitaria\"].sum().reset_index()\n\n    df_ven_cus=df_train.groupby([\"customer_id\"])[\"venta_unitaria\"].sum().reset_index()\n\n    df_ven_cus=df_ven_cus.rename(columns={\"venta_unitaria\":\"venta_unitaria_total\"})\n\n    df_ven_cus_prod=df_ven_cus_prod.merge(df_ven_cus,on=\"customer_id\",how=\"left\")\n\n    df_ven_cus_prod[\"score\"]=df_ven_cus_prod[\"venta_unitaria\"]/df_ven_cus_prod[\"venta_unitaria_total\"]\n\n\n    df_ven_cus_prod=df_ven_cus_prod.sort_values([\"customer_id\",\"score\"],ascending=[True,False]).groupby([\"customer_id\"]).head(75).reset_index(drop=True)\n\n    dict_cus_prod=df_ven_cus_prod.set_index([\"customer_id\",\"product_id\"])[\"score\"].to_dict()\n    \n    dict_lastw_prod_cust=df_train.loc[df_train[\"order_week_customer\"]==0].drop_duplicates([\"customer_id\",\"product_id\"]).groupby(\"customer_id\")[\"product_id\"].agg(list).to_dict()\n    \n    df_ven_prod_type=df_train.query(\"order_week<2\").groupby([\"product_id\",\"type_id\"])[\"venta_unitaria\"].sum().reset_index()\n\n    df_ven_type=df_train.query(\"order_week<2\").groupby([\"type_id\"])[\"venta_unitaria\"].sum().reset_index()\n\n    df_ven_type=df_ven_type.rename(columns={\"venta_unitaria\":\"venta_unitaria_total\"})\n\n    df_ven_prod_type=df_ven_prod_type.merge(df_ven_type,on=\"type_id\",how=\"left\")\n\n    df_ven_prod_type[\"score\"]=df_ven_prod_type[\"venta_unitaria\"]/df_ven_prod_type[\"venta_unitaria_total\"]\n\n    df_ven_prod_type=df_ven_prod_type.sort_values([\"type_id\",\"score\"],ascending=[True,False]).groupby([\"type_id\"]).head(75).reset_index(drop=True)\n\n    dict_prod_type=df_ven_prod_type.set_index([\"product_id\",\"type_id\"])[\"score\"].to_dict()\n    \n    print(df_ven_prod_type.query(\"type_id=='BO'\")[\"score\"].sum())\n    print(df_ven_prod_type.query(\"type_id=='GA'\")[\"score\"].sum())\n    print(df_ven_prod_type.query(\"type_id=='PA'\")[\"score\"].sum())\n    \n    \n    return dict_cus_prod,dict_prod_type,dict_next_buy,dict_lastw_prod_cust\n\n\ndef assign_scores_prod_cus(df_train,dict_cus_prod,dict_prod_type,dict_next_buy,dict_lastw_prod_cust):\n    \n    dict_scores_customer_products={}\n\n\n    for type_product in df_train[\"type_id\"].unique().tolist():\n\n        customers_list=df_train.loc[df_train[\"type_id\"]==type_product,\"customer_id\"].unique().tolist()\n\n        score_products_populares_dict={key[0]:dict_prod_type[key] for key in dict_prod_type.keys() if key[1] == type_product}\n\n        for customer in tqdm(customers_list):\n\n            score_products_comprados_dict={key[1]:dict_cus_prod[key] for key in dict_cus_prod.keys() if key[0] == customer}\n\n            \n            products_comprados_lastw_list=dict_lastw_prod_cust[customer]\n            \n            score_next_buy_dict={key:dict_next_buy[key] for key in dict_next_buy.keys() if key[0] in products_comprados_lastw_list }\n            \n            score_product_next_buy_dict={}\n            \n            for pairs_products in score_next_buy_dict.keys():\n                \n                if pairs_products[1] not in score_product_next_buy_dict.keys():\n                    \n                    score_product_next_buy_dict[pairs_products[1]]=score_next_buy_dict[pairs_products]\n                    \n                else :\n                    \n                    if score_product_next_buy_dict[pairs_products[1]] < score_next_buy_dict[pairs_products] :\n                        \n                        score_product_next_buy_dict[pairs_products[1]] = score_next_buy_dict[pairs_products]\n                    \n                    \n            products_list=list(set(list(score_products_comprados_dict.keys())+list(score_products_populares_dict.keys())+list(score_product_next_buy_dict.keys())))\n\n            for product in products_list:\n\n                \n                if product in products_comprados_lastw_list:\n                    \n                    dict_scores_customer_products[(customer,product)]=1.25*score_products_comprados_dict.get(product,0)+score_products_populares_dict.get(product,0)+0.75*score_product_next_buy_dict.get(product,0)\n                    \n                else :\n                    \n\n                    dict_scores_customer_products[(customer,product)]=1.25*score_products_comprados_dict.get(product,0)+score_products_populares_dict.get(product,0)\n\n                    \n                    \n                \n    return dict_scores_customer_products\n\n\n\ndef assign_top_30_products(dict_scores_customer_products):\n    \n    # Step 1: Group the products by customer using defaultdict\n    customer_products = defaultdict(list)\n    for customer, product in dict_scores_customer_products.keys():\n        rating = dict_scores_customer_products[(customer, product)]\n        customer_products[customer].append((rating, product))\n\n    # Step 2: Select the top 20 products with the highest ratings for each customer using heapq\n    top_30_products_by_customer = {}\n    for customer, products in customer_products.items():\n        top_30_products = heapq.nlargest(30, products)\n        top_30_products_by_customer[customer] = [product for _, product in top_30_products]\n        \n        \n    return top_30_products_by_customer\n        \n    \n\n    \n        \n\n    \n    \n    \n    \n    ","metadata":{"execution":{"iopub.status.busy":"2023-07-31T04:14:56.514357Z","iopub.execute_input":"2023-07-31T04:14:56.514925Z","iopub.status.idle":"2023-07-31T04:14:56.549331Z","shell.execute_reply.started":"2023-07-31T04:14:56.514883Z","shell.execute_reply":"2023-07-31T04:14:56.54815Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dict_cus_prod,dict_prod_type,dict_next_buy,dict_lastw_prod_cust=prepare_dicts_rating(df_train)\n\ndict_scores_customer_products=assign_scores_prod_cus(df_train,dict_cus_prod,dict_prod_type,dict_next_buy,dict_lastw_prod_cust)\n\ntop_30_products_by_customer=assign_top_30_products(dict_scores_customer_products)\n\n","metadata":{"execution":{"iopub.status.busy":"2023-07-31T04:14:56.55126Z","iopub.execute_input":"2023-07-31T04:14:56.551736Z","iopub.status.idle":"2023-07-31T04:25:47.105618Z","shell.execute_reply.started":"2023-07-31T04:14:56.551697Z","shell.execute_reply":"2023-07-31T04:25:47.104578Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dict_comprados=df_test.drop_duplicates([\"customer_id\",\"product_id\"]).groupby(\"customer_id\")[\"product_id\"].agg(list).to_dict()","metadata":{"execution":{"iopub.status.busy":"2023-07-31T04:25:47.106909Z","iopub.execute_input":"2023-07-31T04:25:47.107826Z","iopub.status.idle":"2023-07-31T04:25:47.324852Z","shell.execute_reply.started":"2023-07-31T04:25:47.10779Z","shell.execute_reply":"2023-07-31T04:25:47.323873Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"list_recalls=[]\n\nfor client in dict_comprados.keys():\n    \n    recall=len(set(dict_comprados[client]).intersection(top_30_products_by_customer[client]))/len(dict_comprados[client])\n    \n    list_recalls.append(recall)\n    \nnp.mean(list_recalls)\n\n\n\n","metadata":{"execution":{"iopub.status.busy":"2023-07-31T04:25:47.325958Z","iopub.execute_input":"2023-07-31T04:25:47.326824Z","iopub.status.idle":"2023-07-31T04:25:47.35839Z","shell.execute_reply.started":"2023-07-31T04:25:47.326787Z","shell.execute_reply":"2023-07-31T04:25:47.357149Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train[\"order_week\"]=df_train[\"order_week\"]+1\n\ndf_train=pd.concat([df_train,df_test],ignore_index=True).reset_index(drop=True)","metadata":{"execution":{"iopub.status.busy":"2023-07-31T04:29:17.296755Z","iopub.execute_input":"2023-07-31T04:29:17.297942Z","iopub.status.idle":"2023-07-31T04:29:18.074328Z","shell.execute_reply.started":"2023-07-31T04:29:17.297899Z","shell.execute_reply":"2023-07-31T04:29:18.0732Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del df_test ; gc.collect()","metadata":{"execution":{"iopub.status.busy":"2023-07-31T04:29:30.04384Z","iopub.execute_input":"2023-07-31T04:29:30.044233Z","iopub.status.idle":"2023-07-31T04:29:30.416754Z","shell.execute_reply.started":"2023-07-31T04:29:30.044204Z","shell.execute_reply":"2023-07-31T04:29:30.415504Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dict_cus_prod,dict_prod_type,dict_next_buy,dict_lastw_prod_cust=prepare_dicts_rating(df_train)\n\ndict_scores_customer_products=assign_scores_prod_cus(df_train,dict_cus_prod,dict_prod_type,dict_next_buy,dict_lastw_prod_cust)\n\ntop_30_products_by_customer_test=assign_top_30_products(dict_scores_customer_products)","metadata":{"execution":{"iopub.status.busy":"2023-07-31T04:30:01.149679Z","iopub.execute_input":"2023-07-31T04:30:01.150043Z","iopub.status.idle":"2023-07-31T04:41:30.240517Z","shell.execute_reply.started":"2023-07-31T04:30:01.150016Z","shell.execute_reply":"2023-07-31T04:41:30.239428Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_sub","metadata":{"execution":{"iopub.status.busy":"2023-07-31T04:44:13.670391Z","iopub.execute_input":"2023-07-31T04:44:13.670829Z","iopub.status.idle":"2023-07-31T04:44:13.680594Z","shell.execute_reply.started":"2023-07-31T04:44:13.670792Z","shell.execute_reply":"2023-07-31T04:44:13.679718Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n# Convert the dictionary to a list of dictionaries\ndata_list = [{'customer_id': customer_id, 'product_id': ' '.join(map(str, product_id_list))} for customer_id, product_id_list in top_30_products_by_customer_test.items()]\n\n# Create the DataFrame\ndf = pd.DataFrame(data_list)\n\n# Sort the DataFrame by customer_id (optional)\ndf.sort_values('customer_id', inplace=True)\n\n# Reset the index of the DataFrame\ndf.reset_index(drop=True, inplace=True)\n\nprint(df)","metadata":{"execution":{"iopub.status.busy":"2023-07-31T04:49:45.721192Z","iopub.execute_input":"2023-07-31T04:49:45.721648Z","iopub.status.idle":"2023-07-31T04:49:45.848626Z","shell.execute_reply.started":"2023-07-31T04:49:45.721597Z","shell.execute_reply":"2023-07-31T04:49:45.847461Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.to_csv(\"submission.csv\",index=False)","metadata":{"execution":{"iopub.status.busy":"2023-07-31T04:50:23.502675Z","iopub.execute_input":"2023-07-31T04:50:23.503619Z","iopub.status.idle":"2023-07-31T04:50:23.585664Z","shell.execute_reply.started":"2023-07-31T04:50:23.503566Z","shell.execute_reply":"2023-07-31T04:50:23.584641Z"},"trusted":true},"execution_count":null,"outputs":[]}]}