{"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\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sns","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-04-01T11:14:19.373070Z","iopub.execute_input":"2022-04-01T11:14:19.373405Z","iopub.status.idle":"2022-04-01T11:14:20.396064Z","shell.execute_reply.started":"2022-04-01T11:14:19.373324Z","shell.execute_reply":"2022-04-01T11:14:20.395216Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"articles = 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')","metadata":{"execution":{"iopub.status.busy":"2022-04-01T11:14:20.397537Z","iopub.execute_input":"2022-04-01T11:14:20.397789Z","iopub.status.idle":"2022-04-01T11:15:32.701414Z","shell.execute_reply.started":"2022-04-01T11:14:20.397759Z","shell.execute_reply":"2022-04-01T11:15:32.700511Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Article data**","metadata":{}},{"cell_type":"code","source":"articles.head()","metadata":{"execution":{"iopub.status.busy":"2022-04-01T11:15:32.702875Z","iopub.execute_input":"2022-04-01T11:15:32.703458Z","iopub.status.idle":"2022-04-01T11:15:32.757151Z","shell.execute_reply.started":"2022-04-01T11:15:32.703411Z","shell.execute_reply":"2022-04-01T11:15:32.756176Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"articles.shape","metadata":{"execution":{"iopub.status.busy":"2022-04-01T11:15:32.758986Z","iopub.execute_input":"2022-04-01T11:15:32.759226Z","iopub.status.idle":"2022-04-01T11:15:32.764836Z","shell.execute_reply.started":"2022-04-01T11:15:32.759197Z","shell.execute_reply":"2022-04-01T11:15:32.764085Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in articles.columns:\n    print('{} unique number:'.format(i),len(articles[i].unique()))","metadata":{"execution":{"iopub.status.busy":"2022-04-01T11:15:32.766310Z","iopub.execute_input":"2022-04-01T11:15:32.767142Z","iopub.status.idle":"2022-04-01T11:15:32.947306Z","shell.execute_reply.started":"2022-04-01T11:15:32.767066Z","shell.execute_reply":"2022-04-01T11:15:32.946331Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"articles.info()","metadata":{"execution":{"iopub.status.busy":"2022-04-01T11:15:32.948569Z","iopub.execute_input":"2022-04-01T11:15:32.948905Z","iopub.status.idle":"2022-04-01T11:15:33.127602Z","shell.execute_reply.started":"2022-04-01T11:15:32.948862Z","shell.execute_reply":"2022-04-01T11:15:33.127026Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"articles.groupby(['index_group_name', 'index_name']).count()['article_id']","metadata":{"execution":{"iopub.status.busy":"2022-04-01T11:15:33.128513Z","iopub.execute_input":"2022-04-01T11:15:33.129229Z","iopub.status.idle":"2022-04-01T11:15:33.316944Z","shell.execute_reply.started":"2022-04-01T11:15:33.129193Z","shell.execute_reply":"2022-04-01T11:15:33.316352Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = articles.groupby(['index_name'])['article_id'].nunique()\nx = data.index\ny = data.values\n\nfig,ax = plt.subplots(figsize=(15,5))\nplt.title('Number of Products per each index_name')\n\nplt.bar(x, y)\nax.set_xticklabels(x, rotation=45, ha='right')\nplt.ylabel('counts')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-04-01T11:15:33.317874Z","iopub.execute_input":"2022-04-01T11:15:33.318249Z","iopub.status.idle":"2022-04-01T11:15:33.563926Z","shell.execute_reply.started":"2022-04-01T11:15:33.318215Z","shell.execute_reply":"2022-04-01T11:15:33.563041Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"articles.groupby(['product_group_name']).size()","metadata":{"execution":{"iopub.status.busy":"2022-04-01T11:15:33.565293Z","iopub.execute_input":"2022-04-01T11:15:33.565503Z","iopub.status.idle":"2022-04-01T11:15:33.584334Z","shell.execute_reply.started":"2022-04-01T11:15:33.565478Z","shell.execute_reply":"2022-04-01T11:15:33.583542Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#pd.options.display.max_rows = None\narticles.groupby(['product_group_name', 'product_type_name']).count()['article_id']","metadata":{"execution":{"iopub.status.busy":"2022-04-01T11:15:33.587202Z","iopub.execute_input":"2022-04-01T11:15:33.587416Z","iopub.status.idle":"2022-04-01T11:15:33.771758Z","shell.execute_reply.started":"2022-04-01T11:15:33.587391Z","shell.execute_reply":"2022-04-01T11:15:33.771031Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"articles.groupby(['garment_group_name']).size()","metadata":{"execution":{"iopub.status.busy":"2022-04-01T11:15:33.772873Z","iopub.execute_input":"2022-04-01T11:15:33.773126Z","iopub.status.idle":"2022-04-01T11:15:33.792557Z","shell.execute_reply.started":"2022-04-01T11:15:33.773098Z","shell.execute_reply":"2022-04-01T11:15:33.792009Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# check some sample images of 'Special Offers' and 'Unknown'\nimport matplotlib.image as mpimg\n\nspecial_offers = articles[articles['garment_group_name']=='Special Offers'].iloc[:5][['article_id', 'prod_name' ,'detail_desc']]\nunknowns = articles[articles['garment_group_name']=='Unknown'].iloc[:5][['article_id', 'prod_name' ,'detail_desc']]","metadata":{"execution":{"iopub.status.busy":"2022-04-01T11:15:33.793463Z","iopub.execute_input":"2022-04-01T11:15:33.794061Z","iopub.status.idle":"2022-04-01T11:15:33.837750Z","shell.execute_reply.started":"2022-04-01T11:15:33.794021Z","shell.execute_reply":"2022-04-01T11:15:33.837063Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"f, ax = plt.subplots(1, 5, figsize=(20,10))\ni = 0\nfor _, data in special_offers.iterrows():\n    desc = articles[articles['article_id'] == data['article_id']]['detail_desc'].iloc[0]\n    desc_list = desc.split(' ')\n    for j, elem in enumerate(desc_list):\n        if j > 0 and j % 5 == 0:\n            desc_list[j] = desc_list[j] + '\\n'\n    desc = ' '.join(desc_list)\n    img = mpimg.imread(f'../input/h-and-m-personalized-fashion-recommendations/images/0{str(data.article_id)[:2]}/0{int(data.article_id)}.jpg')\n    ax[i].imshow(img)\n    ax[i].set_title(f'product name: {data.prod_name}')\n    ax[i].set_xticks([], [])\n    ax[i].set_yticks([], [])\n    ax[i].grid(False)\n    ax[i].set_xlabel(desc, fontsize=10)\n    i += 1\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-04-01T11:15:33.838946Z","iopub.execute_input":"2022-04-01T11:15:33.839333Z","iopub.status.idle":"2022-04-01T11:15:35.524749Z","shell.execute_reply.started":"2022-04-01T11:15:33.839303Z","shell.execute_reply":"2022-04-01T11:15:35.523895Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"f, ax = plt.subplots(1, 5, figsize=(20,10))\ni = 0\nfor _, data in unknowns.iterrows():\n    desc = articles[articles['article_id'] == data['article_id']]['detail_desc'].iloc[0]\n    desc_list = desc.split(' ')\n    for j, elem in enumerate(desc_list):\n        if j > 0 and j % 5 == 0:\n            desc_list[j] = desc_list[j] + '\\n'\n    desc = ' '.join(desc_list)\n    img = mpimg.imread(f'../input/h-and-m-personalized-fashion-recommendations/images/0{str(data.article_id)[:2]}/0{int(data.article_id)}.jpg')\n    ax[i].imshow(img)\n    ax[i].set_title(f'product name: {data.prod_name}')\n    ax[i].set_xticks([], [])\n    ax[i].set_yticks([], [])\n    ax[i].grid(False)\n    ax[i].set_xlabel(desc, fontsize=10)\n    i += 1\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-04-01T11:15:35.525895Z","iopub.execute_input":"2022-04-01T11:15:35.526246Z","iopub.status.idle":"2022-04-01T11:15:37.241059Z","shell.execute_reply.started":"2022-04-01T11:15:35.526211Z","shell.execute_reply":"2022-04-01T11:15:37.240327Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"articles.groupby(['garment_group_name', 'section_name']).count()['article_id']","metadata":{"execution":{"iopub.status.busy":"2022-04-01T11:15:37.242219Z","iopub.execute_input":"2022-04-01T11:15:37.242559Z","iopub.status.idle":"2022-04-01T11:15:37.430192Z","shell.execute_reply.started":"2022-04-01T11:15:37.242531Z","shell.execute_reply":"2022-04-01T11:15:37.429390Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"detail = list(articles['detail_desc'].unique())","metadata":{"execution":{"iopub.status.busy":"2022-04-01T11:15:37.431511Z","iopub.execute_input":"2022-04-01T11:15:37.432180Z","iopub.status.idle":"2022-04-01T11:15:37.455023Z","shell.execute_reply.started":"2022-04-01T11:15:37.432144Z","shell.execute_reply":"2022-04-01T11:15:37.454021Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"detail[:20]","metadata":{"execution":{"iopub.status.busy":"2022-04-01T11:15:37.456397Z","iopub.execute_input":"2022-04-01T11:15:37.456639Z","iopub.status.idle":"2022-04-01T11:15:37.467466Z","shell.execute_reply.started":"2022-04-01T11:15:37.456612Z","shell.execute_reply":"2022-04-01T11:15:37.466539Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Customers Data**","metadata":{}},{"cell_type":"code","source":"customers.head()","metadata":{"execution":{"iopub.status.busy":"2022-04-01T11:15:37.468712Z","iopub.execute_input":"2022-04-01T11:15:37.469156Z","iopub.status.idle":"2022-04-01T11:15:37.487670Z","shell.execute_reply.started":"2022-04-01T11:15:37.469120Z","shell.execute_reply":"2022-04-01T11:15:37.487053Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"customers.info()","metadata":{"execution":{"iopub.status.busy":"2022-04-01T11:15:37.488589Z","iopub.execute_input":"2022-04-01T11:15:37.489167Z","iopub.status.idle":"2022-04-01T11:15:38.112807Z","shell.execute_reply.started":"2022-04-01T11:15:37.489132Z","shell.execute_reply":"2022-04-01T11:15:38.111994Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(customers['customer_id'].unique())","metadata":{"execution":{"iopub.status.busy":"2022-04-01T11:15:38.114016Z","iopub.execute_input":"2022-04-01T11:15:38.114254Z","iopub.status.idle":"2022-04-01T11:15:38.630118Z","shell.execute_reply.started":"2022-04-01T11:15:38.114225Z","shell.execute_reply":"2022-04-01T11:15:38.628574Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"customers['postal_code'].nunique()","metadata":{"execution":{"iopub.status.busy":"2022-04-01T11:15:38.631489Z","iopub.execute_input":"2022-04-01T11:15:38.631733Z","iopub.status.idle":"2022-04-01T11:15:39.233917Z","shell.execute_reply.started":"2022-04-01T11:15:38.631704Z","shell.execute_reply":"2022-04-01T11:15:39.233039Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"customers['FN'].unique()","metadata":{"execution":{"iopub.status.busy":"2022-04-01T11:15:39.235319Z","iopub.execute_input":"2022-04-01T11:15:39.235981Z","iopub.status.idle":"2022-04-01T11:15:39.261982Z","shell.execute_reply.started":"2022-04-01T11:15:39.235911Z","shell.execute_reply":"2022-04-01T11:15:39.261123Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"customers.loc[customers['FN'].isna(), 'FN'] = 0\ncustomers['FN'].unique()","metadata":{"execution":{"iopub.status.busy":"2022-04-01T11:15:39.263264Z","iopub.execute_input":"2022-04-01T11:15:39.263997Z","iopub.status.idle":"2022-04-01T11:15:39.309174Z","shell.execute_reply.started":"2022-04-01T11:15:39.263936Z","shell.execute_reply":"2022-04-01T11:15:39.308428Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"customers['Active'].unique()","metadata":{"execution":{"iopub.status.busy":"2022-04-01T11:15:39.310278Z","iopub.execute_input":"2022-04-01T11:15:39.310514Z","iopub.status.idle":"2022-04-01T11:15:39.331676Z","shell.execute_reply.started":"2022-04-01T11:15:39.310486Z","shell.execute_reply":"2022-04-01T11:15:39.330808Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"customers.loc[customers['Active'].isna(), 'Active'] = 0\ncustomers['Active'].unique()","metadata":{"execution":{"iopub.status.busy":"2022-04-01T11:15:39.332917Z","iopub.execute_input":"2022-04-01T11:15:39.333149Z","iopub.status.idle":"2022-04-01T11:15:39.375747Z","shell.execute_reply.started":"2022-04-01T11:15:39.333123Z","shell.execute_reply":"2022-04-01T11:15:39.374849Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"customers['club_member_status'].unique()","metadata":{"execution":{"iopub.status.busy":"2022-04-01T11:15:39.377059Z","iopub.execute_input":"2022-04-01T11:15:39.377354Z","iopub.status.idle":"2022-04-01T11:15:39.528736Z","shell.execute_reply.started":"2022-04-01T11:15:39.377315Z","shell.execute_reply":"2022-04-01T11:15:39.528144Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"customers['fashion_news_frequency'].unique()","metadata":{"execution":{"iopub.status.busy":"2022-04-01T11:15:39.529847Z","iopub.execute_input":"2022-04-01T11:15:39.530179Z","iopub.status.idle":"2022-04-01T11:15:39.698124Z","shell.execute_reply.started":"2022-04-01T11:15:39.530151Z","shell.execute_reply":"2022-04-01T11:15:39.696900Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# replace the values in fashion_news_frequency\n# unify the words meaning 'None'\ncustomers.loc[~customers['fashion_news_frequency'].isin(['Regularly', 'Monthly']), 'fashion_news_frequency'] = 'None'\ncustomers['fashion_news_frequency'].unique()","metadata":{"execution":{"iopub.status.busy":"2022-04-01T11:15:39.706558Z","iopub.execute_input":"2022-04-01T11:15:39.707457Z","iopub.status.idle":"2022-04-01T11:15:39.916636Z","shell.execute_reply.started":"2022-04-01T11:15:39.707406Z","shell.execute_reply":"2022-04-01T11:15:39.915791Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"customers.age.describe()","metadata":{"execution":{"iopub.status.busy":"2022-04-01T11:15:39.918092Z","iopub.execute_input":"2022-04-01T11:15:39.918674Z","iopub.status.idle":"2022-04-01T11:15:39.984526Z","shell.execute_reply.started":"2022-04-01T11:15:39.918632Z","shell.execute_reply":"2022-04-01T11:15:39.983652Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.set()\nsns.histplot(x=customers['age'], bins=50)","metadata":{"execution":{"iopub.status.busy":"2022-04-01T11:15:39.985737Z","iopub.execute_input":"2022-04-01T11:15:39.986344Z","iopub.status.idle":"2022-04-01T11:15:40.514713Z","shell.execute_reply.started":"2022-04-01T11:15:39.986305Z","shell.execute_reply":"2022-04-01T11:15:40.514039Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.countplot(x=customers['club_member_status'])","metadata":{"execution":{"iopub.status.busy":"2022-04-01T11:15:40.515890Z","iopub.execute_input":"2022-04-01T11:15:40.516615Z","iopub.status.idle":"2022-04-01T11:15:42.430582Z","shell.execute_reply.started":"2022-04-01T11:15:40.516580Z","shell.execute_reply":"2022-04-01T11:15:42.429723Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fnf_pie = customers[['customer_id', 'fashion_news_frequency']].groupby('fashion_news_frequency').count()\nfnf_pie","metadata":{"execution":{"iopub.status.busy":"2022-04-01T11:15:42.431711Z","iopub.execute_input":"2022-04-01T11:15:42.431905Z","iopub.status.idle":"2022-04-01T11:15:42.795483Z","shell.execute_reply.started":"2022-04-01T11:15:42.431879Z","shell.execute_reply":"2022-04-01T11:15:42.794553Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"f, ax = plt.subplots(figsize=(5,5))\nax.pie(fnf_pie.customer_id, labels=fnf_pie.index, autopct='%.2f%%')\nax.set_xlabel('Distribution of fashion news frequency')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-04-01T11:15:42.796596Z","iopub.execute_input":"2022-04-01T11:15:42.796815Z","iopub.status.idle":"2022-04-01T11:15:43.060784Z","shell.execute_reply.started":"2022-04-01T11:15:42.796790Z","shell.execute_reply":"2022-04-01T11:15:43.059854Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Transaction data**","metadata":{}},{"cell_type":"code","source":"transactions.head()","metadata":{"execution":{"iopub.status.busy":"2022-04-01T11:15:43.062515Z","iopub.execute_input":"2022-04-01T11:15:43.062814Z","iopub.status.idle":"2022-04-01T11:15:43.082611Z","shell.execute_reply.started":"2022-04-01T11:15:43.062774Z","shell.execute_reply":"2022-04-01T11:15:43.081662Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transactions.info()","metadata":{"execution":{"iopub.status.busy":"2022-04-01T11:15:43.086180Z","iopub.execute_input":"2022-04-01T11:15:43.086977Z","iopub.status.idle":"2022-04-01T11:15:43.102110Z","shell.execute_reply.started":"2022-04-01T11:15:43.086911Z","shell.execute_reply":"2022-04-01T11:15:43.101174Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transactions['t_dat'] = pd.to_datetime(transactions['t_dat'], format='%Y-%m-%d')","metadata":{"execution":{"iopub.status.busy":"2022-04-01T11:15:43.104244Z","iopub.execute_input":"2022-04-01T11:15:43.104914Z","iopub.status.idle":"2022-04-01T11:15:49.275654Z","shell.execute_reply.started":"2022-04-01T11:15:43.104861Z","shell.execute_reply":"2022-04-01T11:15:49.274864Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transactions.info()","metadata":{"execution":{"iopub.status.busy":"2022-04-01T11:15:49.276991Z","iopub.execute_input":"2022-04-01T11:15:49.277232Z","iopub.status.idle":"2022-04-01T11:15:49.286186Z","shell.execute_reply.started":"2022-04-01T11:15:49.277205Z","shell.execute_reply":"2022-04-01T11:15:49.285417Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# number of customers who didn't purchase any articles\ncustomers_purchased = list(transactions['customer_id'].unique())\nlen(customers['customer_id'].unique()) - len(customers_purchased)","metadata":{"execution":{"iopub.status.busy":"2022-04-01T11:15:49.287389Z","iopub.execute_input":"2022-04-01T11:15:49.287616Z","iopub.status.idle":"2022-04-01T11:15:57.947497Z","shell.execute_reply.started":"2022-04-01T11:15:49.287587Z","shell.execute_reply":"2022-04-01T11:15:57.946708Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# number of articles that weren't purchased\npurchased_articles = list(transactions['article_id'].unique())\nlen(articles['article_id'].unique()) - len(purchased_articles)","metadata":{"execution":{"iopub.status.busy":"2022-04-01T11:15:57.948842Z","iopub.execute_input":"2022-04-01T11:15:57.949059Z","iopub.status.idle":"2022-04-01T11:15:58.244987Z","shell.execute_reply.started":"2022-04-01T11:15:57.949033Z","shell.execute_reply":"2022-04-01T11:15:58.244366Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"channel_id_pie = transactions.groupby('sales_channel_id').size()\nchannel_id_pie","metadata":{"execution":{"iopub.status.busy":"2022-04-01T11:15:58.245985Z","iopub.execute_input":"2022-04-01T11:15:58.246311Z","iopub.status.idle":"2022-04-01T11:15:58.887469Z","shell.execute_reply.started":"2022-04-01T11:15:58.246284Z","shell.execute_reply":"2022-04-01T11:15:58.886955Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"f, ax = plt.subplots(figsize=(5,5))\nax.pie(channel_id_pie, labels=channel_id_pie.index, autopct='%.1f%%')\nax.set_xlabel('Distribution of sales_channel_id')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-04-01T11:15:58.888298Z","iopub.execute_input":"2022-04-01T11:15:58.888934Z","iopub.status.idle":"2022-04-01T11:15:59.125623Z","shell.execute_reply.started":"2022-04-01T11:15:58.888902Z","shell.execute_reply":"2022-04-01T11:15:59.124712Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Merge Customer and Transaction data**","metadata":{}},{"cell_type":"code","source":"transactions.groupby(['customer_id', 't_dat', 'article_id']).sum()['price']","metadata":{"execution":{"iopub.status.busy":"2022-04-01T11:15:59.127136Z","iopub.execute_input":"2022-04-01T11:15:59.127656Z","iopub.status.idle":"2022-04-01T11:16:34.621241Z","shell.execute_reply.started":"2022-04-01T11:15:59.127606Z","shell.execute_reply":"2022-04-01T11:16:34.620402Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"new_transactions = transactions.groupby(['customer_id', 't_dat', 'article_id']).sum()['price'].reset_index()\nnew_transactions","metadata":{"execution":{"iopub.status.busy":"2022-04-01T11:16:34.622481Z","iopub.execute_input":"2022-04-01T11:16:34.622929Z","iopub.status.idle":"2022-04-01T11:17:11.373173Z","shell.execute_reply.started":"2022-04-01T11:16:34.622883Z","shell.execute_reply":"2022-04-01T11:17:11.372407Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_of_transaction = transactions.groupby(['customer_id']).count()['t_dat'].reset_index()","metadata":{"execution":{"iopub.status.busy":"2022-04-01T11:17:11.374453Z","iopub.execute_input":"2022-04-01T11:17:11.374708Z","iopub.status.idle":"2022-04-01T11:17:26.512155Z","shell.execute_reply.started":"2022-04-01T11:17:11.374678Z","shell.execute_reply":"2022-04-01T11:17:26.510850Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df1 = num_of_transaction.rename(columns={'t_dat': 'num_of_transactions'})\ndf1","metadata":{"execution":{"iopub.status.busy":"2022-04-01T11:17:26.514344Z","iopub.execute_input":"2022-04-01T11:17:26.514771Z","iopub.status.idle":"2022-04-01T11:17:26.560702Z","shell.execute_reply.started":"2022-04-01T11:17:26.514722Z","shell.execute_reply":"2022-04-01T11:17:26.560114Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df1['num_of_transactions'].describe()","metadata":{"execution":{"iopub.status.busy":"2022-04-01T11:17:26.561918Z","iopub.execute_input":"2022-04-01T11:17:26.562208Z","iopub.status.idle":"2022-04-01T11:17:26.629133Z","shell.execute_reply.started":"2022-04-01T11:17:26.562176Z","shell.execute_reply":"2022-04-01T11:17:26.628253Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.boxplot(y=df1[\"num_of_transactions\"])","metadata":{"execution":{"iopub.status.busy":"2022-04-01T11:17:26.630485Z","iopub.execute_input":"2022-04-01T11:17:26.630726Z","iopub.status.idle":"2022-04-01T11:17:27.338080Z","shell.execute_reply.started":"2022-04-01T11:17:26.630694Z","shell.execute_reply":"2022-04-01T11:17:27.337384Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sum_of_purchase = transactions.groupby(['customer_id']).sum()['price'].reset_index()\nsum_of_purchase","metadata":{"execution":{"iopub.status.busy":"2022-04-01T11:17:27.339533Z","iopub.execute_input":"2022-04-01T11:17:27.339755Z","iopub.status.idle":"2022-04-01T11:17:43.680429Z","shell.execute_reply.started":"2022-04-01T11:17:27.339720Z","shell.execute_reply":"2022-04-01T11:17:43.679479Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df2 = sum_of_purchase.rename(columns={'price': 'consumption_amount'})\ndf2['consumption_amount'].describe()","metadata":{"execution":{"iopub.status.busy":"2022-04-01T11:17:43.681721Z","iopub.execute_input":"2022-04-01T11:17:43.681943Z","iopub.status.idle":"2022-04-01T11:17:43.769397Z","shell.execute_reply.started":"2022-04-01T11:17:43.681916Z","shell.execute_reply":"2022-04-01T11:17:43.768625Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.boxplot(y=df2['consumption_amount'])","metadata":{"execution":{"iopub.status.busy":"2022-04-01T11:17:43.770715Z","iopub.execute_input":"2022-04-01T11:17:43.770935Z","iopub.status.idle":"2022-04-01T11:17:44.497649Z","shell.execute_reply.started":"2022-04-01T11:17:43.770909Z","shell.execute_reply":"2022-04-01T11:17:44.496849Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df3 = pd.merge(df1, df2, how='outer')","metadata":{"execution":{"iopub.status.busy":"2022-04-01T11:17:44.499067Z","iopub.execute_input":"2022-04-01T11:17:44.499291Z","iopub.status.idle":"2022-04-01T11:17:46.211607Z","shell.execute_reply.started":"2022-04-01T11:17:44.499264Z","shell.execute_reply":"2022-04-01T11:17:46.210687Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df3['consumption_mean'] = df3['consumption_amount'] / df3['num_of_transactions']\ndf3","metadata":{"execution":{"iopub.status.busy":"2022-04-01T11:17:46.213250Z","iopub.execute_input":"2022-04-01T11:17:46.213844Z","iopub.status.idle":"2022-04-01T11:17:46.237543Z","shell.execute_reply.started":"2022-04-01T11:17:46.213798Z","shell.execute_reply":"2022-04-01T11:17:46.236719Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"customers1 = pd.merge(customers, df3, how='outer')\ncustomers1","metadata":{"execution":{"iopub.status.busy":"2022-04-01T11:17:46.238952Z","iopub.execute_input":"2022-04-01T11:17:46.239299Z","iopub.status.idle":"2022-04-01T11:17:48.441017Z","shell.execute_reply.started":"2022-04-01T11:17:46.239256Z","shell.execute_reply":"2022-04-01T11:17:48.439997Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"customers1.loc[customers1['num_of_transactions'].isna(), 'num_of_transactions'] = 0\ncustomers1.loc[customers1['consumption_amount'].isna(), 'consumption_amount'] = 0\ncustomers1.loc[customers1['consumption_mean'].isna(), 'consumption_mean'] = 0","metadata":{"execution":{"iopub.status.busy":"2022-04-01T11:17:48.442199Z","iopub.execute_input":"2022-04-01T11:17:48.442425Z","iopub.status.idle":"2022-04-01T11:17:48.478674Z","shell.execute_reply.started":"2022-04-01T11:17:48.442398Z","shell.execute_reply":"2022-04-01T11:17:48.477886Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# each FN, Active, fashion_news vs. consumpotion_amount\ncustomers1.groupby(['FN'])['consumption_amount'].describe()","metadata":{"execution":{"iopub.status.busy":"2022-04-01T11:17:48.479732Z","iopub.execute_input":"2022-04-01T11:17:48.479949Z","iopub.status.idle":"2022-04-01T11:17:48.609798Z","shell.execute_reply.started":"2022-04-01T11:17:48.479921Z","shell.execute_reply":"2022-04-01T11:17:48.608951Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"customers1.groupby(['Active'])['consumption_amount'].describe()","metadata":{"execution":{"iopub.status.busy":"2022-04-01T11:17:48.611361Z","iopub.execute_input":"2022-04-01T11:17:48.611849Z","iopub.status.idle":"2022-04-01T11:17:48.738779Z","shell.execute_reply.started":"2022-04-01T11:17:48.611801Z","shell.execute_reply":"2022-04-01T11:17:48.737516Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"customers1.groupby(['fashion_news_frequency'])['consumption_amount'].describe()","metadata":{"execution":{"iopub.status.busy":"2022-04-01T11:17:48.740513Z","iopub.execute_input":"2022-04-01T11:17:48.740833Z","iopub.status.idle":"2022-04-01T11:17:48.997122Z","shell.execute_reply.started":"2022-04-01T11:17:48.740791Z","shell.execute_reply":"2022-04-01T11:17:48.996139Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"customers1.loc[customers1['club_member_status'].isna(), 'club_member_status'] = 'None'","metadata":{"execution":{"iopub.status.busy":"2022-04-01T11:17:48.998843Z","iopub.execute_input":"2022-04-01T11:17:48.999154Z","iopub.status.idle":"2022-04-01T11:17:49.191556Z","shell.execute_reply.started":"2022-04-01T11:17:48.999114Z","shell.execute_reply":"2022-04-01T11:17:49.190719Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"customers1.groupby(['club_member_status'])['consumption_amount'].describe()","metadata":{"execution":{"iopub.status.busy":"2022-04-01T11:17:49.192771Z","iopub.execute_input":"2022-04-01T11:17:49.193405Z","iopub.status.idle":"2022-04-01T11:17:49.408676Z","shell.execute_reply.started":"2022-04-01T11:17:49.193373Z","shell.execute_reply":"2022-04-01T11:17:49.407857Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = customers1.groupby(['age'])['consumption_amount'].mean()\nx = data.index\ny = data.values\n\nfig,ax = plt.subplots(figsize=(15,5))\nplt.title('consumption  by age')\n\nplt.bar(x, y)\nplt.xticks(np.arange(16, 101, step=2))\nplt.ylabel('num_of_transactions')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-04-01T11:17:49.409873Z","iopub.execute_input":"2022-04-01T11:17:49.410129Z","iopub.status.idle":"2022-04-01T11:17:50.144795Z","shell.execute_reply.started":"2022-04-01T11:17:49.410100Z","shell.execute_reply":"2022-04-01T11:17:50.143941Z"},"trusted":true},"execution_count":null,"outputs":[]}]}