{"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\n# for 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-10-11T18:22:13.760683Z","iopub.execute_input":"2023-10-11T18:22:13.761098Z","iopub.status.idle":"2023-10-11T18:22:13.766954Z","shell.execute_reply.started":"2023-10-11T18:22:13.761070Z","shell.execute_reply":"2023-10-11T18:22:13.765802Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Load data về articles\narticles = pd.read_csv(\"/kaggle/input/h-and-m-personalized-fashion-recommendations/articles.csv\")","metadata":{"execution":{"iopub.status.busy":"2023-10-11T18:22:13.778819Z","iopub.execute_input":"2023-10-11T18:22:13.779265Z","iopub.status.idle":"2023-10-11T18:22:14.496519Z","shell.execute_reply.started":"2023-10-11T18:22:13.779193Z","shell.execute_reply":"2023-10-11T18:22:14.495094Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Kích thước articles\narticles.shape","metadata":{"execution":{"iopub.status.busy":"2023-10-11T18:22:14.498619Z","iopub.execute_input":"2023-10-11T18:22:14.499590Z","iopub.status.idle":"2023-10-11T18:22:14.505614Z","shell.execute_reply.started":"2023-10-11T18:22:14.499553Z","shell.execute_reply":"2023-10-11T18:22:14.504778Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Thông tin articles\n# Gần như tất cả các cột đều không có dữ liệu trống (trừ cột detail_desc)\narticles.info()","metadata":{"execution":{"iopub.status.busy":"2023-10-11T18:22:14.507030Z","iopub.execute_input":"2023-10-11T18:22:14.507362Z","iopub.status.idle":"2023-10-11T18:22:14.621359Z","shell.execute_reply.started":"2023-10-11T18:22:14.507338Z","shell.execute_reply":"2023-10-11T18:22:14.620187Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Lấy các giá trị độc nhất của các cột\nfor col in ['product_code','prod_name','product_type_no','product_type_name','product_group_name']:\n    n = articles[col].nunique()\n    print(f'n of unique {col} is: {n}')","metadata":{"execution":{"iopub.status.busy":"2023-10-11T18:22:14.624378Z","iopub.execute_input":"2023-10-11T18:22:14.625973Z","iopub.status.idle":"2023-10-11T18:22:14.684129Z","shell.execute_reply.started":"2023-10-11T18:22:14.625929Z","shell.execute_reply":"2023-10-11T18:22:14.683131Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 1. Xử lý product_code và prod_name\nTrong H&M EDA FIRST LOOK có ghi \n**\"product_code, prod_name : A unique identifier of every product and its name (not the same).\"**\n\nMỗi sản phẩm có duy nhất một mã code và một tên.\n\nDo số giá trị độc nhất của product_code và prod_name là khác nhau nên có thể xuất hiện một trong 2 trường hợp sau:\n* Tồn tại mã code nào đó mà có nhiều hơn 2 sản phẩm\n* Tồn tại sản phẩm nào đó mà có nhiều hơn 2 mã code","metadata":{}},{"cell_type":"code","source":"# Lọc các sản phẩm có cùng product_code nhưng khác prod_name\n# Cách giải quyết: các sản phẩm có cùng product_code được viết về cùng prod_name đầu tiên của chúng\n# Lưu vào cột product_name mới\narticles['product_name'] = articles.groupby('product_code')['prod_name'].transform('first')\narticles.head()  ","metadata":{"execution":{"iopub.status.busy":"2023-10-11T18:22:14.685670Z","iopub.execute_input":"2023-10-11T18:22:14.686671Z","iopub.status.idle":"2023-10-11T18:22:14.734561Z","shell.execute_reply.started":"2023-10-11T18:22:14.686634Z","shell.execute_reply":"2023-10-11T18:22:14.733497Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Lọc các sản phẩm có cùng prod_name nhưng khác product_code\n# Cách giải quyết: các sản phẩm có cùng prod_name được viết về cùng product_code đầu tiên của chúng\narticles['product_code_new'] = articles.groupby('product_name')['product_code'].transform('first')\narticles.head()  ","metadata":{"execution":{"iopub.status.busy":"2023-10-11T18:22:14.735866Z","iopub.execute_input":"2023-10-11T18:22:14.736850Z","iopub.status.idle":"2023-10-11T18:22:14.824805Z","shell.execute_reply.started":"2023-10-11T18:22:14.736813Z","shell.execute_reply":"2023-10-11T18:22:14.823810Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 2 Cột mới là product_code_new và product_name tương ứng một một\nprint(articles['product_name'].nunique())\nprint(articles['product_code_new'].nunique())","metadata":{"execution":{"iopub.status.busy":"2023-10-11T18:22:14.826418Z","iopub.execute_input":"2023-10-11T18:22:14.827005Z","iopub.status.idle":"2023-10-11T18:22:14.852663Z","shell.execute_reply.started":"2023-10-11T18:22:14.826969Z","shell.execute_reply":"2023-10-11T18:22:14.851632Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 2. Xử lý product_type_no, product_type_name và product_group_name\nTa chia làm 2 bước xử lý là :\n* product_type_no và product_type_name\n* product_type_name và product_group_name","metadata":{}},{"cell_type":"markdown","source":"2.1. Xử lý product_type_no và product_type_name\n\nSố phần tử độc nhất của product_type_no là 132, của product_type_name là 131. Do đó, có hai product_type_no có cùng product_type_name. Nhưng do có ánh xạ từ product_type_no đến product_type_name và không nhất thiết là song ánh nên có thể giữ nguyên điều này","metadata":{}},{"cell_type":"code","source":"# product_type_name có hai product_type_no \nresult = articles.groupby('product_type_name')['product_type_no'].nunique()\nresult[result > 1].index","metadata":{"execution":{"iopub.status.busy":"2023-10-11T18:22:14.853809Z","iopub.execute_input":"2023-10-11T18:22:14.854106Z","iopub.status.idle":"2023-10-11T18:22:14.878179Z","shell.execute_reply.started":"2023-10-11T18:22:14.854075Z","shell.execute_reply":"2023-10-11T18:22:14.877221Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Những sản phẩm có product_type_no = -1 thì có product_type_name = product_group_name = Unknown\n# Cách giải quyết: Vẫn nên giữ lại những sản phẩm này do các thuộc tính khác vẫn có thông tin nhưng nên tránh các thuộc tính này\narticles[(articles['product_type_no']==-1)]","metadata":{"execution":{"iopub.status.busy":"2023-10-11T18:22:14.879448Z","iopub.execute_input":"2023-10-11T18:22:14.879717Z","iopub.status.idle":"2023-10-11T18:22:14.908775Z","shell.execute_reply.started":"2023-10-11T18:22:14.879694Z","shell.execute_reply":"2023-10-11T18:22:14.907834Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from matplotlib import pyplot as plt\nimport seaborn as sns","metadata":{"execution":{"iopub.status.busy":"2023-10-11T18:22:14.912885Z","iopub.execute_input":"2023-10-11T18:22:14.913177Z","iopub.status.idle":"2023-10-11T18:22:14.917466Z","shell.execute_reply.started":"2023-10-11T18:22:14.913144Z","shell.execute_reply":"2023-10-11T18:22:14.916612Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"f, ax = plt.subplots(figsize=(15, 7))\nax = sns.histplot(data=articles, y='product_group_name', color='orange')\nax.set_xlabel('count by group name')\nax.set_ylabel('product group name')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-10-11T18:22:14.918657Z","iopub.execute_input":"2023-10-11T18:22:14.918970Z","iopub.status.idle":"2023-10-11T18:22:15.508039Z","shell.execute_reply.started":"2023-10-11T18:22:14.918946Z","shell.execute_reply":"2023-10-11T18:22:15.507154Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Nhóm Garment Upper body chiếm đa số\narticles.groupby('product_group_name').count()['article_id']","metadata":{"execution":{"iopub.status.busy":"2023-10-11T18:22:15.509230Z","iopub.execute_input":"2023-10-11T18:22:15.509597Z","iopub.status.idle":"2023-10-11T18:22:15.613171Z","shell.execute_reply.started":"2023-10-11T18:22:15.509564Z","shell.execute_reply":"2023-10-11T18:22:15.612310Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Mỗi product_type_name có đúng một product_group_name\npd.options.display.max_rows = None\narticles.groupby(['product_group_name', 'product_type_name']).count()['article_id']","metadata":{"execution":{"iopub.status.busy":"2023-10-11T18:22:15.614545Z","iopub.execute_input":"2023-10-11T18:22:15.614903Z","iopub.status.idle":"2023-10-11T18:22:15.716901Z","shell.execute_reply.started":"2023-10-11T18:22:15.614870Z","shell.execute_reply":"2023-10-11T18:22:15.716198Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 3. Xử lý FN","metadata":{}},{"cell_type":"code","source":"# Load data customers\ncustomers = pd.read_csv(\"/kaggle/input/h-and-m-personalized-fashion-recommendations/customers.csv\")","metadata":{"execution":{"iopub.status.busy":"2023-10-11T18:22:15.717774Z","iopub.execute_input":"2023-10-11T18:22:15.718053Z","iopub.status.idle":"2023-10-11T18:22:19.765767Z","shell.execute_reply.started":"2023-10-11T18:22:15.718030Z","shell.execute_reply":"2023-10-11T18:22:19.764351Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Thông tin của customers\ncustomers.info()","metadata":{"execution":{"iopub.status.busy":"2023-10-11T18:22:19.767145Z","iopub.execute_input":"2023-10-11T18:22:19.767565Z","iopub.status.idle":"2023-10-11T18:22:20.066028Z","shell.execute_reply.started":"2023-10-11T18:22:19.767524Z","shell.execute_reply":"2023-10-11T18:22:20.065004Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Kích thước data\ncustomers.shape","metadata":{"execution":{"iopub.status.busy":"2023-10-11T18:22:20.067165Z","iopub.execute_input":"2023-10-11T18:22:20.067479Z","iopub.status.idle":"2023-10-11T18:22:20.074431Z","shell.execute_reply.started":"2023-10-11T18:22:20.067455Z","shell.execute_reply":"2023-10-11T18:22:20.073339Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Mỗi khách hàng tương ứng với một và chỉ một mã id\ncustomers['customer_id'].nunique()","metadata":{"execution":{"iopub.status.busy":"2023-10-11T18:22:20.075701Z","iopub.execute_input":"2023-10-11T18:22:20.075946Z","iopub.status.idle":"2023-10-11T18:22:20.651715Z","shell.execute_reply.started":"2023-10-11T18:22:20.075925Z","shell.execute_reply":"2023-10-11T18:22:20.650672Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### FN ? Fashion News ? (Dự đoán)","metadata":{}},{"cell_type":"code","source":"# Các giá trị độc nhất của 'fashion_news_frequency'\ncustomers['fashion_news_frequency'].unique()","metadata":{"execution":{"iopub.status.busy":"2023-10-11T18:22:20.653455Z","iopub.execute_input":"2023-10-11T18:22:20.653911Z","iopub.status.idle":"2023-10-11T18:22:20.734546Z","shell.execute_reply.started":"2023-10-11T18:22:20.653876Z","shell.execute_reply":"2023-10-11T18:22:20.733307Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Các giá trị độc nhất của 'FN'\ncustomers['FN'].unique()","metadata":{"execution":{"iopub.status.busy":"2023-10-11T18:22:20.736002Z","iopub.execute_input":"2023-10-11T18:22:20.736821Z","iopub.status.idle":"2023-10-11T18:22:20.762608Z","shell.execute_reply.started":"2023-10-11T18:22:20.736781Z","shell.execute_reply":"2023-10-11T18:22:20.761557Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Nếu FN là fashion news thì khi 'fashion_news_frequency' bằng 'Monthly','Regularly', 'FN' phải bằng 1**","metadata":{}},{"cell_type":"code","source":"# Nhưng vẫn có 2119 hàng có giá trị 'fashion_news_frequency', ko có giá trị 'FN'\n# Cách giải quyết: Có thể đặt giá trị của các hàng này bằng 1\ncustomers[(customers['FN']!=1)&(customers['fashion_news_frequency'].isin(['Monthly','Regularly']))].count()","metadata":{"execution":{"iopub.status.busy":"2023-10-11T18:22:20.763921Z","iopub.execute_input":"2023-10-11T18:22:20.764197Z","iopub.status.idle":"2023-10-11T18:22:20.836134Z","shell.execute_reply.started":"2023-10-11T18:22:20.764173Z","shell.execute_reply":"2023-10-11T18:22:20.835315Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"customers['club_member_status'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2023-10-11T18:22:20.837221Z","iopub.execute_input":"2023-10-11T18:22:20.837949Z","iopub.status.idle":"2023-10-11T18:22:20.950371Z","shell.execute_reply.started":"2023-10-11T18:22:20.837920Z","shell.execute_reply":"2023-10-11T18:22:20.949313Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Nếu trạng thái thành viên là LEFT CLUB thì FN bằng NONE\ncustomers[(customers['FN'] == 1)&(customers['club_member_status'].isin(['LEFT CLUB ']))].count()","metadata":{"execution":{"iopub.status.busy":"2023-10-11T18:22:20.952169Z","iopub.execute_input":"2023-10-11T18:22:20.952572Z","iopub.status.idle":"2023-10-11T18:22:20.988635Z","shell.execute_reply.started":"2023-10-11T18:22:20.952538Z","shell.execute_reply":"2023-10-11T18:22:20.987545Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Số lượng các cột khi FN bằng 1\n# Phần lớn khi đó Active = 1\ncustomers[customers['FN']==1].count()","metadata":{"execution":{"iopub.status.busy":"2023-10-11T18:22:20.990207Z","iopub.execute_input":"2023-10-11T18:22:20.990813Z","iopub.status.idle":"2023-10-11T18:22:21.191790Z","shell.execute_reply.started":"2023-10-11T18:22:20.990785Z","shell.execute_reply":"2023-10-11T18:22:21.190518Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Đa phần khi FN = 1 thì 'fashion_news_frequency' bằng 'Regularly'\nsns.set_style(\"darkgrid\")\nf, ax = plt.subplots(figsize=(10,5))\nax = sns.histplot(data=customers[customers['FN']==1], x='fashion_news_frequency', color='orange')\nax.set_xlabel('Distribution of club member status')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-10-11T18:22:21.193187Z","iopub.execute_input":"2023-10-11T18:22:21.193519Z","iopub.status.idle":"2023-10-11T18:22:22.012566Z","shell.execute_reply.started":"2023-10-11T18:22:21.193495Z","shell.execute_reply":"2023-10-11T18:22:22.011725Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Đa phần khi FN = 1 thì 'club_member_status' bằng 'Active'\nsns.set_style(\"darkgrid\")\nf, ax = plt.subplots(figsize=(10,5))\nax = sns.histplot(data=customers[customers['FN']==1], x='club_member_status', color='orange')\nax.set_xlabel('Distribution of club member status')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-10-11T18:22:22.013779Z","iopub.execute_input":"2023-10-11T18:22:22.014056Z","iopub.status.idle":"2023-10-11T18:22:22.831079Z","shell.execute_reply.started":"2023-10-11T18:22:22.014033Z","shell.execute_reply":"2023-10-11T18:22:22.830064Z"},"trusted":true},"execution_count":null,"outputs":[]}]}