{"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":"<h1 style=\"font-family:roboto;\"> <center>OTTO – Multi-Objective Recommender System \t💵💻 </center> </h1>","metadata":{"id":"5zyCB_WFMSk1"}},{"cell_type":"markdown","source":"### <span style=\"font-family:roboto; word-spacing:1.5px;\"> Credits📑\n    \n* **Kernel an EAD Style**\n    \n    * [OTTO: I was warned this one is complicated](https://www.kaggle.com/code/andradaolteanu/otto-i-was-warned-this-one-is-complicated/notebook)\n    * [OTTO - Getting Started (EDA + Baseline)](https://www.kaggle.com/code/edwardcrookenden/otto-getting-started-eda-baseline)\n    * [Time Series EDA - Users and Real Sessions](https://www.kaggle.com/code/cdeotte/time-series-eda-users-and-real-sessions)\n    \n\n* **Dataset**\n    * [Otto Full Optimized Memory Footprint](https://www.kaggle.com/datasets/radek1/otto-full-optimized-memory-footprint)\n    ","metadata":{}},{"cell_type":"markdown","source":"### <span style=\"font-family:roboto; word-spacing:1.5px;\"> Contents 📚\n\n[The Problem](#first-bullet)\n\n[Data](#second-bullet)\n\n[Sessions](#second-bullet)\n\n[Time Analysis](##first-bullet)\n","metadata":{"id":"I6-_idNeNzhC"}},{"cell_type":"markdown","source":"### <span style=\"font-family:roboto; word-spacing:1.5px;\">  Libraries and Setup 📑","metadata":{"id":"uJuvc1Kv6duu"}},{"cell_type":"code","source":"!pip install polars\n# !pip install matplotlib --upgrade\n# !pip install bar-chart-race","metadata":{"id":"uhdYNogj64RZ","execution":{"iopub.status.busy":"2022-12-13T18:18:31.526105Z","iopub.execute_input":"2022-12-13T18:18:31.526841Z","iopub.status.idle":"2022-12-13T18:18:48.564061Z","shell.execute_reply.started":"2022-12-13T18:18:31.526723Z","shell.execute_reply":"2022-12-13T18:18:48.562842Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import polars as pl\nimport pandas as pd\nimport seaborn as sns\nimport matplotlib.pyplot as plt\nfrom matplotlib.colors import ListedColormap, LinearSegmentedColormap\nimport numpy as np\n\n\nimport matplotlib as mpl\nprint('matplotlib: {}'.format(mpl.__version__))\n\n\n","metadata":{"id":"p8H3sFWY6GiG","outputId":"c3393edd-f22d-4ef7-d338-c84b14789104","execution":{"iopub.status.busy":"2022-12-13T18:18:48.566807Z","iopub.execute_input":"2022-12-13T18:18:48.567310Z","iopub.status.idle":"2022-12-13T18:18:49.266937Z","shell.execute_reply.started":"2022-12-13T18:18:48.567261Z","shell.execute_reply":"2022-12-13T18:18:49.264738Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class CONFIG():\n\n  data_folder='/kaggle/input/otto-full-optimized-memory-footprint/'\n  seed=42\n\n","metadata":{"id":"spOxZiyI7BCp","execution":{"iopub.status.busy":"2022-12-13T18:18:49.268200Z","iopub.execute_input":"2022-12-13T18:18:49.268512Z","iopub.status.idle":"2022-12-13T18:18:49.273667Z","shell.execute_reply.started":"2022-12-13T18:18:49.268483Z","shell.execute_reply":"2022-12-13T18:18:49.272735Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class clr:\n    S = '\\033[1m' + '\\033[94m'\n    E = '\\033[0m'\n    R = '\\033[31m'\n    G = '\\033[1;32m'\n    Y = '\\033[33m'\n    \nmy_colors = [\"#5EAFD9\", \"#449DD1\", \"#3977BB\", \n             \"#2D51A5\", \"#5C4C8F\", \"#8B4679\",\n             \"#C53D4C\", \"#E23836\", \"#FF4633\", \"#FF5746\"]\nCMAP1 = ListedColormap(my_colors)\n\nprint(clr.S+\"Notebook Color Schemes:\"+clr.E)\nsns.palplot(sns.color_palette(my_colors))\nplt.show()","metadata":{"id":"_Ju_9uCz9725","outputId":"61fb6e6c-0cde-4c19-e1c2-f103122588f6","execution":{"iopub.status.busy":"2022-12-13T18:18:49.276128Z","iopub.execute_input":"2022-12-13T18:18:49.276438Z","iopub.status.idle":"2022-12-13T18:18:49.403094Z","shell.execute_reply.started":"2022-12-13T18:18:49.276409Z","shell.execute_reply":"2022-12-13T18:18:49.401571Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### <span style=\"font-family:roboto; word-spacing:1.5px;\">  Funtions 📑","metadata":{"id":"h1McheNb2XlK"}},{"cell_type":"code","source":"def get_percentiles(x):\n  q1 = np.percentile(x,60)\n  q2 = np.percentile(x,80)\n  q3 = np.percentile(x,95)\n\n  return q1,q2,q3\n\n\ndef max_height_ax(ax):\n  for c in ax.containers:\n    # Optional: if the segment is small or 0, customize the labels\n    labels = [round(v.get_height(),2) if v.get_height() > 0 else 0 for v in c]\n\n    return max(labels)\n\ndef show_values(ax):\n  for c in ax.containers:\n\n      # Optional: if the segment is small or 0, customize the labels\n      labels = [round(v.get_height(),2) if v.get_height() > 0 else '' for v in c]\n\n      # remove the labels parameter if it's not needed for customized labels\n      ax.bar_label(c, labels=labels, label_type='edge')\n  \ndef dist_plot(data_plot, title, x_label,percentiles_dist=8):\n    fig, ax = plt.subplots(2,1,figsize=(20,15))\n    #train_session_df['aid_u_count'].hist(bins=100, ax=ax[0])\n    q1,q2,q3 = get_percentiles(data_plot)\n    sns.histplot(x = data_plot,bins=150, ax =ax[0])\n\n    y_max = max_height_ax(ax[0])\n    y_max+=y_max*0.07\n\n    \n\n    dist = percentiles_dist if q2-q1 < percentiles_dist else 0\n    ax[0].axvline(x=q1, ls=\":\", lw=2.5, color=\"black\")\n    ax[0].text(x=q1-dist, y=y_max, s=\"60% \", size=10, color=\"black\", weight=400)\n\n    dist = percentiles_dist if q2-q1 < percentiles_dist else 0\n    ax[0].axvline(x=q2, ls=\"--\", lw=2.5, color=\"black\")\n    ax[0].text(x=q2+(dist/2), y=y_max, s=\"80% \", size=10, color=\"black\", weight=400)\n\n    ax[0].axvline(x=q3, ls=\"-\", lw=2.5, color=\"black\")\n    ax[0].text(x=q3+(dist*1.5), y=y_max, s=\"95% \", size=10, color=\"black\", weight=400)\n\n    sns.boxplot(x = (train_session_df['session_time']/60), ax=ax[1],  boxprops={\"facecolor\": my_colors[0]},\n                medianprops={\"color\": my_colors[5]})\n    ax[0].set_xlabel('')\n    ax[1].set_xlabel(x_label, fontsize=18)\n    plt.suptitle(title, fontsize = 22, weight='bold')\n\n","metadata":{"id":"ltYmR_U32M8P","execution":{"iopub.status.busy":"2022-12-13T18:18:49.405854Z","iopub.execute_input":"2022-12-13T18:18:49.406986Z","iopub.status.idle":"2022-12-13T18:18:49.442855Z","shell.execute_reply.started":"2022-12-13T18:18:49.406919Z","shell.execute_reply":"2022-12-13T18:18:49.440868Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# The Problem\n\nWith more than 10 million products from over 19,000 brands, OTTO is the largest German online shop. OTTO is a member of the Hamburg-based, multi-national Otto Group, which also subsidizes Crate & Barrel (USA) and 3 Suisses (France).\n\nThe goal of this competition is to predict e-commerce **clicks, cart additions, and orders.** You'll build a multi-objective recommender system based on previous events in a user session.","metadata":{"id":"jg-ZsfMzdfY1"}},{"cell_type":"markdown","source":"# Data","metadata":{"id":"hCFAuVso7Gke"}},{"cell_type":"markdown","source":"* `session` - the unique session id\n* `events` - the time ordered sequence of events in the session\n  \n  * `aid` - the article id (product code) of the associated event\n  * `ts` - the Unix timestamp of the event\n  * `type` - the event type, i.e., whether a product was **clicked**, added to the user's **cart**, or **ordered** during the session\n","metadata":{"id":"uLBhnzLrP8jB"}},{"cell_type":"code","source":"''' \ntypes:\n0 -> click\n1 -> add_cart\n2 -> order\n'''\ntrain = pl.read_parquet(CONFIG.data_folder+'train.parquet')\ntest = pl.read_parquet(CONFIG.data_folder+'test.parquet')","metadata":{"id":"g8bGY8WC8B3b","execution":{"iopub.status.busy":"2022-12-13T18:18:49.447013Z","iopub.execute_input":"2022-12-13T18:18:49.448498Z","iopub.status.idle":"2022-12-13T18:19:03.403295Z","shell.execute_reply.started":"2022-12-13T18:18:49.448427Z","shell.execute_reply":"2022-12-13T18:19:03.402259Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Sessions","metadata":{"id":"bYYnP7Zs1U3w"}},{"cell_type":"code","source":"train_session_df = train.groupby(['session']).agg([\n    pl.col('aid').list().alias('action_count'),\n    pl.col('aid').n_unique().alias('aid_u_count'),\n    pl.col('aid').count().alias('aid_count'),\n    pl.col('type').n_unique().alias('types_u_count'),\n    pl.col('ts').max().alias('max_ts'),\n    pl.col('ts').min().alias('min_ts'),\n])#.to_pandas()\n\ntrain_session_df = train_session_df.with_column(((pl.col('max_ts') - pl.col('min_ts'))/60 ).alias('session_time'))\ntrain_session_df = train_session_df.to_pandas().drop(['max_ts','min_ts'], axis=1)\n\ngroup_df_train = train.groupby('session').count()\n\n\ntest_session_df = test.groupby(['session']).agg([\n    pl.col('aid').list().alias('action_count'),\n    pl.col('aid').n_unique().alias('aid_u_count'),\n    pl.col('aid').count().alias('aid_count'),\n    pl.col('type').n_unique().alias('types_u_count'),\n    pl.col('ts').max().alias('max_ts'),\n    pl.col('ts').min().alias('min_ts'),\n])#.to_pandas()\n\ntest_session_df = test_session_df.with_column(((pl.col('max_ts') - pl.col('min_ts'))/60 ).alias('session_time'))\ntest_session_df = test_session_df.to_pandas().drop(['max_ts','min_ts'], axis=1)\n\ngroup_df_test = test.groupby('session').count()","metadata":{"id":"XGzVrK5IthlX","execution":{"iopub.status.busy":"2022-12-13T18:19:03.404976Z","iopub.execute_input":"2022-12-13T18:19:03.405788Z","iopub.status.idle":"2022-12-13T18:19:42.671935Z","shell.execute_reply.started":"2022-12-13T18:19:03.405741Z","shell.execute_reply":"2022-12-13T18:19:42.670578Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### <span style=\"font-family:roboto; word-spacing:1.5px;\">  Train","metadata":{"id":"eJB0Ll_9aB_3"}},{"cell_type":"code","source":"print('Statistics Sessions info')\nprint('----- Train -----')\nprint(group_df_train['count'].describe())\n","metadata":{"id":"DuMPBXEORo2O","outputId":"ac2fe193-d52e-4453-bc82-9cab89ac9cad","execution":{"iopub.status.busy":"2022-12-13T18:19:42.673180Z","iopub.execute_input":"2022-12-13T18:19:42.673505Z","iopub.status.idle":"2022-12-13T18:19:42.910458Z","shell.execute_reply.started":"2022-12-13T18:19:42.673475Z","shell.execute_reply":"2022-12-13T18:19:42.909263Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dist_plot(data_plot = train_session_df['aid_u_count'], \n          title = 'Train - Number of Events per Section', \n          x_label= 'Number of Events')","metadata":{"id":"Z36ieZm_WWn3","outputId":"85751213-6520-4898-cb17-e7a5067a0193","execution":{"iopub.status.busy":"2022-12-13T18:19:42.912036Z","iopub.execute_input":"2022-12-13T18:19:42.913161Z","iopub.status.idle":"2022-12-13T18:19:46.298123Z","shell.execute_reply.started":"2022-12-13T18:19:42.913121Z","shell.execute_reply":"2022-12-13T18:19:46.297221Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Statistics Sessions Leght info')\nprint('----- Train -----')\nprint((train_session_df['session_time']/60).describe())","metadata":{"id":"0g0hO_iZCijI","outputId":"8ed85b15-a2e1-45be-d926-4eb7eec6f429","execution":{"iopub.status.busy":"2022-12-13T18:19:46.301439Z","iopub.execute_input":"2022-12-13T18:19:46.302104Z","iopub.status.idle":"2022-12-13T18:19:46.970806Z","shell.execute_reply.started":"2022-12-13T18:19:46.302066Z","shell.execute_reply":"2022-12-13T18:19:46.967951Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dist_plot(data_plot = (train_session_df['session_time']/60).values, \n          title = 'Train - Lenght of Section', \n          x_label= 'Hours')","metadata":{"id":"eAWCxWyMe7oL","outputId":"e3abafd0-6f90-4595-9353-ceecad0b6466","execution":{"iopub.status.busy":"2022-12-13T18:19:46.972389Z","iopub.execute_input":"2022-12-13T18:19:46.972756Z","iopub.status.idle":"2022-12-13T18:19:51.935542Z","shell.execute_reply.started":"2022-12-13T18:19:46.972722Z","shell.execute_reply":"2022-12-13T18:19:51.934252Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Train Conclusions** \n\n* The sections have an average of ~16 events\n* 80% of session have less than ~20 events \n* The sections have an average of ~164h of duration\n* 60% of sections have less than ~142 of duration\n","metadata":{"id":"Vn2urcxtBhLE"}},{"cell_type":"markdown","source":"### <span style=\"font-family:roboto; word-spacing:1.5px;\">  Test","metadata":{"id":"V9xslClhaG6Q"}},{"cell_type":"code","source":"print('----- Test -----')\nprint(group_df_test['count'].describe())","metadata":{"id":"G3cecuHBXO7t","outputId":"18d5219d-3030-4696-ee8d-44a05282cdbb","execution":{"iopub.status.busy":"2022-12-13T18:19:51.937022Z","iopub.execute_input":"2022-12-13T18:19:51.937365Z","iopub.status.idle":"2022-12-13T18:19:51.962920Z","shell.execute_reply.started":"2022-12-13T18:19:51.937334Z","shell.execute_reply":"2022-12-13T18:19:51.961501Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dist_plot(data_plot = test_session_df['aid_u_count'], \n          title = 'Test - Number of Events per Section', \n          x_label= 'Number of Events')","metadata":{"id":"PmzCWOQocv-I","outputId":"faf508cd-aaf8-4a87-a105-8d7c640a729a","execution":{"iopub.status.busy":"2022-12-13T18:19:51.965151Z","iopub.execute_input":"2022-12-13T18:19:51.966893Z","iopub.status.idle":"2022-12-13T18:19:53.662759Z","shell.execute_reply.started":"2022-12-13T18:19:51.966843Z","shell.execute_reply":"2022-12-13T18:19:53.661611Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Statistics Sessions Leght info')\nprint('----- Train -----')\nprint((test_session_df['session_time']/60).describe())","metadata":{"id":"hGX09QonFhyl","outputId":"8557f6e8-96a2-4a15-a604-1e19f65cb876","execution":{"iopub.status.busy":"2022-12-13T18:19:53.664547Z","iopub.execute_input":"2022-12-13T18:19:53.665343Z","iopub.status.idle":"2022-12-13T18:19:53.750845Z","shell.execute_reply.started":"2022-12-13T18:19:53.665297Z","shell.execute_reply":"2022-12-13T18:19:53.749675Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dist_plot(data_plot = (test_session_df['session_time']/60), \n          title = 'Test - Lenght of Section', \n          x_label= 'Hours',\n          percentiles_dist=4)","metadata":{"id":"_d_-zL0E98yl","outputId":"1c1b41dd-50d3-4e9b-8d1e-177db574cd20","execution":{"iopub.status.busy":"2022-12-13T18:19:53.752722Z","iopub.execute_input":"2022-12-13T18:19:53.753196Z","iopub.status.idle":"2022-12-13T18:19:55.481137Z","shell.execute_reply.started":"2022-12-13T18:19:53.753150Z","shell.execute_reply":"2022-12-13T18:19:55.480303Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Type Actions\n\n* 0: click\n\n* 1: add_cart\n\n* 2: order","metadata":{}},{"cell_type":"code","source":"types_train = train.groupby('type').count().with_column(pl.lit('train').alias('dataset'))\ntypes_train = types_train.with_column( (pl.col('count')*(100/len(train))).alias('%') )\ntypes_test = test.groupby('type').count().with_column(pl.lit('test').alias('dataset'))\ntypes_test = types_test.with_column( (pl.col('count')*(100/len(test))).alias('%') )\n\ntypes_info = pl.concat([types_train, types_test], how = 'vertical').to_pandas().replace({0:'click',1:'cart',2:'order'})\n\nfig, ax = plt.subplots(figsize=(7,7))\ng = sns.barplot(data=types_info, x='type', y='%', hue='dataset', ax =ax, ci = None, palette=[my_colors[0],my_colors[6]])\ng.set_title('Frequency of Actions', fontsize=16,weight='bold')\nshow_values(g)\n","metadata":{"execution":{"iopub.status.busy":"2022-12-13T18:19:55.482548Z","iopub.execute_input":"2022-12-13T18:19:55.483144Z","iopub.status.idle":"2022-12-13T18:19:58.265172Z","shell.execute_reply.started":"2022-12-13T18:19:55.483110Z","shell.execute_reply":"2022-12-13T18:19:58.263988Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Time Session analisys\n\nFrom the Leght session analysis, consider a Real session with leght of 50 hours or more and session with 20 actions or more.","metadata":{"id":"gpdkPINCFsIK"}},{"cell_type":"code","source":"def timestampFormat(dataset:str, sample:int):\n\n  session_df = train_session_df if dataset == 'train' else test_session_df\n  df = train if dataset == 'train' else test\n\n  sessions_ts = session_df['session'][(session_df['aid_count']>=20) & (session_df['session_time']>=50*60)].sample(sample, random_state=CONFIG.seed).tolist()\n  df_ts = df.filter(pl.col('session').is_in(sessions_ts)).to_pandas()\n  df_ts['dif_ts'] = df_ts.ts.diff()\n  df_ts['ts'] = pd.to_datetime(df_ts['ts']*1e9)\n  df_ts['type'] = df_ts['type'].replace({0:'click',1:'cart',2:'order'})\n  df_ts['day'] = df_ts['ts'].dt.day\n  df_ts['hour'] = df_ts['ts'].dt.hour\n  df_ts['year'] = df_ts['ts'].dt.year\n  df_ts['month'] = df_ts['ts'].dt.month\n  df_ts['ts_day']=pd.to_datetime(df_ts.ts.dt.strftime('%Y-%m-%d'))\n  #df_ts['ts_day'] = df_ts['ts_day'].to_datetime()\n  return df_ts\n","metadata":{"id":"A6fX-Cqwfhyc","execution":{"iopub.status.busy":"2022-12-13T18:19:58.266467Z","iopub.execute_input":"2022-12-13T18:19:58.266828Z","iopub.status.idle":"2022-12-13T18:19:58.277307Z","shell.execute_reply.started":"2022-12-13T18:19:58.266788Z","shell.execute_reply":"2022-12-13T18:19:58.276015Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_ts_train = timestampFormat(dataset='train', sample=10000)\ndf_ts_test = timestampFormat(dataset='test', sample=5000)","metadata":{"id":"bOM4UDVaVO22","outputId":"dc6de6d4-5dad-4206-c7f7-cc131e30d0a4","execution":{"iopub.status.busy":"2022-12-13T18:19:58.278791Z","iopub.execute_input":"2022-12-13T18:19:58.279240Z","iopub.status.idle":"2022-12-13T18:20:01.735864Z","shell.execute_reply.started":"2022-12-13T18:19:58.279207Z","shell.execute_reply":"2022-12-13T18:20:01.734382Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Hour Frequency\n\n**TRAIN**","metadata":{"id":"t9qgxiRvahX3"}},{"cell_type":"code","source":"count_type_hour_train = df_ts_train.groupby(by = ['session','hour','type']).count().reset_index().iloc[:,:4].rename(columns = {'aid':'count_type_hour'})\ncount_type_hour_train.head(10)","metadata":{"id":"MuUnzWbVUz7K","outputId":"6f8e0b7b-2edf-41e7-ce91-04442d018d63","execution":{"iopub.status.busy":"2022-12-13T18:20:01.737539Z","iopub.execute_input":"2022-12-13T18:20:01.737936Z","iopub.status.idle":"2022-12-13T18:20:01.778629Z","shell.execute_reply.started":"2022-12-13T18:20:01.737893Z","shell.execute_reply":"2022-12-13T18:20:01.777481Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"types = count_type_hour_train['type'].unique()\nfig, ax = plt.subplots(2,len(types),figsize=(30,15))\nfor i,type_ in enumerate(types):\n  data_plot = count_type_hour_train[count_type_hour_train['type'] == type_]\n  sns.histplot(data = data_plot, x='hour', ax=ax[1,i], bins = 24);\n\n  sns.distplot(x=data_plot['hour'], rug=True, hist=False,ax=ax[0,i],\n             rug_kws={\"color\": my_colors[0]},\n             kde_kws={\"color\": my_colors[0], \"lw\": 5, \"alpha\": 0.7});\n\n  ax[0,i].set_title(type_, fontsize = 18, weight = 400)\n  ax[1,i].set_xlabel('Time of Day', fontsize = 18, weight = 400)\n  ax[0,i].set_ylabel('')\n  ax[1,i].set_ylabel('')\nplt.suptitle('Distribution of types of Shares by time of day', fontsize=22, weight=400);","metadata":{"id":"eKeglJMRUgyl","outputId":"9a9e4668-8c9d-428a-ad79-51cbca2eab01","execution":{"iopub.status.busy":"2022-12-13T18:20:01.780606Z","iopub.execute_input":"2022-12-13T18:20:01.782514Z","iopub.status.idle":"2022-12-13T18:20:03.328340Z","shell.execute_reply.started":"2022-12-13T18:20:01.782475Z","shell.execute_reply":"2022-12-13T18:20:03.324904Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**TEST**","metadata":{"id":"gdmQ3RK9gvDP"}},{"cell_type":"code","source":"count_type_hour_test = df_ts_test.groupby(by = ['session','hour','type']).count().reset_index().iloc[:,:4].rename(columns = {'aid':'count_type_hour'})\ncount_type_hour_test.head(10)","metadata":{"id":"VkgLoopQgxV1","outputId":"88217c2b-c1c7-4e84-82ba-010576da49db","execution":{"iopub.status.busy":"2022-12-13T18:20:03.329706Z","iopub.execute_input":"2022-12-13T18:20:03.330123Z","iopub.status.idle":"2022-12-13T18:20:03.358622Z","shell.execute_reply.started":"2022-12-13T18:20:03.330091Z","shell.execute_reply":"2022-12-13T18:20:03.357470Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"types = count_type_hour_test['type'].unique()\nfig, ax = plt.subplots(2,len(types),figsize=(30,15))\nfor i,type_ in enumerate(types):\n  data_plot = count_type_hour_test[count_type_hour_test['type'] == type_]\n  sns.histplot(data = data_plot, x='hour', ax=ax[1,i], bins = 24);\n\n  sns.distplot(x=data_plot['hour'], rug=True, hist=False,ax=ax[0,i],\n             rug_kws={\"color\": my_colors[0]},\n             kde_kws={\"color\": my_colors[0], \"lw\": 5, \"alpha\": 0.7});\n\n  ax[0,i].set_title(type_, fontsize = 18, weight = 400)\n  ax[1,i].set_xlabel('Hours of Day', fontsize = 18, weight = 400)\n  #ax[0,i].set_xlabel('Hours of Day', fontsize = 18, weight = 400)\n  ax[0,i].set_ylabel('')\n  ax[1,i].set_ylabel('')\nplt.suptitle('Distribution of types of Shares by time of day', fontsize=22, weight=400);","metadata":{"id":"Qgnltz1zg8nO","outputId":"99cd93cc-cd69-43b3-84d1-15b2b98bb1e4","execution":{"iopub.status.busy":"2022-12-13T18:20:03.360366Z","iopub.execute_input":"2022-12-13T18:20:03.360775Z","iopub.status.idle":"2022-12-13T18:20:05.022868Z","shell.execute_reply.started":"2022-12-13T18:20:03.360742Z","shell.execute_reply":"2022-12-13T18:20:05.018846Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Train ans Test Conclusions**\n\n* The behaivor of Users a long of Day is the same of all type of actions (Clicks, Cart an orders). \n\n* The interval of most user's actions varies between 10 and 15 hours approximately.\n\n* Users are least active around midnight","metadata":{"id":"yQO3qA2ibYIN"}}]}