{"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":"raw","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\nfor 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"}},{"cell_type":"markdown","source":"This is very simple customer behaviour plot on a specific Year 2020. We can try different thresholds to analyse the plots. ","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport time\nfrom datetime import datetime as dt\nfrom matplotlib import pyplot as plt\nimport seaborn as sns\n\nimport warnings\nwarnings.filterwarnings('ignore')","metadata":{"execution":{"iopub.status.busy":"2022-02-18T09:32:26.715654Z","iopub.execute_input":"2022-02-18T09:32:26.715958Z","iopub.status.idle":"2022-02-18T09:32:27.702752Z","shell.execute_reply.started":"2022-02-18T09:32:26.715923Z","shell.execute_reply":"2022-02-18T09:32:27.701936Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\n# LOAD TRANSACTIONS DATAFRAME\ntnx_df = pd.read_csv('../input/h-and-m-personalized-fashion-recommendations/transactions_train.csv')\ntnx_df['Year'] = tnx_df.t_dat.apply(lambda x: dt.strptime(x,'%Y-%m-%d')).dt.year\ntnx_df['WK'] = tnx_df.t_dat.apply(lambda x: dt.strptime(x,'%Y-%m-%d')).dt.week\ntnx_df = tnx_df.groupby(by=['customer_id','Year','WK','sales_channel_id']).price.agg(['count', 'sum']).reset_index()\n\ntnx_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-02-18T09:32:27.704655Z","iopub.execute_input":"2022-02-18T09:32:27.705269Z","iopub.status.idle":"2022-02-18T09:42:32.563326Z","shell.execute_reply.started":"2022-02-18T09:32:27.705234Z","shell.execute_reply":"2022-02-18T09:42:32.562391Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Year 2020 Customer","metadata":{}},{"cell_type":"code","source":"tnx_df = tnx_df[tnx_df.Year==2020]","metadata":{"execution":{"iopub.status.busy":"2022-02-18T09:42:32.564776Z","iopub.execute_input":"2022-02-18T09:42:32.565515Z","iopub.status.idle":"2022-02-18T09:42:32.895429Z","shell.execute_reply.started":"2022-02-18T09:42:32.565464Z","shell.execute_reply":"2022-02-18T09:42:32.894705Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## RFM Data Prep","metadata":{}},{"cell_type":"code","source":"rfm= tnx_df.groupby('customer_id').agg({'WK':'max','count': sum,'sum': sum})\nrfm.columns=['recency','frequency','monetary']\nrfm['frequency'] = rfm['frequency'].astype(int)\nrfm['recency'] = rfm['recency'].astype(int)\nrfm.head()","metadata":{"execution":{"iopub.status.busy":"2022-02-18T09:42:32.897066Z","iopub.execute_input":"2022-02-18T09:42:32.897664Z","iopub.status.idle":"2022-02-18T09:42:35.148808Z","shell.execute_reply.started":"2022-02-18T09:42:32.897610Z","shell.execute_reply":"2022-02-18T09:42:35.147953Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rfm['r_qtl'] = pd.qcut(rfm['recency'], 4, ['1','2','3','4'])#,'5','6','7','8','9','10'])\nrfm['f_qtl'] = pd.qcut(rfm['frequency'], 4, ['1','2','3','4'])\nrfm['m_qtl'] = pd.qcut(rfm['monetary'], 4, ['1','2','3','4'])\nrfm['RFM_Score'] = rfm.r_qtl.astype(str)+ rfm.f_qtl.astype(str) + rfm.m_qtl.astype(str)\nrfm =rfm.reset_index()\nrfm.head()","metadata":{"execution":{"iopub.status.busy":"2022-02-18T09:42:35.151705Z","iopub.execute_input":"2022-02-18T09:42:35.152553Z","iopub.status.idle":"2022-02-18T09:42:35.660738Z","shell.execute_reply.started":"2022-02-18T09:42:35.152500Z","shell.execute_reply":"2022-02-18T09:42:35.659957Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## User define Threshold","metadata":{}},{"cell_type":"code","source":"def cust_group(x):\n    if x=='444':\n        xx = 'Most Valued Customer'\n    elif x == '111':\n        xx = 'Lazy Customer'\n    elif x in ['441','442','443']:\n        xx = 'Most frequent customer'\n    else:\n        xx= 'Other Customer'\n    return xx;","metadata":{"execution":{"iopub.status.busy":"2022-02-18T09:42:35.662130Z","iopub.execute_input":"2022-02-18T09:42:35.662476Z","iopub.status.idle":"2022-02-18T09:42:35.666181Z","shell.execute_reply.started":"2022-02-18T09:42:35.662438Z","shell.execute_reply":"2022-02-18T09:42:35.665659Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rfm['Customer group'] =rfm['RFM_Score'].apply(lambda x: cust_group(x) )\n\nrfm_plot_df = rfm.groupby('Customer group').customer_id.count().reset_index()\nrfm_plot_df = rfm_plot_df[rfm_plot_df['Customer group']!='Other Customer']\nplt.figure(figsize = (10,7))\nax = sns.barplot(x=\"Customer group\", y=\"customer_id\", data=rfm_plot_df)","metadata":{"execution":{"iopub.status.busy":"2022-02-18T09:51:53.929158Z","iopub.execute_input":"2022-02-18T09:51:53.930121Z","iopub.status.idle":"2022-02-18T09:51:55.223748Z","shell.execute_reply.started":"2022-02-18T09:51:53.930065Z","shell.execute_reply":"2022-02-18T09:51:55.222920Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}