{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.12.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceType":"competition","sourceId":31254,"databundleVersionId":3103714}],"dockerImageVersionId":31286,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"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\n# import numpy as np # linear algebra\n# import 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\n# import 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","trusted":true,"execution":{"iopub.status.busy":"2026-03-16T06:44:30.543012Z","iopub.execute_input":"2026-03-16T06:44:30.543326Z","iopub.status.idle":"2026-03-16T06:44:30.547972Z","shell.execute_reply.started":"2026-03-16T06:44:30.543292Z","shell.execute_reply":"2026-03-16T06:44:30.547199Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Lệnh này chỉ liệt kê các thư mục cấp 1, không in chi tiết file\n!ls /kaggle/input/competitions/h-and-m-personalized-fashion-recommendations/","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-16T06:44:30.549614Z","iopub.execute_input":"2026-03-16T06:44:30.549875Z","iopub.status.idle":"2026-03-16T06:44:30.677365Z","shell.execute_reply.started":"2026-03-16T06:44:30.549825Z","shell.execute_reply":"2026-03-16T06:44:30.676395Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pandas as pd\nimport os\nimport shutil\nfrom tqdm import tqdm\n\n# 1. Đọc dữ liệu (Kaggle lưu ở đường dẫn /kaggle/input/...)\npath = \"/kaggle/input/competitions/h-and-m-personalized-fashion-recommendations/\"\ntransactions = pd.read_csv(path + \"transactions_train.csv\")\narticles = pd.read_csv(path + \"articles.csv\")\n\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-16T06:44:30.678628Z","iopub.execute_input":"2026-03-16T06:44:30.678989Z","iopub.status.idle":"2026-03-16T06:45:50.257218Z","shell.execute_reply.started":"2026-03-16T06:44:30.678940Z","shell.execute_reply":"2026-03-16T06:45:50.256289Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# 2. Tìm Top 2000 khách hàng mua nhiều nhất để đảm bảo dữ liệu có độ kết nối cao\ntop_customers = transactions['customer_id'].value_counts().head(2000).index\nsub_transactions = transactions[transactions['customer_id'].isin(top_customers)]\n\n# 3. Lấy danh sách sản phẩm từ các khách hàng này cho đến khi đủ khoảng 10,000 món\nunique_articles = sub_transactions['article_id'].unique()\nchosen_articles = unique_articles[:10000]\n\n# 4. Lọc lại file articles và transactions theo danh sách 10k món này\nfinal_articles = articles[articles['article_id'].isin(chosen_articles)]\nfinal_transactions = sub_transactions[sub_transactions['article_id'].isin(chosen_articles)]\n\n# 5. Lưu thành file CSV mới để dùng cho Backend sau này\nfinal_articles.to_csv(\"sub_articles_10k.csv\", index=False)\nfinal_transactions.to_csv(\"sub_transactions_10k.csv\", index=False)\n\nprint(f\"Đã lọc xong: {len(final_articles)} sản phẩm và {len(final_transactions)} giao dịch.\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-16T06:45:50.258518Z","iopub.execute_input":"2026-03-16T06:45:50.258901Z","iopub.status.idle":"2026-03-16T06:45:59.307317Z","shell.execute_reply.started":"2026-03-16T06:45:50.258865Z","shell.execute_reply":"2026-03-16T06:45:59.306404Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Tạo thư mục chứa ảnh lọc\nos.makedirs(\"selected_images\", exist_ok=True)\n\nprint(\"Đang sao chép hình ảnh...\")\ncount = 0\nfor art_id in tqdm(chosen_articles):\n    # ID ảnh trong H&M có định dạng: 012/012345678.jpg (3 số đầu là tên thư mục)\n    folder = \"0\" + str(art_id)[:2]\n    filename = \"0\" + str(art_id) + \".jpg\"\n    src_path = os.path.join(path, \"images\", folder, filename)\n    dst_path = os.path.join(\"selected_images\", filename)\n    \n    if os.path.exists(src_path):\n        shutil.copy(src_path, dst_path)\n        count += 1\n\nprint(f\"Hoàn thành! Đã chép {count} ảnh vào thư mục 'selected_images'.\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-16T06:45:59.309617Z","iopub.execute_input":"2026-03-16T06:45:59.309930Z","iopub.status.idle":"2026-03-16T06:49:12.422115Z","shell.execute_reply.started":"2026-03-16T06:45:59.309902Z","shell.execute_reply":"2026-03-16T06:49:12.421328Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# 1. Nén thư mục ảnh thành file zip (lệnh này chạy rất nhanh)\n!zip -q -r selected_images_10k.zip selected_images\n\n# 2. Kiểm tra xem file zip đã xuất hiện chưa\nimport os\nif os.path.exists(\"selected_images_10k.zip\"):\n    print(\"Nén thành công! Hãy nhấn nút Refresh ở mục Output để thấy file zip.\")\nelse:\n    print(\"Có lỗi xảy ra khi nén.\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-16T06:49:12.423346Z","iopub.execute_input":"2026-03-16T06:49:12.423719Z","iopub.status.idle":"2026-03-16T06:51:38.359490Z","shell.execute_reply.started":"2026-03-16T06:49:12.423692Z","shell.execute_reply":"2026-03-16T06:51:38.357798Z"}},"outputs":[],"execution_count":null}]}