{"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":"import numpy as np, pandas as pd, datetime as dt\nimport matplotlib.pyplot as plt\n\nfrom pathlib import Path\npath = Path(\"/kaggle/input/h-and-m-personalized-fashion-recommendations/\")\n\nfrom PIL import Image\ndef show_images(article_ids, cols=1, texts=[], suptitle=''):\n    if isinstance(article_ids, int) or isinstance(article_ids, str):\n        article_ids = [article_ids]\n    rows = (len(article_ids) // cols) + 1\n    plt.figure(figsize=(3 + 3.5 * cols, 3 + 5 * rows))\n    for i, article_id in enumerate(article_ids):\n        article_id = (\"0\" + str(article_id))[-10:]\n        text = '' if len(texts) <= i else ('\\n' + texts[i])\n        plt.subplot(rows, cols, i + 1)\n        plt.axis('off')\n        plt.title(f\"{article_id}{text}\", fontsize=16)\n        try:\n            image = Image.open(f\"/kaggle/input/h-and-m-personalized-fashion-recommendations/images/{article_id[:3]}/{article_id}.jpg\")\n            plt.imshow(image)\n        except:\n            pass\n    if suptitle != '': plt.suptitle(suptitle, fontsize=36, fontweight='bold')\n    plt.tight_layout()\n\nitems = pd.read_csv(path / 'articles.csv')\ntrain = pd.read_parquet('../input/hm-parquets-of-datasets/transactions_train.parquet')\nadf = pd.read_parquet('../input/hm-parquets-of-datasets/articles.parquet')\n\ndef show_best_sellings(query, nitem, ncol, title, dat=train):\n    usecols = [\"article_id\", \"prod_name\", \"colour_group_name\"]\n    temp = dat.query(query).groupby('article_id').t_dat.count()\\\n        .reset_index().sort_values('t_dat', ascending=False).head(nitem)[['article_id']]\\\n        .merge(items[usecols], on='article_id', how='left').rename(columns={'t_dat':'weekly_sales_count'})\n    show_images(temp.article_id.tolist(),\n                ncol,\n                [', '.join([p, c]) for p, c in zip(temp.prod_name.tolist(), temp.colour_group_name.tolist())],\n                suptitle=title)","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-04-06T17:30:04.178723Z","iopub.execute_input":"2022-04-06T17:30:04.179802Z","iopub.status.idle":"2022-04-06T17:30:07.298290Z","shell.execute_reply.started":"2022-04-06T17:30:04.179751Z","shell.execute_reply":"2022-04-06T17:30:07.297501Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Best Selling Items : Weekly**","metadata":{}},{"cell_type":"code","source":"for target_week in range(104, 92, -1):\n    init_date = train[train.week == target_week].t_dat.min()\n    last_date = train[train.week == target_week].t_dat.max()\n    show_best_sellings(f'week == {target_week}',\n                       nitem=12, ncol=(6 if 104 == target_week else 12),\n                       title=f'Popular Items in Week {target_week}  :  {str(init_date)[:10]}  ---  {str(last_date)[:10]}\\n')","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-04-06T17:35:28.272053Z","iopub.execute_input":"2022-04-06T17:35:28.272598Z","iopub.status.idle":"2022-04-06T17:36:33.421908Z","shell.execute_reply.started":"2022-04-06T17:35:28.272565Z","shell.execute_reply":"2022-04-06T17:36:33.421033Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Best Selling Items : Autumn 2019**","metadata":{}},{"cell_type":"code","source":"for target_week in range(55, 49, -1):\n    init_date = train[train.week == target_week].t_dat.min()\n    last_date = train[train.week == target_week].t_dat.max()\n    show_best_sellings(f'week == {target_week}',\n                       nitem=12, ncol=(6 if 52 <= target_week <= 53 else 12),\n                       title=f'Popular Items in Week {target_week}  :  {str(init_date)[:10]}  ---  {str(last_date)[:10]}\\n')","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-04-06T17:34:54.734302Z","iopub.execute_input":"2022-04-06T17:34:54.735015Z","iopub.status.idle":"2022-04-06T17:35:28.270756Z","shell.execute_reply.started":"2022-04-06T17:34:54.734975Z","shell.execute_reply":"2022-04-06T17:35:28.270136Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Index Groups**","metadata":{}},{"cell_type":"code","source":"dat = train[train.week == 104].merge(items, on='article_id', how='left')\n\ncolname = 'index_group_name'\npop_groups = dat.groupby(colname).t_dat.count().nlargest(5).index.tolist()\nfor group in pop_groups:\n    show_best_sellings(f\"{colname} == '{group}'\",\n                       nitem=12, ncol=(6 if (group == pop_groups[0]) else 12),\n                       title=f'Index Groups : {group}\\n', dat=dat)","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-04-06T17:36:33.423423Z","iopub.execute_input":"2022-04-06T17:36:33.423652Z","iopub.status.idle":"2022-04-06T17:37:01.166710Z","shell.execute_reply.started":"2022-04-06T17:36:33.423624Z","shell.execute_reply":"2022-04-06T17:37:01.165779Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Popular Sections**","metadata":{}},{"cell_type":"code","source":"colname = 'section_name'\npop_groups = dat.groupby(colname).t_dat.count().nlargest(6).index.tolist()\nfor group in pop_groups:\n    show_best_sellings(f\"{colname} == '{group}'\",\n                       nitem=12, ncol=(6 if (group == pop_groups[0]) else 12),\n                       title=f'Popular Sections : {group}\\n', dat=dat)","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-04-06T17:12:11.338345Z","iopub.execute_input":"2022-04-06T17:12:11.338734Z","iopub.status.idle":"2022-04-06T17:12:42.836359Z","shell.execute_reply.started":"2022-04-06T17:12:11.338689Z","shell.execute_reply":"2022-04-06T17:12:42.835295Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Popular Colours**","metadata":{}},{"cell_type":"code","source":"colname = 'perceived_colour_master_name'\npop_groups = dat.groupby(colname).t_dat.count().nlargest(5).index.tolist()\nfor group in pop_groups:\n    show_best_sellings(f\"{colname} == '{group}'\",\n                       nitem=12, ncol=(6 if (group == pop_groups[0]) else 12),\n                       title=f'Popular Colours : {group}\\n', dat=dat)","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-04-06T17:14:28.819426Z","iopub.execute_input":"2022-04-06T17:14:28.819714Z","iopub.status.idle":"2022-04-06T17:14:55.845562Z","shell.execute_reply.started":"2022-04-06T17:14:28.819681Z","shell.execute_reply":"2022-04-06T17:14:55.844643Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Popular Departments**","metadata":{}},{"cell_type":"code","source":"colname = 'department_name'\npop_groups = dat.groupby(colname).t_dat.count().nlargest(10).index.tolist()\nfor group in pop_groups:\n    show_best_sellings(f\"{colname} == '{group}'\",\n                       nitem=12, ncol=(6 if (group == pop_groups[0]) else 12),\n                       title=f'Popular Departments : {group}\\n', dat=dat)","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-04-06T17:15:54.014622Z","iopub.execute_input":"2022-04-06T17:15:54.014899Z","iopub.status.idle":"2022-04-06T17:16:46.944882Z","shell.execute_reply.started":"2022-04-06T17:15:54.014868Z","shell.execute_reply":"2022-04-06T17:16:46.944076Z"},"trusted":true},"execution_count":null,"outputs":[]}]}