{"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":"<div style=\"color:white;\n           display:fill;\n           border-radius:5px;\n           background-color:#5642C5;\n           font-size:110%;\n           font-family:Verdana;\n           letter-spacing:0.5px\">\n\n<h1 style=\"padding: 10px;\n              color:white;\">\n\n              H&M Data Visualization\n</h1>\n</div>","metadata":{}},{"cell_type":"markdown","source":"***","metadata":{}},{"cell_type":"markdown","source":"<h4 style=\"color:purple;\">In this competitions H&M wants you to build a personalize fashion recommendation system because they have huge number of products on their online platform 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.</h4>","metadata":{}},{"cell_type":"markdown","source":"***","metadata":{}},{"cell_type":"markdown","source":"# Dataset","metadata":{}},{"cell_type":"markdown","source":"<ol style=\"color:purple;\"><li><h4>images/ - a folder of images corresponding to each article_id; images are placed in subfolders starting with the first three digits of the article_id; note, not all article_id values have a corresponding image.</h4></li>\n    <li><h4>articles.csv - detailed metadata for each article_id available for purchase</h4></li>\n    <li><h4>customers.csv - metadata for each customer_id in dataset</h4></li>\n    <li><h4>sample_submission.csv - a sample submission file in the correct format</h4></li>\n<li><h4>transactions_train.csv - the training data, consisting of the purchases each customer for each date, as well as additional information. Duplicate rows correspond to multiple purchases of the same item. Your task is to predict the article_ids each customer will purchase during the 7-day period immediately after the training data period.</h4></li><ol>","metadata":{}},{"cell_type":"markdown","source":"***","metadata":{}},{"cell_type":"code","source":"# import necessary libraries\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sns \nimport os\nimport numpy as np\nimport cv2\nimport warnings\nwarnings.filterwarnings('ignore')\n\npd.set_option('display.max_rows', None)\npd.set_option('display.max_columns', None)\npd.set_option('float_format', '{:f}'.format)","metadata":{"execution":{"iopub.status.busy":"2022-02-11T16:43:43.229007Z","iopub.execute_input":"2022-02-11T16:43:43.229775Z","iopub.status.idle":"2022-02-11T16:43:43.235680Z","shell.execute_reply.started":"2022-02-11T16:43:43.229736Z","shell.execute_reply":"2022-02-11T16:43:43.234927Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"IMG_DIR=\"../input/h-and-m-personalized-fashion-recommendations/images\"","metadata":{"execution":{"iopub.status.busy":"2022-02-11T16:43:43.282435Z","iopub.execute_input":"2022-02-11T16:43:43.283040Z","iopub.status.idle":"2022-02-11T16:43:43.287484Z","shell.execute_reply.started":"2022-02-11T16:43:43.282997Z","shell.execute_reply":"2022-02-11T16:43:43.286598Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Reading all the csv files\narticles=pd.read_csv(\"../input/h-and-m-personalized-fashion-recommendations/articles.csv\")\ncustomers=pd.read_csv(\"../input/h-and-m-personalized-fashion-recommendations/customers.csv\")\ntransactions=pd.read_csv(\"../input/h-and-m-personalized-fashion-recommendations/transactions_train.csv\")\nsample_submission=pd.read_csv(\"../input/h-and-m-personalized-fashion-recommendations/sample_submission.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-02-11T16:43:43.381338Z","iopub.execute_input":"2022-02-11T16:43:43.381817Z","iopub.status.idle":"2022-02-11T16:44:23.404314Z","shell.execute_reply.started":"2022-02-11T16:43:43.381778Z","shell.execute_reply":"2022-02-11T16:44:23.403504Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h4 style=\"color:blue;\">Let's display first few rows of all the dataframes</h4>","metadata":{}},{"cell_type":"code","source":"articles.head(2)","metadata":{"execution":{"iopub.status.busy":"2022-02-11T16:44:23.405893Z","iopub.execute_input":"2022-02-11T16:44:23.406288Z","iopub.status.idle":"2022-02-11T16:44:23.433154Z","shell.execute_reply.started":"2022-02-11T16:44:23.406252Z","shell.execute_reply":"2022-02-11T16:44:23.431843Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"customers.head(2)","metadata":{"execution":{"iopub.status.busy":"2022-02-11T16:44:23.434347Z","iopub.execute_input":"2022-02-11T16:44:23.434697Z","iopub.status.idle":"2022-02-11T16:44:23.447630Z","shell.execute_reply.started":"2022-02-11T16:44:23.434664Z","shell.execute_reply":"2022-02-11T16:44:23.447022Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transactions.head(2)","metadata":{"execution":{"iopub.status.busy":"2022-02-11T16:44:23.449394Z","iopub.execute_input":"2022-02-11T16:44:23.449779Z","iopub.status.idle":"2022-02-11T16:44:23.629275Z","shell.execute_reply.started":"2022-02-11T16:44:23.449743Z","shell.execute_reply":"2022-02-11T16:44:23.628560Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Let's find out the shapes of all three dataframes\nshape=pd.DataFrame({\"Row\":[articles.shape[0],customers.shape[0],transactions.shape[0]],\n             \"Column\":[articles.shape[1],customers.shape[1],transactions.shape[1]]},index=['articles',\n                                                                                          'customers','transactions'])\ngreen = [{'selector': 'th', 'props': 'background-color: green'}]\nred = [{'selector': 'th', 'props': 'background-color: red'}]\nshape.style.set_table_styles({\"articles\": green, \"customers\": red, \"transactions\": green}, axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-02-11T16:44:23.630556Z","iopub.execute_input":"2022-02-11T16:44:23.630963Z","iopub.status.idle":"2022-02-11T16:44:23.645467Z","shell.execute_reply.started":"2022-02-11T16:44:23.630924Z","shell.execute_reply":"2022-02-11T16:44:23.644821Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"articles.product_group_name.value_counts().to_frame()","metadata":{"execution":{"iopub.status.busy":"2022-02-11T16:44:23.646689Z","iopub.execute_input":"2022-02-11T16:44:23.647059Z","iopub.status.idle":"2022-02-11T16:44:23.676447Z","shell.execute_reply.started":"2022-02-11T16:44:23.647026Z","shell.execute_reply":"2022-02-11T16:44:23.675779Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(16,10))\nsns.countplot(y='product_group_name',data=articles,order=articles['product_group_name'].value_counts().index[:10])\nplt.title(\"Product Group Name\",font='serif',size=20,color=\"purple\")\nplt.xlabel(\"Count\",size=20,color=\"purple\")\nplt.ylabel(\"Product_Group_Name\",size=20,color=\"purple\")\nplt.xticks(size=16)\nplt.yticks(size=16)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-02-11T16:44:23.680001Z","iopub.execute_input":"2022-02-11T16:44:23.681795Z","iopub.status.idle":"2022-02-11T16:44:24.012555Z","shell.execute_reply.started":"2022-02-11T16:44:23.681752Z","shell.execute_reply":"2022-02-11T16:44:24.011849Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"These are the 10 most frequent ","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"articles.dtypes","metadata":{"execution":{"iopub.status.busy":"2022-02-11T16:44:24.013724Z","iopub.execute_input":"2022-02-11T16:44:24.015071Z","iopub.status.idle":"2022-02-11T16:44:24.023841Z","shell.execute_reply.started":"2022-02-11T16:44:24.015026Z","shell.execute_reply":"2022-02-11T16:44:24.022801Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"articles['dir'] = articles.article_id.astype(str).str[:2].astype(int)","metadata":{"execution":{"iopub.status.busy":"2022-02-11T16:44:24.025895Z","iopub.execute_input":"2022-02-11T16:44:24.026283Z","iopub.status.idle":"2022-02-11T16:44:24.196041Z","shell.execute_reply.started":"2022-02-11T16:44:24.026243Z","shell.execute_reply":"2022-02-11T16:44:24.195292Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"articles.head()","metadata":{"execution":{"iopub.status.busy":"2022-02-11T16:44:24.198713Z","iopub.execute_input":"2022-02-11T16:44:24.198985Z","iopub.status.idle":"2022-02-11T16:44:24.220803Z","shell.execute_reply.started":"2022-02-11T16:44:24.198949Z","shell.execute_reply":"2022-02-11T16:44:24.219926Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h3 style=\"color:purple\">article_id from the articles dataset is the image id from the image folder\nHere i am trying to access the article_id corresponding to the particular product_group_name for instance article_id corresponding to the shoes product_group_name and trying to visualiza them.</h3>","metadata":{}},{"cell_type":"code","source":"def get_article_id(df,group_name):\n    article_id=df[df['product_group_name']==group_name]\n    article_id['article_id']=\"0\"+article_id['article_id'].astype(str)\n    article_id['dir']=\"0\"+article_id['dir'].astype(str)\n    return article_id[['article_id','dir']].reset_index(drop=True)\n\n\n\ndef read_img(data):\n    li=[]\n    for i in range(10):\n        arti=data['article_id'][i]\n        di=data['dir'][i]\n        im=cv2.imread(\"../input/h-and-m-personalized-fashion-recommendations/images/\"+di+\"/\"+arti+\".jpg\")\n        im=cv2.resize(im,(224,224),fx=0,fy=0, interpolation = cv2.INTER_CUBIC)\n        li.append(im)\n    return li\n\n\ndef show_img(data):\n    f, axarr = plt.subplots(1,5,figsize=(15,10)) \n    axarr[0].imshow(data[0])\n    axarr[1].imshow(data[1])\n    axarr[2].imshow(data[2])\n    axarr[3].imshow(data[3])\n    axarr[4].imshow(data[4])\n    f.tight_layout()\n    \ndef call(df,group_name):\n    _id=get_article_id(df,group_name)\n    img=read_img(_id)\n    display_img=show_img(img)\n    return display_img\n\n\n\n    ","metadata":{"execution":{"iopub.status.busy":"2022-02-11T16:44:24.222479Z","iopub.execute_input":"2022-02-11T16:44:24.223062Z","iopub.status.idle":"2022-02-11T16:44:24.236206Z","shell.execute_reply.started":"2022-02-11T16:44:24.223025Z","shell.execute_reply":"2022-02-11T16:44:24.235458Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"call(articles,\"Garment Lower body\")","metadata":{"execution":{"iopub.status.busy":"2022-02-11T16:44:24.238922Z","iopub.execute_input":"2022-02-11T16:44:24.239206Z","iopub.status.idle":"2022-02-11T16:44:25.200331Z","shell.execute_reply.started":"2022-02-11T16:44:24.239178Z","shell.execute_reply":"2022-02-11T16:44:25.196124Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"call(articles,\"Garment Upper body\")","metadata":{"execution":{"iopub.status.busy":"2022-02-11T16:44:25.201779Z","iopub.execute_input":"2022-02-11T16:44:25.202251Z","iopub.status.idle":"2022-02-11T16:44:26.261053Z","shell.execute_reply.started":"2022-02-11T16:44:25.202202Z","shell.execute_reply":"2022-02-11T16:44:26.260404Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"call(articles,\"Accessories\")","metadata":{"execution":{"iopub.status.busy":"2022-02-11T16:44:26.262114Z","iopub.execute_input":"2022-02-11T16:44:26.262477Z","iopub.status.idle":"2022-02-11T16:44:27.268351Z","shell.execute_reply.started":"2022-02-11T16:44:26.262443Z","shell.execute_reply":"2022-02-11T16:44:27.267710Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"call(articles,\"Underwear\")","metadata":{"execution":{"iopub.status.busy":"2022-02-11T16:44:27.269502Z","iopub.execute_input":"2022-02-11T16:44:27.272065Z","iopub.status.idle":"2022-02-11T16:44:28.177325Z","shell.execute_reply.started":"2022-02-11T16:44:27.272018Z","shell.execute_reply":"2022-02-11T16:44:28.176742Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"call(articles,\"Swimwear\")","metadata":{"execution":{"iopub.status.busy":"2022-02-11T16:44:28.178599Z","iopub.execute_input":"2022-02-11T16:44:28.179054Z","iopub.status.idle":"2022-02-11T16:44:29.276481Z","shell.execute_reply.started":"2022-02-11T16:44:28.179012Z","shell.execute_reply":"2022-02-11T16:44:29.275794Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"call(articles,\"Socks & Tights\")","metadata":{"execution":{"iopub.status.busy":"2022-02-11T16:44:29.277941Z","iopub.execute_input":"2022-02-11T16:44:29.278413Z","iopub.status.idle":"2022-02-11T16:44:30.219347Z","shell.execute_reply.started":"2022-02-11T16:44:29.278373Z","shell.execute_reply":"2022-02-11T16:44:30.218707Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}