{"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":"","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Imports","metadata":{}},{"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n","metadata":{"execution":{"iopub.status.busy":"2023-10-12T23:22:07.343575Z","iopub.execute_input":"2023-10-12T23:22:07.344166Z","iopub.status.idle":"2023-10-12T23:22:07.646261Z","shell.execute_reply.started":"2023-10-12T23:22:07.344137Z","shell.execute_reply":"2023-10-12T23:22:07.644820Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Load, Explore and Prepare dataset Articies.csv","metadata":{}},{"cell_type":"code","source":"df_a = pd.read_csv('/kaggle/input/h-and-m-personalized-fashion-recommendations/articles.csv')\nrow,col=df_a.shape\nprint(f\"rows:{row} columns:{col}\")","metadata":{"execution":{"iopub.status.busy":"2023-10-12T23:22:07.647803Z","iopub.execute_input":"2023-10-12T23:22:07.648127Z","iopub.status.idle":"2023-10-12T23:22:08.468941Z","shell.execute_reply.started":"2023-10-12T23:22:07.648108Z","shell.execute_reply":"2023-10-12T23:22:08.467894Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_a.head(5)","metadata":{"execution":{"iopub.status.busy":"2023-10-12T23:22:08.470239Z","iopub.execute_input":"2023-10-12T23:22:08.470531Z","iopub.status.idle":"2023-10-12T23:22:08.507348Z","shell.execute_reply.started":"2023-10-12T23:22:08.470507Z","shell.execute_reply":"2023-10-12T23:22:08.505991Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"\n- `article_id`: A unique identifier for each fashion product.\n- `product_code`: A code associated with the product.\n- `prod_name`: The name or description of the product.\n- `product_type_no`: A numerical code indicating the type of the product.\n- `product_type_name`: The name of the product type.\n- `product_group_name`: The name of the product group to which the product belongs, typically related to the body area it covers.\n- `graphical_appearance_no`: A numerical code indicating the graphical appearance of the product.\n- `graphical_appearance_name`: The name describing the graphical appearance of the product.\n- `colour_group_code`: A code representing the color group of the product.\n- `colour_group_name`: The name of the color group.\n- `department_name`: The name of the department or category associated with the product.\n- `index_code`: A code related to the product's index.\n- `index_name`: The name of the index associated with the product.\n- `index_group_no`: A numerical code for the index group.\n- `index_group_name`: The name of the index group.\n- `section_no`: A numerical code representing the section to which the product belongs.\n- `section_name`: The name of the section.\n- `garment_group_no`: A numerical code indicating the garment group of the product.\n- `garment_group_name`: The name of the garment group.\n- `detail_desc`: A detailed description of the product.\n\n\n","metadata":{}},{"cell_type":"code","source":"df_a.columns","metadata":{"execution":{"iopub.status.busy":"2023-10-12T23:22:08.509635Z","iopub.execute_input":"2023-10-12T23:22:08.509952Z","iopub.status.idle":"2023-10-12T23:22:08.517932Z","shell.execute_reply.started":"2023-10-12T23:22:08.509927Z","shell.execute_reply":"2023-10-12T23:22:08.516896Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"feature_subset = ['product_group_name', 'product_type_name',\n       'graphical_appearance_name', 'colour_group_name',\n       'perceived_colour_value_name',\n       'perceived_colour_master_name',\n       'department_name', 'index_name',\n       'index_group_name', 'section_name',\n       'garment_group_name']","metadata":{"execution":{"iopub.status.busy":"2023-10-12T23:22:08.519156Z","iopub.execute_input":"2023-10-12T23:22:08.519446Z","iopub.status.idle":"2023-10-12T23:22:08.528640Z","shell.execute_reply.started":"2023-10-12T23:22:08.519425Z","shell.execute_reply":"2023-10-12T23:22:08.527568Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_a.info()","metadata":{"execution":{"iopub.status.busy":"2023-10-12T23:22:08.529668Z","iopub.execute_input":"2023-10-12T23:22:08.530568Z","iopub.status.idle":"2023-10-12T23:22:08.609969Z","shell.execute_reply.started":"2023-10-12T23:22:08.530543Z","shell.execute_reply":"2023-10-12T23:22:08.608926Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_a.isna().sum()","metadata":{"execution":{"iopub.status.busy":"2023-10-12T23:22:08.610926Z","iopub.execute_input":"2023-10-12T23:22:08.612100Z","iopub.status.idle":"2023-10-12T23:22:08.686446Z","shell.execute_reply.started":"2023-10-12T23:22:08.612074Z","shell.execute_reply":"2023-10-12T23:22:08.685561Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"markdown","source":"### Load, Explore and Prepare Dataset customers.csv","metadata":{}},{"cell_type":"code","source":"df_c = pd.read_csv('/kaggle/input/h-and-m-personalized-fashion-recommendations/customers.csv')\nrow,col=df_c.shape\nprint(f\"rows:{row} columns:{col}\")","metadata":{"execution":{"iopub.status.busy":"2023-10-12T23:22:08.687568Z","iopub.execute_input":"2023-10-12T23:22:08.687860Z","iopub.status.idle":"2023-10-12T23:22:13.531111Z","shell.execute_reply.started":"2023-10-12T23:22:08.687834Z","shell.execute_reply":"2023-10-12T23:22:13.530011Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_c.head(5)","metadata":{"execution":{"iopub.status.busy":"2023-10-12T23:22:13.532245Z","iopub.execute_input":"2023-10-12T23:22:13.532987Z","iopub.status.idle":"2023-10-12T23:22:13.544960Z","shell.execute_reply.started":"2023-10-12T23:22:13.532950Z","shell.execute_reply":"2023-10-12T23:22:13.543704Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_c.columns","metadata":{"execution":{"iopub.status.busy":"2023-10-12T23:22:13.548112Z","iopub.execute_input":"2023-10-12T23:22:13.548496Z","iopub.status.idle":"2023-10-12T23:22:13.565039Z","shell.execute_reply.started":"2023-10-12T23:22:13.548471Z","shell.execute_reply":"2023-10-12T23:22:13.563796Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_c.info()","metadata":{"execution":{"iopub.status.busy":"2023-10-12T23:22:13.566083Z","iopub.execute_input":"2023-10-12T23:22:13.566354Z","iopub.status.idle":"2023-10-12T23:22:13.776380Z","shell.execute_reply.started":"2023-10-12T23:22:13.566330Z","shell.execute_reply":"2023-10-12T23:22:13.775378Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_c.isna().sum()","metadata":{"execution":{"iopub.status.busy":"2023-10-12T23:22:13.777860Z","iopub.execute_input":"2023-10-12T23:22:13.778150Z","iopub.status.idle":"2023-10-12T23:22:13.980234Z","shell.execute_reply.started":"2023-10-12T23:22:13.778125Z","shell.execute_reply":"2023-10-12T23:22:13.979472Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Load, Explore and prepare dataset sample_submission.csv","metadata":{}},{"cell_type":"code","source":"df_ss=pd.read_csv('/kaggle/input/h-and-m-personalized-fashion-recommendations/sample_submission.csv')\nrow,col=df_ss.shape\nprint(f\"rows:{row} columns:{col}\")","metadata":{"execution":{"iopub.status.busy":"2023-10-12T23:22:13.981469Z","iopub.execute_input":"2023-10-12T23:22:13.981927Z","iopub.status.idle":"2023-10-12T23:22:18.142363Z","shell.execute_reply.started":"2023-10-12T23:22:13.981902Z","shell.execute_reply":"2023-10-12T23:22:18.141515Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_ss.head(5)","metadata":{"execution":{"iopub.status.busy":"2023-10-12T23:22:18.143463Z","iopub.execute_input":"2023-10-12T23:22:18.143694Z","iopub.status.idle":"2023-10-12T23:22:18.150364Z","shell.execute_reply.started":"2023-10-12T23:22:18.143675Z","shell.execute_reply":"2023-10-12T23:22:18.149583Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Load, Explore and Pepare dataset transaction_train.csv","metadata":{}},{"cell_type":"code","source":"df_train=pd.read_csv('/kaggle/input/h-and-m-personalized-fashion-recommendations/transactions_train.csv')\nrow,col=df_train.shape\nprint(f\"rows:{row} columns:{col}\")","metadata":{"execution":{"iopub.status.busy":"2023-10-12T23:22:18.151490Z","iopub.execute_input":"2023-10-12T23:22:18.151940Z","iopub.status.idle":"2023-10-12T23:23:11.366726Z","shell.execute_reply.started":"2023-10-12T23:22:18.151916Z","shell.execute_reply":"2023-10-12T23:23:11.365692Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.head(5)","metadata":{"execution":{"iopub.status.busy":"2023-10-12T23:23:11.367940Z","iopub.execute_input":"2023-10-12T23:23:11.368237Z","iopub.status.idle":"2023-10-12T23:23:11.377086Z","shell.execute_reply.started":"2023-10-12T23:23:11.368211Z","shell.execute_reply":"2023-10-12T23:23:11.375986Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.columns","metadata":{"execution":{"iopub.status.busy":"2023-10-12T23:23:11.378476Z","iopub.execute_input":"2023-10-12T23:23:11.379208Z","iopub.status.idle":"2023-10-12T23:23:11.393533Z","shell.execute_reply.started":"2023-10-12T23:23:11.379184Z","shell.execute_reply":"2023-10-12T23:23:11.392248Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.isna().sum()","metadata":{"execution":{"iopub.status.busy":"2023-10-12T23:23:11.394918Z","iopub.execute_input":"2023-10-12T23:23:11.395487Z","iopub.status.idle":"2023-10-12T23:23:13.283249Z","shell.execute_reply.started":"2023-10-12T23:23:11.395460Z","shell.execute_reply":"2023-10-12T23:23:13.282386Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#result_df = pd.DataFrame({'customer_id': df_c['customer_id'], 'prediction': df_ss['prediction']})\n\n\n\nimport pandas as pd\ndf_tt=df_train.head(5000)\n# Assuming you have a DataFrame named 'df' with your data\n# If not, you can read your data into a DataFrame using pd.read_csv or another method\n\n# Group the data by 'customer_id' and aggregate 'article_id' into a list\ngrouped = df_tt.groupby('customer_id')['article_id'].agg(list).reset_index()\n\n# Create a list to store the results\nresult_data = []\n\n# Iterate through each row in the grouped DataFrame and find shared articles\nfor index, row in grouped.iterrows():\n    customer_id = row['customer_id']\n    articles = row['article_id']\n    \n    shared_articles = []\n    for other_index, other_row in grouped.iterrows():\n        if other_row['customer_id'] != customer_id:\n            shared = set(articles).intersection(other_row['article_id'])\n            if len(shared) > 0:\n                shared_articles.extend(list(shared))\n    \n    # Append the results to the list\n    result_data.append({'customer_id': customer_id, 'prediction': shared_articles})\n\n# Create the result DataFrame\nresult_df = pd.DataFrame(result_data)\n\n# Display the result DataFrame\nprint(result_df)\nfile = open('submission.csv', 'w')\nfile.write(str(result_df))\nfile.close()","metadata":{"execution":{"iopub.status.busy":"2023-10-12T23:45:56.413336Z","iopub.execute_input":"2023-10-12T23:45:56.414009Z","iopub.status.idle":"2023-10-12T23:47:30.730546Z","shell.execute_reply.started":"2023-10-12T23:45:56.413981Z","shell.execute_reply":"2023-10-12T23:47:30.729614Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n","metadata":{"execution":{"iopub.status.busy":"2023-10-12T23:24:45.613773Z","iopub.status.idle":"2023-10-12T23:24:45.614006Z","shell.execute_reply.started":"2023-10-12T23:24:45.613893Z","shell.execute_reply":"2023-10-12T23:24:45.613903Z"},"trusted":true},"execution_count":null,"outputs":[]}]}