{"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":"markdown","source":"Following [the suggestion by \nPaweł Jankiewicz](https://www.kaggle.com/code/paweljankiewicz/hm-create-dataset-samples/notebook), I created 20 of 5%-downsampled datasets of customers and transactions. Unlike the original notebook, I include all the articles to keep negative samples.","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nfrom sklearn.model_selection import KFold\nfrom tqdm.notebook import tqdm","metadata":{"execution":{"iopub.status.busy":"2022-04-18T01:29:30.055283Z","iopub.execute_input":"2022-04-18T01:29:30.055561Z","iopub.status.idle":"2022-04-18T01:29:31.159994Z","shell.execute_reply.started":"2022-04-18T01:29:30.055481Z","shell.execute_reply":"2022-04-18T01:29:31.159209Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"N_SPLITS = 5 # downsample to 20%","metadata":{"execution":{"iopub.status.busy":"2022-04-18T01:34:22.778415Z","iopub.execute_input":"2022-04-18T01:34:22.779212Z","iopub.status.idle":"2022-04-18T01:34:22.782978Z","shell.execute_reply.started":"2022-04-18T01:34:22.779171Z","shell.execute_reply":"2022-04-18T01:34:22.782087Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transactions = pd.read_csv(\"/kaggle/input/h-and-m-personalized-fashion-recommendations/transactions_train.csv\",\n                           dtype={\"t_dat\": \"object\", \"customer_id\": \"object\", \"article_id\": \"object\", \"price\": float, \"sales_channel_id\": int})\ncustomers = pd.read_csv(\"/kaggle/input/h-and-m-personalized-fashion-recommendations/customers.csv\")\narticles = pd.read_csv(\"/kaggle/input/h-and-m-personalized-fashion-recommendations/articles.csv\", dtype={\"article_id\": \"object\"})","metadata":{"execution":{"iopub.status.busy":"2022-04-18T01:29:31.161185Z","iopub.execute_input":"2022-04-18T01:29:31.161359Z","iopub.status.idle":"2022-04-18T01:30:53.962782Z","shell.execute_reply.started":"2022-04-18T01:29:31.161337Z","shell.execute_reply":"2022-04-18T01:30:53.961916Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"kf = KFold(n_splits=N_SPLITS, random_state=46, shuffle=True)\nfor fold, (_, indices) in enumerate(kf.split(customers)):\n    customers.loc[indices, 'fold'] = fold\ncustomers","metadata":{"execution":{"iopub.status.busy":"2022-04-18T01:35:25.871064Z","iopub.execute_input":"2022-04-18T01:35:25.871877Z","iopub.status.idle":"2022-04-18T01:35:26.298191Z","shell.execute_reply.started":"2022-04-18T01:35:25.871843Z","shell.execute_reply":"2022-04-18T01:35:26.297386Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for fold in tqdm(range(N_SPLITS)):\n    customers_sample = customers.query(\"fold == @fold\")\n    customers_sample_ids = set(customers_sample[\"customer_id\"])\n    transactions_sample = transactions[transactions[\"customer_id\"].isin(customers_sample_ids)]\n    \n    customers_sample.drop(\"fold\", axis=1).to_csv(f\"customers_{fold}.csv\", index=False)\n    transactions_sample.to_csv(f\"transactions_train_{fold}.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2022-04-18T01:41:14.79679Z","iopub.execute_input":"2022-04-18T01:41:14.797068Z","iopub.status.idle":"2022-04-18T01:44:57.998778Z","shell.execute_reply.started":"2022-04-18T01:41:14.797036Z","shell.execute_reply":"2022-04-18T01:44:57.998077Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"customers_sample","metadata":{"execution":{"iopub.status.busy":"2022-04-18T01:45:04.710498Z","iopub.execute_input":"2022-04-18T01:45:04.710905Z","iopub.status.idle":"2022-04-18T01:45:04.729352Z","shell.execute_reply.started":"2022-04-18T01:45:04.710881Z","shell.execute_reply":"2022-04-18T01:45:04.728374Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transactions_sample","metadata":{"execution":{"iopub.status.busy":"2022-04-18T01:45:07.949029Z","iopub.execute_input":"2022-04-18T01:45:07.949259Z","iopub.status.idle":"2022-04-18T01:45:07.965517Z","shell.execute_reply.started":"2022-04-18T01:45:07.949233Z","shell.execute_reply":"2022-04-18T01:45:07.964307Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}