{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":31254,"databundleVersionId":3103714,"sourceType":"competition"}],"dockerImageVersionId":30664,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport matplotlib.image as mpimg\nimport seaborn as sns\nfrom mlxtend.preprocessing import TransactionEncoder\nfrom mlxtend.frequent_patterns import apriori, association_rules, fpgrowth","metadata":{"ExecuteTime":{"end_time":"2024-03-01T04:13:36.413760Z","start_time":"2024-03-01T04:13:36.278391Z"},"execution":{"iopub.status.busy":"2024-03-06T16:36:08.108299Z","iopub.execute_input":"2024-03-06T16:36:08.108782Z","iopub.status.idle":"2024-03-06T16:36:09.498136Z","shell.execute_reply.started":"2024-03-06T16:36:08.108742Z","shell.execute_reply":"2024-03-06T16:36:09.496516Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n\n# Read the CSV file into a pandas DataFrame\ntransaction_data = pd.read_csv('/kaggle/input/h-and-m-personalized-fashion-recommendations/transactions_train.csv')\n\n# Convert the last 16 characters of 'customer_id' to hex, then to int64\ntransaction_data['customer_id'] = transaction_data['customer_id'].str[-16:].apply(lambda x: int(x, 16)).astype('int64')\n\n# Convert 'article_id' to int32\ntransaction_data['article_id'] = transaction_data['article_id'].astype('int32')\n\n# Convert 't_dat' to datetime\ntransaction_data['t_dat'] = pd.to_datetime(transaction_data['t_dat'])\n\n# Select specific columns and rearrange the DataFrame\ntransaction_data = transaction_data[['t_dat', 'customer_id', 'article_id', 'price']]\n\n# Display the shape of the DataFrame and its first few rows\nprint(transaction_data.shape)\ntransaction_data.head()\n","metadata":{"ExecuteTime":{"end_time":"2024-03-06T07:04:08.368476Z","start_time":"2024-03-06T07:04:08.211658Z"},"execution":{"iopub.status.busy":"2024-03-06T16:44:52.455816Z","iopub.execute_input":"2024-03-06T16:44:52.456263Z","iopub.status.idle":"2024-03-06T16:46:34.697219Z","shell.execute_reply.started":"2024-03-06T16:44:52.456230Z","shell.execute_reply":"2024-03-06T16:46:34.695658Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd","metadata":{"collapsed":false,"ExecuteTime":{"end_time":"2024-03-06T07:04:13.069797Z","start_time":"2024-03-06T07:04:13.025764Z"},"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## Data preprocess\ntransaction_data.loc[:,'t_dat'] = pd.to_datetime(transaction_data['t_dat'])\ntransaction_data.loc[:, 'year'] = transaction_data['t_dat'].dt.year\ntransaction_data.loc[:, 'month'] = transaction_data['t_dat'].dt.month\ntransaction_data.loc[:, 'day'] = transaction_data['t_dat'].dt.day\ntransaction_data.head()","metadata":{"execution":{"iopub.status.busy":"2024-03-06T16:47:09.491332Z","iopub.execute_input":"2024-03-06T16:47:09.491715Z","iopub.status.idle":"2024-03-06T16:47:13.179874Z","shell.execute_reply.started":"2024-03-06T16:47:09.491683Z","shell.execute_reply":"2024-03-06T16:47:13.178596Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transaction_data.to_parquet('transaction_data_hm.pqt',index=False)","metadata":{"execution":{"iopub.status.busy":"2024-03-06T16:47:16.583104Z","iopub.execute_input":"2024-03-06T16:47:16.584100Z","iopub.status.idle":"2024-03-06T16:47:23.743509Z","shell.execute_reply.started":"2024-03-06T16:47:16.584058Z","shell.execute_reply":"2024-03-06T16:47:23.742048Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transaction_data = pd.read_parquet(\"./transaction_data_hm.pqt\")","metadata":{"execution":{"iopub.status.busy":"2024-03-06T16:47:30.465791Z","iopub.execute_input":"2024-03-06T16:47:30.466290Z","iopub.status.idle":"2024-03-06T16:47:31.740878Z","shell.execute_reply.started":"2024-03-06T16:47:30.466254Z","shell.execute_reply":"2024-03-06T16:47:31.739608Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.countplot(x=transaction_data['year']);","metadata":{"execution":{"iopub.status.busy":"2024-03-06T16:47:35.523668Z","iopub.execute_input":"2024-03-06T16:47:35.524123Z","iopub.status.idle":"2024-03-06T16:47:37.941892Z","shell.execute_reply.started":"2024-03-06T16:47:35.524084Z","shell.execute_reply":"2024-03-06T16:47:37.940588Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.xlabel(\"count of transactions\")\nplt.ylabel(\"months\")\ntransaction_data.month.value_counts().plot(kind=\"barh\");","metadata":{"execution":{"iopub.status.busy":"2024-03-06T16:47:43.647660Z","iopub.execute_input":"2024-03-06T16:47:43.648084Z","iopub.status.idle":"2024-03-06T16:47:44.193824Z","shell.execute_reply.started":"2024-03-06T16:47:43.648049Z","shell.execute_reply":"2024-03-06T16:47:44.192544Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.ylabel(\"count of transactions\")\nplt.xlabel(\"days\")\nsns.countplot(x=\"day\", data=transaction_data);","metadata":{"execution":{"iopub.status.busy":"2024-03-06T16:47:48.348390Z","iopub.execute_input":"2024-03-06T16:47:48.348861Z","iopub.status.idle":"2024-03-06T16:47:50.919794Z","shell.execute_reply.started":"2024-03-06T16:47:48.348822Z","shell.execute_reply":"2024-03-06T16:47:50.918505Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transactions_2020 =  transaction_data[transaction_data['year'] == 2020]\nplt.xticks(rotation=90)\nplt.xlabel(\"2020 data\") \nsns.lineplot(x=\"t_dat\", y=\"price\", data=transactions_2020);","metadata":{"execution":{"iopub.status.busy":"2024-03-06T16:48:00.353326Z","iopub.execute_input":"2024-03-06T16:48:00.354786Z","iopub.status.idle":"2024-03-06T16:49:35.057291Z","shell.execute_reply.started":"2024-03-06T16:48:00.354740Z","shell.execute_reply":"2024-03-06T16:49:35.055811Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transaction_2020 = transaction_data[transaction_data['year'] == 2020]","metadata":{"execution":{"iopub.status.busy":"2024-03-06T16:50:09.478470Z","iopub.execute_input":"2024-03-06T16:50:09.478923Z","iopub.status.idle":"2024-03-06T16:50:09.967845Z","shell.execute_reply.started":"2024-03-06T16:50:09.478887Z","shell.execute_reply":"2024-03-06T16:50:09.966441Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"max(transaction_2020.price), min(transaction_2020.price)","metadata":{"execution":{"iopub.status.busy":"2024-03-06T16:50:27.519192Z","iopub.execute_input":"2024-03-06T16:50:27.520488Z","iopub.status.idle":"2024-03-06T16:50:30.827913Z","shell.execute_reply.started":"2024-03-06T16:50:27.520368Z","shell.execute_reply":"2024-03-06T16:50:30.826499Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transactions_article_ids_value_counts = transaction_2020.article_id.value_counts()\ntransactions_article_ids_top_20 = transactions_article_ids_value_counts[:20].index.to_list()\narticles_data = pd.read_csv(\"/kaggle/input/h-and-m-personalized-fashion-recommendations/articles.csv\", index_col = \"article_id\")\ntop_20_articles = articles_data.filter(items=transactions_article_ids_top_20, axis=0)\ntop_20_articles","metadata":{"execution":{"iopub.status.busy":"2024-03-06T16:52:09.808485Z","iopub.execute_input":"2024-03-06T16:52:09.808907Z","iopub.status.idle":"2024-03-06T16:52:10.771545Z","shell.execute_reply.started":"2024-03-06T16:52:09.808875Z","shell.execute_reply":"2024-03-06T16:52:10.770326Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.xticks(rotation=45)\nsns.countplot(x = top_20_articles.product_type_name);","metadata":{"execution":{"iopub.status.busy":"2024-03-06T16:52:16.765939Z","iopub.execute_input":"2024-03-06T16:52:16.766359Z","iopub.status.idle":"2024-03-06T16:52:17.097259Z","shell.execute_reply.started":"2024-03-06T16:52:16.766328Z","shell.execute_reply":"2024-03-06T16:52:17.095921Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"names, values = top_20_articles.product_group_name.value_counts().index, top_20_articles.product_group_name.value_counts().values\nplt.pie(values, labels = names, autopct='%1.1f%%');","metadata":{"execution":{"iopub.status.busy":"2024-03-06T16:52:20.048108Z","iopub.execute_input":"2024-03-06T16:52:20.048544Z","iopub.status.idle":"2024-03-06T16:52:20.254110Z","shell.execute_reply.started":"2024-03-06T16:52:20.048511Z","shell.execute_reply":"2024-03-06T16:52:20.252399Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"count_top_20_transaction_articles = transactions_article_ids_value_counts[transactions_article_ids_top_20[:10]].values\nprice_top_20_transaction_articles = transaction_2020[transaction_2020.article_id.isin(transactions_article_ids_top_20)]\nprice_top_20_transaction_articles = price_top_20_transaction_articles.groupby('article_id').agg({\"price\": \"sum\"}).sort_values('price',ascending=False)","metadata":{"execution":{"iopub.status.busy":"2024-03-06T16:52:26.208708Z","iopub.execute_input":"2024-03-06T16:52:26.210117Z","iopub.status.idle":"2024-03-06T16:52:26.407613Z","shell.execute_reply.started":"2024-03-06T16:52:26.210061Z","shell.execute_reply":"2024-03-06T16:52:26.406514Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"top_20_articles = price_top_20_transaction_articles.merge(articles_data, how = 'left', left_index = True, right_index = True)\nsns.barplot(y = \"prod_name\", x = \"price\", data = top_20_articles);","metadata":{"execution":{"iopub.status.busy":"2024-03-06T16:52:28.297862Z","iopub.execute_input":"2024-03-06T16:52:28.298330Z","iopub.status.idle":"2024-03-06T16:52:28.821755Z","shell.execute_reply.started":"2024-03-06T16:52:28.298293Z","shell.execute_reply":"2024-03-06T16:52:28.820498Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path = \"/kaggle/input/h-and-m-personalized-fashion-recommendations/images\"\nf, ax = plt.subplots(1, 5, figsize=(20,10))\ni = 0\nfor idx, data in top_20_articles[:5].iterrows():\n    file_name = \"0\" + str(idx) + \".jpg\"\n    dir_name = \"0\" + str(idx)[:2]\n    image = mpimg.imread(path + \"/\" + dir_name + \"/\" + file_name)\n    ax[i].imshow(image)\n    ax[i].set_title(f'price: {data.price:.2f}')\n    ax[i].set_xticks([], [])\n    ax[i].set_yticks([], [])\n    ax[i].grid(False)\n    ax[i].set_xlabel(data['prod_name'], fontsize=10)\n    i += 1\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-03-06T16:52:35.753381Z","iopub.execute_input":"2024-03-06T16:52:35.753871Z","iopub.status.idle":"2024-03-06T16:52:37.792313Z","shell.execute_reply.started":"2024-03-06T16:52:35.753831Z","shell.execute_reply":"2024-03-06T16:52:37.791183Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n\n# Read customers data and convert 'customer_id' to int64\ncustomers_data = pd.read_csv(\"/kaggle/input/h-and-m-personalized-fashion-recommendations/customers.csv\")\ncustomers_data['customer_id'] = customers_data['customer_id'].str[-16:].apply(lambda x: int(x, 16)).astype('int64')\n\n# Read articles data\narticles_data = pd.read_csv(\"/kaggle/input/h-and-m-personalized-fashion-recommendations/articles.csv\")\n\n# Filter transaction data for the year 2020, month > 6, and day > 20\ntransactions_data_2020 = transaction_data[(transaction_data['t_dat'].dt.year == 2020) & \n                                           (transaction_data['t_dat'].dt.month > 6) & \n                                           (transaction_data['t_dat'].dt.day > 20)]\n\n# Group transactions in 2020 by customer_id and retrieve unique article_ids\ntransactions_customers_all_articles = transactions_data_2020.groupby('customer_id')['article_id'].unique().reset_index()\n","metadata":{"execution":{"iopub.status.busy":"2024-03-06T16:53:27.318578Z","iopub.execute_input":"2024-03-06T16:53:27.319024Z","iopub.status.idle":"2024-03-06T16:53:56.795947Z","shell.execute_reply.started":"2024-03-06T16:53:27.318981Z","shell.execute_reply":"2024-03-06T16:53:56.794276Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport matplotlib.image as mpimg\n\nprint(\"Articles that customers purchased together in single transaction: \")\nfor _, data in transactions_customers_all_articles.head(5).iterrows():\n    print(\"Customer id\", data['customer_id'])\n    f, ax = plt.subplots(1, len(data['article_id']), figsize=(6, 6))\n    path = \"/kaggle/input/h-and-m-personalized-fashion-recommendations/images\"\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        i += 1\n    plt.show()\n","metadata":{"execution":{"iopub.status.busy":"2024-03-06T16:55:40.917608Z","iopub.execute_input":"2024-03-06T16:55:40.918124Z","iopub.status.idle":"2024-03-06T16:55:46.255893Z","shell.execute_reply.started":"2024-03-06T16:55:40.918084Z","shell.execute_reply":"2024-03-06T16:55:46.254711Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"top_customers_id = transaction_2020[ 'customer_id'].value_counts().index.to_list()\ntop_10_customers = customers_data[customers_data['customer_id'].isin(top_customers_id[:10])]\ntop_10_customers","metadata":{"execution":{"iopub.status.busy":"2024-03-06T16:55:52.198356Z","iopub.execute_input":"2024-03-06T16:55:52.198860Z","iopub.status.idle":"2024-03-06T16:55:52.837520Z","shell.execute_reply.started":"2024-03-06T16:55:52.198818Z","shell.execute_reply":"2024-03-06T16:55:52.836499Z"},"trusted":true},"execution_count":null,"outputs":[]}]}