{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":31254,"databundleVersionId":3103714,"sourceType":"competition"}],"dockerImageVersionId":30587,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import os\nimport numpy as np\nimport pandas as pd\nimport seaborn as sns\nfrom matplotlib import pyplot as plt\nfrom tqdm.notebook import tqdm\n# !pip install cudf","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-11-19T17:52:21.688049Z","iopub.execute_input":"2023-11-19T17:52:21.688427Z","iopub.status.idle":"2023-11-19T17:52:22.713391Z","shell.execute_reply.started":"2023-11-19T17:52:21.688398Z","shell.execute_reply":"2023-11-19T17:52:22.712422Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# https://www.kaggle.com/code/vanguarde/h-m-eda-first-look\n# https://www.kaggle.com/code/cdeotte/recommend-items-purchased-together-0-021\narticles = pd.read_csv(\"../input/h-and-m-personalized-fashion-recommendations/articles.csv\")\ncustomers = pd.read_csv(\"../input/h-and-m-personalized-fashion-recommendations/customers.csv\")\ntransactions = pd.read_csv(\"../input/h-and-m-personalized-fashion-recommendations/transactions_train.csv\")","metadata":{"execution":{"iopub.status.busy":"2023-11-19T17:52:22.715249Z","iopub.execute_input":"2023-11-19T17:52:22.715739Z","iopub.status.idle":"2023-11-19T17:53:36.458469Z","shell.execute_reply.started":"2023-11-19T17:52:22.715703Z","shell.execute_reply":"2023-11-19T17:53:36.457598Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"articles['prod_name'].unique()","metadata":{"execution":{"iopub.status.busy":"2023-11-19T17:53:36.463431Z","iopub.execute_input":"2023-11-19T17:53:36.463726Z","iopub.status.idle":"2023-11-19T17:53:36.502677Z","shell.execute_reply.started":"2023-11-19T17:53:36.463701Z","shell.execute_reply":"2023-11-19T17:53:36.501630Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"articles.sample()","metadata":{"execution":{"iopub.status.busy":"2023-11-19T17:53:36.505068Z","iopub.execute_input":"2023-11-19T17:53:36.505708Z","iopub.status.idle":"2023-11-19T17:53:36.563888Z","shell.execute_reply.started":"2023-11-19T17:53:36.505670Z","shell.execute_reply":"2023-11-19T17:53:36.563045Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transactions.sample()","metadata":{"execution":{"iopub.status.busy":"2023-11-19T17:53:36.565160Z","iopub.execute_input":"2023-11-19T17:53:36.565496Z","iopub.status.idle":"2023-11-19T17:53:37.945808Z","shell.execute_reply.started":"2023-11-19T17:53:36.565464Z","shell.execute_reply":"2023-11-19T17:53:37.944859Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = transactions.copy()\n# train['customer_id'] = train['customer_id'].str[-16:].str.hex_to_int().astype('int64')\ntrain['article_id'] = train.article_id.astype('int32')\ntrain.t_dat = pd.to_datetime(train.t_dat)\ntrain = train[['t_dat','customer_id','article_id']]\ntrain.to_parquet('train.pqt',index=False)\nprint( train.shape )\ntrain.sample()","metadata":{"execution":{"iopub.status.busy":"2023-11-19T17:53:37.947112Z","iopub.execute_input":"2023-11-19T17:53:37.947432Z","iopub.status.idle":"2023-11-19T17:53:59.297742Z","shell.execute_reply.started":"2023-11-19T17:53:37.947407Z","shell.execute_reply":"2023-11-19T17:53:59.296802Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Find Each Customer's Last Week of Purchases\n","metadata":{}},{"cell_type":"code","source":"tmp = train.groupby('customer_id').t_dat.max().reset_index()\ntmp.columns = ['customer_id','max_dat']\ntrain = train.merge(tmp,on=['customer_id'],how='left')\ntrain['diff_dat'] = (train.max_dat - train.t_dat).dt.days\ntrain = train.loc[train['diff_dat']<=6]\nprint('Train shape:',train.shape)","metadata":{"execution":{"iopub.status.busy":"2023-11-19T17:53:59.299210Z","iopub.execute_input":"2023-11-19T17:53:59.299882Z","iopub.status.idle":"2023-11-19T17:54:25.693103Z","shell.execute_reply.started":"2023-11-19T17:53:59.299844Z","shell.execute_reply":"2023-11-19T17:54:25.692198Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.merge(train, articles[['article_id', 'product_type_name']], how='left', on = 'article_id')\ntrain = train[train['product_type_name'] == 'Shirt']","metadata":{"execution":{"iopub.status.busy":"2023-11-19T17:54:25.694385Z","iopub.execute_input":"2023-11-19T17:54:25.696787Z","iopub.status.idle":"2023-11-19T17:54:27.555491Z","shell.execute_reply.started":"2023-11-19T17:54:25.696759Z","shell.execute_reply":"2023-11-19T17:54:27.554597Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### (1) Recommend Most Often Previously Purchased Items","metadata":{}},{"cell_type":"code","source":"tmp = train.groupby(['customer_id','article_id'])['t_dat'].agg('count').reset_index()\ntmp.columns = ['customer_id','article_id','ct']\ntrain = train.merge(tmp,on=['customer_id','article_id'],how='left')\ntrain = train.sort_values(['ct','t_dat'],ascending=False)\ntrain = train.drop_duplicates(['customer_id','article_id'])\ntrain = train.sort_values(['ct','t_dat'],ascending=False)\ntrain.head()","metadata":{"execution":{"iopub.status.busy":"2023-11-19T17:54:27.556985Z","iopub.execute_input":"2023-11-19T17:54:27.557332Z","iopub.status.idle":"2023-11-19T17:54:28.020637Z","shell.execute_reply.started":"2023-11-19T17:54:27.557300Z","shell.execute_reply":"2023-11-19T17:54:28.019637Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.shape","metadata":{"execution":{"iopub.status.busy":"2023-11-19T17:54:28.024912Z","iopub.execute_input":"2023-11-19T17:54:28.025249Z","iopub.status.idle":"2023-11-19T17:54:28.031284Z","shell.execute_reply.started":"2023-11-19T17:54:28.025222Z","shell.execute_reply":"2023-11-19T17:54:28.030291Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install apyori","metadata":{"execution":{"iopub.status.busy":"2023-11-19T17:54:28.032669Z","iopub.execute_input":"2023-11-19T17:54:28.032949Z","iopub.status.idle":"2023-11-19T17:54:45.923997Z","shell.execute_reply.started":"2023-11-19T17:54:28.032925Z","shell.execute_reply":"2023-11-19T17:54:45.922984Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from apyori import apriori","metadata":{"execution":{"iopub.status.busy":"2023-11-19T17:54:45.925603Z","iopub.execute_input":"2023-11-19T17:54:45.926043Z","iopub.status.idle":"2023-11-19T17:54:45.937309Z","shell.execute_reply.started":"2023-11-19T17:54:45.925986Z","shell.execute_reply":"2023-11-19T17:54:45.936531Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# First, calculate the number of articles per customer\narticle_counts = train.groupby('customer_id')['article_id'].count()\n\n# Now, filter out customers who have 10 or more articles\ncustomers_to_keep = article_counts[article_counts < 10].index\n\n# Finally, filter the original DataFrame\nfiltered_train = train[train['customer_id'].isin(customers_to_keep)]","metadata":{"execution":{"iopub.status.busy":"2023-11-19T17:54:45.938382Z","iopub.execute_input":"2023-11-19T17:54:45.938685Z","iopub.status.idle":"2023-11-19T17:54:46.295537Z","shell.execute_reply.started":"2023-11-19T17:54:45.938661Z","shell.execute_reply":"2023-11-19T17:54:46.294475Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ntransaction_counts = filtered_train.groupby('customer_id').size().reset_index()\nsns.histplot(data=transaction_counts, x=0)","metadata":{"execution":{"iopub.status.busy":"2023-11-19T17:54:46.297068Z","iopub.execute_input":"2023-11-19T17:54:46.297483Z","iopub.status.idle":"2023-11-19T17:54:46.879621Z","shell.execute_reply.started":"2023-11-19T17:54:46.297446Z","shell.execute_reply":"2023-11-19T17:54:46.878746Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# transactions_list = filtered_train.groupby('customer_id')['article_id'].apply(list).tolist()\n# transactions_list = filtered_train.groupby('customer_id')['article_id'].apply(lambda x: list(set(x))).tolist()\ntransactions_list = filtered_train.groupby('customer_id')['article_id'].apply(\n    lambda x: list(set(x))[:2] if len(set(x)) >= 2 else None\n).dropna().tolist()","metadata":{"execution":{"iopub.status.busy":"2023-11-19T17:54:46.880776Z","iopub.execute_input":"2023-11-19T17:54:46.881062Z","iopub.status.idle":"2023-11-19T17:54:49.906972Z","shell.execute_reply.started":"2023-11-19T17:54:46.881036Z","shell.execute_reply":"2023-11-19T17:54:49.906097Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"filtered_train['article_id'].nunique()","metadata":{"execution":{"iopub.status.busy":"2023-11-19T17:54:49.908352Z","iopub.execute_input":"2023-11-19T17:54:49.908763Z","iopub.status.idle":"2023-11-19T17:54:49.916430Z","shell.execute_reply.started":"2023-11-19T17:54:49.908722Z","shell.execute_reply":"2023-11-19T17:54:49.915359Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(transactions_list)","metadata":{"execution":{"iopub.status.busy":"2023-11-19T17:54:49.917452Z","iopub.execute_input":"2023-11-19T17:54:49.917719Z","iopub.status.idle":"2023-11-19T17:54:49.931475Z","shell.execute_reply.started":"2023-11-19T17:54:49.917695Z","shell.execute_reply":"2023-11-19T17:54:49.930558Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n# rules = apriori(transactions = transactions_list, min_support = 0.003, min_confidence = 0.2, min_lift = 3, min_length = 2, max_length = 2)\nrules = apriori(transactions = transactions_list, min_support=0.001, min_confidence=0.001, min_lift=1.001, min_length=2, max_length=2)\n# rules = apriori(transactions = transactions_list, min_support = 0.003, min_confidence = 0.2, min_lift = 3, min_length = 2, max_length = 2)","metadata":{"execution":{"iopub.status.busy":"2023-11-19T17:54:49.932535Z","iopub.execute_input":"2023-11-19T17:54:49.932758Z","iopub.status.idle":"2023-11-19T17:54:49.945486Z","shell.execute_reply.started":"2023-11-19T17:54:49.932738Z","shell.execute_reply":"2023-11-19T17:54:49.944635Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"results = list(rules)\nlen(results)","metadata":{"execution":{"iopub.status.busy":"2023-11-19T17:54:49.946407Z","iopub.execute_input":"2023-11-19T17:54:49.946735Z","iopub.status.idle":"2023-11-19T17:54:50.429985Z","shell.execute_reply.started":"2023-11-19T17:54:49.946704Z","shell.execute_reply":"2023-11-19T17:54:50.429104Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def inspect(results):\n    lhs         = [tuple(result[2][0][0])[0] for result in results]\n    rhs         = [tuple(result[2][0][1])[0] for result in results]\n    supports    = [result[1] for result in results]\n    confidences = [result[2][0][2] for result in results]\n    lifts       = [result[2][0][3] for result in results]\n    return list(zip(lhs, rhs, supports, confidences, lifts))\nresultsinDataFrame = pd.DataFrame(inspect(results), columns = ['Left Hand Side', 'Right Hand Side', 'Support', 'Confidence', 'Lift'])","metadata":{"execution":{"iopub.status.busy":"2023-11-19T17:54:50.431106Z","iopub.execute_input":"2023-11-19T17:54:50.431390Z","iopub.status.idle":"2023-11-19T17:54:50.438875Z","shell.execute_reply.started":"2023-11-19T17:54:50.431366Z","shell.execute_reply":"2023-11-19T17:54:50.438046Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Group by 'Left Hand Side' and find the index of the max 'Lift' value in each group\nmax_lift_indices = resultsinDataFrame.groupby('Left Hand Side')['Lift'].idxmax()\n\n# Filter the original DataFrame to keep only rows with the maximum 'Lift' per 'Left Hand Side'\nfiltered_df = resultsinDataFrame.loc[max_lift_indices]","metadata":{"execution":{"iopub.status.busy":"2023-11-19T17:54:50.440060Z","iopub.execute_input":"2023-11-19T17:54:50.440327Z","iopub.status.idle":"2023-11-19T17:54:50.456250Z","shell.execute_reply.started":"2023-11-19T17:54:50.440304Z","shell.execute_reply":"2023-11-19T17:54:50.455564Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"filtered_df.shape","metadata":{"execution":{"iopub.status.busy":"2023-11-19T17:54:50.457409Z","iopub.execute_input":"2023-11-19T17:54:50.457653Z","iopub.status.idle":"2023-11-19T17:54:50.472979Z","shell.execute_reply.started":"2023-11-19T17:54:50.457631Z","shell.execute_reply":"2023-11-19T17:54:50.472166Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"filtered_df.nlargest(n = 10, columns = 'Lift')","metadata":{"execution":{"iopub.status.busy":"2023-11-19T17:54:50.473861Z","iopub.execute_input":"2023-11-19T17:54:50.474130Z","iopub.status.idle":"2023-11-19T17:54:50.496604Z","shell.execute_reply.started":"2023-11-19T17:54:50.474107Z","shell.execute_reply":"2023-11-19T17:54:50.495612Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"BASE = '../input/h-and-m-personalized-fashion-recommendations/images/'\n\nname1 = BASE+'0'+str(855748001)[:2]+'/0'+str(855748001)+'.jpg'\nname2 = BASE+'0'+str(855748002)[:2]+'/0'+str(855748002)+'.jpg'","metadata":{"execution":{"iopub.status.busy":"2023-11-19T18:16:45.353160Z","iopub.execute_input":"2023-11-19T18:16:45.354056Z","iopub.status.idle":"2023-11-19T18:16:45.359155Z","shell.execute_reply.started":"2023-11-19T18:16:45.354023Z","shell.execute_reply":"2023-11-19T18:16:45.358183Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import cv2, matplotlib.pyplot as plt\nplt.figure(figsize=(20,5))\nimg1 = cv2.imread(name1)[:,:,::-1]\nplt.imshow(img1)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-11-19T18:16:47.417620Z","iopub.execute_input":"2023-11-19T18:16:47.417979Z","iopub.status.idle":"2023-11-19T18:16:48.014046Z","shell.execute_reply.started":"2023-11-19T18:16:47.417949Z","shell.execute_reply":"2023-11-19T18:16:48.013056Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img2 = cv2.imread(name2)[:,:,::-1]\nplt.imshow(img2)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-11-19T18:16:49.199982Z","iopub.execute_input":"2023-11-19T18:16:49.200799Z","iopub.status.idle":"2023-11-19T18:16:49.775690Z","shell.execute_reply.started":"2023-11-19T18:16:49.200764Z","shell.execute_reply":"2023-11-19T18:16:49.774662Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}