{"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":"# Introduction","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19"}},{"cell_type":"markdown","source":"**If you like my notebook , please support me by upvoting!**","metadata":{}},{"cell_type":"markdown","source":"# What is the Problem?\nH&M Group is a family of brands and businesses with 53 online markets and approximately 4,850 stores. Their online store offers shoppers an extensive selection of products to browse through. But with too many choices, customers might not quickly find what interests them or what they are looking for, and ultimately, they might not make a purchase. To enhance the shopping experience, product recommendations are key. More importantly, helping customers make the right choices also has a positive implications for sustainability, as it reduces returns, and thereby minimizes emissions from transportation.\n\nIn this competition, H&M Group invites us to develop product recommendations based on data from previous transactions, as well as from customer and product meta data. The available meta data spans from simple data, such as garment type and customer age, to text data from product descriptions, to image data from garment images.\n\nThere are no preconceptions on what information that may be useful – that is for us to find out. If we want to investigate a categorical data type algorithm, or dive into NLP and image processing deep learning, that is up to us.","metadata":{}},{"cell_type":"markdown","source":"# Way of Solving\n1. Knowing about Data\n2. EDA\n3. Data Cleaning\n4. Modelling\n5. Prediction & Submission","metadata":{}},{"cell_type":"markdown","source":"# Checking Working Directory","metadata":{}},{"cell_type":"code","source":"#Checking current working directory!\nimport os\ncwd = os.getcwd()\nprint(\"Your current working directory is : \" , cwd)","metadata":{"execution":{"iopub.status.busy":"2022-02-22T19:43:35.728108Z","iopub.execute_input":"2022-02-22T19:43:35.729203Z","iopub.status.idle":"2022-02-22T19:43:35.751396Z","shell.execute_reply.started":"2022-02-22T19:43:35.729077Z","shell.execute_reply":"2022-02-22T19:43:35.750606Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Importing Python Libraries","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport os\nfrom os import listdir\nfrom tqdm import tqdm\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nfrom wordcloud import WordCloud, STOPWORDS\nfrom PIL import Image","metadata":{"execution":{"iopub.status.busy":"2022-02-22T19:43:35.753446Z","iopub.execute_input":"2022-02-22T19:43:35.753716Z","iopub.status.idle":"2022-02-22T19:43:36.638313Z","shell.execute_reply.started":"2022-02-22T19:43:35.753669Z","shell.execute_reply":"2022-02-22T19:43:36.637572Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Finding location of Dataset","metadata":{}},{"cell_type":"code","source":"articlesData = pd.read_csv(\"../input/h-and-m-personalized-fashion-recommendations/articles.csv\")\ncustomersData = pd.read_csv(\"../input/h-and-m-personalized-fashion-recommendations/customers.csv\")\ntransactionsData = pd.read_csv(\"../input/h-and-m-personalized-fashion-recommendations/transactions_train.csv\")\nsubmissionData = pd.read_csv(\"/kaggle/input/h-and-m-personalized-fashion-recommendations/sample_submission.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-02-22T19:43:36.639521Z","iopub.execute_input":"2022-02-22T19:43:36.640511Z","iopub.status.idle":"2022-02-22T19:44:44.958058Z","shell.execute_reply.started":"2022-02-22T19:43:36.640454Z","shell.execute_reply":"2022-02-22T19:44:44.957319Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images_dir = '../input/h-and-m-personalized-fashion-recommendations/images'\ncat_images = [f for f in listdir(images_dir)]","metadata":{"execution":{"iopub.status.busy":"2022-02-22T19:44:44.960008Z","iopub.execute_input":"2022-02-22T19:44:44.960263Z","iopub.status.idle":"2022-02-22T19:44:44.979953Z","shell.execute_reply.started":"2022-02-22T19:44:44.960229Z","shell.execute_reply":"2022-02-22T19:44:44.979332Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Statistics of data","metadata":{}},{"cell_type":"code","source":"articlesData.info()","metadata":{"execution":{"iopub.status.busy":"2022-02-22T19:44:44.981136Z","iopub.execute_input":"2022-02-22T19:44:44.981719Z","iopub.status.idle":"2022-02-22T19:44:45.146170Z","shell.execute_reply.started":"2022-02-22T19:44:44.981681Z","shell.execute_reply":"2022-02-22T19:44:45.144906Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"articlesData.head()","metadata":{"execution":{"iopub.status.busy":"2022-02-22T19:44:45.147536Z","iopub.execute_input":"2022-02-22T19:44:45.147800Z","iopub.status.idle":"2022-02-22T19:44:45.176725Z","shell.execute_reply.started":"2022-02-22T19:44:45.147765Z","shell.execute_reply":"2022-02-22T19:44:45.175908Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"customersData.info()","metadata":{"execution":{"iopub.status.busy":"2022-02-22T19:44:45.177950Z","iopub.execute_input":"2022-02-22T19:44:45.178281Z","iopub.status.idle":"2022-02-22T19:44:45.717050Z","shell.execute_reply.started":"2022-02-22T19:44:45.178247Z","shell.execute_reply":"2022-02-22T19:44:45.716213Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"customersData.head()","metadata":{"execution":{"iopub.status.busy":"2022-02-22T19:44:45.718153Z","iopub.execute_input":"2022-02-22T19:44:45.718428Z","iopub.status.idle":"2022-02-22T19:44:45.732166Z","shell.execute_reply.started":"2022-02-22T19:44:45.718390Z","shell.execute_reply":"2022-02-22T19:44:45.731453Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transactionsData.info()","metadata":{"execution":{"iopub.status.busy":"2022-02-22T19:44:45.733398Z","iopub.execute_input":"2022-02-22T19:44:45.733802Z","iopub.status.idle":"2022-02-22T19:44:45.746154Z","shell.execute_reply.started":"2022-02-22T19:44:45.733763Z","shell.execute_reply":"2022-02-22T19:44:45.745355Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transactionsData.head()","metadata":{"execution":{"iopub.status.busy":"2022-02-22T19:44:45.749332Z","iopub.execute_input":"2022-02-22T19:44:45.749541Z","iopub.status.idle":"2022-02-22T19:44:45.759144Z","shell.execute_reply.started":"2022-02-22T19:44:45.749517Z","shell.execute_reply":"2022-02-22T19:44:45.758384Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data Visualization","metadata":{}},{"cell_type":"code","source":"temp = articlesData.groupby([\"product_group_name\"])[\"product_type_name\"].nunique()\ndf = pd.DataFrame({'Product Group': temp.index,\n                   'Product Types': temp.values\n                  })\ndf = df.sort_values(['Product Types'], ascending=False)\nplt.figure(figsize = (8,6))\nplt.title('Number of Product Types/Product Group')\nsns.set_color_codes(\"pastel\")\ns = sns.barplot(x = 'Product Group', y=\"Product Types\", data=df)\ns.set_xticklabels(s.get_xticklabels(),rotation=90)\nlocs, labels = plt.xticks()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-02-22T19:44:45.760325Z","iopub.execute_input":"2022-02-22T19:44:45.760997Z","iopub.status.idle":"2022-02-22T19:44:46.223193Z","shell.execute_reply.started":"2022-02-22T19:44:45.760955Z","shell.execute_reply":"2022-02-22T19:44:46.222533Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"temp = articlesData.groupby([\"product_group_name\"])[\"article_id\"].nunique()\ndf = pd.DataFrame({'Product Group': temp.index,\n                   'Articles': temp.values\n                  })\ndf = df.sort_values(['Articles'], ascending=False)\nplt.figure(figsize = (8,6))\nplt.title('Number of Articles per each Product Group')\nsns.set_color_codes(\"pastel\")\ns = sns.barplot(x = 'Product Group', y=\"Articles\", data=df)\ns.set_xticklabels(s.get_xticklabels(),rotation=90)\nlocs, labels = plt.xticks()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-02-22T19:44:46.224343Z","iopub.execute_input":"2022-02-22T19:44:46.224767Z","iopub.status.idle":"2022-02-22T19:44:46.680630Z","shell.execute_reply.started":"2022-02-22T19:44:46.224727Z","shell.execute_reply":"2022-02-22T19:44:46.679952Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"temp = articlesData.groupby([\"product_type_name\"])[\"article_id\"].nunique()\ndf = pd.DataFrame({'Product Type': temp.index,\n                   'Articles': temp.values\n                  })\ntotal_types = len(df['Product Type'].unique())\ndf = df.sort_values(['Articles'], ascending=False)[0:50]\nplt.figure(figsize = (16,6))\nplt.title(f'Number of Articles per each Product Type (top 50 from total: {total_types})')\nsns.set_color_codes(\"pastel\")\ns = sns.barplot(x = 'Product Type', y=\"Articles\", data=df)\ns.set_xticklabels(s.get_xticklabels(),rotation=90)\nlocs, labels = plt.xticks()\nplt.show()\n\n","metadata":{"execution":{"iopub.status.busy":"2022-02-22T19:44:46.681660Z","iopub.execute_input":"2022-02-22T19:44:46.682014Z","iopub.status.idle":"2022-02-22T19:44:47.844270Z","shell.execute_reply.started":"2022-02-22T19:44:46.681981Z","shell.execute_reply":"2022-02-22T19:44:47.843479Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"temp = articlesData.groupby([\"department_name\"])[\"article_id\"].nunique()\ndf = pd.DataFrame({'Department Name': temp.index,\n                   'Articles': temp.values\n                  })\ntotal_depts = len(df['Department Name'].unique())\ndf = df.sort_values(['Articles'], ascending=False).head(50)\nplt.figure(figsize = (16,6))\nplt.title(f'Number of Articles per each Department (top 50 from total: {total_depts})')\nsns.set_color_codes(\"pastel\")\ns = sns.barplot(x = 'Department Name', y=\"Articles\", data=df)\ns.set_xticklabels(s.get_xticklabels(),rotation=90)\nlocs, labels = plt.xticks()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-02-22T19:44:47.847949Z","iopub.execute_input":"2022-02-22T19:44:47.848263Z","iopub.status.idle":"2022-02-22T19:44:48.917607Z","shell.execute_reply.started":"2022-02-22T19:44:47.848227Z","shell.execute_reply":"2022-02-22T19:44:48.916916Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Image Data & Visualization ","metadata":{}},{"cell_type":"code","source":"total_folders = total_files = 0\nfolder_info = []\nimages_names = []\nfor base, dirs, files in tqdm(os.walk('/kaggle/input/h-and-m-personalized-fashion-recommendations/')):\n    for directories in dirs:\n        folder_info.append((directories, len(os.listdir(os.path.join(base, directories)))))\n        total_folders += 1\n    for _files in files:\n        total_files += 1\n        if len(_files.split(\".jpg\"))==2:\n            images_names.append(_files.split(\".jpg\")[0])","metadata":{"execution":{"iopub.status.busy":"2022-02-22T19:44:48.918863Z","iopub.execute_input":"2022-02-22T19:44:48.919251Z","iopub.status.idle":"2022-02-22T19:45:43.903213Z","shell.execute_reply.started":"2022-02-22T19:44:48.919209Z","shell.execute_reply":"2022-02-22T19:45:43.902532Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"imageData = pd.DataFrame(images_names, columns = [\"image_name\"])\nimageData[\"article_id\"] = imageData[\"image_name\"].apply(lambda x: int(x[1:]))\nimageData.head(10)\n","metadata":{"execution":{"iopub.status.busy":"2022-02-22T19:45:43.904489Z","iopub.execute_input":"2022-02-22T19:45:43.905622Z","iopub.status.idle":"2022-02-22T19:45:44.001324Z","shell.execute_reply.started":"2022-02-22T19:45:43.905580Z","shell.execute_reply":"2022-02-22T19:45:44.000543Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"imageArticle = articlesData[[\"article_id\", \"product_code\", \"product_group_name\", \"product_type_name\"]].merge(imageData, on=[\"article_id\"], how=\"left\")\nprint(imageArticle.shape)\nimageArticle.head()","metadata":{"execution":{"iopub.status.busy":"2022-02-22T19:45:44.002432Z","iopub.execute_input":"2022-02-22T19:45:44.002692Z","iopub.status.idle":"2022-02-22T19:45:44.051957Z","shell.execute_reply.started":"2022-02-22T19:45:44.002656Z","shell.execute_reply":"2022-02-22T19:45:44.051208Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"article_no_image_df = imageArticle.loc[imageArticle.image_name.isna()]\nprint(article_no_image_df.shape)\narticle_no_image_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-02-22T19:45:44.053025Z","iopub.execute_input":"2022-02-22T19:45:44.053754Z","iopub.status.idle":"2022-02-22T19:45:44.081164Z","shell.execute_reply.started":"2022-02-22T19:45:44.053716Z","shell.execute_reply":"2022-02-22T19:45:44.080524Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Product codes without images: \", article_no_image_df.product_code.nunique())\nprint(\"Product group names without images: \", list(article_no_image_df.product_group_name.unique()))","metadata":{"execution":{"iopub.status.busy":"2022-02-22T19:45:44.082553Z","iopub.execute_input":"2022-02-22T19:45:44.083006Z","iopub.status.idle":"2022-02-22T19:45:44.089020Z","shell.execute_reply.started":"2022-02-22T19:45:44.082970Z","shell.execute_reply":"2022-02-22T19:45:44.088335Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_image_samples(imageArticle, product_group_name, cols=1, rows=-1):\n    image_path = \"/kaggle/input/h-and-m-personalized-fashion-recommendations/images/\"\n    _df = imageArticle.loc[imageArticle.product_group_name==product_group_name]\n    article_ids = _df.article_id.values[0:cols*rows]\n    plt.figure(figsize=(2 + 3 * cols, 2 + 4 * rows))\n    for i in range(cols * rows):\n        article_id = (\"0\" + str(article_ids[i]))[-10:]\n        plt.subplot(rows, cols, i + 1)\n        plt.axis('off')\n        plt.title(f\"{product_group_name} {article_id[:3]}\\n{article_id}.jpg\")\n        image = Image.open(f\"{image_path}{article_id[:3]}/{article_id}.jpg\")\n        plt.imshow(image)","metadata":{"execution":{"iopub.status.busy":"2022-02-22T19:45:44.090527Z","iopub.execute_input":"2022-02-22T19:45:44.091029Z","iopub.status.idle":"2022-02-22T19:45:44.098775Z","shell.execute_reply.started":"2022-02-22T19:45:44.090992Z","shell.execute_reply":"2022-02-22T19:45:44.097998Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(imageArticle.product_group_name.unique())","metadata":{"execution":{"iopub.status.busy":"2022-02-22T19:45:44.100109Z","iopub.execute_input":"2022-02-22T19:45:44.100354Z","iopub.status.idle":"2022-02-22T19:45:44.116937Z","shell.execute_reply.started":"2022-02-22T19:45:44.100322Z","shell.execute_reply":"2022-02-22T19:45:44.116112Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_image_samples(imageArticle, \"Garment Upper body\", 5, 1)","metadata":{"execution":{"iopub.status.busy":"2022-02-22T19:45:44.118209Z","iopub.execute_input":"2022-02-22T19:45:44.120917Z","iopub.status.idle":"2022-02-22T19:45:46.266968Z","shell.execute_reply.started":"2022-02-22T19:45:44.120890Z","shell.execute_reply":"2022-02-22T19:45:46.266269Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_image_samples(imageArticle, \"Underwear\", 5, 1)","metadata":{"execution":{"iopub.status.busy":"2022-02-22T19:45:46.267945Z","iopub.execute_input":"2022-02-22T19:45:46.268174Z","iopub.status.idle":"2022-02-22T19:45:47.987901Z","shell.execute_reply.started":"2022-02-22T19:45:46.268135Z","shell.execute_reply":"2022-02-22T19:45:47.986157Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_image_samples(imageArticle, \"Socks & Tights\", 5, 1)","metadata":{"execution":{"iopub.status.busy":"2022-02-22T19:45:47.989271Z","iopub.execute_input":"2022-02-22T19:45:47.990100Z","iopub.status.idle":"2022-02-22T19:45:49.786919Z","shell.execute_reply.started":"2022-02-22T19:45:47.990044Z","shell.execute_reply":"2022-02-22T19:45:49.785536Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_image_samples(imageArticle, \"Garment Lower body\", 5, 1)","metadata":{"execution":{"iopub.status.busy":"2022-02-22T19:45:49.788346Z","iopub.execute_input":"2022-02-22T19:45:49.789206Z","iopub.status.idle":"2022-02-22T19:45:51.378252Z","shell.execute_reply.started":"2022-02-22T19:45:49.789148Z","shell.execute_reply":"2022-02-22T19:45:51.377538Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_image_samples(imageArticle, \"Accessories\", 5, 1)","metadata":{"execution":{"iopub.status.busy":"2022-02-22T19:45:51.379700Z","iopub.execute_input":"2022-02-22T19:45:51.380154Z","iopub.status.idle":"2022-02-22T19:45:54.336001Z","shell.execute_reply.started":"2022-02-22T19:45:51.380116Z","shell.execute_reply":"2022-02-22T19:45:54.335307Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_image_samples(imageArticle, \"Items\", 5, 1)","metadata":{"execution":{"iopub.status.busy":"2022-02-22T19:45:54.337165Z","iopub.execute_input":"2022-02-22T19:45:54.337580Z","iopub.status.idle":"2022-02-22T19:45:56.058036Z","shell.execute_reply.started":"2022-02-22T19:45:54.337543Z","shell.execute_reply":"2022-02-22T19:45:56.056312Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_image_samples(imageArticle, \"Nightwear\", 5, 1)","metadata":{"execution":{"iopub.status.busy":"2022-02-22T19:45:56.065420Z","iopub.execute_input":"2022-02-22T19:45:56.065769Z","iopub.status.idle":"2022-02-22T19:45:57.818018Z","shell.execute_reply.started":"2022-02-22T19:45:56.065731Z","shell.execute_reply":"2022-02-22T19:45:57.817368Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_image_samples(imageArticle, \"Unknown\", 5, 1)","metadata":{"execution":{"iopub.status.busy":"2022-02-22T19:45:57.819365Z","iopub.execute_input":"2022-02-22T19:45:57.819844Z","iopub.status.idle":"2022-02-22T19:45:59.523663Z","shell.execute_reply.started":"2022-02-22T19:45:57.819808Z","shell.execute_reply":"2022-02-22T19:45:59.522989Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_image_samples(imageArticle, \"Underwear/nightwear\", 5, 1)","metadata":{"execution":{"iopub.status.busy":"2022-02-22T19:45:59.524949Z","iopub.execute_input":"2022-02-22T19:45:59.525289Z","iopub.status.idle":"2022-02-22T19:46:01.134855Z","shell.execute_reply.started":"2022-02-22T19:45:59.525257Z","shell.execute_reply":"2022-02-22T19:46:01.130422Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_image_samples(imageArticle, \"Shoes\", 2, 1)","metadata":{"execution":{"iopub.status.busy":"2022-02-22T19:46:01.136128Z","iopub.execute_input":"2022-02-22T19:46:01.136783Z","iopub.status.idle":"2022-02-22T19:46:01.933960Z","shell.execute_reply.started":"2022-02-22T19:46:01.136746Z","shell.execute_reply":"2022-02-22T19:46:01.933337Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_image_samples(imageArticle, \"Swimwear\", 5, 1)","metadata":{"execution":{"iopub.status.busy":"2022-02-22T19:46:01.935313Z","iopub.execute_input":"2022-02-22T19:46:01.935744Z","iopub.status.idle":"2022-02-22T19:46:03.953969Z","shell.execute_reply.started":"2022-02-22T19:46:01.935710Z","shell.execute_reply":"2022-02-22T19:46:03.953316Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_image_samples(imageArticle, \"'Garment Full body\", 0, 1)","metadata":{"execution":{"iopub.status.busy":"2022-02-22T19:46:03.955377Z","iopub.execute_input":"2022-02-22T19:46:03.955825Z","iopub.status.idle":"2022-02-22T19:46:03.979504Z","shell.execute_reply.started":"2022-02-22T19:46:03.955789Z","shell.execute_reply":"2022-02-22T19:46:03.978681Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_image_samples(imageArticle, \"Cosmetic\", 5, 1)","metadata":{"execution":{"iopub.status.busy":"2022-02-22T19:46:03.981063Z","iopub.execute_input":"2022-02-22T19:46:03.981397Z","iopub.status.idle":"2022-02-22T19:46:06.081575Z","shell.execute_reply.started":"2022-02-22T19:46:03.981361Z","shell.execute_reply":"2022-02-22T19:46:06.080000Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_image_samples(imageArticle, \"Interior textile\", 3, 1)","metadata":{"execution":{"iopub.status.busy":"2022-02-22T19:46:06.082840Z","iopub.execute_input":"2022-02-22T19:46:06.084649Z","iopub.status.idle":"2022-02-22T19:46:07.211684Z","shell.execute_reply.started":"2022-02-22T19:46:06.084607Z","shell.execute_reply":"2022-02-22T19:46:07.210930Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_image_samples(imageArticle, \"Bags\", 5, 1)","metadata":{"execution":{"iopub.status.busy":"2022-02-22T19:46:07.212941Z","iopub.execute_input":"2022-02-22T19:46:07.213277Z","iopub.status.idle":"2022-02-22T19:46:08.983845Z","shell.execute_reply.started":"2022-02-22T19:46:07.213245Z","shell.execute_reply":"2022-02-22T19:46:08.983077Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_image_samples(imageArticle, \"Furniture\", 5, 1)","metadata":{"execution":{"iopub.status.busy":"2022-02-22T19:46:08.985298Z","iopub.execute_input":"2022-02-22T19:46:08.985748Z","iopub.status.idle":"2022-02-22T19:46:10.687047Z","shell.execute_reply.started":"2022-02-22T19:46:08.985713Z","shell.execute_reply":"2022-02-22T19:46:10.686354Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_image_samples(imageArticle, \"Garment and Shoe care\", 5, 1)","metadata":{"execution":{"iopub.status.busy":"2022-02-22T19:46:10.688225Z","iopub.execute_input":"2022-02-22T19:46:10.689142Z","iopub.status.idle":"2022-02-22T19:46:12.630956Z","shell.execute_reply.started":"2022-02-22T19:46:10.689098Z","shell.execute_reply":"2022-02-22T19:46:12.630289Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_image_samples(imageArticle, \"Fun\", 2, 1)","metadata":{"execution":{"iopub.status.busy":"2022-02-22T19:46:12.632204Z","iopub.execute_input":"2022-02-22T19:46:12.632977Z","iopub.status.idle":"2022-02-22T19:46:13.434041Z","shell.execute_reply.started":"2022-02-22T19:46:12.632931Z","shell.execute_reply":"2022-02-22T19:46:13.433320Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_image_samples(imageArticle, \"Stationery\", 5, 1)","metadata":{"execution":{"iopub.status.busy":"2022-02-22T19:46:13.435265Z","iopub.execute_input":"2022-02-22T19:46:13.435505Z","iopub.status.idle":"2022-02-22T19:46:15.370749Z","shell.execute_reply.started":"2022-02-22T19:46:13.435479Z","shell.execute_reply":"2022-02-22T19:46:15.370071Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Modelling & Submitting","metadata":{}},{"cell_type":"code","source":"from pathlib import Path\n\ndata_path = Path('/kaggle/input/h-and-m-personalized-fashion-recommendations/')\ndf = pd.read_csv(\n    data_path / 'transactions_train.csv',\n    # set dtype or pandas will drop the leading '0' and convert to int\n    dtype={'article_id': str} \n)","metadata":{"execution":{"iopub.status.busy":"2022-02-22T19:46:15.371943Z","iopub.execute_input":"2022-02-22T19:46:15.372292Z","iopub.status.idle":"2022-02-22T19:46:51.330191Z","shell.execute_reply.started":"2022-02-22T19:46:15.372256Z","shell.execute_reply":"2022-02-22T19:46:51.329483Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(df.shape)\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2022-02-22T19:46:51.331528Z","iopub.execute_input":"2022-02-22T19:46:51.331773Z","iopub.status.idle":"2022-02-22T19:46:51.344673Z","shell.execute_reply.started":"2022-02-22T19:46:51.331741Z","shell.execute_reply":"2022-02-22T19:46:51.343784Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['t_dat'] = pd.to_datetime(df['t_dat'])","metadata":{"execution":{"iopub.status.busy":"2022-02-22T19:46:51.346356Z","iopub.execute_input":"2022-02-22T19:46:51.346921Z","iopub.status.idle":"2022-02-22T19:46:57.424345Z","shell.execute_reply.started":"2022-02-22T19:46:51.346847Z","shell.execute_reply":"2022-02-22T19:46:57.423617Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_3_week = df[df['t_dat'] >= pd.to_datetime('2020-08-31')].copy()\ndf_2_week = df[df['t_dat'] >= pd.to_datetime('2020-09-07')].copy()\ndf_1_week = df[df['t_dat'] >= pd.to_datetime('2020-09-15')].copy()","metadata":{"execution":{"iopub.status.busy":"2022-02-22T19:46:57.425756Z","iopub.execute_input":"2022-02-22T19:46:57.425996Z","iopub.status.idle":"2022-02-22T19:46:57.851685Z","shell.execute_reply.started":"2022-02-22T19:46:57.425963Z","shell.execute_reply":"2022-02-22T19:46:57.850926Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"purchase_dict_3_week= {}\n\nfor i,x in enumerate(zip(df_3_week['customer_id'], df_3_week['article_id'])):\n    cust_id, art_id = x\n    if cust_id not in purchase_dict_3_week:\n        purchase_dict_3_week[cust_id] = {}\n    \n    if art_id not in purchase_dict_3_week[cust_id]:\n        purchase_dict_3_week[cust_id][art_id] = 0\n    \n    purchase_dict_3_week[cust_id][art_id] += 1\n    \nprint(len(purchase_dict_3_week))\n\ndummy_list_3_week = list((df_3_week['article_id'].value_counts()).index)[:12]","metadata":{"execution":{"iopub.status.busy":"2022-02-22T19:46:57.853105Z","iopub.execute_input":"2022-02-22T19:46:57.853392Z","iopub.status.idle":"2022-02-22T19:46:59.201498Z","shell.execute_reply.started":"2022-02-22T19:46:57.853355Z","shell.execute_reply":"2022-02-22T19:46:59.200640Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"purchase_dict_2_week= {}\n\nfor i,x in enumerate(zip(df_2_week['customer_id'], df_2_week['article_id'])):\n    cust_id, art_id = x\n    if cust_id not in purchase_dict_2_week:\n        purchase_dict_2_week[cust_id] = {}\n    \n    if art_id not in purchase_dict_2_week[cust_id]:\n        purchase_dict_2_week[cust_id][art_id] = 0\n    \n    purchase_dict_2_week[cust_id][art_id] += 1\n    \nprint(len(purchase_dict_2_week))\n\ndummy_list_2_week = list((df_2_week['article_id'].value_counts()).index)[:12]","metadata":{"execution":{"iopub.status.busy":"2022-02-22T19:46:59.202937Z","iopub.execute_input":"2022-02-22T19:46:59.203403Z","iopub.status.idle":"2022-02-22T19:46:59.960089Z","shell.execute_reply.started":"2022-02-22T19:46:59.203359Z","shell.execute_reply":"2022-02-22T19:46:59.959328Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"purchase_dict_1_week= {}\n\nfor i,x in enumerate(zip(df_1_week['customer_id'], df_1_week['article_id'])):\n    cust_id, art_id = x\n    if cust_id not in purchase_dict_1_week:\n        purchase_dict_1_week[cust_id] = {}\n    \n    if art_id not in purchase_dict_1_week[cust_id]:\n        purchase_dict_1_week[cust_id][art_id] = 0\n    \n    purchase_dict_1_week[cust_id][art_id] += 1\n    \nprint(len(purchase_dict_1_week))\n\ndummy_list_1_week = list((df_1_week['article_id'].value_counts()).index)[:12]","metadata":{"execution":{"iopub.status.busy":"2022-02-22T19:46:59.961263Z","iopub.execute_input":"2022-02-22T19:46:59.961847Z","iopub.status.idle":"2022-02-22T19:47:00.315455Z","shell.execute_reply.started":"2022-02-22T19:46:59.961804Z","shell.execute_reply":"2022-02-22T19:47:00.314684Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(submissionData.shape)\nsubmissionData.head()","metadata":{"execution":{"iopub.status.busy":"2022-02-22T19:47:00.316847Z","iopub.execute_input":"2022-02-22T19:47:00.317256Z","iopub.status.idle":"2022-02-22T19:47:00.327945Z","shell.execute_reply.started":"2022-02-22T19:47:00.317219Z","shell.execute_reply":"2022-02-22T19:47:00.327262Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"need_improvemnet_model = submissionData[['customer_id']]\nprediction_list = []\n\ndummy_list = list((df_2_week['article_id'].value_counts()).index)[:12]\ndummy_pred = ' '.join(dummy_list)\n\nfor i, cust_id in enumerate(submissionData['customer_id'].values.reshape((-1,))):\n    if cust_id in purchase_dict_1_week:\n        l = sorted((purchase_dict_1_week[cust_id]).items(), key=lambda x: x[1], reverse=True)\n        l = [y[0] for y in l]\n        if len(l)>12:\n            s = ' '.join(l[:12])\n        else:\n            s = ' '.join(l+dummy_list_1_week[:(12-len(l))])\n    elif cust_id in purchase_dict_2_week:\n        l = sorted((purchase_dict_2_week[cust_id]).items(), key=lambda x: x[1], reverse=True)\n        l = [y[0] for y in l]\n        if len(l)>12:\n            s = ' '.join(l[:12])\n        else:\n            s = ' '.join(l+dummy_list_2_week[:(12-len(l))])\n    elif cust_id in purchase_dict_3_week:\n        l = sorted((purchase_dict_3_week[cust_id]).items(), key=lambda x: x[1], reverse=True)\n        l = [y[0] for y in l]\n        if len(l)>12:\n            s = ' '.join(l[:12])\n        else:\n            s = ' '.join(l+dummy_list_3_week[:(12-len(l))])\n    else:\n        s = dummy_pred\n    prediction_list.append(s)\n\nneed_improvemnet_model['prediction'] = prediction_list\nprint(need_improvemnet_model.shape)\nneed_improvemnet_model.head()","metadata":{"execution":{"iopub.status.busy":"2022-02-22T19:47:00.329164Z","iopub.execute_input":"2022-02-22T19:47:00.329572Z","iopub.status.idle":"2022-02-22T19:47:02.195488Z","shell.execute_reply.started":"2022-02-22T19:47:00.329534Z","shell.execute_reply":"2022-02-22T19:47:02.194766Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"need_improvemnet_model.to_csv('submission.csv', index=False)\nneed_improvemnet_model.head()","metadata":{"execution":{"iopub.status.busy":"2022-02-22T19:47:02.196684Z","iopub.execute_input":"2022-02-22T19:47:02.197132Z","iopub.status.idle":"2022-02-22T19:47:13.524043Z","shell.execute_reply.started":"2022-02-22T19:47:02.197075Z","shell.execute_reply":"2022-02-22T19:47:13.523346Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"References-\n~https://www.kaggle.com/chiranjeevbit/h-m-personalized-recommendation-eda-wordcloud\n~https://www.kaggle.com/remekkinas/h-m-eda-first-look-into-data#DATASET-INFORMATION\n~https://www.kaggle.com/jillanisofttech/h-m-personalized-fashion-recommendation","metadata":{}},{"cell_type":"markdown","source":"# This Notebook will be modified . If you like it , please support me by upvoting .","metadata":{}}]}