{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.14","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":30786,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"#### Các nội dung sử dụng chung","metadata":{}},{"cell_type":"code","source":"import numpy as np \nimport pandas as pd \n\narticles_df = pd.read_csv(\"/kaggle/input/h-and-m-personalized-fashion-recommendations/articles.csv\")\ncustomers_df = pd.read_csv(\"/kaggle/input/h-and-m-personalized-fashion-recommendations/customers.csv\")\nsample_submission_df = pd.read_csv(\"/kaggle/input/h-and-m-personalized-fashion-recommendations/sample_submission.csv\")\ntransactions_train_df = pd.read_csv(\"/kaggle/input/h-and-m-personalized-fashion-recommendations/transactions_train.csv\")","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-10-16T14:14:00.138131Z","iopub.execute_input":"2024-10-16T14:14:00.138561Z","iopub.status.idle":"2024-10-16T14:15:34.262344Z","shell.execute_reply.started":"2024-10-16T14:14:00.138522Z","shell.execute_reply":"2024-10-16T14:15:34.260979Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nfrom tqdm import tqdm\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nfrom wordcloud import WordCloud, STOPWORDS\nfrom datetime import datetime\nfrom PIL import Image","metadata":{"execution":{"iopub.status.busy":"2024-10-16T14:15:34.264876Z","iopub.execute_input":"2024-10-16T14:15:34.265321Z","iopub.status.idle":"2024-10-16T14:15:36.014529Z","shell.execute_reply.started":"2024-10-16T14:15:34.265280Z","shell.execute_reply":"2024-10-16T14:15:36.012811Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def missing_data(data):\n    total = data.isnull().sum().sort_values(ascending = False)\n    percent = (data.isnull().sum()/data.isnull().count()*100).sort_values(ascending = False)\n    return pd.concat([total, percent], axis=1, keys=['Total', 'Percent'])","metadata":{"execution":{"iopub.status.busy":"2024-10-16T13:03:55.993369Z","iopub.execute_input":"2024-10-16T13:03:55.993982Z","iopub.status.idle":"2024-10-16T13:03:56.002549Z","shell.execute_reply.started":"2024-10-16T13:03:55.993933Z","shell.execute_reply":"2024-10-16T13:03:56.000936Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## Hàm chuyển id sang dạng số \ndef build_map(data,col_name):\n    key = data[col_name].unique()\n    m = dict(zip(key, range(len(key))))\n    data[col_name] = data[col_name].map(lambda x: m[x])\n    return key, m","metadata":{"execution":{"iopub.status.busy":"2024-10-16T14:15:36.016453Z","iopub.execute_input":"2024-10-16T14:15:36.017152Z","iopub.status.idle":"2024-10-16T14:15:36.024748Z","shell.execute_reply.started":"2024-10-16T14:15:36.017101Z","shell.execute_reply":"2024-10-16T14:15:36.023400Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Thông tin liên quan đến sản phẩm","metadata":{}},{"cell_type":"code","source":"articles_df.head()","metadata":{"execution":{"iopub.status.busy":"2024-10-16T14:15:36.028135Z","iopub.execute_input":"2024-10-16T14:15:36.029414Z","iopub.status.idle":"2024-10-16T14:15:36.075430Z","shell.execute_reply.started":"2024-10-16T14:15:36.029339Z","shell.execute_reply":"2024-10-16T14:15:36.073859Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"articles_df.info()","metadata":{"execution":{"iopub.status.busy":"2024-10-16T13:04:08.803799Z","iopub.execute_input":"2024-10-16T13:04:08.804270Z","iopub.status.idle":"2024-10-16T13:04:09.006433Z","shell.execute_reply.started":"2024-10-16T13:04:08.804225Z","shell.execute_reply":"2024-10-16T13:04:09.005057Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"missing_data(articles_df)","metadata":{"execution":{"iopub.status.busy":"2024-10-15T11:21:26.299076Z","iopub.execute_input":"2024-10-15T11:21:26.299610Z","iopub.status.idle":"2024-10-15T11:21:26.832977Z","shell.execute_reply.started":"2024-10-15T11:21:26.299565Z","shell.execute_reply":"2024-10-15T11:21:26.831356Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"### Chuyển article_id về dạng số\n_, articles_map = build_map(articles_df,'article_id')\n\narticles_df.head()","metadata":{"execution":{"iopub.status.busy":"2024-10-16T14:15:36.076809Z","iopub.execute_input":"2024-10-16T14:15:36.077222Z","iopub.status.idle":"2024-10-16T14:15:36.264857Z","shell.execute_reply.started":"2024-10-16T14:15:36.077180Z","shell.execute_reply":"2024-10-16T14:15:36.263489Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"### Số lượng sản phẩm cho mỗi product_type\ntemp = articles_df.groupby([\"product_type_name\"])[\"article_id\"].nunique()\ndf = pd.DataFrame({'Product Type': temp.index,\n                   'Articles': temp.values\n                  })\ntotal_types = len(df['Product Type'].unique())\ndf = df.sort_values(['Articles'], ascending=False)[0:50]\nplt.figure(figsize = (16,6))\nplt.title(f'Number of Articles per each Product Type (top 50 from total: {total_types})')\nsns.set_color_codes(\"pastel\")\ns = sns.barplot(x = 'Product Type', y=\"Articles\", data=df)\ns.set_xticklabels(s.get_xticklabels(),rotation=90)\nlocs, labels = plt.xticks()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-10-15T11:21:27.844921Z","iopub.execute_input":"2024-10-15T11:21:27.845420Z","iopub.status.idle":"2024-10-15T11:21:34.600810Z","shell.execute_reply.started":"2024-10-15T11:21:27.845375Z","shell.execute_reply":"2024-10-15T11:21:34.599415Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"### Số lượng sản phẩm cho mỗi color_group\ntemp = articles_df.groupby([\"colour_group_name\"])[\"article_id\"].nunique()\ndf = pd.DataFrame({'Colour Group Name': temp.index,\n                   'Articles': temp.values\n                  })\ndf = df.sort_values(['Articles'], ascending=False)\nplt.figure(figsize = (12,6))\nplt.title(f'Number of Articles per each Colour Group Name')\nsns.set_color_codes(\"pastel\")\ns = sns.barplot(x = 'Colour Group Name', y=\"Articles\", data=df)\ns.set_xticklabels(s.get_xticklabels(),rotation=90)\nlocs, labels = plt.xticks()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-10-15T11:21:34.603199Z","iopub.execute_input":"2024-10-15T11:21:34.603639Z","iopub.status.idle":"2024-10-15T11:21:35.439238Z","shell.execute_reply.started":"2024-10-15T11:21:34.603593Z","shell.execute_reply":"2024-10-15T11:21:35.437349Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Thông tin liên quan đến khách hàng","metadata":{}},{"cell_type":"code","source":"customers_df.head()","metadata":{"execution":{"iopub.status.busy":"2024-10-16T13:16:02.693809Z","iopub.execute_input":"2024-10-16T13:16:02.694292Z","iopub.status.idle":"2024-10-16T13:16:02.715088Z","shell.execute_reply.started":"2024-10-16T13:16:02.694245Z","shell.execute_reply":"2024-10-16T13:16:02.713794Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"customers_df.info()","metadata":{"execution":{"iopub.status.busy":"2024-10-16T13:16:04.020622Z","iopub.execute_input":"2024-10-16T13:16:04.021107Z","iopub.status.idle":"2024-10-16T13:16:04.730164Z","shell.execute_reply.started":"2024-10-16T13:16:04.021063Z","shell.execute_reply":"2024-10-16T13:16:04.729011Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"missing_data(customers_df)","metadata":{"execution":{"iopub.status.busy":"2024-10-15T11:21:45.633140Z","iopub.execute_input":"2024-10-15T11:21:45.633911Z","iopub.status.idle":"2024-10-15T11:21:47.708283Z","shell.execute_reply.started":"2024-10-15T11:21:45.633845Z","shell.execute_reply":"2024-10-15T11:21:47.706497Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"### Điền những giá trị thiếu\ncustomer_column_name = ['customer_id', 'FN', 'Active', 'club_member_status',\n                        'fashion_news_frequency', 'age', 'postal_code']\n\ncustomers_df.loc[customers_df.club_member_status.isnull(), 'club_member_status'] = 'PRE-CREATE'\ncustomers_df.loc[customers_df.fashion_news_frequency.isnull(), 'fashion_news_frequency'] = 'NONE'\ncustomers_df.loc[customers_df.fashion_news_frequency == 'None', 'fashion_news_frequency'] = 'NONE'\ncustomers_df.loc[customers_df.FN.isnull(), 'FN'] = 0.0\ncustomers_df.loc[customers_df.Active.isnull(), 'Active'] = 0.0\n\n# customers_df.loc[customers_df.age.isnull(), 'age'] = customers_df.age.mode().values[0]\n### Chia khoảng cho tuổi, gán thành 1->6\n### Chưa biết nên fill giá trị thiếu như nào vì nó cũng có ảnh hưởng đến sự mua, đoạn code đang để mặc định là từ 20-29\ncustomers_df['age_block'] = pd.cut(customers_df['age'],[1,19,29,39,49,59,200],labels=[1,2,3,4,5,6]).fillna(2)\n\ncustomers_df.head()","metadata":{"execution":{"iopub.status.busy":"2024-10-16T14:15:36.266735Z","iopub.execute_input":"2024-10-16T14:15:36.267177Z","iopub.status.idle":"2024-10-16T14:15:36.971302Z","shell.execute_reply.started":"2024-10-16T14:15:36.267132Z","shell.execute_reply":"2024-10-16T14:15:36.970045Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"### Chuyển customer_id về dạng số\n_, customers_map = build_map(customers_df, 'customer_id')\n\ncustomers_df.head()","metadata":{"execution":{"iopub.status.busy":"2024-10-16T14:15:36.972786Z","iopub.execute_input":"2024-10-16T14:15:36.973166Z","iopub.status.idle":"2024-10-16T14:15:39.567786Z","shell.execute_reply.started":"2024-10-16T14:15:36.973125Z","shell.execute_reply":"2024-10-16T14:15:39.566468Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"### Số lượng khách hàng theo độ tuổi\ntemp = customers_df.groupby([\"age\"])[\"customer_id\"].count()\ndf = pd.DataFrame({'Age': temp.index,\n                   'Customers': temp.values\n                  })\ndf = df.sort_values(['Age'], ascending=False)\nplt.figure(figsize = (16,6))\nplt.title(f'Number of Customers per each Age')\nsns.set_color_codes(\"pastel\")\ns = sns.barplot(x = 'Age', y=\"Customers\", data=df)\ns.set_xticklabels(s.get_xticklabels(),rotation=90)\nlocs, labels = plt.xticks()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-10-15T11:21:48.718536Z","iopub.execute_input":"2024-10-15T11:21:48.720162Z","iopub.status.idle":"2024-10-15T11:21:50.110144Z","shell.execute_reply.started":"2024-10-15T11:21:48.720097Z","shell.execute_reply":"2024-10-15T11:21:50.108114Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Thông tin liên quan đến các giao dịch","metadata":{}},{"cell_type":"code","source":"transactions_train_df.head()","metadata":{"execution":{"iopub.status.busy":"2024-10-16T09:53:12.924906Z","iopub.execute_input":"2024-10-16T09:53:12.925393Z","iopub.status.idle":"2024-10-16T09:53:12.940426Z","shell.execute_reply.started":"2024-10-16T09:53:12.925350Z","shell.execute_reply":"2024-10-16T09:53:12.938950Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transactions_train_df.info()","metadata":{"execution":{"iopub.status.busy":"2024-10-16T09:53:15.221415Z","iopub.execute_input":"2024-10-16T09:53:15.221893Z","iopub.status.idle":"2024-10-16T09:53:15.234629Z","shell.execute_reply.started":"2024-10-16T09:53:15.221842Z","shell.execute_reply":"2024-10-16T09:53:15.233266Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"missing_data(transactions_train_df)","metadata":{"execution":{"iopub.status.busy":"2024-10-15T11:21:59.120441Z","iopub.execute_input":"2024-10-15T11:21:59.121181Z","iopub.status.idle":"2024-10-15T11:22:10.586326Z","shell.execute_reply.started":"2024-10-15T11:21:59.121120Z","shell.execute_reply":"2024-10-15T11:22:10.584853Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"### Định dạng lại customer_id và article_id\ntransactions_train_df['customer_id'] = transactions_train_df['customer_id'].map(lambda x: customers_map.get(x, x))\ntransactions_train_df['article_id'] = transactions_train_df['article_id'].map(lambda x: articles_map.get(x, x))\n\ntransactions_train_df.head()","metadata":{"execution":{"iopub.status.busy":"2024-10-16T14:15:39.569379Z","iopub.execute_input":"2024-10-16T14:15:39.569737Z","iopub.status.idle":"2024-10-16T14:16:51.665036Z","shell.execute_reply.started":"2024-10-16T14:15:39.569700Z","shell.execute_reply":"2024-10-16T14:16:51.663550Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"### Lấy thuộc tính t_week, t_month, t_quarter từ t_dat\ntransactions_train_df['t_dat'] = pd.to_datetime(transactions_train_df['t_dat'])\n\n# Thêm cột t_week\ntransactions_train_df['t_week'] = transactions_train_df['t_dat'].dt.isocalendar().week\n\n# Thêm cột t_month\ntransactions_train_df['t_month'] = transactions_train_df['t_dat'].dt.month\n\n# Thêm cột t_quarter\ntransactions_train_df['t_quarter'] = transactions_train_df['t_dat'].dt.quarter\n\ntransactions_train_df.head()","metadata":{"execution":{"iopub.status.busy":"2024-10-16T14:16:51.667172Z","iopub.execute_input":"2024-10-16T14:16:51.667945Z","iopub.status.idle":"2024-10-16T14:17:01.925020Z","shell.execute_reply.started":"2024-10-16T14:16:51.667882Z","shell.execute_reply":"2024-10-16T14:17:01.923024Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"### Số lượng giao dịch mỗi ngày\ntransactions_train_df['t_dat'] = transactions_train_df['t_dat'].astype(str)\ndf = transactions_train_df.groupby([\"t_dat\"])[\"article_id\"].count().reset_index()\ndf[\"t_dat\"] = df[\"t_dat\"].apply(lambda x: datetime.strptime(x, '%Y-%m-%d'))\ndf.columns = [\"Date\", \"Transactions\"]\nfig, ax = plt.subplots(1, 1, figsize=(16,6))\nplt.plot(df[\"Date\"], df[\"Transactions\"], color=\"Darkgreen\")\nplt.xlabel(\"Date\")\nplt.ylabel(\"Transactions\")\nplt.title(\"Transactions per day\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-10-15T11:26:12.074079Z","iopub.execute_input":"2024-10-15T11:26:12.075170Z","iopub.status.idle":"2024-10-15T11:26:55.380688Z","shell.execute_reply.started":"2024-10-15T11:26:12.075113Z","shell.execute_reply":"2024-10-15T11:26:55.378933Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transactions_train_df['t_dat'] = pd.to_datetime(transactions_train_df['t_dat'])\ntransactions_train_df['month'] = transactions_train_df['t_dat'].dt.to_period('M')\nmonthly_transactions = transactions_train_df.groupby('month').size()\n\nplt.figure(figsize=(16, 6))\nmonthly_transactions.plot(kind='line', marker='o')\nplt.title('Sự biến động số lượng giao dịch theo tháng')\nplt.xlabel('Tháng')\nplt.ylabel('Số lượng giao dịch')\nplt.grid(True)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-10-15T11:23:21.667880Z","iopub.execute_input":"2024-10-15T11:23:21.668592Z","iopub.status.idle":"2024-10-15T11:23:25.444823Z","shell.execute_reply.started":"2024-10-15T11:23:21.668533Z","shell.execute_reply":"2024-10-15T11:23:25.442785Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"### Đếm số lượng giao dịch trong 7 ngày, 1 tháng, 3 tháng ứng với mỗi khách hàng\n# Định nghĩa các khoảng thời gian\nlast_7_days = transactions_train_df['t_dat'].max() - pd.DateOffset(days=7)\nlast_1_month = transactions_train_df['t_dat'].max() - pd.DateOffset(months=1)\nlast_3_months = transactions_train_df['t_dat'].max() - pd.DateOffset(months=3)\n\n# Đếm số lượng giao dịch cho từng khách hàng trong từng khoảng thời gian\ntransactions_last_7_days = transactions_train_df[transactions_train_df['t_dat'] >= last_7_days] \\\n                            .groupby('customer_id').size().reset_index(name='transactions_last_7_days')\n\ntransactions_last_1_month = transactions_train_df[transactions_train_df['t_dat'] >= last_1_month] \\\n                            .groupby('customer_id').size().reset_index(name='transactions_last_1_month')\n\ntransactions_last_3_months = transactions_train_df[transactions_train_df['t_dat'] >= last_3_months] \\\n                             .groupby('customer_id').size().reset_index(name='transactions_last_3_months')\n\n# Gộp các kết quả lại thành một dataframe\ncustomer_transactions_summary = pd.merge(transactions_last_7_days, transactions_last_1_month, on='customer_id', how='outer')\ncustomer_transactions_summary = pd.merge(customer_transactions_summary, transactions_last_3_months, on='customer_id', how='outer')\n\n# Điền giá trị NaN (nếu có) bằng 0\ncustomer_transactions_summary.fillna(0, inplace=True)\n\n# Lưu kết quả ra file CSV\ncustomer_transactions_summary.to_csv('customer_transactions_summary.csv', index=False)\n\n# Kiểm tra kết quả\ncustomer_transactions_summary.head()","metadata":{"execution":{"iopub.status.busy":"2024-10-16T13:41:27.111314Z","iopub.execute_input":"2024-10-16T13:41:27.111765Z","iopub.status.idle":"2024-10-16T13:41:30.261630Z","shell.execute_reply.started":"2024-10-16T13:41:27.111713Z","shell.execute_reply":"2024-10-16T13:41:30.258875Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"### Ngày mua hàng gần nhất và số ngày kể từ ngày mua hàng gần nhất của mỗi khách hàng\n\n# Tính toán thời gian mua hàng gần nhất\ntransactions_last = transactions_train_df.groupby('customer_id')['t_dat'].max().reset_index()\ntransactions_last.columns = ['customer_id', 't_dat_nearly']\n\n# Tính số ngày mua hàng gần nhất\ncurrent_date = transactions_train_df['t_dat'].max()\ntransactions_last['t_dat_nearly_number'] = (current_date - transactions_last['t_dat_nearly']).dt.days\n\n# Gộp thông tin vào dataframe tóm tắt\ncustomer_transactions_summary = pd.merge(customer_transactions_summary, transactions_last, on='customer_id', how='left')\n\ncustomer_transactions_summary.head()","metadata":{"execution":{"iopub.status.busy":"2024-10-16T13:47:23.872817Z","iopub.execute_input":"2024-10-16T13:47:23.873234Z","iopub.status.idle":"2024-10-16T13:47:26.524093Z","shell.execute_reply.started":"2024-10-16T13:47:23.873191Z","shell.execute_reply":"2024-10-16T13:47:26.522929Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"### Sản phẩm gần nhất và sản phẩm mua nhiều nhất của khách hàng\n\n# Thêm thuộc tính sản phẩm gần nhất mà khách hàng mua\ntransactions_last_product = transactions_train_df.loc[transactions_train_df.groupby('customer_id')['t_dat'].idxmax(), ['customer_id', 'article_id']]\ntransactions_last_product.columns = ['customer_id', 'last_product']\n\n# Tính sản phẩm mà khách hàng mua nhiều nhất\nmost_purchased_product = transactions_train_df.groupby('customer_id')['article_id'].agg(lambda x: x.mode()[0]).reset_index()\nmost_purchased_product.columns = ['customer_id', 'most_purchased_product']\n\n# Gộp thông tin vào dataframe tóm tắt\ncustomer_transactions_summary = pd.merge(customer_transactions_summary, transactions_last_product, on='customer_id', how='left')\ncustomer_transactions_summary = pd.merge(customer_transactions_summary, most_purchased_product, on='customer_id', how='left')\n\ncustomer_transactions_summary.head()","metadata":{"execution":{"iopub.status.busy":"2024-10-16T13:50:48.601252Z","iopub.execute_input":"2024-10-16T13:50:48.602594Z","iopub.status.idle":"2024-10-16T13:54:33.637856Z","shell.execute_reply.started":"2024-10-16T13:50:48.602539Z","shell.execute_reply":"2024-10-16T13:54:33.636703Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Nhóm theo article_id và đếm số lượng giao dịch\narticle_transaction_count = transactions_train_df.groupby('article_id').size().reset_index(name='transaction_count')\n\n# Sắp xếp theo số lượng giao dịch giảm dần\narticle_transaction_count = article_transaction_count.sort_values(by='transaction_count', ascending=False)\n\narticle_transaction_count.head(10)","metadata":{"execution":{"iopub.status.busy":"2024-10-16T13:59:14.875438Z","iopub.execute_input":"2024-10-16T13:59:14.875974Z","iopub.status.idle":"2024-10-16T13:59:15.704401Z","shell.execute_reply.started":"2024-10-16T13:59:14.875917Z","shell.execute_reply":"2024-10-16T13:59:15.703208Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Tạo dictionary để lưu trữ thông tin\ncustomer_product_dict = {}\n\nfor index, row in transactions_train_df.iterrows():\n    customer_id = row['customer_id']\n    article_id = row['article_id']\n    \n    if customer_id not in customer_product_dict:\n        customer_product_dict[customer_id] = {}\n        \n    if article_id not in customer_product_dict[customer_id]:\n        customer_product_dict[customer_id][article_id] = 0\n        \n    customer_product_dict[customer_id][article_id] += 1","metadata":{"execution":{"iopub.status.busy":"2024-10-16T14:17:01.929355Z","iopub.execute_input":"2024-10-16T14:17:01.929806Z","iopub.status.idle":"2024-10-16T14:49:23.328115Z","shell.execute_reply.started":"2024-10-16T14:17:01.929751Z","shell.execute_reply":"2024-10-16T14:49:23.325374Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}