{"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":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport os\n\nimport seaborn as sns\nimport matplotlib.pyplot as plt","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-04-01T10:26:20.834977Z","iopub.execute_input":"2023-04-01T10:26:20.835419Z","iopub.status.idle":"2023-04-01T10:26:21.576480Z","shell.execute_reply.started":"2023-04-01T10:26:20.835364Z","shell.execute_reply":"2023-04-01T10:26:21.574983Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"In this notebook lets try to find relation between the notebook events and answering the question\n\n**Hyposthesis: **\n\nwhen the students plays the game, student is going to refer back to notebook pages and may spend some time to understand. \nDoing so, tries to absorb crucial knowledge gained so far.\n\n**following questions to be answered:**\n\nsince to proceed in the game, it is must to add a note to the notebook.\n\n1. How many times notebook is opened.\n2. Time spent on the page.\n3. when is the last time the page was opened during the chapter.\n4. Is there any distributional difference for the time spent/number of times pages are opened (vs) correct answers.","metadata":{}},{"cell_type":"code","source":"%%time\ntrain_df = pd.read_csv(\"/kaggle/input/predict-student-performance-from-game-play/train.csv\",\n                       usecols=['session_id', \n                                'index', 'elapsed_time',\n                                'page', 'level', 'level_group', 'event_name', 'name'],\n                       dtype={\n                           'index' : np.int16,\n                           'level': np.int8,\n                           'page': np.float16,\n                           'level_group': \"category\",\n                           'event_name': \"category\",\n                           'name': \"category\"\n                       })\ntrain_df.head()","metadata":{"execution":{"iopub.status.busy":"2023-04-01T10:26:21.578915Z","iopub.execute_input":"2023-04-01T10:26:21.579266Z","iopub.status.idle":"2023-04-01T10:28:00.389228Z","shell.execute_reply.started":"2023-04-01T10:26:21.579233Z","shell.execute_reply":"2023-04-01T10:28:00.388165Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"page_df = train_df[~train_df.page.isna()]\npage_df.head()","metadata":{"execution":{"iopub.status.busy":"2023-04-01T10:28:00.390483Z","iopub.execute_input":"2023-04-01T10:28:00.391697Z","iopub.status.idle":"2023-04-01T10:28:00.607492Z","shell.execute_reply.started":"2023-04-01T10:28:00.391652Z","shell.execute_reply":"2023-04-01T10:28:00.606205Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"page_df.isna().sum()","metadata":{"execution":{"iopub.status.busy":"2023-04-01T10:28:00.609797Z","iopub.execute_input":"2023-04-01T10:28:00.610360Z","iopub.status.idle":"2023-04-01T10:28:00.635838Z","shell.execute_reply.started":"2023-04-01T10:28:00.610313Z","shell.execute_reply":"2023-04-01T10:28:00.634621Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"event names:\", page_df.event_name.unique())\nprint()","metadata":{"execution":{"iopub.status.busy":"2023-04-01T10:28:00.637111Z","iopub.execute_input":"2023-04-01T10:28:00.637453Z","iopub.status.idle":"2023-04-01T10:28:00.650130Z","shell.execute_reply.started":"2023-04-01T10:28:00.637401Z","shell.execute_reply":"2023-04-01T10:28:00.648841Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.title(\"Name of Notebook event\")\nsns.countplot(data=page_df, x='name')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-04-01T10:28:00.651227Z","iopub.execute_input":"2023-04-01T10:28:00.652079Z","iopub.status.idle":"2023-04-01T10:28:00.912002Z","shell.execute_reply.started":"2023-04-01T10:28:00.652036Z","shell.execute_reply":"2023-04-01T10:28:00.910952Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"page_df.groupby(['level_group'])[['page']].nunique().reset_index().plot(kind='bar', \n                                                                        x='level_group', \n                                                                        y='page'\n                                                                       )\nplt.title(\"Number of pages per level group\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-04-01T10:28:00.913151Z","iopub.execute_input":"2023-04-01T10:28:00.913743Z","iopub.status.idle":"2023-04-01T10:28:01.181006Z","shell.execute_reply.started":"2023-04-01T10:28:00.913707Z","shell.execute_reply":"2023-04-01T10:28:01.179988Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Checking notebook open and close events should be same, or if some level of noise exists\")\nprint(\"(#Open - #Close):\", len(page_df['name']=='open') - len(page_df['name']=='close'))","metadata":{"execution":{"iopub.status.busy":"2023-04-01T10:28:01.182165Z","iopub.execute_input":"2023-04-01T10:28:01.182753Z","iopub.status.idle":"2023-04-01T10:28:01.190113Z","shell.execute_reply.started":"2023-04-01T10:28:01.182698Z","shell.execute_reply":"2023-04-01T10:28:01.188743Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"1. We can expect the page0 open more than page1, page2 open more page3 etc.\n2. we may not get to know if reader is reading page0 or page1 since they are both visible to user.","metadata":{}},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, ax = plt.subplots(1, 3, figsize=(15, 5), sharey=True)\nfor i, l in enumerate(['0-4', '5-12', '13-22']):\n    tmp_df = page_df[(page_df['name']=='open') & \n                     (page_df.level_group==l)]\n    s = tmp_df.page.value_counts()\n    \n    plt.suptitle(\"page open counts\")\n    sns.barplot(x=s.index.values, y=s.values, ax=ax[i])\n    ax[i].set_title(l)\n    ax[i].set_xlabel(\"page\")\n    if i==0:\n        ax[i].set_ylabel(\"count\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-04-01T10:28:01.191675Z","iopub.execute_input":"2023-04-01T10:28:01.192242Z","iopub.status.idle":"2023-04-01T10:28:01.576505Z","shell.execute_reply.started":"2023-04-01T10:28:01.192205Z","shell.execute_reply":"2023-04-01T10:28:01.574460Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"1. hypothesis is for page views went wrong\n2. for level group(13-22), page 0-3 got lower number of page opens. \n    i. 1st 2 pages are for rescuing Teddy, It makes the student involved, but not directly related to the end task.","metadata":{}},{"cell_type":"code","source":"page_freq_df = page_df[page_df['name']=='open'].groupby([\"session_id\", 'level_group', 'page'])[['level']].count().rename(columns={'level': 'num_opens'}).reset_index()\npage_freq_df = page_freq_df[page_freq_df.num_opens!=0]\npage_freq_df.head()","metadata":{"execution":{"iopub.status.busy":"2023-04-01T10:32:51.732983Z","iopub.execute_input":"2023-04-01T10:32:51.733415Z","iopub.status.idle":"2023-04-01T10:32:53.967704Z","shell.execute_reply.started":"2023-04-01T10:32:51.733360Z","shell.execute_reply":"2023-04-01T10:32:53.966286Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"page_freq_df[page_freq_df.level_group == '0-4']['num_opens'].hist(bins=100)","metadata":{"execution":{"iopub.status.busy":"2023-04-01T10:37:29.163683Z","iopub.execute_input":"2023-04-01T10:37:29.164096Z","iopub.status.idle":"2023-04-01T10:37:29.543541Z","shell.execute_reply.started":"2023-04-01T10:37:29.164060Z","shell.execute_reply":"2023-04-01T10:37:29.542437Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_group_level(q):\n    qno = int(q[1:])\n    if qno < 4:\n        return '0-4'\n    elif qno < 14:\n        return '5-12'\n    return '13-22'\n\ntrain_label = pd.read_csv(\"/kaggle/input/predict-student-performance-from-game-play/train_labels.csv\")\ntrain_label['q'] = train_label['session_id'].apply(lambda s: s.split(\"_\")[-1])\ntrain_label['session_id'] = train_label['session_id'].apply(lambda s: int(s.split(\"_\")[0]))\ntrain_label['level_group'] = train_label.q.apply(get_group_level)\n\ntrain_label.head()","metadata":{"execution":{"iopub.status.busy":"2023-04-01T10:28:17.202193Z","iopub.execute_input":"2023-04-01T10:28:17.202652Z","iopub.status.idle":"2023-04-01T10:28:18.851740Z","shell.execute_reply.started":"2023-04-01T10:28:17.202613Z","shell.execute_reply":"2023-04-01T10:28:18.850515Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_label[train_label.session_id == 20090312433251036]","metadata":{"execution":{"iopub.status.busy":"2023-04-01T10:34:13.788827Z","iopub.execute_input":"2023-04-01T10:34:13.789243Z","iopub.status.idle":"2023-04-01T10:34:13.804751Z","shell.execute_reply.started":"2023-04-01T10:34:13.789207Z","shell.execute_reply":"2023-04-01T10:34:13.803363Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sids = train_label[train_label.correct==0]['session_id'].unique()\n","metadata":{"execution":{"iopub.status.busy":"2023-04-01T10:39:14.772220Z","iopub.execute_input":"2023-04-01T10:39:14.772698Z","iopub.status.idle":"2023-04-01T10:39:14.791140Z","shell.execute_reply.started":"2023-04-01T10:39:14.772656Z","shell.execute_reply":"2023-04-01T10:39:14.789953Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sids.shape","metadata":{"execution":{"iopub.status.busy":"2023-04-01T10:44:46.185363Z","iopub.execute_input":"2023-04-01T10:44:46.185787Z","iopub.status.idle":"2023-04-01T10:44:46.192448Z","shell.execute_reply.started":"2023-04-01T10:44:46.185750Z","shell.execute_reply":"2023-04-01T10:44:46.191548Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nsessions = train_df.session_id.nunique()\npos_cnt = nsessions-len(sids)\nneg_cnt  =len(sids)\n\nprint(nsessions, len(sids))\nprint(pos_cnt)\n\npage_freq_df[(~page_freq_df.session_id.isin(sids)) & \n             (page_freq_df.level_group == '0-4') &\n             (page_freq_df.num_opens == 1)\n            ]['num_opens'].sum()/pos_cnt","metadata":{"execution":{"iopub.status.busy":"2023-04-01T10:46:19.266141Z","iopub.execute_input":"2023-04-01T10:46:19.266603Z","iopub.status.idle":"2023-04-01T10:46:19.434154Z","shell.execute_reply.started":"2023-04-01T10:46:19.266556Z","shell.execute_reply":"2023-04-01T10:46:19.433171Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"page_freq_df[(page_freq_df.session_id.isin(sids)) & \n             (page_freq_df.level_group == '0-4') &\n             (page_freq_df.num_opens == 1)\n            ]['num_opens'].sum()/neg_cnt","metadata":{"execution":{"iopub.status.busy":"2023-04-01T10:46:38.084559Z","iopub.execute_input":"2023-04-01T10:46:38.084989Z","iopub.status.idle":"2023-04-01T10:46:38.098414Z","shell.execute_reply.started":"2023-04-01T10:46:38.084953Z","shell.execute_reply":"2023-04-01T10:46:38.097035Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}