{"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":"# 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)\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":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-03-27T18:47:50.034668Z","iopub.execute_input":"2023-03-27T18:47:50.035112Z","iopub.status.idle":"2023-03-27T18:50:04.360885Z","shell.execute_reply.started":"2023-03-27T18:47:50.035072Z","shell.execute_reply":"2023-03-27T18:50:04.359788Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import cudf\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport matplotlib.image as mpimg\nfrom mlxtend.preprocessing import TransactionEncoder\nfrom mlxtend.frequent_patterns import apriori, association_rules, fpgrowth","metadata":{"execution":{"iopub.status.busy":"2023-03-27T18:50:04.362716Z","iopub.execute_input":"2023-03-27T18:50:04.3637Z","iopub.status.idle":"2023-03-27T18:50:04.415525Z","shell.execute_reply.started":"2023-03-27T18:50:04.363656Z","shell.execute_reply":"2023-03-27T18:50:04.414511Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transcation_data = cudf.read_csv('../input/h-and-m-personalized-fashion-recommendations/transactions_train.csv')\ntranscation_data['customer_id'] = transcation_data['customer_id'].str[-16:].str.hex_to_int().astype('int64')\ntranscation_data['article_id'] = transcation_data.article_id.astype('int32')\ntranscation_data.t_dat = cudf.to_datetime(transcation_data.t_dat)\ntranscation_data = transcation_data[['t_dat','customer_id','article_id', 'price']]\nprint( transcation_data.shape )\ntranscation_data.head()","metadata":{"execution":{"iopub.status.busy":"2023-03-27T18:50:45.708288Z","iopub.execute_input":"2023-03-27T18:50:45.709191Z","iopub.status.idle":"2023-03-27T18:51:28.594506Z","shell.execute_reply.started":"2023-03-27T18:50:45.709139Z","shell.execute_reply":"2023-03-27T18:51:28.593347Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transcation_data.loc[:,'t_dat'] = cudf.to_datetime(transcation_data['t_dat'])\ntranscation_data.loc[:, 'year'] = transcation_data['t_dat'].dt.year\ntranscation_data.loc[:, 'month'] = transcation_data['t_dat'].dt.month\ntranscation_data.loc[:, 'day'] = transcation_data['t_dat'].dt.day\ntranscation_data.head()","metadata":{"execution":{"iopub.status.busy":"2023-03-27T18:52:06.087412Z","iopub.execute_input":"2023-03-27T18:52:06.088353Z","iopub.status.idle":"2023-03-27T18:52:06.134633Z","shell.execute_reply.started":"2023-03-27T18:52:06.0883Z","shell.execute_reply":"2023-03-27T18:52:06.133659Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transcation_data.to_parquet('transcation_data_hm.pqt',index=False)","metadata":{"execution":{"iopub.status.busy":"2023-03-27T18:52:29.996532Z","iopub.execute_input":"2023-03-27T18:52:29.996913Z","iopub.status.idle":"2023-03-27T18:52:30.694753Z","shell.execute_reply.started":"2023-03-27T18:52:29.996877Z","shell.execute_reply":"2023-03-27T18:52:30.693707Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transcation_data = pd.read_parquet(\"./transcation_data_hm.pqt\")","metadata":{"execution":{"iopub.status.busy":"2023-03-27T18:52:46.07471Z","iopub.execute_input":"2023-03-27T18:52:46.075329Z","iopub.status.idle":"2023-03-27T18:52:48.198492Z","shell.execute_reply.started":"2023-03-27T18:52:46.075284Z","shell.execute_reply":"2023-03-27T18:52:48.197507Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transcations_data_2020 = transcation_data[(transcation_data['year'] == 2020) & (transcation_data['month'] > 6) & (transcation_data['day'] > 20)]\ntranscation_aprori = transcations_data_2020.groupby('customer_id')['article_id'].unique().reset_index()\ntranscation_aprori","metadata":{"execution":{"iopub.status.busy":"2023-03-27T18:53:03.345263Z","iopub.execute_input":"2023-03-27T18:53:03.345644Z","iopub.status.idle":"2023-03-27T18:53:13.530681Z","shell.execute_reply.started":"2023-03-27T18:53:03.345609Z","shell.execute_reply":"2023-03-27T18:53:13.529514Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"te = TransactionEncoder()\nte.fit(transcation_aprori['article_id'])\norders_one_hot_encoded = te.transform(transcation_aprori['article_id'])\n\norders_one_hot_encoded = pd.DataFrame(orders_one_hot_encoded, columns =te.columns_)\norders_one_hot_encoded.head()","metadata":{"execution":{"iopub.status.busy":"2023-03-27T18:53:37.202522Z","iopub.execute_input":"2023-03-27T18:53:37.202949Z","iopub.status.idle":"2023-03-27T18:53:40.291357Z","shell.execute_reply.started":"2023-03-27T18:53:37.20291Z","shell.execute_reply":"2023-03-27T18:53:40.290326Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"results = apriori(orders_one_hot_encoded, min_support=0.003, max_len=2, use_colnames=True)\nresults.sort_values(by=['support'])","metadata":{"execution":{"iopub.status.busy":"2023-03-27T18:53:59.279469Z","iopub.execute_input":"2023-03-27T18:53:59.280415Z","iopub.status.idle":"2023-03-27T18:54:12.644476Z","shell.execute_reply.started":"2023-03-27T18:53:59.280375Z","shell.execute_reply":"2023-03-27T18:54:12.643413Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"association_rules = association_rules(results, metric=\"lift\")\nassociation_rules.head()","metadata":{"execution":{"iopub.status.busy":"2023-03-27T18:54:25.65911Z","iopub.execute_input":"2023-03-27T18:54:25.659855Z","iopub.status.idle":"2023-03-27T18:54:25.673553Z","shell.execute_reply.started":"2023-03-27T18:54:25.659809Z","shell.execute_reply":"2023-03-27T18:54:25.672476Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transcations_data_2020 = transcation_data[(transcation_data['year'] == 2020) & (transcation_data['month'] > 6)]\ntranscation_apri = transcations_data_2020.groupby('customer_id')['article_id'].unique().reset_index()\nte = TransactionEncoder()\nte.fit(transcation_apri['article_id'])\norders_1hot = te.transform(transcation_apri['article_id'])\norders_1hot = pd.DataFrame(orders_1hot, columns =te.columns_)\nfpgrowth(orders_1hot, min_support=0.6, use_colnames=True)","metadata":{"execution":{"iopub.status.busy":"2023-03-27T18:54:40.892379Z","iopub.execute_input":"2023-03-27T18:54:40.893351Z","iopub.status.idle":"2023-03-27T18:55:32.728553Z","shell.execute_reply.started":"2023-03-27T18:54:40.893307Z","shell.execute_reply":"2023-03-27T18:55:32.727384Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transcation_aprori","metadata":{"execution":{"iopub.status.busy":"2023-03-27T18:55:46.439357Z","iopub.execute_input":"2023-03-27T18:55:46.440335Z","iopub.status.idle":"2023-03-27T18:55:46.454801Z","shell.execute_reply.started":"2023-03-27T18:55:46.440296Z","shell.execute_reply":"2023-03-27T18:55:46.453774Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"customers_data = cudf.read_csv(\"../input/h-and-m-personalized-fashion-recommendations/customers.csv\")\ncustomers_data['customer_id'] = customers_data['customer_id'].str[-16:].str.hex_to_int().astype('int64')\narticles_data = pd.read_csv(\"../input/h-and-m-personalized-fashion-recommendations/articles.csv\")","metadata":{"execution":{"iopub.status.busy":"2023-03-27T18:56:03.815188Z","iopub.execute_input":"2023-03-27T18:56:03.815677Z","iopub.status.idle":"2023-03-27T18:56:08.240227Z","shell.execute_reply.started":"2023-03-27T18:56:03.815638Z","shell.execute_reply":"2023-03-27T18:56:08.239193Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Items that customers buy together\")\nfor _, data in transcation_aprori.head(5).iterrows():\n    path = \"../input/h-and-m-personalized-fashion-recommendations/images\"\n    f, ax = plt.subplots(1, len(data['article_id']), figsize=(6, 6))\n    for i, article in enumerate(data['article_id']):\n        file_name = \"0\" + str(article) + \".jpg\"\n        dir_name = \"0\" + str(article)[:2]\n        image = mpimg.imread(path + \"/\" + dir_name + \"/\" + file_name)\n        ax[i].imshow(image)\n        ax[i].set_xticks([], [])\n        ax[i].set_yticks([], [])\n        ax[i].grid(False)\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2023-03-27T18:56:22.689308Z","iopub.execute_input":"2023-03-27T18:56:22.690011Z","iopub.status.idle":"2023-03-27T18:56:27.623451Z","shell.execute_reply.started":"2023-03-27T18:56:22.68997Z","shell.execute_reply":"2023-03-27T18:56:27.622398Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print customers_data.head(10)","metadata":{"execution":{"iopub.status.busy":"2023-03-30T07:32:58.805444Z","iopub.execute_input":"2023-03-30T07:32:58.806156Z","iopub.status.idle":"2023-03-30T07:32:58.835599Z","shell.execute_reply.started":"2023-03-30T07:32:58.806125Z","shell.execute_reply":"2023-03-30T07:32:58.834271Z"},"trusted":true},"execution_count":null,"outputs":[]}]}