{"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":[{"sourceId":31254,"databundleVersionId":3103714,"sourceType":"competition"}],"dockerImageVersionId":31259,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"!ls /kaggle/input","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2026-01-19T10:47:20.315624Z","iopub.execute_input":"2026-01-19T10:47:20.316708Z","iopub.status.idle":"2026-01-19T10:47:20.441837Z","shell.execute_reply.started":"2026-01-19T10:47:20.316674Z","shell.execute_reply":"2026-01-19T10:47:20.440369Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!ls /kaggle/input/h-and-m-personalized-fashion-recommendations","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-19T10:47:20.946573Z","iopub.execute_input":"2026-01-19T10:47:20.947237Z","iopub.status.idle":"2026-01-19T10:47:21.074729Z","shell.execute_reply.started":"2026-01-19T10:47:20.947187Z","shell.execute_reply":"2026-01-19T10:47:21.073339Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nfrom sklearn.model_selection import train_test_split","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-19T10:47:21.077124Z","iopub.execute_input":"2026-01-19T10:47:21.077582Z","iopub.status.idle":"2026-01-19T10:47:26.877044Z","shell.execute_reply.started":"2026-01-19T10:47:21.077545Z","shell.execute_reply":"2026-01-19T10:47:26.875869Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"DATA_PATH = \"/kaggle/input/h-and-m-personalized-fashion-recommendations/\"\n\ncustomers = pd.read_csv(DATA_PATH + \"customers.csv\")\narticles = pd.read_csv(DATA_PATH + \"articles.csv\")\ntransactions = pd.read_csv(\n    DATA_PATH + \"transactions_train.csv\",\n    parse_dates=[\"t_dat\"]\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-19T10:47:26.878230Z","iopub.execute_input":"2026-01-19T10:47:26.878907Z","iopub.status.idle":"2026-01-19T10:48:42.481094Z","shell.execute_reply.started":"2026-01-19T10:47:26.878869Z","shell.execute_reply":"2026-01-19T10:48:42.479909Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(\"Customers:\", customers.shape)\nprint(\"Articles:\", articles.shape)\nprint(\"Transactions:\", transactions.shape)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-19T10:48:42.484212Z","iopub.execute_input":"2026-01-19T10:48:42.485239Z","iopub.status.idle":"2026-01-19T10:48:42.491055Z","shell.execute_reply.started":"2026-01-19T10:48:42.485206Z","shell.execute_reply":"2026-01-19T10:48:42.489823Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"customers.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-19T10:48:42.492422Z","iopub.execute_input":"2026-01-19T10:48:42.493228Z","iopub.status.idle":"2026-01-19T10:48:42.534688Z","shell.execute_reply.started":"2026-01-19T10:48:42.493178Z","shell.execute_reply":"2026-01-19T10:48:42.533864Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"articles.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-19T10:48:42.535716Z","iopub.execute_input":"2026-01-19T10:48:42.536119Z","iopub.status.idle":"2026-01-19T10:48:42.555463Z","shell.execute_reply.started":"2026-01-19T10:48:42.536082Z","shell.execute_reply":"2026-01-19T10:48:42.554101Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"transactions.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-19T10:48:42.556900Z","iopub.execute_input":"2026-01-19T10:48:42.557314Z","iopub.status.idle":"2026-01-19T10:48:42.580668Z","shell.execute_reply.started":"2026-01-19T10:48:42.557285Z","shell.execute_reply":"2026-01-19T10:48:42.579789Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"articles[[\n    \"article_id\",\n    \"product_type_name\",\n    \"product_group_name\",\n    \"colour_group_name\",\n    \"graphical_appearance_name\",\n    \"index_group_name\"\n]].head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-19T10:48:42.582154Z","iopub.execute_input":"2026-01-19T10:48:42.582568Z","iopub.status.idle":"2026-01-19T10:48:42.614177Z","shell.execute_reply.started":"2026-01-19T10:48:42.582540Z","shell.execute_reply":"2026-01-19T10:48:42.613268Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"transactions[[\n    \"customer_id\",\n    \"article_id\",\n    \"t_dat\"\n]].head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-19T10:48:42.615274Z","iopub.execute_input":"2026-01-19T10:48:42.615659Z","iopub.status.idle":"2026-01-19T10:48:43.400982Z","shell.execute_reply.started":"2026-01-19T10:48:42.615630Z","shell.execute_reply":"2026-01-19T10:48:43.399878Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"articles.isnull().sum()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-19T10:48:43.405367Z","iopub.execute_input":"2026-01-19T10:48:43.405939Z","iopub.status.idle":"2026-01-19T10:48:43.495895Z","shell.execute_reply.started":"2026-01-19T10:48:43.405895Z","shell.execute_reply":"2026-01-19T10:48:43.494857Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"top_items = (\n    transactions[\"article_id\"]\n    .value_counts()\n    .head(10)\n)\n\ntop_items","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-19T10:48:43.497950Z","iopub.execute_input":"2026-01-19T10:48:43.498379Z","iopub.status.idle":"2026-01-19T10:48:44.964321Z","shell.execute_reply.started":"2026-01-19T10:48:43.498329Z","shell.execute_reply":"2026-01-19T10:48:44.963511Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"transactions[\"year\"] = transactions[\"t_dat\"].dt.year\ntransactions[\"month\"] = transactions[\"t_dat\"].dt.month\n\ntransactions.groupby(\"year\").size().plot(title=\"Transactions per Year\")\nplt.show()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-19T10:48:44.965561Z","iopub.execute_input":"2026-01-19T10:48:44.965914Z","iopub.status.idle":"2026-01-19T10:48:47.632170Z","shell.execute_reply.started":"2026-01-19T10:48:44.965887Z","shell.execute_reply":"2026-01-19T10:48:47.631061Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"user_activity = transactions.groupby(\"customer_id\").size()\n\nuser_activity.describe()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-19T10:48:47.633445Z","iopub.execute_input":"2026-01-19T10:48:47.633926Z","iopub.status.idle":"2026-01-19T10:48:59.089203Z","shell.execute_reply.started":"2026-01-19T10:48:47.633885Z","shell.execute_reply":"2026-01-19T10:48:59.088101Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"user_activity.hist(bins=50)\nplt.title(\"Transactions per User\")\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-19T10:48:59.090265Z","iopub.execute_input":"2026-01-19T10:48:59.090622Z","iopub.status.idle":"2026-01-19T10:48:59.428384Z","shell.execute_reply.started":"2026-01-19T10:48:59.090595Z","shell.execute_reply":"2026-01-19T10:48:59.427183Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"item_popularity = transactions.groupby(\"article_id\").size()\n\nitem_popularity.describe()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-19T10:48:59.429610Z","iopub.execute_input":"2026-01-19T10:48:59.430634Z","iopub.status.idle":"2026-01-19T10:49:00.324254Z","shell.execute_reply.started":"2026-01-19T10:48:59.430602Z","shell.execute_reply":"2026-01-19T10:49:00.322992Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"item_popularity.hist(bins=50)\nplt.title(\"Transactions per Item\")\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-19T10:49:00.325493Z","iopub.execute_input":"2026-01-19T10:49:00.325803Z","iopub.status.idle":"2026-01-19T10:49:00.506098Z","shell.execute_reply.started":"2026-01-19T10:49:00.325727Z","shell.execute_reply":"2026-01-19T10:49:00.505101Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"articles[\"product_group_name\"].value_counts().head(10)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-19T10:49:00.507427Z","iopub.execute_input":"2026-01-19T10:49:00.507825Z","iopub.status.idle":"2026-01-19T10:49:00.525405Z","shell.execute_reply.started":"2026-01-19T10:49:00.507786Z","shell.execute_reply":"2026-01-19T10:49:00.524036Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"transactions.merge(\n    articles[[\"article_id\", \"product_group_name\"]],\n    on=\"article_id\",\n    how=\"left\"\n)[\"product_group_name\"].value_counts().head(10)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-19T10:49:00.527077Z","iopub.execute_input":"2026-01-19T10:49:00.527489Z","iopub.status.idle":"2026-01-19T10:49:08.272449Z","shell.execute_reply.started":"2026-01-19T10:49:00.527457Z","shell.execute_reply":"2026-01-19T10:49:08.271377Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"active_users = user_activity[user_activity >= 5].index\npopular_items = item_popularity[item_popularity >= 5].index\n\ntransactions_filtered = transactions[\n    transactions[\"customer_id\"].isin(active_users) &\n    transactions[\"article_id\"].isin(popular_items)\n]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-19T10:49:08.273588Z","iopub.execute_input":"2026-01-19T10:49:08.274246Z","iopub.status.idle":"2026-01-19T10:49:16.671444Z","shell.execute_reply.started":"2026-01-19T10:49:08.274213Z","shell.execute_reply":"2026-01-19T10:49:16.670298Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(\"Before:\", transactions.shape)\nprint(\"After:\", transactions_filtered.shape)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-19T10:49:16.672706Z","iopub.execute_input":"2026-01-19T10:49:16.673118Z","iopub.status.idle":"2026-01-19T10:49:16.679169Z","shell.execute_reply.started":"2026-01-19T10:49:16.673078Z","shell.execute_reply":"2026-01-19T10:49:16.677824Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Count interactions\nuser_activity = transactions.groupby(\"customer_id\").size()\nitem_popularity = transactions.groupby(\"article_id\").size()\n\n# Filtering thresholds\nactive_users = user_activity[user_activity >= 5].index\npopular_items = item_popularity[item_popularity >= 5].index\n\n# Filter transactions\ntransactions_filtered = transactions[\n    transactions[\"customer_id\"].isin(active_users) &\n    transactions[\"article_id\"].isin(popular_items)\n]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-19T10:49:16.680621Z","iopub.execute_input":"2026-01-19T10:49:16.680999Z","iopub.status.idle":"2026-01-19T10:49:37.603054Z","shell.execute_reply.started":"2026-01-19T10:49:16.680962Z","shell.execute_reply":"2026-01-19T10:49:37.602056Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"transactions_filtered = transactions_filtered.sort_values(\"t_dat\")\n\nsplit_date = transactions_filtered[\"t_dat\"].quantile(0.8)\n\ntrain = transactions_filtered[\n    transactions_filtered[\"t_dat\"] <= split_date\n]\n\nvalid = transactions_filtered[\n    transactions_filtered[\"t_dat\"] > split_date\n]\n\nprint(\"Train:\", train.shape)\nprint(\"Valid:\", valid.shape)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-19T10:49:37.604522Z","iopub.execute_input":"2026-01-19T10:49:37.605018Z","iopub.status.idle":"2026-01-19T10:49:44.246847Z","shell.execute_reply.started":"2026-01-19T10:49:37.604971Z","shell.execute_reply":"2026-01-19T10:49:44.245668Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\nos.listdir(\"/kaggle/working\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-19T10:49:44.248277Z","iopub.execute_input":"2026-01-19T10:49:44.248686Z","iopub.status.idle":"2026-01-19T10:49:44.255662Z","shell.execute_reply.started":"2026-01-19T10:49:44.248660Z","shell.execute_reply":"2026-01-19T10:49:44.254558Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\n\nos.makedirs(\"/kaggle/working/data\", exist_ok=True)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-19T10:49:44.256903Z","iopub.execute_input":"2026-01-19T10:49:44.257529Z","iopub.status.idle":"2026-01-19T10:49:44.275497Z","shell.execute_reply.started":"2026-01-19T10:49:44.257488Z","shell.execute_reply":"2026-01-19T10:49:44.274223Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train.to_csv(\"/kaggle/working/data/train.csv\", index=False)\nvalid.to_csv(\"/kaggle/working/data/valid.csv\", index=False)\n\nprint(\"Files saved successfully\")\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-19T10:49:44.276931Z","iopub.execute_input":"2026-01-19T10:49:44.277314Z","iopub.status.idle":"2026-01-19T10:53:01.453549Z","shell.execute_reply.started":"2026-01-19T10:49:44.277285Z","shell.execute_reply":"2026-01-19T10:53:01.452248Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"os.listdir(\"/kaggle/working/data\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-19T10:53:01.454912Z","iopub.execute_input":"2026-01-19T10:53:01.455299Z","iopub.status.idle":"2026-01-19T10:53:01.464038Z","shell.execute_reply.started":"2026-01-19T10:53:01.455261Z","shell.execute_reply":"2026-01-19T10:53:01.461913Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import shutil\nimport os\n\nzip_path = \"/kaggle/working/data_export.zip\"\n\nshutil.make_archive(\n    base_name=\"/kaggle/working/data_export\",\n    format=\"zip\",\n    root_dir=\"/kaggle/working/data\"\n)\n\nzip_path\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-19T10:53:01.465250Z","iopub.execute_input":"2026-01-19T10:53:01.465823Z","iopub.status.idle":"2026-01-19T10:54:42.025899Z","shell.execute_reply.started":"2026-01-19T10:53:01.465742Z","shell.execute_reply":"2026-01-19T10:54:42.024275Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\n\nos.path.getsize(\"/kaggle/working/data/train.csv\") / (1024**2)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-19T11:08:13.235808Z","iopub.execute_input":"2026-01-19T11:08:13.237050Z","iopub.status.idle":"2026-01-19T11:08:13.244226Z","shell.execute_reply.started":"2026-01-19T11:08:13.236997Z","shell.execute_reply":"2026-01-19T11:08:13.243026Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}