{"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 os\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport plotly.express as px\n\n\nfrom tqdm import tqdm\nfrom PIL import Image","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-02-14T08:29:03.732379Z","iopub.execute_input":"2022-02-14T08:29:03.733065Z","iopub.status.idle":"2022-02-14T08:29:05.982882Z","shell.execute_reply.started":"2022-02-14T08:29:03.732946Z","shell.execute_reply":"2022-02-14T08:29:05.981958Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv('../input/h-and-m-personalized-fashion-recommendations/transactions_train.csv',\n                   dtype={'article_id':str})\nprint(train.shape)\ntrain.head()","metadata":{"execution":{"iopub.status.busy":"2022-02-14T08:51:23.548778Z","iopub.execute_input":"2022-02-14T08:51:23.549385Z","iopub.status.idle":"2022-02-14T08:52:09.75526Z","shell.execute_reply.started":"2022-02-14T08:51:23.549348Z","shell.execute_reply":"2022-02-14T08:52:09.754293Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.info()","metadata":{"execution":{"iopub.status.busy":"2022-02-14T08:30:14.721726Z","iopub.execute_input":"2022-02-14T08:30:14.722177Z","iopub.status.idle":"2022-02-14T08:30:14.74447Z","shell.execute_reply.started":"2022-02-14T08:30:14.722137Z","shell.execute_reply":"2022-02-14T08:30:14.743354Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"article = pd.read_csv('../input/h-and-m-personalized-fashion-recommendations/articles.csv')\nprint(article.shape)\narticle.head()","metadata":{"execution":{"iopub.status.busy":"2022-02-14T08:30:14.746593Z","iopub.execute_input":"2022-02-14T08:30:14.746987Z","iopub.status.idle":"2022-02-14T08:30:15.756551Z","shell.execute_reply.started":"2022-02-14T08:30:14.746956Z","shell.execute_reply":"2022-02-14T08:30:15.755912Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"article.info()","metadata":{"execution":{"iopub.status.busy":"2022-02-14T08:30:15.757472Z","iopub.execute_input":"2022-02-14T08:30:15.758198Z","iopub.status.idle":"2022-02-14T08:30:15.929376Z","shell.execute_reply.started":"2022-02-14T08:30:15.758161Z","shell.execute_reply":"2022-02-14T08:30:15.928556Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"customers = pd.read_csv('../input/h-and-m-personalized-fashion-recommendations/customers.csv')\nprint(customers.shape)\ncustomers.head()","metadata":{"execution":{"iopub.status.busy":"2022-02-14T08:30:15.930443Z","iopub.execute_input":"2022-02-14T08:30:15.930689Z","iopub.status.idle":"2022-02-14T08:30:21.248127Z","shell.execute_reply.started":"2022-02-14T08:30:15.93064Z","shell.execute_reply":"2022-02-14T08:30:21.247191Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.read_csv('../input/h-and-m-personalized-fashion-recommendations/sample_submission.csv')\nprint(submission.shape)\nsubmission.head()","metadata":{"execution":{"iopub.status.busy":"2022-02-14T08:30:21.249268Z","iopub.execute_input":"2022-02-14T08:30:21.249585Z","iopub.status.idle":"2022-02-14T08:30:25.960756Z","shell.execute_reply.started":"2022-02-14T08:30:21.249549Z","shell.execute_reply":"2022-02-14T08:30:25.959748Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.info()","metadata":{"execution":{"iopub.status.busy":"2022-02-14T08:30:25.962065Z","iopub.execute_input":"2022-02-14T08:30:25.962557Z","iopub.status.idle":"2022-02-14T08:30:25.973281Z","shell.execute_reply.started":"2022-02-14T08:30:25.962523Z","shell.execute_reply":"2022-02-14T08:30:25.972597Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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-14T08:26:43.960264Z","iopub.execute_input":"2022-02-14T08:26:43.960972Z","iopub.status.idle":"2022-02-14T08:27:14.066224Z","shell.execute_reply.started":"2022-02-14T08:26:43.960926Z","shell.execute_reply":"2022-02-14T08:27:14.0653Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"folder_info_df = pd.DataFrame(folder_info,columns=['folder','files count'])\nfolder_info_df.sort_values(['files count'],ascending=False)","metadata":{"execution":{"iopub.status.busy":"2022-02-14T08:27:14.068554Z","iopub.execute_input":"2022-02-14T08:27:14.069065Z","iopub.status.idle":"2022-02-14T08:27:14.090646Z","shell.execute_reply.started":"2022-02-14T08:27:14.069017Z","shell.execute_reply":"2022-02-14T08:27:14.089951Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"article_group = article.groupby(['product_group_name'])['product_type_name'].nunique()\ndf = pd.DataFrame({\n    'product group': article_group.index,\n    'product types': article_group.values\n})\ndf.sort_values(['product types'],ascending=False,inplace=True)\nplt.figure(figsize=(15,10))\nplt.title('Number of product types per each product group')\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-14T08:27:14.091867Z","iopub.execute_input":"2022-02-14T08:27:14.092372Z","iopub.status.idle":"2022-02-14T08:27:14.616196Z","shell.execute_reply.started":"2022-02-14T08:27:14.092338Z","shell.execute_reply":"2022-02-14T08:27:14.615278Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = train.copy()\nfig,ax = plt.subplots(1,1,figsize=(15,10))\nsns.kdeplot(np.log(df.loc[df.sales_channel_id == 1].price.value_counts()))\nsns.kdeplot(np.log(df.loc[df.sales_channel_id == 2].price.value_counts()))\nax.legend(labels=['Sales channel 1','Sales channel 2'])\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-02-14T08:27:14.619341Z","iopub.execute_input":"2022-02-14T08:27:14.619791Z","iopub.status.idle":"2022-02-14T08:27:22.770726Z","shell.execute_reply.started":"2022-02-14T08:27:14.619741Z","shell.execute_reply":"2022-02-14T08:27:22.769914Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_name_df = pd.DataFrame(images_names, columns=['image_name'])\nimage_name_df['article_id'] = image_name_df['image_name'].apply(lambda x: int(x[1:]))\nimage_name_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-02-14T08:27:22.773035Z","iopub.execute_input":"2022-02-14T08:27:22.774206Z","iopub.status.idle":"2022-02-14T08:27:22.896162Z","shell.execute_reply.started":"2022-02-14T08:27:22.774142Z","shell.execute_reply":"2022-02-14T08:27:22.895255Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_article_df = article[['article_id','product_code','product_group_name','product_type_name']].merge(\nimage_name_df,on=['article_id'],how='left')\nprint(image_article_df.shape)\nimage_article_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-02-14T08:27:22.89747Z","iopub.execute_input":"2022-02-14T08:27:22.897694Z","iopub.status.idle":"2022-02-14T08:27:22.979954Z","shell.execute_reply.started":"2022-02-14T08:27:22.897667Z","shell.execute_reply":"2022-02-14T08:27:22.978935Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"article_no_image_df = image_article_df.loc[image_article_df.image_name.isna()]\nprint(article_no_image_df.shape)\narticle_no_image_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-02-14T08:27:22.983478Z","iopub.execute_input":"2022-02-14T08:27:22.983725Z","iopub.status.idle":"2022-02-14T08:27:23.028898Z","shell.execute_reply.started":"2022-02-14T08:27:22.983696Z","shell.execute_reply":"2022-02-14T08:27:23.027652Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_image_samples(image_article_df, product_group_name, cols=1,rows=-1):\n    image_path = '../input/h-and-m-personalized-fashion-recommendations/images/'\n    df_ = image_article_df.loc[image_article_df.product_group_name == product_group_name]\n    article_ids = df_.article_id.values[0:cols*rows]\n    article_product = df_.product_type_name.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        product_type_name = article_product[i]\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{product_type_name}')\n        image = Image.open(f'{image_path}{article_id[:3]}/{article_id}.jpg')\n        plt.imshow(image)","metadata":{"execution":{"iopub.status.busy":"2022-02-14T08:27:23.03007Z","iopub.execute_input":"2022-02-14T08:27:23.030332Z","iopub.status.idle":"2022-02-14T08:27:23.039894Z","shell.execute_reply.started":"2022-02-14T08:27:23.030299Z","shell.execute_reply":"2022-02-14T08:27:23.039171Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(image_article_df.product_group_name.value_counts())","metadata":{"execution":{"iopub.status.busy":"2022-02-14T08:27:23.040894Z","iopub.execute_input":"2022-02-14T08:27:23.041123Z","iopub.status.idle":"2022-02-14T08:27:23.07191Z","shell.execute_reply.started":"2022-02-14T08:27:23.041094Z","shell.execute_reply":"2022-02-14T08:27:23.070881Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_image_samples(image_article_df,'Accessories',5,1)","metadata":{"execution":{"iopub.status.busy":"2022-02-14T08:27:23.075505Z","iopub.execute_input":"2022-02-14T08:27:23.075796Z","iopub.status.idle":"2022-02-14T08:27:25.974015Z","shell.execute_reply.started":"2022-02-14T08:27:23.075759Z","shell.execute_reply":"2022-02-14T08:27:25.972916Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_image_samples(image_article_df,'Furniture',5,1)","metadata":{"execution":{"iopub.status.busy":"2022-02-14T08:27:25.975336Z","iopub.execute_input":"2022-02-14T08:27:25.975559Z","iopub.status.idle":"2022-02-14T08:27:28.040174Z","shell.execute_reply.started":"2022-02-14T08:27:25.97553Z","shell.execute_reply":"2022-02-14T08:27:28.039288Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.t_dat = pd.to_datetime(train.t_dat)","metadata":{"execution":{"iopub.status.busy":"2022-02-14T08:52:14.190134Z","iopub.execute_input":"2022-02-14T08:52:14.19068Z","iopub.status.idle":"2022-02-14T08:52:20.972051Z","shell.execute_reply.started":"2022-02-14T08:52:14.190609Z","shell.execute_reply":"2022-02-14T08:52:20.971066Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_ = train[train.t_dat >= pd.to_datetime('2020-09-01')].copy()","metadata":{"execution":{"iopub.status.busy":"2022-02-14T08:52:20.973951Z","iopub.execute_input":"2022-02-14T08:52:20.974292Z","iopub.status.idle":"2022-02-14T08:52:21.609207Z","shell.execute_reply.started":"2022-02-14T08:52:20.974248Z","shell.execute_reply":"2022-02-14T08:52:21.608299Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_","metadata":{"execution":{"iopub.status.busy":"2022-02-14T08:52:21.61054Z","iopub.execute_input":"2022-02-14T08:52:21.610936Z","iopub.status.idle":"2022-02-14T08:52:21.630129Z","shell.execute_reply.started":"2022-02-14T08:52:21.610885Z","shell.execute_reply":"2022-02-14T08:52:21.629015Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"purchase_dict = {}\n\nfor i,x in enumerate(zip(train_['customer_id'], train_['article_id'])):\n    cust_id, art_id = x\n    if cust_id not in purchase_dict:\n        purchase_dict[cust_id] = {}\n    \n    if art_id not in purchase_dict[cust_id]:\n        purchase_dict[cust_id][art_id] = 0\n    \n    purchase_dict[cust_id][art_id] += 1\n    \nprint(len(purchase_dict))","metadata":{"execution":{"iopub.status.busy":"2022-02-14T08:48:07.596348Z","iopub.execute_input":"2022-02-14T08:48:07.596872Z","iopub.status.idle":"2022-02-14T08:48:15.649768Z","shell.execute_reply.started":"2022-02-14T08:48:07.596806Z","shell.execute_reply":"2022-02-14T08:48:15.648868Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"not_so_fancy_but_fast_benchmark = submission[['customer_id']]\nprediction_list = []\ndummy_list = list((train_['article_id'].value_counts()).index)[:12]\ndummy_pred = ' '.join(dummy_list)\n\nfor i, cust_id in enumerate(submission['customer_id'].values.reshape((-1,))):\n    if cust_id in purchase_dict:\n        l = sorted((purchase_dict[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[:(12-len(l))])\n    else:\n        s = dummy_pred\n    prediction_list.append(s)\n\nnot_so_fancy_but_fast_benchmark['prediction'] = prediction_list\nprint(not_so_fancy_but_fast_benchmark.shape)\nnot_so_fancy_but_fast_benchmark.head()","metadata":{"execution":{"iopub.status.busy":"2022-02-14T08:52:25.278261Z","iopub.execute_input":"2022-02-14T08:52:25.27854Z","iopub.status.idle":"2022-02-14T08:52:30.431456Z","shell.execute_reply.started":"2022-02-14T08:52:25.278512Z","shell.execute_reply":"2022-02-14T08:52:30.430878Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import gc\ndel train \ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2022-02-14T08:48:26.078448Z","iopub.execute_input":"2022-02-14T08:48:26.078765Z","iopub.status.idle":"2022-02-14T08:48:26.381741Z","shell.execute_reply.started":"2022-02-14T08:48:26.07873Z","shell.execute_reply":"2022-02-14T08:48:26.381028Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"not_so_fancy_but_fast_benchmark.to_csv('submission.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2022-02-14T08:52:35.838798Z","iopub.execute_input":"2022-02-14T08:52:35.839293Z","iopub.status.idle":"2022-02-14T08:52:48.600918Z","shell.execute_reply.started":"2022-02-14T08:52:35.839242Z","shell.execute_reply":"2022-02-14T08:52:48.600202Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Reference\nhttps://www.kaggle.com/abhilashawasthi/not-so-fancy-but-fast-benchmark?scriptVersionId=87382298","metadata":{}}]}