{"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 pandas as pd\nimport numpy as np\nimport matplotlib.pylab as plt\nimport os\nfrom os import listdir\nimport cv2\nimport seaborn as sns","metadata":{"execution":{"iopub.status.busy":"2022-03-09T06:20:12.187554Z","iopub.execute_input":"2022-03-09T06:20:12.187840Z","iopub.status.idle":"2022-03-09T06:20:13.585904Z","shell.execute_reply.started":"2022-03-09T06:20:12.187764Z","shell.execute_reply":"2022-03-09T06:20:13.585230Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.ticker as mtick\n\ndef plot_bar(database, col, figsize=(13,5), pct=False, label='articles'):\n    fig, ax = plt.subplots(figsize=figsize, facecolor='#f6f6f6')\n    for loc in ['bottom', 'left']:\n        ax.spines[loc].set_visible(True)\n        ax.spines[loc].set_linewidth(2)\n        ax.spines[loc].set_color('black')\n    ax.spines['right'].set_visible(False)\n    ax.spines['top'].set_visible(False)\n    \n    if pct:\n        data = database[col].value_counts()\n        data = data.div(data.sum()).mul(100)\n        data = data.reset_index()\n        ax = sns.barplot(data=data, x=col, y='index', color='#2693d7', lw=1.5, ec='black', zorder=2)\n        ax.set_xlabel('% of ' + label, fontsize=10, weight='bold')\n        ax.xaxis.set_major_formatter(mtick.PercentFormatter())\n    else:\n        data = database[col].value_counts().reset_index()\n        ax = sns.barplot(data=data, x=col, y='index', color='#2693d7', lw=1.5, ec='black', zorder=2)        \n        ax.set_xlabel('# of articles' + label)\n        \n    ax.grid(zorder=0)\n    #ax.text(0, -1, \"column:\", color='black', fontsize=12, ha='left', va='bottom', weight='bold')\n    ax.text(0, -0.75, col, color='black', fontsize=10, ha='left', va='bottom', weight='bold', style='italic')\n    ax.set_ylabel('')\n        \n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-03-09T06:20:13.587472Z","iopub.execute_input":"2022-03-09T06:20:13.587810Z","iopub.status.idle":"2022-03-09T06:20:13.599819Z","shell.execute_reply.started":"2022-03-09T06:20:13.587768Z","shell.execute_reply":"2022-03-09T06:20:13.598839Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def show_items_in_category(column, value, no_imgs=4, title=None):\n    data = article[article[column]==value]\n    cat_ids = data['article_id'].iloc[:no_imgs].to_list()\n    \n    fig, ax = plt.subplots(1, no_imgs, figsize=(12,4))\n\n    for i, prod_id in enumerate(cat_ids):\n        folder = str(prod_id)[:2]\n        file_path = '../input/h-and-m-personalized-fashion-recommendations/images/0{}/0{}.jpg'.format(folder, prod_id)\n\n        img = plt.imread(file_path)       \n        ax[i].imshow(img, aspect='equal')\n        ax[i].grid(False)\n        ax[i].set_xticks([], [])\n        ax[i].set_yticks([], [])\n        ax[i].set_xlabel(article[article['article_id']==int(prod_id)]['prod_name'].iloc[0])\n    \n    fig.suptitle(title)\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-03-09T06:20:13.601228Z","iopub.execute_input":"2022-03-09T06:20:13.601480Z","iopub.status.idle":"2022-03-09T06:20:13.611991Z","shell.execute_reply.started":"2022-03-09T06:20:13.601453Z","shell.execute_reply":"2022-03-09T06:20:13.611330Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub = pd.read_csv(\"../input/h-and-m-personalized-fashion-recommendations/sample_submission.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-03-09T06:20:13.613846Z","iopub.execute_input":"2022-03-09T06:20:13.614197Z","iopub.status.idle":"2022-03-09T06:20:18.500986Z","shell.execute_reply.started":"2022-03-09T06:20:13.614152Z","shell.execute_reply":"2022-03-09T06:20:18.500139Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub.head()","metadata":{"execution":{"iopub.status.busy":"2022-03-09T06:20:18.502073Z","iopub.execute_input":"2022-03-09T06:20:18.503017Z","iopub.status.idle":"2022-03-09T06:20:18.521601Z","shell.execute_reply.started":"2022-03-09T06:20:18.502975Z","shell.execute_reply":"2022-03-09T06:20:18.520686Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"The sub variable above is part of the \"submission_sample.csv\" file. consists of 2 columns, namely customer_id and the prediction results of product purchases in the next 7 days as many as 12 items. The amount of data is 1,371,980 data samples for each customer.\n\nThis file is the reference for the prediction results in this competition, which is evaluated using the calculation Mean Average Precision @ 12 (MAP@12)","metadata":{}},{"cell_type":"code","source":"sub.info()","metadata":{"execution":{"iopub.status.busy":"2022-03-09T06:20:18.522772Z","iopub.execute_input":"2022-03-09T06:20:18.523060Z","iopub.status.idle":"2022-03-09T06:20:18.670034Z","shell.execute_reply.started":"2022-03-09T06:20:18.523031Z","shell.execute_reply":"2022-03-09T06:20:18.669119Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"article = pd.read_csv(\"../input/h-and-m-personalized-fashion-recommendations/articles.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-03-09T06:20:18.671305Z","iopub.execute_input":"2022-03-09T06:20:18.671579Z","iopub.status.idle":"2022-03-09T06:20:19.596501Z","shell.execute_reply.started":"2022-03-09T06:20:18.671539Z","shell.execute_reply":"2022-03-09T06:20:19.595758Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"article.info()","metadata":{"execution":{"iopub.status.busy":"2022-03-09T06:20:19.597614Z","iopub.execute_input":"2022-03-09T06:20:19.597829Z","iopub.status.idle":"2022-03-09T06:20:19.673268Z","shell.execute_reply.started":"2022-03-09T06:20:19.597804Z","shell.execute_reply":"2022-03-09T06:20:19.672557Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"the above article variable is part of the \"articles.csv\" file.\n\n5 product related columns:\n* •\tproduct_code (int64) - 6-digit product code, 47 224 unique values\n* •\tprod_name (object) - name of a product, 132 unique values\n* •\tproduct_type_no (int64) - product type number, 131 unique values\n* •\tproduct_type_name (object) - name of a product type, equivalent of product_type_no\n* •\tproduct_group_name (object) - name of a product group, in total 19 groups\n\n2 columns related to the pattern:\n* •\tgraphical_appearance_no - code of a pattern, 30 unique values\n* •\tgraphical_appearance_name - name of a pattern, 30 unique values\n\n2 columns related to the color:\n* •\tcolour_group_code - code of a color, 50 unique values\n* •\tcolour_group_name - name of a color, 50 unique values\n\n4 columns related to perceived colour (general tone):\n* •\tperceived_colour_value_id - perceived color id, 8 unique values\n* •\tperceived_colour_value_name - perceived color name, 8 unique values\n* •\tperceived_colour_master_id - perceived master color id, 20 unique values\n* •\tperceived_colour_master_name - perceived master color name, 20 unique values\n\n2 columns related to the department:\n* •\tdepartment_no - department number, 299 unique values\n* •\tdepartment_name - department name, 299 unique values\n\n4 columns related to the index, which is actually a top-level category:\n* •\tindex_code - index code, 10 unique values\n* •\tindex_name - index name, 10 unique values\n* •\tindex_group_no - index group code, 5 unique values\n* •\tindex_group_name - index group code, 5 unique values\n\n2 columns related to the section:\t\n* •\tsection_no - section number, 56 unique values\n* •\tsection_name - section name, 56 unique values\n\n2 columns related to the garment group:\n* •\tgarment_group_n - section number, 56 unique values\n* •\tgarment_group_name - section name, 56 unique values\n\n1 column with a detailed description of the article:\n* •\tdetail_desc - 43 404 unique values","metadata":{}},{"cell_type":"code","source":"article.head()","metadata":{"execution":{"iopub.status.busy":"2022-03-09T06:20:19.674320Z","iopub.execute_input":"2022-03-09T06:20:19.674642Z","iopub.status.idle":"2022-03-09T06:20:19.701262Z","shell.execute_reply.started":"2022-03-09T06:20:19.674616Z","shell.execute_reply":"2022-03-09T06:20:19.700437Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_bar(article, 'index_group_name', pct=True)","metadata":{"execution":{"iopub.status.busy":"2022-03-09T06:20:19.702505Z","iopub.execute_input":"2022-03-09T06:20:19.702760Z","iopub.status.idle":"2022-03-09T06:20:19.955666Z","shell.execute_reply.started":"2022-03-09T06:20:19.702727Z","shell.execute_reply":"2022-03-09T06:20:19.954779Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_bar(article, 'perceived_colour_value_name', pct=True)","metadata":{"execution":{"iopub.status.busy":"2022-03-09T06:20:19.957053Z","iopub.execute_input":"2022-03-09T06:20:19.957369Z","iopub.status.idle":"2022-03-09T06:20:20.190428Z","shell.execute_reply.started":"2022-03-09T06:20:19.957329Z","shell.execute_reply":"2022-03-09T06:20:20.189781Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"show_items_in_category('product_group_name', 'Garment Upper body', 5, 'Articles from a \"Garment Upper body\" category')","metadata":{"execution":{"iopub.status.busy":"2022-03-09T06:20:20.191583Z","iopub.execute_input":"2022-03-09T06:20:20.191948Z","iopub.status.idle":"2022-03-09T06:20:22.008981Z","shell.execute_reply.started":"2022-03-09T06:20:20.191906Z","shell.execute_reply":"2022-03-09T06:20:22.007990Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"show_items_in_category('index_group_name', 'Sport', 5, 'Articles from a \"Sport\" category')","metadata":{"execution":{"iopub.status.busy":"2022-03-09T06:20:22.011571Z","iopub.execute_input":"2022-03-09T06:20:22.011827Z","iopub.status.idle":"2022-03-09T06:20:23.582896Z","shell.execute_reply.started":"2022-03-09T06:20:22.011798Z","shell.execute_reply":"2022-03-09T06:20:23.582243Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cust = pd.read_csv(\"../input/h-and-m-personalized-fashion-recommendations/customers.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-03-09T06:20:23.583967Z","iopub.execute_input":"2022-03-09T06:20:23.584290Z","iopub.status.idle":"2022-03-09T06:20:28.790736Z","shell.execute_reply.started":"2022-03-09T06:20:23.584253Z","shell.execute_reply":"2022-03-09T06:20:28.790095Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"the above cust variable is part of the \"customers.csv\" file.\n\n5 product related columns:\n* •\tFN - binary feature (1 or NaN)\n* •\tActive - binary feature (1 or NaN)\n* •\tclub_member_status - status in a club, 3 unique values\n* •\tfashion_news_frequency - frequency of sending communication to the customer, 4 unique values\n* •\tage - age of the customer\n* •\tpostal_code - postal code (anonimized), 352 899 unique values\n","metadata":{}},{"cell_type":"code","source":"cust.head()","metadata":{"execution":{"iopub.status.busy":"2022-03-09T06:20:28.791973Z","iopub.execute_input":"2022-03-09T06:20:28.792197Z","iopub.status.idle":"2022-03-09T06:20:28.805826Z","shell.execute_reply.started":"2022-03-09T06:20:28.792170Z","shell.execute_reply":"2022-03-09T06:20:28.804799Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cust.isna().sum()/len(article)*100","metadata":{"execution":{"iopub.status.busy":"2022-03-09T06:20:28.807293Z","iopub.execute_input":"2022-03-09T06:20:28.807500Z","iopub.status.idle":"2022-03-09T06:20:29.100603Z","shell.execute_reply.started":"2022-03-09T06:20:28.807475Z","shell.execute_reply":"2022-03-09T06:20:29.099810Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cust_backup = cust.copy()\ncust['Active'] = cust['Active'].fillna(0)\n\nfig, ax = plt.subplots(figsize=(5,5))\nexplode = (0, 0.1)\ncolors = sns.color_palette('Paired')\nax.pie(cust['Active'].value_counts(), explode=explode, labels=['Not-active','Active'],\n      autopct='%1.1f%%',shadow=True, startangle=90, colors=colors)\nax.axis('equal')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-03-09T06:20:29.102247Z","iopub.execute_input":"2022-03-09T06:20:29.102540Z","iopub.status.idle":"2022-03-09T06:20:29.358599Z","shell.execute_reply.started":"2022-03-09T06:20:29.102499Z","shell.execute_reply":"2022-03-09T06:20:29.357620Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels = [\"10\",\"20\",\"30\",\"40\",\"50\",\"60\",\"70\",\"80\",\"90\",\"100\"]\nlabels_number = [int(label) for label in labels]\nlabels = [f\"{label}'s\" for label in labels]\nd = pd.cut(cust[\"age\"],labels_number,include_lowest=True, right=False, labels=labels[:-1])\ncust[\"age_binned\"] = pd.Categorical(d , categories=labels[:-1], ordered=True)","metadata":{"execution":{"iopub.status.busy":"2022-03-09T06:20:29.362796Z","iopub.execute_input":"2022-03-09T06:20:29.363539Z","iopub.status.idle":"2022-03-09T06:20:29.444689Z","shell.execute_reply.started":"2022-03-09T06:20:29.363486Z","shell.execute_reply":"2022-03-09T06:20:29.443984Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cust[\"age_binned\"].value_counts(sort=False)","metadata":{"execution":{"iopub.status.busy":"2022-03-09T06:20:29.445768Z","iopub.execute_input":"2022-03-09T06:20:29.446367Z","iopub.status.idle":"2022-03-09T06:20:29.462767Z","shell.execute_reply.started":"2022-03-09T06:20:29.446337Z","shell.execute_reply":"2022-03-09T06:20:29.461768Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_bar(cust, 'age_binned', pct=True)","metadata":{"execution":{"iopub.status.busy":"2022-03-09T06:20:29.464000Z","iopub.execute_input":"2022-03-09T06:20:29.464379Z","iopub.status.idle":"2022-03-09T06:20:29.705178Z","shell.execute_reply.started":"2022-03-09T06:20:29.464339Z","shell.execute_reply":"2022-03-09T06:20:29.704585Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trans = pd.read_csv(\"../input/h-and-m-personalized-fashion-recommendations/transactions_train.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-03-09T06:20:29.706079Z","iopub.execute_input":"2022-03-09T06:20:29.706828Z","iopub.status.idle":"2022-03-09T06:21:33.089472Z","shell.execute_reply.started":"2022-03-09T06:20:29.706797Z","shell.execute_reply":"2022-03-09T06:21:33.087686Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transaction_time = pd.to_datetime(trans[\"t_dat\"])","metadata":{"execution":{"iopub.status.busy":"2022-03-09T06:21:33.092408Z","iopub.execute_input":"2022-03-09T06:21:33.092761Z","iopub.status.idle":"2022-03-09T06:21:36.859850Z","shell.execute_reply.started":"2022-03-09T06:21:33.092718Z","shell.execute_reply":"2022-03-09T06:21:36.858930Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"table = pd.Categorical(transaction_time.dt.year.values.squeeze(),ordered=True).value_counts()\nprint(pd.DataFrame(table).T)\ntable.plot.bar(figsize=(16,4), rot=90)","metadata":{"execution":{"iopub.status.busy":"2022-03-09T06:21:36.861171Z","iopub.execute_input":"2022-03-09T06:21:36.861976Z","iopub.status.idle":"2022-03-09T06:21:40.671183Z","shell.execute_reply.started":"2022-03-09T06:21:36.861942Z","shell.execute_reply":"2022-03-09T06:21:40.670479Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, ax = plt.subplots(figsize=(5,5))\nexplode = (0, 0.1)\ncolors = sns.color_palette('Paired')\nax.pie(trans['sales_channel_id'].value_counts(), explode=explode, labels=['1','2'],\n       autopct='%1.1f%%',shadow=True, startangle=90, colors=colors)\nax.axis('equal')\nax.set_title('Sale channel')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-03-09T06:21:40.672222Z","iopub.execute_input":"2022-03-09T06:21:40.672446Z","iopub.status.idle":"2022-03-09T06:21:40.945150Z","shell.execute_reply.started":"2022-03-09T06:21:40.672419Z","shell.execute_reply":"2022-03-09T06:21:40.944024Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Collaborative Filtering Method for prediction fashion recommendation**\n\nreference:\nhttps://medium.com/@toprak.mhmt/collaborative-filtering-3ceb89080ade#:~:text=Collaborative%20filtering%20is%20based%20on,like%20or%20be%20interested%20in.","metadata":{}}]}