{"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":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\n#for dirname, _, filenames in os.walk('/kaggle/input'):\n  #  for filename in filenames:\n   #     print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-12-15T02:25:49.385130Z","iopub.execute_input":"2022-12-15T02:25:49.385669Z","iopub.status.idle":"2022-12-15T02:25:49.393457Z","shell.execute_reply.started":"2022-12-15T02:25:49.385625Z","shell.execute_reply":"2022-12-15T02:25:49.392149Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"### read the customers dataset\n\ncustomers_df = pd.read_csv('../input/h-and-m-personalized-fashion-recommendations/customers.csv')\nprint(customers_df.head())","metadata":{"execution":{"iopub.status.busy":"2022-12-15T02:25:49.396116Z","iopub.execute_input":"2022-12-15T02:25:49.396457Z","iopub.status.idle":"2022-12-15T02:25:55.705576Z","shell.execute_reply.started":"2022-12-15T02:25:49.396420Z","shell.execute_reply":"2022-12-15T02:25:55.704780Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## Check the null values for the customers dataset\n\nimport seaborn as sns\nimport matplotlib\nmatplotlib.rcParams['figure.figsize'] = (20,6)\nsns.heatmap(customers_df.isnull(),yticklabels = False, cbar = False , cmap = 'viridis')\nmatplotlib.pyplot.title(\"Missing null values\")","metadata":{"execution":{"iopub.status.busy":"2022-12-15T02:25:55.707189Z","iopub.execute_input":"2022-12-15T02:25:55.707537Z","iopub.status.idle":"2022-12-15T02:26:06.694220Z","shell.execute_reply.started":"2022-12-15T02:25:55.707505Z","shell.execute_reply":"2022-12-15T02:26:06.692812Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"customers_df.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-12-15T02:26:06.696046Z","iopub.execute_input":"2022-12-15T02:26:06.696455Z","iopub.status.idle":"2022-12-15T02:26:06.963858Z","shell.execute_reply.started":"2022-12-15T02:26:06.696409Z","shell.execute_reply":"2022-12-15T02:26:06.962432Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"customers_df = customers_df.drop(['FN'], axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-12-15T02:26:06.967737Z","iopub.execute_input":"2022-12-15T02:26:06.968207Z","iopub.status.idle":"2022-12-15T02:26:07.116557Z","shell.execute_reply.started":"2022-12-15T02:26:06.968141Z","shell.execute_reply":"2022-12-15T02:26:07.115409Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"customers_df['age'].mean()","metadata":{"execution":{"iopub.status.busy":"2022-12-15T02:26:07.118485Z","iopub.execute_input":"2022-12-15T02:26:07.118825Z","iopub.status.idle":"2022-12-15T02:26:07.130220Z","shell.execute_reply.started":"2022-12-15T02:26:07.118779Z","shell.execute_reply":"2022-12-15T02:26:07.129083Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## use mean value of the age to deal with null value\ncustomers_df['age']= customers_df['age'].fillna(customers_df['age'].mean())","metadata":{"execution":{"iopub.status.busy":"2022-12-15T02:26:07.131457Z","iopub.execute_input":"2022-12-15T02:26:07.132609Z","iopub.status.idle":"2022-12-15T02:26:07.146295Z","shell.execute_reply.started":"2022-12-15T02:26:07.132536Z","shell.execute_reply":"2022-12-15T02:26:07.144845Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"customers_df['club_member_status'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-12-15T02:26:07.147779Z","iopub.execute_input":"2022-12-15T02:26:07.148128Z","iopub.status.idle":"2022-12-15T02:26:07.226380Z","shell.execute_reply.started":"2022-12-15T02:26:07.148081Z","shell.execute_reply":"2022-12-15T02:26:07.225598Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"customers_df['club_member_status'] = customers_df['club_member_status'].replace(np.NaN, 'UNKOWN')","metadata":{"execution":{"iopub.status.busy":"2022-12-15T02:26:07.227862Z","iopub.execute_input":"2022-12-15T02:26:07.228738Z","iopub.status.idle":"2022-12-15T02:26:07.313632Z","shell.execute_reply.started":"2022-12-15T02:26:07.228687Z","shell.execute_reply":"2022-12-15T02:26:07.312810Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"customers_df['fashion_news_frequency'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-12-15T02:26:07.315052Z","iopub.execute_input":"2022-12-15T02:26:07.315810Z","iopub.status.idle":"2022-12-15T02:26:07.396722Z","shell.execute_reply.started":"2022-12-15T02:26:07.315762Z","shell.execute_reply":"2022-12-15T02:26:07.395878Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"customers_df['fashion_news_frequency'] = customers_df['fashion_news_frequency'].replace('None', 'UNKOWN')\ncustomers_df['fashion_news_frequency'] = customers_df['fashion_news_frequency'].replace('NONE', 'UNKOWN')\ncustomers_df['fashion_news_frequency'] = customers_df['fashion_news_frequency'].replace(np.NaN, 'UNKOWN')","metadata":{"execution":{"iopub.status.busy":"2022-12-15T02:26:07.399529Z","iopub.execute_input":"2022-12-15T02:26:07.399746Z","iopub.status.idle":"2022-12-15T02:26:07.599175Z","shell.execute_reply.started":"2022-12-15T02:26:07.399720Z","shell.execute_reply":"2022-12-15T02:26:07.598211Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"customers_df['Active'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-12-15T02:26:07.600626Z","iopub.execute_input":"2022-12-15T02:26:07.600965Z","iopub.status.idle":"2022-12-15T02:26:07.626003Z","shell.execute_reply.started":"2022-12-15T02:26:07.600889Z","shell.execute_reply":"2022-12-15T02:26:07.624435Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"customers_df['Active'] = customers_df['Active'].fillna(0)","metadata":{"execution":{"iopub.status.busy":"2022-12-15T02:26:07.627816Z","iopub.execute_input":"2022-12-15T02:26:07.628133Z","iopub.status.idle":"2022-12-15T02:26:07.648959Z","shell.execute_reply.started":"2022-12-15T02:26:07.628093Z","shell.execute_reply":"2022-12-15T02:26:07.647628Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## convert age and active column data type to int\nfrom numpy import int64\ncustomers_df['age'] = customers_df['age'].astype(int64)\ncustomers_df['Active'] = customers_df['Active'].astype(int64)","metadata":{"execution":{"iopub.status.busy":"2022-12-15T02:26:07.650835Z","iopub.execute_input":"2022-12-15T02:26:07.651232Z","iopub.status.idle":"2022-12-15T02:26:07.674664Z","shell.execute_reply.started":"2022-12-15T02:26:07.651189Z","shell.execute_reply":"2022-12-15T02:26:07.673154Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## Bin and classify the age column into three group \n\ncustomers_df['age_by_decade'] = pd.cut(x=customers_df['age'], bins=[ 10, 49, 69, 100], labels=['Young age', 'Medium age', 'Older age'])","metadata":{"execution":{"iopub.status.busy":"2022-12-15T02:26:07.676163Z","iopub.execute_input":"2022-12-15T02:26:07.676427Z","iopub.status.idle":"2022-12-15T02:26:07.710489Z","shell.execute_reply.started":"2022-12-15T02:26:07.676391Z","shell.execute_reply":"2022-12-15T02:26:07.708615Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(customers_df.head())","metadata":{"execution":{"iopub.status.busy":"2022-12-15T02:26:07.712441Z","iopub.execute_input":"2022-12-15T02:26:07.712711Z","iopub.status.idle":"2022-12-15T02:26:07.725379Z","shell.execute_reply.started":"2022-12-15T02:26:07.712676Z","shell.execute_reply":"2022-12-15T02:26:07.723644Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"customers_df.info()","metadata":{"execution":{"iopub.status.busy":"2022-12-15T02:26:07.726733Z","iopub.execute_input":"2022-12-15T02:26:07.727002Z","iopub.status.idle":"2022-12-15T02:26:08.009494Z","shell.execute_reply.started":"2022-12-15T02:26:07.726973Z","shell.execute_reply":"2022-12-15T02:26:08.007798Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"customers_df.describe()","metadata":{"execution":{"iopub.status.busy":"2022-12-15T02:26:08.012574Z","iopub.execute_input":"2022-12-15T02:26:08.013093Z","iopub.status.idle":"2022-12-15T02:26:08.100819Z","shell.execute_reply.started":"2022-12-15T02:26:08.013035Z","shell.execute_reply":"2022-12-15T02:26:08.099249Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"customers_df.boxplot()\n","metadata":{"execution":{"iopub.status.busy":"2022-12-15T02:26:08.102234Z","iopub.execute_input":"2022-12-15T02:26:08.102497Z","iopub.status.idle":"2022-12-15T02:26:10.125187Z","shell.execute_reply.started":"2022-12-15T02:26:08.102463Z","shell.execute_reply":"2022-12-15T02:26:10.124410Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## find the number of each customer age groups\nsns.countplot(data=customers_df, x=\"age_by_decade\", hue=\"Active\")","metadata":{"execution":{"iopub.status.busy":"2022-12-15T02:26:10.126592Z","iopub.execute_input":"2022-12-15T02:26:10.126999Z","iopub.status.idle":"2022-12-15T02:26:10.456847Z","shell.execute_reply.started":"2022-12-15T02:26:10.126969Z","shell.execute_reply":"2022-12-15T02:26:10.455357Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## number of the customer in different age group in all club status\n\nimport matplotlib.pyplot as plt\n\nsns.countplot(data=customers_df, x= \"club_member_status\", hue=\"age_by_decade\")","metadata":{"execution":{"iopub.status.busy":"2022-12-15T02:26:10.459527Z","iopub.execute_input":"2022-12-15T02:26:10.459875Z","iopub.status.idle":"2022-12-15T02:26:11.542507Z","shell.execute_reply.started":"2022-12-15T02:26:10.459831Z","shell.execute_reply":"2022-12-15T02:26:11.541106Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## Try to plot the customer in different age group in left club status\n\nc = customers_df[customers_df.club_member_status.isin(['LEFT CLUB'])]\nprint(c.head())","metadata":{"execution":{"iopub.status.busy":"2022-12-15T02:26:11.544023Z","iopub.execute_input":"2022-12-15T02:26:11.544738Z","iopub.status.idle":"2022-12-15T02:26:11.593182Z","shell.execute_reply.started":"2022-12-15T02:26:11.544698Z","shell.execute_reply":"2022-12-15T02:26:11.591286Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.countplot(data=c, x= \"club_member_status\", hue=\"age_by_decade\")","metadata":{"execution":{"iopub.status.busy":"2022-12-15T02:26:11.594829Z","iopub.execute_input":"2022-12-15T02:26:11.595105Z","iopub.status.idle":"2022-12-15T02:26:11.807050Z","shell.execute_reply.started":"2022-12-15T02:26:11.595069Z","shell.execute_reply":"2022-12-15T02:26:11.806135Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x= customers_df[customers_df['fashion_news_frequency']=='Monthly']\nprint(x.head())","metadata":{"execution":{"iopub.status.busy":"2022-12-15T02:26:11.809530Z","iopub.execute_input":"2022-12-15T02:26:11.809863Z","iopub.status.idle":"2022-12-15T02:26:11.904543Z","shell.execute_reply.started":"2022-12-15T02:26:11.809824Z","shell.execute_reply":"2022-12-15T02:26:11.902955Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## number of the customer in different age group in all fashion news frequency\n\nsns.countplot(data=customers_df, x= \"fashion_news_frequency\", hue=\"age_by_decade\")","metadata":{"execution":{"iopub.status.busy":"2022-12-15T02:26:11.906403Z","iopub.execute_input":"2022-12-15T02:26:11.906900Z","iopub.status.idle":"2022-12-15T02:26:12.997160Z","shell.execute_reply.started":"2022-12-15T02:26:11.906855Z","shell.execute_reply":"2022-12-15T02:26:12.996136Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## Find more about monthly news fashion\nsns.countplot(data=x, x=\"fashion_news_frequency\", hue=\"age_by_decade\")","metadata":{"execution":{"iopub.status.busy":"2022-12-15T02:26:12.998699Z","iopub.execute_input":"2022-12-15T02:26:12.998971Z","iopub.status.idle":"2022-12-15T02:26:13.190361Z","shell.execute_reply.started":"2022-12-15T02:26:12.998933Z","shell.execute_reply":"2022-12-15T02:26:13.189759Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"articles_df = pd.read_csv('../input/h-and-m-personalized-fashion-recommendations/articles.csv')\nprint(articles_df.head())","metadata":{"execution":{"iopub.status.busy":"2022-12-15T02:26:13.191597Z","iopub.execute_input":"2022-12-15T02:26:13.192045Z","iopub.status.idle":"2022-12-15T02:26:14.163010Z","shell.execute_reply.started":"2022-12-15T02:26:13.192011Z","shell.execute_reply":"2022-12-15T02:26:14.161766Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"articles_df.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-12-15T02:26:14.164752Z","iopub.execute_input":"2022-12-15T02:26:14.164994Z","iopub.status.idle":"2022-12-15T02:26:14.238267Z","shell.execute_reply.started":"2022-12-15T02:26:14.164965Z","shell.execute_reply":"2022-12-15T02:26:14.236144Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"articles_df.info()","metadata":{"execution":{"iopub.status.busy":"2022-12-15T02:26:14.242537Z","iopub.execute_input":"2022-12-15T02:26:14.242791Z","iopub.status.idle":"2022-12-15T02:26:14.318285Z","shell.execute_reply.started":"2022-12-15T02:26:14.242763Z","shell.execute_reply":"2022-12-15T02:26:14.316962Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transaction_df = pd.read_csv('../input/h-and-m-personalized-fashion-recommendations/transactions_train.csv')\nprint(transaction_df.head())","metadata":{"execution":{"iopub.status.busy":"2022-12-15T02:26:14.319928Z","iopub.execute_input":"2022-12-15T02:26:14.320182Z","iopub.status.idle":"2022-12-15T02:27:21.545204Z","shell.execute_reply.started":"2022-12-15T02:26:14.320154Z","shell.execute_reply":"2022-12-15T02:27:21.544483Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transaction_df.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-12-15T02:27:21.546402Z","iopub.execute_input":"2022-12-15T02:27:21.546820Z","iopub.status.idle":"2022-12-15T02:27:23.962535Z","shell.execute_reply.started":"2022-12-15T02:27:21.546785Z","shell.execute_reply":"2022-12-15T02:27:23.961019Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transaction_df.describe()","metadata":{"execution":{"iopub.status.busy":"2022-12-15T02:27:23.963990Z","iopub.execute_input":"2022-12-15T02:27:23.964258Z","iopub.status.idle":"2022-12-15T02:27:25.972155Z","shell.execute_reply.started":"2022-12-15T02:27:23.964222Z","shell.execute_reply":"2022-12-15T02:27:25.970756Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transaction_df.info()","metadata":{"execution":{"iopub.status.busy":"2022-12-15T02:27:25.973719Z","iopub.execute_input":"2022-12-15T02:27:25.974052Z","iopub.status.idle":"2022-12-15T02:27:25.990957Z","shell.execute_reply.started":"2022-12-15T02:27:25.974007Z","shell.execute_reply":"2022-12-15T02:27:25.988761Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.boxplot(transaction_df['price'])","metadata":{"execution":{"iopub.status.busy":"2022-12-15T02:27:25.992755Z","iopub.execute_input":"2022-12-15T02:27:25.993088Z","iopub.status.idle":"2022-12-15T02:27:29.289824Z","shell.execute_reply.started":"2022-12-15T02:27:25.993007Z","shell.execute_reply":"2022-12-15T02:27:29.288837Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transaction_df['t_dat'] = pd.to_datetime(transaction_df['t_dat'])","metadata":{"execution":{"iopub.status.busy":"2022-12-15T02:27:29.291222Z","iopub.execute_input":"2022-12-15T02:27:29.291499Z","iopub.status.idle":"2022-12-15T02:27:33.399638Z","shell.execute_reply.started":"2022-12-15T02:27:29.291464Z","shell.execute_reply":"2022-12-15T02:27:33.398232Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transaction_df['t_dat'].max()","metadata":{"execution":{"iopub.status.busy":"2022-12-15T02:27:33.401290Z","iopub.execute_input":"2022-12-15T02:27:33.401575Z","iopub.status.idle":"2022-12-15T02:27:33.477079Z","shell.execute_reply.started":"2022-12-15T02:27:33.401532Z","shell.execute_reply":"2022-12-15T02:27:33.475312Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transaction_df['t_dat'].min()","metadata":{"execution":{"iopub.status.busy":"2022-12-15T02:27:33.478758Z","iopub.execute_input":"2022-12-15T02:27:33.478994Z","iopub.status.idle":"2022-12-15T02:27:33.553716Z","shell.execute_reply.started":"2022-12-15T02:27:33.478968Z","shell.execute_reply":"2022-12-15T02:27:33.552629Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"### find number of transaction each day  \n\ntran_freq = pd.DataFrame(transaction_df.groupby(['t_dat']).size(), columns= ['Frequency'])\n\nprint(tran_freq)\n\n## Plot transactions over time day by day from 2018-09-20 to 2020-09-22\n\n\nfrom matplotlib import pyplot\ntran_freq.plot()\npyplot.show()","metadata":{"execution":{"iopub.status.busy":"2022-12-15T02:27:33.555099Z","iopub.execute_input":"2022-12-15T02:27:33.555363Z","iopub.status.idle":"2022-12-15T02:27:34.268026Z","shell.execute_reply.started":"2022-12-15T02:27:33.555331Z","shell.execute_reply":"2022-12-15T02:27:34.267175Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tran_freq = tran_freq.reset_index(level=['t_dat'])\ntran_freq.head()","metadata":{"execution":{"iopub.status.busy":"2022-12-15T02:27:34.269499Z","iopub.execute_input":"2022-12-15T02:27:34.270725Z","iopub.status.idle":"2022-12-15T02:27:34.281472Z","shell.execute_reply.started":"2022-12-15T02:27:34.270628Z","shell.execute_reply":"2022-12-15T02:27:34.280781Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"### find the year and months columns from date column\n\ntran_freq['Year'] = pd.DatetimeIndex(tran_freq['t_dat']).year\ntran_freq['Month'] = pd.DatetimeIndex(tran_freq['t_dat']).month\ntran_freq['Month_Year'] = pd.to_datetime(tran_freq['t_dat']).dt.to_period('M')\n \ntran_freq.head()","metadata":{"execution":{"iopub.status.busy":"2022-12-15T02:27:34.282743Z","iopub.execute_input":"2022-12-15T02:27:34.283061Z","iopub.status.idle":"2022-12-15T02:27:34.301441Z","shell.execute_reply.started":"2022-12-15T02:27:34.283033Z","shell.execute_reply":"2022-12-15T02:27:34.300475Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"### find the total sales for each month in 2018 and 2019 \n\npv = pd.pivot_table(tran_freq, index='Month', columns= 'Year', values= 'Frequency', aggfunc='sum')\n\nprint(pv)\n\npv.plot(kind='bar', figsize=(17, 10), color=['red', 'black', 'green'], rot=0)                                       \nplt.title(\"Historical Count TOT_SALES: Month by Month Comparison\", y=1.013, fontsize=22)\nplt.xlabel(\"Date [Month]\", labelpad=16)\nplt.ylabel(\"Count [TOT_SALES]\", labelpad=16);\n\n#### Generally there is more sale in the summer than in the winter","metadata":{"execution":{"iopub.status.busy":"2022-12-15T02:27:34.302811Z","iopub.execute_input":"2022-12-15T02:27:34.303159Z","iopub.status.idle":"2022-12-15T02:27:34.661992Z","shell.execute_reply.started":"2022-12-15T02:27:34.303127Z","shell.execute_reply":"2022-12-15T02:27:34.660750Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"### distribution of the articles price\n\nimport seaborn as sns\n\nsns.distplot(transaction_df['price'], kde=False, color='red', bins=100)\nplt.title('distribution of the price', fontsize=18)\nplt.xlabel('price', fontsize=16)\nplt.ylabel('Frequency', fontsize=16)","metadata":{"execution":{"iopub.status.busy":"2022-12-15T02:27:34.663217Z","iopub.execute_input":"2022-12-15T02:27:34.663438Z","iopub.status.idle":"2022-12-15T02:27:35.701196Z","shell.execute_reply.started":"2022-12-15T02:27:34.663408Z","shell.execute_reply":"2022-12-15T02:27:35.699958Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## merge transaction and customer dataframes\n\nresult_df = pd.merge(transaction_df, customers_df, on=\"customer_id\")\nprint(result_df.head())","metadata":{"execution":{"iopub.status.busy":"2022-12-15T02:27:35.702926Z","iopub.execute_input":"2022-12-15T02:27:35.703299Z","iopub.status.idle":"2022-12-15T02:28:03.206282Z","shell.execute_reply.started":"2022-12-15T02:27:35.703249Z","shell.execute_reply":"2022-12-15T02:28:03.204609Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## merge transaction, customer dataframes with articles dataframe\n\nnew_df = pd.merge(result_df, articles_df, on=\"article_id\")\nprint(new_df.head())","metadata":{"execution":{"iopub.status.busy":"2022-12-15T02:28:03.208932Z","iopub.execute_input":"2022-12-15T02:28:03.209383Z","iopub.status.idle":"2022-12-15T02:30:02.269801Z","shell.execute_reply.started":"2022-12-15T02:28:03.209330Z","shell.execute_reply":"2022-12-15T02:30:02.269065Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"-- find the most products which were bought\n","metadata":{}},{"cell_type":"code","source":"\nimport matplotlib.pyplot as plt\n\ntop = new_df['prod_name'].value_counts().head(n=20)\ntop.plot(kind='bar')\nplt.title('Popular products')\nfig = plt.gcf()\nfig.set_size_inches(10, 5)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-12-15T02:30:02.271033Z","iopub.execute_input":"2022-12-15T02:30:02.272067Z","iopub.status.idle":"2022-12-15T02:30:03.701939Z","shell.execute_reply.started":"2022-12-15T02:30:02.272025Z","shell.execute_reply":"2022-12-15T02:30:03.700569Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":" -- find the most customer age group who bought products","metadata":{}},{"cell_type":"code","source":"\n\nimport matplotlib.pyplot as plt\n\ntop = new_df['age'].value_counts().head(n=20)\ntop.plot(kind='bar')\nplt.title('Popular age group')\nfig = plt.gcf()\nfig.set_size_inches(10, 5)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-12-15T02:30:03.705507Z","iopub.execute_input":"2022-12-15T02:30:03.708002Z","iopub.status.idle":"2022-12-15T02:30:04.199437Z","shell.execute_reply.started":"2022-12-15T02:30:03.707901Z","shell.execute_reply":"2022-12-15T02:30:04.198763Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"--find the most popular garment groups name were bought ","metadata":{}},{"cell_type":"code","source":"\n\nimport matplotlib.pyplot as plt\n\ntop = new_df['garment_group_name'].value_counts().head(n=20)\ntop.plot(kind='bar')\nplt.title('Popular garments group')\nfig = plt.gcf()\nfig.set_size_inches(10, 5)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-12-15T02:30:04.200592Z","iopub.execute_input":"2022-12-15T02:30:04.201632Z","iopub.status.idle":"2022-12-15T02:30:05.788438Z","shell.execute_reply.started":"2022-12-15T02:30:04.201591Z","shell.execute_reply":"2022-12-15T02:30:05.787714Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":" --find the most popular clothing sections name were bought ","metadata":{}},{"cell_type":"code","source":"\n\nimport matplotlib.pyplot as plt\n\ntop = new_df['index_group_name'].value_counts().head(n=20)\ntop.plot(kind='bar')\nplt.title('Popular clothing group')\nfig = plt.gcf()\nfig.set_size_inches(10, 5)\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2022-12-15T02:30:05.789780Z","iopub.execute_input":"2022-12-15T02:30:05.790738Z","iopub.status.idle":"2022-12-15T02:30:07.315406Z","shell.execute_reply.started":"2022-12-15T02:30:05.790694Z","shell.execute_reply":"2022-12-15T02:30:07.314612Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"-- How many times each piece of clothing was bought by each customer?","metadata":{}},{"cell_type":"code","source":"tran_freq_per_customer = pd.DataFrame(new_df.groupby(['customer_id','article_id']).size(), columns= ['Frequency'])\n\nprint(tran_freq_per_customer)","metadata":{"execution":{"iopub.status.busy":"2022-12-15T02:30:07.316548Z","iopub.execute_input":"2022-12-15T02:30:07.316785Z","iopub.status.idle":"2022-12-15T02:30:59.878237Z","shell.execute_reply.started":"2022-12-15T02:30:07.316756Z","shell.execute_reply":"2022-12-15T02:30:59.876094Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tran_freq_per_customer = tran_freq_per_customer.reset_index(level=['customer_id','article_id']).sort_values(by=['Frequency', 'customer_id'], ascending=False)\ntran_freq_per_customer.head(20)","metadata":{"execution":{"iopub.status.busy":"2022-12-15T02:30:59.879932Z","iopub.execute_input":"2022-12-15T02:30:59.880232Z","iopub.status.idle":"2022-12-15T02:31:12.531048Z","shell.execute_reply.started":"2022-12-15T02:30:59.880194Z","shell.execute_reply":"2022-12-15T02:31:12.529632Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":" -- How many customers are in each garments group for each age group ?","metadata":{}},{"cell_type":"code","source":"\n\ndf1_customer =pd.pivot_table(new_df, index=['garment_group_name','age_by_decade'],values=['customer_id'], aggfunc=lambda x: len(x.unique())).sort_values(by= ['garment_group_name','age_by_decade'], ascending=False).reset_index()\n#print(df1_customer)\n\ntotal_customer_povit =df1_customer.pivot(index='garment_group_name', columns='age_by_decade', values='customer_id')\nprint(total_customer_povit)\n\ntotal_customer_povit.plot(kind='bar').legend( bbox_to_anchor=(1.05, 1), loc='upper left', fontsize='medium')","metadata":{"execution":{"iopub.status.busy":"2022-12-15T02:31:12.532573Z","iopub.execute_input":"2022-12-15T02:31:12.532884Z","iopub.status.idle":"2022-12-15T02:32:14.713052Z","shell.execute_reply.started":"2022-12-15T02:31:12.532840Z","shell.execute_reply":"2022-12-15T02:32:14.706623Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"-- How many articles are bought per customer age group by different garments group?","metadata":{}},{"cell_type":"code","source":"\n\ndf1_units_per_customer = pd.pivot_table(new_df, index=['garment_group_name','age_by_decade'],values=['article_id'], aggfunc=lambda x: len(x.unique())).sort_values(by= ['garment_group_name','age_by_decade'], ascending=False).reset_index()\n\n\ntotal_units_povit =df1_units_per_customer.pivot(index='garment_group_name', columns='age_by_decade', values='article_id')\nprint(total_units_povit)\n\ntotal_units_povit.plot(kind='bar').legend( bbox_to_anchor=(1.05, 1), loc='upper left', fontsize='medium')","metadata":{"execution":{"iopub.status.busy":"2022-12-15T02:32:14.714661Z","iopub.execute_input":"2022-12-15T02:32:14.714897Z","iopub.status.idle":"2022-12-15T02:32:22.661510Z","shell.execute_reply.started":"2022-12-15T02:32:14.714867Z","shell.execute_reply":"2022-12-15T02:32:22.660172Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}