{"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","scrolled":true,"execution":{"iopub.status.busy":"2022-02-13T16:58:48.144925Z","iopub.execute_input":"2022-02-13T16:58:48.145453Z","iopub.status.idle":"2022-02-13T16:58:48.174861Z","shell.execute_reply.started":"2022-02-13T16:58:48.145314Z","shell.execute_reply":"2022-02-13T16:58:48.174086Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport seaborn as sns","metadata":{"execution":{"iopub.status.busy":"2022-02-13T16:58:48.176512Z","iopub.execute_input":"2022-02-13T16:58:48.176893Z","iopub.status.idle":"2022-02-13T16:58:49.316780Z","shell.execute_reply.started":"2022-02-13T16:58:48.176861Z","shell.execute_reply":"2022-02-13T16:58:49.315572Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"DATA_DIR = '/kaggle/input/h-and-m-personalized-fashion-recommendations'\nos.listdir(DATA_DIR)","metadata":{"execution":{"iopub.status.busy":"2022-02-13T16:58:49.318201Z","iopub.execute_input":"2022-02-13T16:58:49.318521Z","iopub.status.idle":"2022-02-13T16:58:49.329969Z","shell.execute_reply.started":"2022-02-13T16:58:49.318487Z","shell.execute_reply":"2022-02-13T16:58:49.328953Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# EDA","metadata":{"execution":{"iopub.status.busy":"2022-02-13T14:59:35.342383Z","iopub.execute_input":"2022-02-13T14:59:35.342705Z","iopub.status.idle":"2022-02-13T14:59:35.348138Z","shell.execute_reply.started":"2022-02-13T14:59:35.342663Z","shell.execute_reply":"2022-02-13T14:59:35.346855Z"}}},{"cell_type":"code","source":"articles = pd.read_csv(os.path.join(DATA_DIR, 'articles.csv'))\nprint(articles.shape)\narticles.head()","metadata":{"execution":{"iopub.status.busy":"2022-02-13T16:58:49.331672Z","iopub.execute_input":"2022-02-13T16:58:49.332485Z","iopub.status.idle":"2022-02-13T16:58:50.604569Z","shell.execute_reply.started":"2022-02-13T16:58:49.332427Z","shell.execute_reply":"2022-02-13T16:58:50.603323Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# All article_ids are unique\narticles['article_id'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-02-13T16:58:50.607398Z","iopub.execute_input":"2022-02-13T16:58:50.607639Z","iopub.status.idle":"2022-02-13T16:58:50.639106Z","shell.execute_reply.started":"2022-02-13T16:58:50.607612Z","shell.execute_reply":"2022-02-13T16:58:50.637597Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Train transactions","metadata":{"execution":{"iopub.status.busy":"2022-02-13T14:59:52.775480Z","iopub.execute_input":"2022-02-13T14:59:52.775859Z","iopub.status.idle":"2022-02-13T14:59:52.782155Z","shell.execute_reply.started":"2022-02-13T14:59:52.775820Z","shell.execute_reply":"2022-02-13T14:59:52.780377Z"}}},{"cell_type":"code","source":"trx_train = pd.read_csv(os.path.join(DATA_DIR, 'transactions_train.csv'))\nprint(trx_train.shape)\ntrx_train.head()","metadata":{"execution":{"iopub.status.busy":"2022-02-13T16:58:50.640173Z","iopub.execute_input":"2022-02-13T16:58:50.640426Z","iopub.status.idle":"2022-02-13T17:00:05.717131Z","shell.execute_reply.started":"2022-02-13T16:58:50.640397Z","shell.execute_reply":"2022-02-13T17:00:05.715995Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trx_train['t_dat'] = pd.to_datetime(trx_train['t_dat']).dt.date","metadata":{"execution":{"iopub.status.busy":"2022-02-13T17:00:05.718557Z","iopub.execute_input":"2022-02-13T17:00:05.718812Z","iopub.status.idle":"2022-02-13T17:00:28.553171Z","shell.execute_reply.started":"2022-02-13T17:00:05.718781Z","shell.execute_reply":"2022-02-13T17:00:28.552081Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.to_datetime(trx_train['t_dat']).describe()","metadata":{"execution":{"iopub.status.busy":"2022-02-13T17:00:28.554613Z","iopub.execute_input":"2022-02-13T17:00:28.554858Z","iopub.status.idle":"2022-02-13T17:00:38.431577Z","shell.execute_reply.started":"2022-02-13T17:00:28.554829Z","shell.execute_reply":"2022-02-13T17:00:38.430689Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trx_train['t_dat'].value_counts().sort_index().plot(kind='line', figsize=(20, 5))\nplt.axvline(x=pd.to_datetime('2019-09-23'), color='red')\nplt.axvline(x=pd.to_datetime('2019-09-30'), color='red')\nplt.axvline(x=pd.to_datetime('2018-09-23'), color='red')\nplt.axvline(x=pd.to_datetime('2018-09-30'), color='red')","metadata":{"execution":{"iopub.status.busy":"2022-02-13T17:00:38.433025Z","iopub.execute_input":"2022-02-13T17:00:38.433276Z","iopub.status.idle":"2022-02-13T17:00:43.999563Z","shell.execute_reply.started":"2022-02-13T17:00:38.433246Z","shell.execute_reply":"2022-02-13T17:00:43.998191Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trx_train['customer_id'].value_counts().describe()","metadata":{"execution":{"iopub.status.busy":"2022-02-13T17:00:44.001832Z","iopub.execute_input":"2022-02-13T17:00:44.002214Z","iopub.status.idle":"2022-02-13T17:00:55.620247Z","shell.execute_reply.started":"2022-02-13T17:00:44.002164Z","shell.execute_reply":"2022-02-13T17:00:55.619389Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trx_train = trx_train.merge(articles.loc[:, ['article_id', 'product_type_no']], on='article_id', how='left', copy=False)","metadata":{"execution":{"iopub.status.busy":"2022-02-13T17:00:55.621681Z","iopub.execute_input":"2022-02-13T17:00:55.621995Z","iopub.status.idle":"2022-02-13T17:01:03.684058Z","shell.execute_reply.started":"2022-02-13T17:00:55.621962Z","shell.execute_reply":"2022-02-13T17:01:03.682732Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(trx_train) - trx_train.count()","metadata":{"execution":{"iopub.status.busy":"2022-02-13T17:01:03.685797Z","iopub.execute_input":"2022-02-13T17:01:03.686031Z","iopub.status.idle":"2022-02-13T17:01:10.780603Z","shell.execute_reply.started":"2022-02-13T17:01:03.686004Z","shell.execute_reply":"2022-02-13T17:01:10.779719Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"(trx_train['article_id'].value_counts()/trx_train.shape[0])[:12]","metadata":{"execution":{"iopub.status.busy":"2022-02-13T17:01:10.782340Z","iopub.execute_input":"2022-02-13T17:01:10.782611Z","iopub.status.idle":"2022-02-13T17:01:12.370051Z","shell.execute_reply.started":"2022-02-13T17:01:10.782581Z","shell.execute_reply":"2022-02-13T17:01:12.369065Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trx_train['relevant_period'] = 0\ntrx_train['relevant_period_2018'] = 0\ntrx_train['relevant_period_2019'] = 0\n\n\ntrx_train.loc[((trx_train['t_dat']>=pd.to_datetime('2019-09-23')) & (trx_train['t_dat']<=pd.to_datetime('2019-09-29'))), 'relevant_period_2019'] = 1\ntrx_train.loc[((trx_train['t_dat']>=pd.to_datetime('2018-09-23')) & (trx_train['t_dat']<=pd.to_datetime('2018-09-29'))), 'relevant_period_2018'] = 1\n\ntrx_train['relevant_period'] = trx_train['relevant_period_2018'] + trx_train['relevant_period_2019']\ntrx_train['relevant_period'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-02-13T17:01:12.375134Z","iopub.execute_input":"2022-02-13T17:01:12.375391Z","iopub.status.idle":"2022-02-13T17:04:03.239633Z","shell.execute_reply.started":"2022-02-13T17:01:12.375362Z","shell.execute_reply":"2022-02-13T17:04:03.238262Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trx_train['prev_week_2019'] = 0\ntrx_train.loc[((trx_train['t_dat']>=pd.to_datetime('2019-09-15')) & (trx_train['t_dat']<=pd.to_datetime('2019-09-22'))), 'prev_week_2019'] = 1","metadata":{"execution":{"iopub.status.busy":"2022-02-13T17:04:03.241229Z","iopub.execute_input":"2022-02-13T17:04:03.241553Z","iopub.status.idle":"2022-02-13T17:05:28.923471Z","shell.execute_reply.started":"2022-02-13T17:04:03.241511Z","shell.execute_reply":"2022-02-13T17:05:28.921895Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trx_train['prev_month_2019'] = 0\ntrx_train.loc[((trx_train['t_dat']>=pd.to_datetime('2019-08-22')) & (trx_train['t_dat']<=pd.to_datetime('2019-09-22'))), 'prev_month_2019'] = 1","metadata":{"execution":{"iopub.status.busy":"2022-02-13T17:05:28.925512Z","iopub.execute_input":"2022-02-13T17:05:28.925847Z","iopub.status.idle":"2022-02-13T17:06:54.081311Z","shell.execute_reply.started":"2022-02-13T17:05:28.925812Z","shell.execute_reply":"2022-02-13T17:06:54.079726Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trx_train['prev_day_2019'] = 0\ntrx_train.loc[((trx_train['t_dat']==pd.to_datetime('2019-09-22'))), 'prev_day_2019'] = 1","metadata":{"execution":{"iopub.status.busy":"2022-02-13T17:06:54.083655Z","iopub.execute_input":"2022-02-13T17:06:54.084020Z","iopub.status.idle":"2022-02-13T17:07:37.387546Z","shell.execute_reply.started":"2022-02-13T17:06:54.083966Z","shell.execute_reply":"2022-02-13T17:07:37.386519Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trx_train['compound_factor'] = trx_train['prev_week_2019'] + trx_train['prev_month_2019'] + trx_train['prev_day_2019']","metadata":{"execution":{"iopub.status.busy":"2022-02-13T17:07:37.389418Z","iopub.execute_input":"2022-02-13T17:07:37.389772Z","iopub.status.idle":"2022-02-13T17:07:37.695250Z","shell.execute_reply.started":"2022-02-13T17:07:37.389727Z","shell.execute_reply":"2022-02-13T17:07:37.694169Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"vc18 = (trx_train.loc[trx_train['relevant_period_2018']==1, 'article_id'].value_counts()/trx_train['relevant_period_2018'].sum())[:12]\nprint(vc18.sum())\nprint(vc18)","metadata":{"execution":{"iopub.status.busy":"2022-02-13T17:07:37.696871Z","iopub.execute_input":"2022-02-13T17:07:37.697239Z","iopub.status.idle":"2022-02-13T17:07:37.817785Z","shell.execute_reply.started":"2022-02-13T17:07:37.697195Z","shell.execute_reply":"2022-02-13T17:07:37.816465Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"vc19 = (trx_train.loc[trx_train['relevant_period_2019']==1, 'article_id'].value_counts()/trx_train['relevant_period_2019'].sum())[:12]\nprint(vc19.sum())\nprint(vc19)","metadata":{"execution":{"iopub.status.busy":"2022-02-13T17:07:37.819971Z","iopub.execute_input":"2022-02-13T17:07:37.820488Z","iopub.status.idle":"2022-02-13T17:07:37.913821Z","shell.execute_reply.started":"2022-02-13T17:07:37.820434Z","shell.execute_reply":"2022-02-13T17:07:37.912863Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"vc_prevwk19 = (trx_train.loc[trx_train['prev_week_2019']==1, 'article_id'].value_counts()/trx_train['prev_week_2019'].sum())[:12]\nprint(vc_prevwk19.sum())\nprint(vc_prevwk19)","metadata":{"execution":{"iopub.status.busy":"2022-02-13T17:07:37.915136Z","iopub.execute_input":"2022-02-13T17:07:37.915415Z","iopub.status.idle":"2022-02-13T17:07:38.003121Z","shell.execute_reply.started":"2022-02-13T17:07:37.915382Z","shell.execute_reply":"2022-02-13T17:07:38.002237Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"vc_prevmnth19 = (trx_train.loc[trx_train['prev_month_2019']==1, 'article_id'].value_counts()/trx_train['prev_month_2019'].sum())[:12]\nprint(vc_prevmnth19.sum())\nprint(vc_prevmnth19)","metadata":{"execution":{"iopub.status.busy":"2022-02-13T17:07:38.004624Z","iopub.execute_input":"2022-02-13T17:07:38.004933Z","iopub.status.idle":"2022-02-13T17:07:38.130114Z","shell.execute_reply.started":"2022-02-13T17:07:38.004889Z","shell.execute_reply":"2022-02-13T17:07:38.129218Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"vc_prevday19 = (trx_train.loc[trx_train['prev_day_2019']==1, 'article_id'].value_counts()/trx_train['prev_day_2019'].sum())[:12]\nprint(vc_prevday19.sum())\nprint(vc_prevday19)","metadata":{"execution":{"iopub.status.busy":"2022-02-13T17:07:38.131462Z","iopub.execute_input":"2022-02-13T17:07:38.131703Z","iopub.status.idle":"2022-02-13T17:07:38.209988Z","shell.execute_reply.started":"2022-02-13T17:07:38.131673Z","shell.execute_reply":"2022-02-13T17:07:38.209086Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"incount = vc19.index.isin(vc_prevwk19.index)\nprint(len(incount[incount]))\nprint(incount)","metadata":{"execution":{"iopub.status.busy":"2022-02-13T17:07:38.211320Z","iopub.execute_input":"2022-02-13T17:07:38.211803Z","iopub.status.idle":"2022-02-13T17:07:38.221003Z","shell.execute_reply.started":"2022-02-13T17:07:38.211764Z","shell.execute_reply":"2022-02-13T17:07:38.219656Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"incount = vc19.index.isin(vc_prevmnth19.index)\nprint(len(incount[incount]))\nprint(incount)","metadata":{"execution":{"iopub.status.busy":"2022-02-13T17:07:38.222330Z","iopub.execute_input":"2022-02-13T17:07:38.222567Z","iopub.status.idle":"2022-02-13T17:07:38.239311Z","shell.execute_reply.started":"2022-02-13T17:07:38.222538Z","shell.execute_reply":"2022-02-13T17:07:38.238347Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"incount = vc19.index.isin(vc_prevday19.index)\nprint(len(incount[incount]))\nprint(incount)","metadata":{"execution":{"iopub.status.busy":"2022-02-13T17:07:38.240709Z","iopub.execute_input":"2022-02-13T17:07:38.241080Z","iopub.status.idle":"2022-02-13T17:07:38.250673Z","shell.execute_reply.started":"2022-02-13T17:07:38.241033Z","shell.execute_reply":"2022-02-13T17:07:38.249772Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"concat_incount = (vc19.index.isin(vc_prevday19.index))|(vc19.index.isin(vc_prevmnth19.index))|(vc19.index.isin(vc_prevwk19.index))\nprint(len(concat_incount[concat_incount]))\nprint(concat_incount)","metadata":{"execution":{"iopub.status.busy":"2022-02-13T17:07:38.252218Z","iopub.execute_input":"2022-02-13T17:07:38.252802Z","iopub.status.idle":"2022-02-13T17:07:38.264755Z","shell.execute_reply.started":"2022-02-13T17:07:38.252762Z","shell.execute_reply":"2022-02-13T17:07:38.263980Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"compound_factor_pivot = pd.pivot_table(trx_train.loc[trx_train['prev_month_2019']>0], index='article_id', values='compound_factor', aggfunc='sum').sort_values(by='compound_factor', ascending=False)\ncompound_factor_pivot","metadata":{"execution":{"iopub.status.busy":"2022-02-13T17:08:30.716256Z","iopub.execute_input":"2022-02-13T17:08:30.717053Z","iopub.status.idle":"2022-02-13T17:08:36.161581Z","shell.execute_reply.started":"2022-02-13T17:08:30.717006Z","shell.execute_reply":"2022-02-13T17:08:36.160375Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"top_ids_by_compound = compound_factor_pivot.index[:12]\ncomp_incount = (vc19.index.isin(top_ids_by_compound))\nprint(len(comp_incount[comp_incount]))\nprint(comp_incount)","metadata":{"execution":{"iopub.status.busy":"2022-02-13T17:08:45.101627Z","iopub.execute_input":"2022-02-13T17:08:45.102582Z","iopub.status.idle":"2022-02-13T17:08:45.109231Z","shell.execute_reply.started":"2022-02-13T17:08:45.102532Z","shell.execute_reply":"2022-02-13T17:08:45.108546Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trx_train.head()","metadata":{"execution":{"iopub.status.busy":"2022-02-13T17:07:38.447161Z","iopub.status.idle":"2022-02-13T17:07:38.448284Z","shell.execute_reply.started":"2022-02-13T17:07:38.448069Z","shell.execute_reply":"2022-02-13T17:07:38.448092Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Define recurring product_type_no","metadata":{"execution":{"iopub.status.busy":"2022-02-13T15:00:18.068910Z","iopub.execute_input":"2022-02-13T15:00:18.069251Z","iopub.status.idle":"2022-02-13T15:00:18.075873Z","shell.execute_reply.started":"2022-02-13T15:00:18.069216Z","shell.execute_reply":"2022-02-13T15:00:18.074623Z"}}},{"cell_type":"code","source":"num_purch_df = pd.pivot_table(trx_train, index=['customer_id', 'product_type_no'], values='t_dat', aggfunc=lambda x: len(x.unique())).reset_index()\nnum_purch_df = pd.pivot_table(num_purch_df, index='product_type_no', values='t_dat', aggfunc='mean').reset_index()\nnum_purch_df.rename(columns={'t_dat':'mean_purch_num'}, inplace=True)\nnum_purch_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-02-13T17:07:38.449287Z","iopub.status.idle":"2022-02-13T17:07:38.450166Z","shell.execute_reply.started":"2022-02-13T17:07:38.449948Z","shell.execute_reply":"2022-02-13T17:07:38.449974Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_purch_df['recurring'] = np.where(num_purch_df['mean_purch_num']>1, 1, 0)\nnum_purch_df['recurring'].describe()","metadata":{"execution":{"iopub.status.busy":"2022-02-13T17:07:38.451044Z","iopub.status.idle":"2022-02-13T17:07:38.451378Z","shell.execute_reply.started":"2022-02-13T17:07:38.451189Z","shell.execute_reply":"2022-02-13T17:07:38.451204Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_purch_df.loc[num_purch_df['recurring']==1]","metadata":{"execution":{"iopub.status.busy":"2022-02-13T17:07:38.453701Z","iopub.status.idle":"2022-02-13T17:07:38.454046Z","shell.execute_reply.started":"2022-02-13T17:07:38.453871Z","shell.execute_reply":"2022-02-13T17:07:38.453888Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"articles[articles['product_type_no'].isin([252, 265,272])].head()","metadata":{"execution":{"iopub.status.busy":"2022-02-13T17:07:38.454871Z","iopub.status.idle":"2022-02-13T17:07:38.455167Z","shell.execute_reply.started":"2022-02-13T17:07:38.455010Z","shell.execute_reply":"2022-02-13T17:07:38.455026Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Customers","metadata":{}},{"cell_type":"code","source":"customers = pd.read_csv(os.path.join(DATA_DIR, 'customers.csv'))\nprint(customers.shape)\ncustomers.head()","metadata":{"execution":{"iopub.status.busy":"2022-02-13T17:07:38.456381Z","iopub.status.idle":"2022-02-13T17:07:38.456690Z","shell.execute_reply.started":"2022-02-13T17:07:38.456526Z","shell.execute_reply":"2022-02-13T17:07:38.456542Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"customers['club_member_status'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-02-13T17:07:38.457473Z","iopub.status.idle":"2022-02-13T17:07:38.457784Z","shell.execute_reply.started":"2022-02-13T17:07:38.457616Z","shell.execute_reply":"2022-02-13T17:07:38.457633Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"customers['age'].hist(bins=50)","metadata":{"execution":{"iopub.status.busy":"2022-02-13T17:07:38.459721Z","iopub.status.idle":"2022-02-13T17:07:38.460782Z","shell.execute_reply.started":"2022-02-13T17:07:38.460535Z","shell.execute_reply":"2022-02-13T17:07:38.460562Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"customers['postal_code'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-02-13T17:07:38.461858Z","iopub.status.idle":"2022-02-13T17:07:38.462326Z","shell.execute_reply.started":"2022-02-13T17:07:38.462105Z","shell.execute_reply":"2022-02-13T17:07:38.462125Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Modelling attempts","metadata":{"execution":{"iopub.status.busy":"2022-02-13T15:57:28.617447Z","iopub.execute_input":"2022-02-13T15:57:28.618452Z","iopub.status.idle":"2022-02-13T15:57:28.622984Z","shell.execute_reply.started":"2022-02-13T15:57:28.618401Z","shell.execute_reply":"2022-02-13T15:57:28.622029Z"}}},{"cell_type":"markdown","source":"## Most frequent articles in last periods","metadata":{"execution":{"iopub.status.busy":"2022-02-13T15:57:42.722887Z","iopub.execute_input":"2022-02-13T15:57:42.723499Z","iopub.status.idle":"2022-02-13T15:57:42.728180Z","shell.execute_reply.started":"2022-02-13T15:57:42.723420Z","shell.execute_reply":"2022-02-13T15:57:42.727005Z"}}},{"cell_type":"code","source":"trx_train.head()","metadata":{"execution":{"iopub.status.busy":"2022-02-13T17:07:38.463538Z","iopub.status.idle":"2022-02-13T17:07:38.463859Z","shell.execute_reply.started":"2022-02-13T17:07:38.463686Z","shell.execute_reply":"2022-02-13T17:07:38.463702Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trx_train['last_week_20'] = 0\ntrx_train.loc[((trx_train['t_dat']>=pd.to_datetime('2020-09-15')) & (trx_train['t_dat']<=pd.to_datetime('2020-09-22'))), 'last_week_20'] = 1","metadata":{"execution":{"iopub.status.busy":"2022-02-13T17:10:47.205516Z","iopub.execute_input":"2022-02-13T17:10:47.205885Z","iopub.status.idle":"2022-02-13T17:12:11.502576Z","shell.execute_reply.started":"2022-02-13T17:10:47.205840Z","shell.execute_reply":"2022-02-13T17:12:11.501458Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trx_train['last_month_20'] = 0\ntrx_train.loc[((trx_train['t_dat']>=pd.to_datetime('2020-08-22')) & (trx_train['t_dat']<=pd.to_datetime('2020-09-22'))), 'last_month_20'] = 1","metadata":{"execution":{"iopub.status.busy":"2022-02-13T17:12:11.504564Z","iopub.execute_input":"2022-02-13T17:12:11.504833Z","iopub.status.idle":"2022-02-13T17:13:36.012980Z","shell.execute_reply.started":"2022-02-13T17:12:11.504799Z","shell.execute_reply":"2022-02-13T17:13:36.011709Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trx_train['last_day_20'] = 0\ntrx_train.loc[((trx_train['t_dat']==pd.to_datetime('2020-09-22'))), 'last_day_20'] = 1","metadata":{"execution":{"iopub.status.busy":"2022-02-13T17:13:36.017399Z","iopub.execute_input":"2022-02-13T17:13:36.017984Z","iopub.status.idle":"2022-02-13T17:14:19.606821Z","shell.execute_reply.started":"2022-02-13T17:13:36.017938Z","shell.execute_reply":"2022-02-13T17:14:19.605347Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"vc_prevday20 = trx_train.loc[trx_train['last_day_20']==1, 'article_id'].value_counts().index[:12]\nvc_prevday20","metadata":{"execution":{"iopub.status.busy":"2022-02-13T17:15:13.961856Z","iopub.execute_input":"2022-02-13T17:15:13.963054Z","iopub.status.idle":"2022-02-13T17:15:14.006327Z","shell.execute_reply.started":"2022-02-13T17:15:13.962969Z","shell.execute_reply":"2022-02-13T17:15:14.005382Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Make preds","metadata":{}},{"cell_type":"code","source":"sample_submission = pd.read_csv(os.path.join(DATA_DIR, 'sample_submission.csv'))\nprint(sample_submission.shape)\nsample_submission.head()","metadata":{"execution":{"iopub.status.busy":"2022-02-13T17:14:19.801427Z","iopub.execute_input":"2022-02-13T17:14:19.802215Z","iopub.status.idle":"2022-02-13T17:14:25.520064Z","shell.execute_reply.started":"2022-02-13T17:14:19.802159Z","shell.execute_reply":"2022-02-13T17:14:25.519281Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"base_pred = ''\nfor i in vc_prevday20:\n    base_pred += str(i)+' '\nbase_pred = base_pred[:-1]\nbase_pred","metadata":{"execution":{"iopub.status.busy":"2022-02-13T17:17:26.816903Z","iopub.execute_input":"2022-02-13T17:17:26.818068Z","iopub.status.idle":"2022-02-13T17:17:26.826787Z","shell.execute_reply.started":"2022-02-13T17:17:26.818024Z","shell.execute_reply":"2022-02-13T17:17:26.825601Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_submission['prediction'] = base_pred\nsample_submission","metadata":{"execution":{"iopub.status.busy":"2022-02-13T17:17:46.261433Z","iopub.execute_input":"2022-02-13T17:17:46.261748Z","iopub.status.idle":"2022-02-13T17:17:46.290400Z","shell.execute_reply.started":"2022-02-13T17:17:46.261717Z","shell.execute_reply":"2022-02-13T17:17:46.289270Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_submission.to_csv('/kaggle/working/preds.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-02-13T17:18:36.607938Z","iopub.execute_input":"2022-02-13T17:18:36.608277Z","iopub.status.idle":"2022-02-13T17:18:49.226102Z","shell.execute_reply.started":"2022-02-13T17:18:36.608244Z","shell.execute_reply":"2022-02-13T17:18:49.224842Z"},"trusted":true},"execution_count":null,"outputs":[]}]}