{"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=\"text-align: center;\"><font color=\"blue\"> Why are some sessions so long? Part II</h1>\n<h2><center> ","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19"}},{"cell_type":"markdown","source":"There are two measures for the length of a session: the number of events and the elapsed time.  [**Part I**](https://www.kaggle.com/code/gehallak/why-are-some-sessions-so-long) was about the number of events. In **part II** we will focus on the **elapsed time**.\n\n","metadata":{}},{"cell_type":"markdown","source":"# 1. Session example 21040207565119420\n\nLet's start by isolating one session and look at the evolution of elapsed time to understand how it works. We will use **Chris Deotte's parquet file** which loads faster. Thank you Chris for making it available to all.","metadata":{}},{"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport matplotlib.pyplot as plt\ntrain = pd.read_parquet('/kaggle/input/how-to-get-32gb-ram/train.parquet')","metadata":{"execution":{"iopub.status.busy":"2023-03-29T06:04:51.381800Z","iopub.execute_input":"2023-03-29T06:04:51.382253Z","iopub.status.idle":"2023-03-29T06:05:13.172497Z","shell.execute_reply.started":"2023-03-29T06:04:51.382216Z","shell.execute_reply":"2023-03-29T06:05:13.170849Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"SESSION_ID=21030109563292748","metadata":{"execution":{"iopub.status.busy":"2023-03-29T06:05:13.174569Z","iopub.execute_input":"2023-03-29T06:05:13.174931Z","iopub.status.idle":"2023-03-29T06:05:13.185113Z","shell.execute_reply.started":"2023-03-29T06:05:13.174880Z","shell.execute_reply":"2023-03-29T06:05:13.183890Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"session_df=train[train['session_id']==SESSION_ID].reset_index(drop=True)\nsession_df","metadata":{"execution":{"iopub.status.busy":"2023-03-29T06:05:13.189326Z","iopub.execute_input":"2023-03-29T06:05:13.190199Z","iopub.status.idle":"2023-03-29T06:05:13.459995Z","shell.execute_reply.started":"2023-03-29T06:05:13.190151Z","shell.execute_reply":"2023-03-29T06:05:13.458850Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"This session has 1080 rows, each row is an event in the session.\nThe elapsed time is in millisecond, we will convert it to minutes to make it easier to grasp.","metadata":{}},{"cell_type":"code","source":"session_df['elapsed_time']=session_df['elapsed_time']/60000.\nsession_df.plot(y='elapsed_time')\nplt.title(f'Session {SESSION_ID}')\nplt.xlabel('events')\nplt.ylabel('elapsed time')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-03-29T06:05:13.463011Z","iopub.execute_input":"2023-03-29T06:05:13.463693Z","iopub.status.idle":"2023-03-29T06:05:13.812888Z","shell.execute_reply.started":"2023-03-29T06:05:13.463652Z","shell.execute_reply":"2023-03-29T06:05:13.806234Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"the session lasts about 30 minutes during which the player has generated 1080 events. We see here that elapsed_time is the **cumulative time** spent playing the game. We notice that during the game, the player has stopped playing for some minutes. Time was still elapsing but no new event was created during this break.","metadata":{}},{"cell_type":"code","source":"session_df.plot(y='level')\nplt.title('Session 21040207565119420')\nplt.xlabel('events')\nplt.ylabel('level')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-03-29T06:05:13.814316Z","iopub.execute_input":"2023-03-29T06:05:13.814785Z","iopub.status.idle":"2023-03-29T06:05:14.182469Z","shell.execute_reply.started":"2023-03-29T06:05:13.814747Z","shell.execute_reply":"2023-03-29T06:05:14.181452Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"We can see the players moving up the levels while generating events, ","metadata":{}},{"cell_type":"code","source":"session_df.plot(x='elapsed_time',y='level')\nplt.title('Session 21040207565119420')\nplt.xlabel('elapsed time')\nplt.ylabel('level')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-03-29T06:05:14.184241Z","iopub.execute_input":"2023-03-29T06:05:14.184925Z","iopub.status.idle":"2023-03-29T06:05:14.456155Z","shell.execute_reply.started":"2023-03-29T06:05:14.184885Z","shell.execute_reply":"2023-03-29T06:05:14.455050Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Here we see the time spent at each level of the game, There are a total of 22 levels which the player achieved in about 30 minutes.","metadata":{}},{"cell_type":"markdown","source":"# 2. Group by sessions.\n\nWe now look at all sessions and focus on **the elapsed time to finish the game.**\nIt is simply the max of elapsed_time for each session.\n\nAgain, we will group by session. Here we will agregate elapsed_time using the max.","metadata":{}},{"cell_type":"code","source":"train_session = train.groupby('session_id').agg({'elapsed_time': ['max'], 'session_id': ['count']})","metadata":{"execution":{"iopub.status.busy":"2023-03-29T06:05:14.457847Z","iopub.execute_input":"2023-03-29T06:05:14.458567Z","iopub.status.idle":"2023-03-29T06:05:15.358931Z","shell.execute_reply.started":"2023-03-29T06:05:14.458521Z","shell.execute_reply":"2023-03-29T06:05:15.357917Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_session.columns=['end_time','event_nb']\ntrain_session['end_time']=train_session['end_time']/60000.","metadata":{"execution":{"iopub.status.busy":"2023-03-29T06:05:15.360332Z","iopub.execute_input":"2023-03-29T06:05:15.360707Z","iopub.status.idle":"2023-03-29T06:05:15.367279Z","shell.execute_reply.started":"2023-03-29T06:05:15.360660Z","shell.execute_reply":"2023-03-29T06:05:15.366202Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_session.hist('end_time',bins=50)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-03-29T06:05:15.369014Z","iopub.execute_input":"2023-03-29T06:05:15.369832Z","iopub.status.idle":"2023-03-29T06:05:15.630935Z","shell.execute_reply.started":"2023-03-29T06:05:15.369785Z","shell.execute_reply":"2023-03-29T06:05:15.629893Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"If we clip the extreme values, the histogram becomes:","metadata":{}},{"cell_type":"code","source":"train_session[(train_session['end_time']>5) & (train_session['end_time']<200)].hist('end_time',bins=50)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-03-29T06:05:15.635016Z","iopub.execute_input":"2023-03-29T06:05:15.635311Z","iopub.status.idle":"2023-03-29T06:05:15.906884Z","shell.execute_reply.started":"2023-03-29T06:05:15.635283Z","shell.execute_reply":"2023-03-29T06:05:15.905930Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Most sessions last between 20 and 95 minutes.","metadata":{}},{"cell_type":"code","source":"train_session['end_time'].quantile([0,0.01, 0.10,0.25, 0.50, 0.75 ,0.90,0.98,0.99,1.])","metadata":{"execution":{"iopub.status.busy":"2023-03-29T06:05:15.908414Z","iopub.execute_input":"2023-03-29T06:05:15.908776Z","iopub.status.idle":"2023-03-29T06:05:15.922170Z","shell.execute_reply.started":"2023-03-29T06:05:15.908738Z","shell.execute_reply":"2023-03-29T06:05:15.921006Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":" but there seems to be some pathological cases, both very short and very long sessions. We will look at them now.","metadata":{}},{"cell_type":"markdown","source":"# 3. Very short session","metadata":{}},{"cell_type":"code","source":"train_session=train_session.sort_values('end_time')\ntrain_session.head(10)","metadata":{"execution":{"iopub.status.busy":"2023-03-29T06:05:15.923408Z","iopub.execute_input":"2023-03-29T06:05:15.923840Z","iopub.status.idle":"2023-03-29T06:05:15.938271Z","shell.execute_reply.started":"2023-03-29T06:05:15.923792Z","shell.execute_reply":"2023-03-29T06:05:15.937135Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"The fastest session ended in 0.017 minute. That seems impossible. The second fastest ended in 7.6 minutes which is fast but possible. There seems to be a problem with session **21020618143279870**.","metadata":{}},{"cell_type":"code","source":"SESSION_ID=21020618143279870\nsession_df=train[train['session_id']==SESSION_ID].reset_index(drop=True)\nsession_df['elapsed_time']=session_df['elapsed_time']/60000.\nsession_df.plot(y='elapsed_time')\nplt.title(f'Session {SESSION_ID}')\nplt.xlabel('events')\nplt.ylabel('elapsed time')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-03-29T06:05:15.939924Z","iopub.execute_input":"2023-03-29T06:05:15.940342Z","iopub.status.idle":"2023-03-29T06:05:16.211178Z","shell.execute_reply.started":"2023-03-29T06:05:15.940305Z","shell.execute_reply":"2023-03-29T06:05:16.210152Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"It looks like the organisers have not cumulated the elapsed time for this session. If we cumulate elapsed time:","metadata":{}},{"cell_type":"code","source":"session_df['elapsed_time']=session_df['elapsed_time'].cumsum()\nsession_df.plot(y='elapsed_time')\nplt.title(f'Session {SESSION_ID}')\nplt.xlabel('events')\nplt.ylabel('elapsed time')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-03-29T06:05:16.212222Z","iopub.execute_input":"2023-03-29T06:05:16.213238Z","iopub.status.idle":"2023-03-29T06:05:16.433036Z","shell.execute_reply.started":"2023-03-29T06:05:16.213196Z","shell.execute_reply":"2023-03-29T06:05:16.431920Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Now it looks more normal. As a warning, the session 21020618143279870 elapsed time has not been cumulated and you may exclude this session from the training or calculate its cumulative elapsed time before feeding it to the training.","metadata":{}},{"cell_type":"markdown","source":"# 4. Very long sessions","metadata":{}},{"cell_type":"code","source":"train_session.tail(10)","metadata":{"execution":{"iopub.status.busy":"2023-03-29T06:05:16.434327Z","iopub.execute_input":"2023-03-29T06:05:16.434666Z","iopub.status.idle":"2023-03-29T06:05:16.446242Z","shell.execute_reply.started":"2023-03-29T06:05:16.434628Z","shell.execute_reply":"2023-03-29T06:05:16.444958Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"the longest session last 33,143 minutes, ie 23 days. The player clearly couldn't play 23 days non stop. Something is wrong here. ","metadata":{}},{"cell_type":"code","source":"SESSION_ID=21040114240062616\nsession_df=train[train['session_id']==SESSION_ID].reset_index(drop=True)\nsession_df['elapsed_time']=session_df['elapsed_time']/60000.\nsession_df.plot(y='elapsed_time')\nplt.title(f'Session {SESSION_ID}')\nplt.xlabel('events')\nplt.ylabel('elapsed time')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-03-29T06:05:16.448190Z","iopub.execute_input":"2023-03-29T06:05:16.449381Z","iopub.status.idle":"2023-03-29T06:05:16.689959Z","shell.execute_reply.started":"2023-03-29T06:05:16.449341Z","shell.execute_reply":"2023-03-29T06:05:16.688951Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"There are vey long periods where no event is generated. The game has been paused for a very long time.","metadata":{}},{"cell_type":"code","source":"session_df.plot(y='level')\nplt.title(f'Session {SESSION_ID}')\nplt.xlabel('events')\nplt.ylabel('level')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-03-29T06:05:16.691505Z","iopub.execute_input":"2023-03-29T06:05:16.692163Z","iopub.status.idle":"2023-03-29T06:05:16.896196Z","shell.execute_reply.started":"2023-03-29T06:05:16.692122Z","shell.execute_reply":"2023-03-29T06:05:16.895059Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"The game has been **played twice!!** Chris Deotte has warned us about some sessions with this issue. We may need to do something about these sessions. The best way to detect them is to count the events '**checkpoint**'. A normal session has 3 **checkpoint** events.","metadata":{}},{"cell_type":"markdown","source":"# 5. Relation between the session's elapsed time and the score.\n\nWe would like to know if there is a relation between the time spent to finish the game (max elapsed_time) and the player's score (number of correct questions out of 18). First we exclude the pathological sessions.","metadata":{}},{"cell_type":"code","source":"train_session=train_session[(train_session['end_time']>1.) & (train_session['end_time']<100.)]","metadata":{"execution":{"iopub.status.busy":"2023-03-29T06:05:16.897865Z","iopub.execute_input":"2023-03-29T06:05:16.898224Z","iopub.status.idle":"2023-03-29T06:05:16.906389Z","shell.execute_reply.started":"2023-03-29T06:05:16.898188Z","shell.execute_reply":"2023-03-29T06:05:16.905310Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"We load train_labels to get the session scores and merge train and train_labels.","metadata":{}},{"cell_type":"code","source":"train_labels = pd.read_parquet('/kaggle/input/how-to-get-32gb-ram/train_labels.parquet')\ntrain_labels['session'] = train_labels.session_id.apply(lambda x: int(x.split('_')[0]))\ntrain_labels['q'] = train_labels.session_id.apply(lambda x: int(x.split('_')[-1][1:]))","metadata":{"execution":{"iopub.status.busy":"2023-03-29T06:05:16.907848Z","iopub.execute_input":"2023-03-29T06:05:16.908396Z","iopub.status.idle":"2023-03-29T06:05:17.863920Z","shell.execute_reply.started":"2023-03-29T06:05:16.908356Z","shell.execute_reply":"2023-03-29T06:05:17.862886Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_labels_score=train_labels.groupby('session').agg({'correct': ['sum']}).reset_index()\ntrain_labels_score.columns=['session_id','score']\ntrain_session=train_session.merge(train_labels_score,on='session_id',how='inner')","metadata":{"execution":{"iopub.status.busy":"2023-03-29T06:05:17.865448Z","iopub.execute_input":"2023-03-29T06:05:17.865929Z","iopub.status.idle":"2023-03-29T06:05:17.909335Z","shell.execute_reply.started":"2023-03-29T06:05:17.865887Z","shell.execute_reply":"2023-03-29T06:05:17.908288Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_session","metadata":{"execution":{"iopub.status.busy":"2023-03-29T06:05:17.910766Z","iopub.execute_input":"2023-03-29T06:05:17.912159Z","iopub.status.idle":"2023-03-29T06:05:17.926776Z","shell.execute_reply.started":"2023-03-29T06:05:17.912091Z","shell.execute_reply":"2023-03-29T06:05:17.925422Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_session_score=train_session.groupby('score').agg({'end_time': ['mean']}).reset_index()\ntrain_session_score.plot.scatter(x='score',y='end_time')\nplt.xticks(np.arange(1,19))\nplt.xlabel('correct answers')\nplt.ylabel('average session duration')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-03-29T06:05:17.928481Z","iopub.execute_input":"2023-03-29T06:05:17.929177Z","iopub.status.idle":"2023-03-29T06:05:18.174961Z","shell.execute_reply.started":"2023-03-29T06:05:17.929133Z","shell.execute_reply":"2023-03-29T06:05:18.173948Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"There is a clear pattern where the best players also end the game faster.","metadata":{}},{"cell_type":"markdown","source":"# 6. conclusion\nThe best players, who answer correctly all 18 questions, finish the game in a bit less than 30 minutes in average. The players who make many mistakes take in average about 50 minutes to end the game.\n\nWe also noticed that there are some pathological sessions. \n* In Session 21020618143279870, the elapsed time cumsum is missing.\n* In several sessions the game has been played twice or even more times.\n\nWe probably need to deal with these pathological sessions in a first phase of data preprocessing.","metadata":{}}]}