{"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":"# H&M Personalized Fashion Recommendations","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19"}},{"cell_type":"markdown","source":"### Import packages","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport seaborn as sns\nfrom matplotlib import pyplot as plt\nfrom tqdm.notebook import tqdm\n\nimport plotly.express as px\nimport plotly.figure_factory as ff\n\nimport warnings\nwarnings.filterwarnings(\"ignore\")","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-04-21T07:09:42.394889Z","iopub.execute_input":"2022-04-21T07:09:42.395515Z","iopub.status.idle":"2022-04-21T07:09:45.609418Z","shell.execute_reply.started":"2022-04-21T07:09:42.395418Z","shell.execute_reply":"2022-04-21T07:09:45.608553Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"  def reduce_mem_usage(df):\n    \"\"\"iterate through all the columns of a dataframe and modify the data type\n    to reduce memory usage.\n    \"\"\"\n    start_mem = df.memory_usage().sum() / 1024 ** 2\n    print(\"Memory usage of dataframe is {:.2f} MB\".format(start_mem))\n    for col in df.select_dtypes(exclude=[np.datetime64]).columns:\n        col_type = df[col].dtype\n        if col_type != object:\n            c_min = df[col].min()\n            c_max = df[col].max()\n            if str(col_type)[:3] == \"int\":\n                if c_min > np.iinfo(np.int8).min and c_max < np.iinfo(np.int8).max:\n                    df[col] = df[col].astype(np.int8)\n                elif c_min > np.iinfo(np.int16).min and c_max < np.iinfo(np.int16).max:\n                    df[col] = df[col].astype(np.int16)\n                elif c_min > np.iinfo(np.int32).min and c_max < np.iinfo(np.int32).max:\n                    df[col] = df[col].astype(np.int32)\n                elif c_min > np.iinfo(np.int64).min and c_max < np.iinfo(np.int64).max:\n                    df[col] = df[col].astype(np.int64)\n            else:\n                if (\n                    c_min > np.finfo(np.float16).min\n                    and c_max < np.finfo(np.float16).max\n                ):\n                    df[col] = df[col].astype(np.float16)\n                elif (\n                    c_min > np.finfo(np.float32).min\n                    and c_max < np.finfo(np.float32).max\n                ):\n                    df[col] = df[col].astype(np.float32)\n    end_mem = df.memory_usage().sum() / 1024 ** 2\n    print(\"Memory usage after optimization is: {:.2f} MB\".format(end_mem))\n    print(\"Decreased by {:.1f}%\".format(100 * (start_mem - end_mem) / start_mem))\n    return df","metadata":{"execution":{"iopub.status.busy":"2022-04-21T07:09:45.613522Z","iopub.execute_input":"2022-04-21T07:09:45.613938Z","iopub.status.idle":"2022-04-21T07:09:45.629909Z","shell.execute_reply.started":"2022-04-21T07:09:45.613888Z","shell.execute_reply":"2022-04-21T07:09:45.629313Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Import dataset","metadata":{}},{"cell_type":"code","source":"transactions = pd.read_csv(\"/kaggle/input/h-and-m-personalized-fashion-recommendations/transactions_train.csv\", dtype={\"article_id\": \"str\"})\ncustomers = pd.read_csv(\"/kaggle/input/h-and-m-personalized-fashion-recommendations/customers.csv\")\narticles = pd.read_csv(\"/kaggle/input/h-and-m-personalized-fashion-recommendations/articles.csv\", dtype={\"article_id\": \"str\"})","metadata":{"execution":{"iopub.status.busy":"2022-04-21T07:09:45.631536Z","iopub.execute_input":"2022-04-21T07:09:45.631749Z","iopub.status.idle":"2022-04-21T07:11:04.361718Z","shell.execute_reply.started":"2022-04-21T07:09:45.631723Z","shell.execute_reply":"2022-04-21T07:11:04.360707Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"files = [articles,customers,transactions]\n\nfor file in files:\n    reduce_mem_usage(file)","metadata":{"execution":{"iopub.status.busy":"2022-04-21T07:11:04.364561Z","iopub.execute_input":"2022-04-21T07:11:04.365021Z","iopub.status.idle":"2022-04-21T07:11:07.363096Z","shell.execute_reply.started":"2022-04-21T07:11:04.364987Z","shell.execute_reply":"2022-04-21T07:11:07.362174Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Articles\n\n\n- **article_id** : A unique identifier of every article. /n\n- **product_code, prod_name** : A unique identifier of every product and its name (not the same).\n- **product_type, product_type_name** : The group of product_code and its name\n- **graphical_appearance_no, graphical_appearance_name** : The group of graphics and its name\n- **colour_group_code, colour_group_name** : The group of color and its name\n- **perceived_colour_value_id, perceived_colour_value_name, perceived_colour_master_id, perceived_colour_master_name** : The added color info\n- **department_no, department_name**: A unique identifier of every dep and its name\n- **index_code, index_name**: A unique identifier of every index and its name\n- **index_group_no, index_group_name**: A group of indeces and its name\n- **section_no, section_name**: A unique identifier of every section and its name\n- **garment_group_no, garment_group_name**: A unique identifier of every garment and its name\n- **detail_desc**: Details","metadata":{}},{"cell_type":"code","source":"articles.head()","metadata":{"execution":{"iopub.status.busy":"2022-04-21T07:11:07.364673Z","iopub.execute_input":"2022-04-21T07:11:07.364989Z","iopub.status.idle":"2022-04-21T07:11:07.401709Z","shell.execute_reply.started":"2022-04-21T07:11:07.36495Z","shell.execute_reply":"2022-04-21T07:11:07.400884Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"f, ax = plt.subplots(figsize = (15,10))\nax = sns.histplot(data = articles, y = 'index_name', color = 'green')\nax.set_xlabel('Count by index name')\nax.set_ylabel('Index name')\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2022-04-21T07:11:07.403108Z","iopub.execute_input":"2022-04-21T07:11:07.403497Z","iopub.status.idle":"2022-04-21T07:11:07.822347Z","shell.execute_reply.started":"2022-04-21T07:11:07.403456Z","shell.execute_reply":"2022-04-21T07:11:07.819686Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"temp = articles.groupby([\"product_group_name\"])[\"product_type_name\"].nunique()\ndf = pd.DataFrame({'Product Group': temp.index,\n                   'Product Types': temp.values\n                  })\ndf = df.sort_values(['Product Types'], ascending=False)\n\n# Plotly code\npx.bar(df, x='Product Group', y='Product Types', \n       title='Number of Product Types per each Product Group', \n      )","metadata":{"execution":{"iopub.status.busy":"2022-04-21T07:11:07.82376Z","iopub.execute_input":"2022-04-21T07:11:07.824284Z","iopub.status.idle":"2022-04-21T07:11:08.726749Z","shell.execute_reply.started":"2022-04-21T07:11:07.824252Z","shell.execute_reply":"2022-04-21T07:11:08.726173Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"articles.groupby(['index_group_name', 'section_name']).count()['article_id']","metadata":{"execution":{"iopub.status.busy":"2022-04-21T07:11:08.727704Z","iopub.execute_input":"2022-04-21T07:11:08.728359Z","iopub.status.idle":"2022-04-21T07:11:08.941612Z","shell.execute_reply.started":"2022-04-21T07:11:08.728309Z","shell.execute_reply":"2022-04-21T07:11:08.940785Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for col in articles.columns:\n    n_unique = articles[col].nunique()\n    print(f'Number of unique values in {col}: {n_unique}')","metadata":{"execution":{"iopub.status.busy":"2022-04-21T07:11:08.94284Z","iopub.execute_input":"2022-04-21T07:11:08.943167Z","iopub.status.idle":"2022-04-21T07:11:09.154914Z","shell.execute_reply.started":"2022-04-21T07:11:08.943137Z","shell.execute_reply":"2022-04-21T07:11:09.153975Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Customers\n\n- **customer_id** : A unique identifier of every customer\n- **FN** : 1 or missed\n- **Active** : 1 or missed\n- **club_member_status** : Status in club\n- **fashion_news_frequency** : How often H&M may send news to customer\n- **age** : The current age\n- **postal_code** : Postal code of customer","metadata":{}},{"cell_type":"code","source":"import seaborn as sns\nfrom matplotlib import pyplot as plt\n\ntemp = customers.groupby([\"age\"])[\"customer_id\"].count()\ndf = pd.DataFrame({'Age': temp.index,\n                   'Customers': temp.values\n                  })\ndf = df.sort_values(['Age'], ascending=False)\n\n# Plotly code\npx.bar(df, x='Age', y='Customers', \n       title='Number of Customers per each Age', \n      )","metadata":{"execution":{"iopub.status.busy":"2022-04-21T07:11:09.158091Z","iopub.execute_input":"2022-04-21T07:11:09.158344Z","iopub.status.idle":"2022-04-21T07:11:09.412988Z","shell.execute_reply.started":"2022-04-21T07:11:09.158317Z","shell.execute_reply":"2022-04-21T07:11:09.412173Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"temp = customers.groupby([\"fashion_news_frequency\"])[\"customer_id\"].count()\ndf = pd.DataFrame({'Fashion News Frequency': temp.index,\n                   'Customers': temp.values\n                  })\ndf = df.sort_values(['Customers'], ascending=False)\n\n# Plotly code\npx.bar(df, x='Fashion News Frequency', y='Customers', \n       title='Number of Customers per each Fashion News Frequency', \n      )","metadata":{"execution":{"iopub.status.busy":"2022-04-21T07:11:09.414397Z","iopub.execute_input":"2022-04-21T07:11:09.414668Z","iopub.status.idle":"2022-04-21T07:11:09.824638Z","shell.execute_reply.started":"2022-04-21T07:11:09.414638Z","shell.execute_reply":"2022-04-21T07:11:09.823788Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"temp = customers.groupby([\"club_member_status\"])[\"customer_id\"].count()\ndf = pd.DataFrame({'Club Member Status': temp.index,\n                   'Customers': temp.values\n                  })\ndf = df.sort_values(['Customers'], ascending=False)\n\n# Plotly code\npx.bar(df, x='Club Member Status', y='Customers', \n       title='Number of Customers per each Club Member Status', \n      )","metadata":{"execution":{"iopub.status.busy":"2022-04-21T07:11:09.825754Z","iopub.execute_input":"2022-04-21T07:11:09.82597Z","iopub.status.idle":"2022-04-21T07:11:10.230608Z","shell.execute_reply.started":"2022-04-21T07:11:09.825945Z","shell.execute_reply":"2022-04-21T07:11:10.22976Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"customers[\"FN\"].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-04-21T07:11:10.231672Z","iopub.execute_input":"2022-04-21T07:11:10.231883Z","iopub.status.idle":"2022-04-21T07:11:10.264017Z","shell.execute_reply.started":"2022-04-21T07:11:10.231859Z","shell.execute_reply":"2022-04-21T07:11:10.263176Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"- **1 - offline**\n- **2 - online**","metadata":{}},{"cell_type":"code","source":"x = transactions[transactions['sales_channel_id'] == 1].count()['sales_channel_id']\ny = transactions['sales_channel_id'].count()\nprint(f'Percent of articles bought offline = {x/y* 100:.2f}%, and online {100 - x/y*100:.2f}%')","metadata":{"execution":{"iopub.status.busy":"2022-04-21T07:11:10.265583Z","iopub.execute_input":"2022-04-21T07:11:10.266273Z","iopub.status.idle":"2022-04-21T07:11:14.697527Z","shell.execute_reply.started":"2022-04-21T07:11:10.266226Z","shell.execute_reply":"2022-04-21T07:11:14.696614Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Merge all 3 tables (transactions, customers, articles)","metadata":{}},{"cell_type":"code","source":"cus_tran = pd.merge(left = transactions, right = customers, how = 'inner', on = 'customer_id')","metadata":{"execution":{"iopub.status.busy":"2022-04-21T07:11:14.699179Z","iopub.execute_input":"2022-04-21T07:11:14.699532Z","iopub.status.idle":"2022-04-21T07:11:49.561515Z","shell.execute_reply.started":"2022-04-21T07:11:14.699487Z","shell.execute_reply":"2022-04-21T07:11:49.560647Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"art = articles[['article_id','product_type_name']]\ncus_tran_art = pd.merge(left = cus_tran, right = art, how = 'left', on = 'article_id')\ncus_tran_art.t_dat = pd.to_datetime(cus_tran_art['t_dat'])","metadata":{"execution":{"iopub.status.busy":"2022-04-21T07:11:49.562875Z","iopub.execute_input":"2022-04-21T07:11:49.563181Z","iopub.status.idle":"2022-04-21T07:12:26.107912Z","shell.execute_reply.started":"2022-04-21T07:11:49.563141Z","shell.execute_reply":"2022-04-21T07:12:26.107277Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cus_tran_art.t_dat = pd.to_datetime(cus_tran_art['t_dat'])","metadata":{"execution":{"iopub.status.busy":"2022-04-21T07:12:26.10916Z","iopub.execute_input":"2022-04-21T07:12:26.109936Z","iopub.status.idle":"2022-04-21T07:12:27.245572Z","shell.execute_reply.started":"2022-04-21T07:12:26.109893Z","shell.execute_reply":"2022-04-21T07:12:27.24469Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#extract month from date\ncus_tran_art['month'] = cus_tran_art.t_dat.dt.month","metadata":{"execution":{"iopub.status.busy":"2022-04-21T07:12:27.247047Z","iopub.execute_input":"2022-04-21T07:12:27.247302Z","iopub.status.idle":"2022-04-21T07:12:30.397777Z","shell.execute_reply.started":"2022-04-21T07:12:27.247271Z","shell.execute_reply":"2022-04-21T07:12:30.397099Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Online vs offline","metadata":{}},{"cell_type":"code","source":"#split data by sales channel id, 1 = online, 2 = offline\ndf_online = cus_tran_art[cus_tran_art['sales_channel_id'] == 2]\ndf_offline = cus_tran_art[cus_tran_art['sales_channel_id'] == 1]","metadata":{"execution":{"iopub.status.busy":"2022-04-21T07:12:30.399021Z","iopub.execute_input":"2022-04-21T07:12:30.399243Z","iopub.status.idle":"2022-04-21T07:12:49.08109Z","shell.execute_reply.started":"2022-04-21T07:12:30.399219Z","shell.execute_reply":"2022-04-21T07:12:49.080311Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#hist of age distribution for split data\nsns.set(rc={'figure.figsize':(24,8)})\nfig, ax = plt.subplots(1,2)\nsns.histplot(data=df_online, x = 'age', binwidth = 2, ax = ax[0]).set(title='Age of customer bought online')\nsns.histplot(data=df_offline, x = 'age', binwidth = 2, ax = ax[1]).set(title='Age of customer bought offline')\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2022-04-21T07:12:49.082444Z","iopub.execute_input":"2022-04-21T07:12:49.082643Z","iopub.status.idle":"2022-04-21T07:13:01.900596Z","shell.execute_reply.started":"2022-04-21T07:12:49.082617Z","shell.execute_reply":"2022-04-21T07:13:01.899807Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"top_10_online = df_online['product_type_name'].value_counts()\ntop_offline = df_offline['product_type_name'].value_counts()\ntop10offline = top_offline.head(10)\ntop10online = top_10_online.head(10)","metadata":{"execution":{"iopub.status.busy":"2022-04-21T07:13:01.901839Z","iopub.execute_input":"2022-04-21T07:13:01.902407Z","iopub.status.idle":"2022-04-21T07:13:06.626146Z","shell.execute_reply.started":"2022-04-21T07:13:01.902276Z","shell.execute_reply":"2022-04-21T07:13:06.625547Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.set(rc={'figure.figsize':(24,8)})\nfig, ax = plt.subplots(1,2)\nsns.barplot(x = top10online,y = top10online.index, ax = ax[0]).set(title='Top 10 popular products bought online')\nsns.barplot(x = top10offline,y = top10offline.index, ax = ax[1]).set(title='Top 10 popular products bought offline')\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2022-04-21T07:13:06.62757Z","iopub.execute_input":"2022-04-21T07:13:06.628023Z","iopub.status.idle":"2022-04-21T07:13:07.21098Z","shell.execute_reply.started":"2022-04-21T07:13:06.627981Z","shell.execute_reply":"2022-04-21T07:13:07.210155Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = cus_tran_art.groupby('sales_channel_id')['age'].mean()\nx","metadata":{"execution":{"iopub.status.busy":"2022-04-21T07:13:07.21233Z","iopub.execute_input":"2022-04-21T07:13:07.212809Z","iopub.status.idle":"2022-04-21T07:13:08.067083Z","shell.execute_reply.started":"2022-04-21T07:13:07.212763Z","shell.execute_reply":"2022-04-21T07:13:08.066233Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cus_tran_art.groupby('club_member_status')['age'].mean()","metadata":{"execution":{"iopub.status.busy":"2022-04-21T07:13:08.068438Z","iopub.execute_input":"2022-04-21T07:13:08.06865Z","iopub.status.idle":"2022-04-21T07:13:12.651743Z","shell.execute_reply.started":"2022-04-21T07:13:08.068624Z","shell.execute_reply":"2022-04-21T07:13:12.650987Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Monthly sales, trending products","metadata":{}},{"cell_type":"code","source":"top10all = cus_tran_art.groupby('product_type_name',as_index = False)['customer_id'].count().sort_values('customer_id', ascending = False).head(10)\ntop10all","metadata":{"execution":{"iopub.status.busy":"2022-04-21T07:13:12.653032Z","iopub.execute_input":"2022-04-21T07:13:12.653334Z","iopub.status.idle":"2022-04-21T07:13:22.307533Z","shell.execute_reply.started":"2022-04-21T07:13:12.653303Z","shell.execute_reply":"2022-04-21T07:13:22.306704Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.set(rc={'figure.figsize':(24,6)})\nsns.barplot(x = 'product_type_name', y = 'customer_id', data = top10all).set_title(f'Top 10 product types for all customers')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-04-21T07:13:22.308953Z","iopub.execute_input":"2022-04-21T07:13:22.309299Z","iopub.status.idle":"2022-04-21T07:13:22.86681Z","shell.execute_reply.started":"2022-04-21T07:13:22.309247Z","shell.execute_reply":"2022-04-21T07:13:22.865914Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"monthly = cus_tran_art.groupby(['month','product_type_name'],as_index = False)['customer_id'].count()\n\nmonthly = monthly.sort_values(['month','customer_id'], ascending = [True, False])\n\n#Top 10 product types for every month\n\nsns.set(rc={'figure.figsize':(24,6)})\nfor i in range(1,13):\n    plt.figure()\n    sns.barplot(x = 'product_type_name', y = 'customer_id', data = monthly[monthly['month'] == i].head(10)).set_title(f'Top 10 product types for month number {i}')\n    fig.show()","metadata":{"execution":{"iopub.status.busy":"2022-04-21T07:13:22.868355Z","iopub.execute_input":"2022-04-21T07:13:22.868658Z","iopub.status.idle":"2022-04-21T07:13:38.294767Z","shell.execute_reply.started":"2022-04-21T07:13:22.868618Z","shell.execute_reply":"2022-04-21T07:13:38.293975Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"top10month = pd.DataFrame()\nfor i in range(1,13):\n    top10month[i] = monthly[monthly['month'] == i].head(10).reset_index()['product_type_name']\n\ntop10month","metadata":{"execution":{"iopub.status.busy":"2022-04-21T07:13:38.298593Z","iopub.execute_input":"2022-04-21T07:13:38.298795Z","iopub.status.idle":"2022-04-21T07:13:38.33777Z","shell.execute_reply.started":"2022-04-21T07:13:38.29877Z","shell.execute_reply":"2022-04-21T07:13:38.336957Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Grouped by age","metadata":{}},{"cell_type":"code","source":"listBin = [-1, 19, 29, 39, 49, 59, 69, 119]\ncus_tran_art.age = cus_tran_art.age.fillna(0)\nlabels = [\"-1:19\",\"19:29\", \"29:39\", \"39:49\", \"49:59\", \"59:69\", \"69:119\"]\ncus_tran_art['age_bins'] = pd.cut(cus_tran_art['age'], listBin, labels=labels)","metadata":{"execution":{"iopub.status.busy":"2022-04-21T07:13:38.338884Z","iopub.execute_input":"2022-04-21T07:13:38.339179Z","iopub.status.idle":"2022-04-21T07:13:39.448304Z","shell.execute_reply.started":"2022-04-21T07:13:38.339148Z","shell.execute_reply":"2022-04-21T07:13:39.447392Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cus_tran_art","metadata":{"execution":{"iopub.status.busy":"2022-04-21T07:13:39.449743Z","iopub.execute_input":"2022-04-21T07:13:39.450013Z","iopub.status.idle":"2022-04-21T07:13:39.482293Z","shell.execute_reply.started":"2022-04-21T07:13:39.449971Z","shell.execute_reply":"2022-04-21T07:13:39.481691Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels = [\"-1:19\",\"19:29\", \"29:39\", \"39:49\", \"49:59\", \"59:69\", \"69:119\"]\nfor label in labels:\n    \n    df = cus_tran_art[cus_tran_art['age_bins'] == label]\n    top10 = pd.DataFrame(df.groupby('product_type_name').count()['customer_id'].sort_values(ascending = False).head(10))\n    plt.figure()\n    sns.barplot(x = top10.index,y = 'customer_id', data = top10).set_title(f'Top 10 product types for {label} age group')\n    fig.show()","metadata":{"execution":{"iopub.status.busy":"2022-04-21T07:13:39.483283Z","iopub.execute_input":"2022-04-21T07:13:39.483727Z","iopub.status.idle":"2022-04-21T07:14:24.396429Z","shell.execute_reply.started":"2022-04-21T07:13:39.483697Z","shell.execute_reply":"2022-04-21T07:14:24.395648Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ages = cus_tran_art.groupby(['age_bins','product_type_name'],as_index = False)['customer_id'].count()\nages = ages.sort_values(['age_bins','customer_id'], ascending = [True, False])\n\nlabels = [\"-1:19\",\"19:29\", \"29:39\", \"39:49\", \"49:59\", \"59:69\", \"69:119\"]\ntop10age = pd.DataFrame()\nfor label in labels:\n    top10age[label] = ages[ages['age_bins'] == label].head(10).reset_index()['product_type_name']\n\ntop10age","metadata":{"execution":{"iopub.status.busy":"2022-04-21T07:14:24.397466Z","iopub.execute_input":"2022-04-21T07:14:24.39766Z","iopub.status.idle":"2022-04-21T07:14:35.192307Z","shell.execute_reply.started":"2022-04-21T07:14:24.397636Z","shell.execute_reply":"2022-04-21T07:14:35.191412Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## How sales and price changed","metadata":{}},{"cell_type":"code","source":"transactions.t_dat = pd.to_datetime(transactions['t_dat'])\ntransactions['week'] = (transactions['t_dat'].max() - transactions['t_dat']).dt.days // 7","metadata":{"execution":{"iopub.status.busy":"2022-04-21T07:14:35.193718Z","iopub.execute_input":"2022-04-21T07:14:35.194029Z","iopub.status.idle":"2022-04-21T07:14:44.271966Z","shell.execute_reply.started":"2022-04-21T07:14:35.193989Z","shell.execute_reply":"2022-04-21T07:14:44.271039Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transactions","metadata":{"execution":{"iopub.status.busy":"2022-04-21T07:14:44.273673Z","iopub.execute_input":"2022-04-21T07:14:44.273985Z","iopub.status.idle":"2022-04-21T07:14:44.291873Z","shell.execute_reply.started":"2022-04-21T07:14:44.273944Z","shell.execute_reply":"2022-04-21T07:14:44.290987Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"transactions['article_id'] = transactions.article_id.str[:-3]","metadata":{"execution":{"iopub.status.busy":"2022-04-21T07:14:44.293272Z","iopub.execute_input":"2022-04-21T07:14:44.295333Z","iopub.status.idle":"2022-04-21T07:15:03.151388Z","shell.execute_reply.started":"2022-04-21T07:14:44.295294Z","shell.execute_reply":"2022-04-21T07:15:03.150486Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = transactions.groupby(['week','article_id'],as_index = False).agg({'price': 'mean','customer_id': 'count'})","metadata":{"execution":{"iopub.status.busy":"2022-04-21T07:15:03.153028Z","iopub.execute_input":"2022-04-21T07:15:03.153262Z","iopub.status.idle":"2022-04-21T07:15:17.1822Z","shell.execute_reply.started":"2022-04-21T07:15:03.153227Z","shell.execute_reply":"2022-04-21T07:15:17.18125Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df","metadata":{"execution":{"iopub.status.busy":"2022-04-21T07:15:17.183455Z","iopub.execute_input":"2022-04-21T07:15:17.183682Z","iopub.status.idle":"2022-04-21T07:15:17.198834Z","shell.execute_reply.started":"2022-04-21T07:15:17.183655Z","shell.execute_reply":"2022-04-21T07:15:17.198052Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"articles.product_code.nunique()","metadata":{"execution":{"iopub.status.busy":"2022-04-21T07:15:17.200015Z","iopub.execute_input":"2022-04-21T07:15:17.200252Z","iopub.status.idle":"2022-04-21T07:15:17.214275Z","shell.execute_reply.started":"2022-04-21T07:15:17.200223Z","shell.execute_reply":"2022-04-21T07:15:17.213367Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"art1 = df[df['article_id'] == '0729931001']","metadata":{"execution":{"iopub.status.busy":"2022-04-21T07:15:17.215362Z","iopub.execute_input":"2022-04-21T07:15:17.215582Z","iopub.status.idle":"2022-04-21T07:15:17.4651Z","shell.execute_reply.started":"2022-04-21T07:15:17.215555Z","shell.execute_reply":"2022-04-21T07:15:17.464272Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"top10_week104 = df[df['week'] == 104].sort_values(by = 'customer_id', ascending = False).head(10)['article_id'].reset_index(drop = True).to_list()\ntop10_week104","metadata":{"execution":{"iopub.status.busy":"2022-04-21T07:15:17.466393Z","iopub.execute_input":"2022-04-21T07:15:17.466767Z","iopub.status.idle":"2022-04-21T07:15:17.48227Z","shell.execute_reply.started":"2022-04-21T07:15:17.466724Z","shell.execute_reply":"2022-04-21T07:15:17.48128Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"article_id = '0685687'\nproduct_code = int(article_id[1:])\narticles[articles['product_code'] == product_code]['detail_desc'].iloc[0]\n#print(articles[articles['product_code'] == product_code]['detail_desc'])","metadata":{"execution":{"iopub.status.busy":"2022-04-21T07:15:17.483564Z","iopub.execute_input":"2022-04-21T07:15:17.484118Z","iopub.status.idle":"2022-04-21T07:15:17.491558Z","shell.execute_reply.started":"2022-04-21T07:15:17.484088Z","shell.execute_reply":"2022-04-21T07:15:17.490964Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib\nimport matplotlib.pyplot as plt\nimport seaborn as sns\n\ndf = transactions.groupby(['week','article_id'],as_index = False).agg({'price': 'median','customer_id': 'count'})\ntop10_week104\n    \nfor article_id in top10_week104:\n\n    df_art = df[df['article_id'] == article_id]\n    product_code = int(article_id[1:])\n    desc = articles[articles['product_code'] == product_code]['detail_desc'].iloc[0]\n    plt.figure()\n    ax1 = sns.set_style(style=None, rc=None )\n\n    fig, ax1 = plt.subplots(figsize=(20,6))\n\n    sns.lineplot(data = df_art,x = 'week',y = 'price', ax=ax1)\n    ax2 = ax1.twinx()\n\n    sns.barplot(data = df_art, x='week', y='customer_id', alpha=0.5, ax=ax2).set_title(f'{desc}')\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-04-21T07:15:17.492844Z","iopub.execute_input":"2022-04-21T07:15:17.493352Z","iopub.status.idle":"2022-04-21T07:16:07.903151Z","shell.execute_reply.started":"2022-04-21T07:15:17.493316Z","shell.execute_reply":"2022-04-21T07:16:07.90245Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"articles_ids = ['0685687003','0685687001','0685687004','0685687002']\n\nfor ids in articles_ids:\n    print(articles[articles['article_id'] == ids]['detail_desc'])","metadata":{"execution":{"iopub.status.busy":"2022-04-21T07:16:07.904278Z","iopub.execute_input":"2022-04-21T07:16:07.904483Z","iopub.status.idle":"2022-04-21T07:16:07.986999Z","shell.execute_reply.started":"2022-04-21T07:16:07.904457Z","shell.execute_reply":"2022-04-21T07:16:07.986253Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"articles","metadata":{"execution":{"iopub.status.busy":"2022-04-21T07:16:07.987977Z","iopub.execute_input":"2022-04-21T07:16:07.988161Z","iopub.status.idle":"2022-04-21T07:16:08.117416Z","shell.execute_reply.started":"2022-04-21T07:16:07.988137Z","shell.execute_reply":"2022-04-21T07:16:08.116496Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}