{"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":"## Import","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport seaborn as sns\nimport matplotlib.pyplot as plt\nimport scipy.stats as stats\nimport plotly.express as px","metadata":{"execution":{"iopub.status.busy":"2023-04-03T11:14:43.020684Z","iopub.execute_input":"2023-04-03T11:14:43.021436Z","iopub.status.idle":"2023-04-03T11:14:47.685743Z","shell.execute_reply.started":"2023-04-03T11:14:43.021393Z","shell.execute_reply":"2023-04-03T11:14:47.684353Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Loading Students Logs","metadata":{}},{"cell_type":"code","source":"use_columns = ['session_id', \n               'index', \n               'elapsed_time', \n               'level_group']\ndtypes={'session_id':'category', \n'elapsed_time':np.int32,\n    'event_name':'category',\n    'name':'category',\n    'level':np.uint8,\n    'page':'category',\n    'room_coor_x':np.float32,\n    'room_coor_y':np.float32,\n    'screen_coor_x':np.float32,\n    'screen_coor_y':np.float32,\n    'hover_duration':np.float32,\n     'text':'category',\n     'fqid':'category',\n     'room_fqid':'category',\n     'text_fqid':'category',\n     'fullscreen':'category',\n     'hq':'category',\n     'music':'category',\n     'level_group':'category'}\ndf = pd.read_csv('/kaggle/input/predict-student-performance-from-game-play/train.csv', \n                 dtype=dtypes, usecols=use_columns)","metadata":{"execution":{"iopub.status.busy":"2023-04-03T11:14:47.690790Z","iopub.execute_input":"2023-04-03T11:14:47.691541Z","iopub.status.idle":"2023-04-03T11:16:27.880096Z","shell.execute_reply.started":"2023-04-03T11:14:47.691493Z","shell.execute_reply":"2023-04-03T11:16:27.878433Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"q_inf = df[[\"session_id\", \"elapsed_time\", 'level_group' ]].copy()\nq_inf.head()","metadata":{"execution":{"iopub.status.busy":"2023-04-03T11:16:27.881957Z","iopub.execute_input":"2023-04-03T11:16:27.882321Z","iopub.status.idle":"2023-04-03T11:16:28.125622Z","shell.execute_reply.started":"2023-04-03T11:16:27.882287Z","shell.execute_reply":"2023-04-03T11:16:28.123867Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Time between events","metadata":{}},{"cell_type":"code","source":"q_inf['previous_elapsed_time'] = q_inf['elapsed_time'].shift()\nq_inf['time_from_previous'] = q_inf['elapsed_time'] - q_inf['previous_elapsed_time']\nq_inf['previous_session_id'] = q_inf['session_id'].shift()\nq_inf['is_from_previous'] = q_inf['session_id'] == q_inf['previous_session_id']\nq_inf['is_from_previous'] = q_inf['is_from_previous'].map({False:np.NaN, True:1})\nq_inf['time_from_previous'] = q_inf['time_from_previous'] * q_inf['is_from_previous']\nq_inf.head(10)\n","metadata":{"execution":{"iopub.status.busy":"2023-04-03T11:16:28.127309Z","iopub.execute_input":"2023-04-03T11:16:28.127811Z","iopub.status.idle":"2023-04-03T11:16:32.441076Z","shell.execute_reply.started":"2023-04-03T11:16:28.127773Z","shell.execute_reply":"2023-04-03T11:16:32.439870Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ik = np.log(q_inf.groupby(['level_group'])['time_from_previous'].mean()+2)\nik","metadata":{"execution":{"iopub.status.busy":"2023-04-03T11:16:32.443867Z","iopub.execute_input":"2023-04-03T11:16:32.444913Z","iopub.status.idle":"2023-04-03T11:16:32.744775Z","shell.execute_reply.started":"2023-04-03T11:16:32.444870Z","shell.execute_reply":"2023-04-03T11:16:32.743746Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"q_inf['time_from_previous_log']=np.log(q_inf['time_from_previous']+2)\nq_low = q_inf['time_from_previous'].quantile(0.05)\nq_max = q_inf['time_from_previous'].quantile(0.95)\nq_inf['time_from_previous_tream'] = q_inf[(q_inf['time_from_previous']>=q_low) & (q_inf['time_from_previous']<=q_max)]['time_from_previous']","metadata":{"execution":{"iopub.status.busy":"2023-04-03T11:26:00.503941Z","iopub.execute_input":"2023-04-03T11:26:00.504727Z","iopub.status.idle":"2023-04-03T11:26:07.921797Z","shell.execute_reply.started":"2023-04-03T11:26:00.504687Z","shell.execute_reply":"2023-04-03T11:26:07.920568Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.histplot(data = q_inf, x = 'time_from_previous_tream')","metadata":{"execution":{"iopub.status.busy":"2023-04-03T11:16:42.430270Z","iopub.execute_input":"2023-04-03T11:16:42.431008Z","iopub.status.idle":"2023-04-03T11:17:08.087238Z","shell.execute_reply.started":"2023-04-03T11:16:42.430967Z","shell.execute_reply":"2023-04-03T11:17:08.085563Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"q_inf['time_from_previous'].quantile(0.99)","metadata":{"execution":{"iopub.status.busy":"2023-04-03T11:17:08.090003Z","iopub.execute_input":"2023-04-03T11:17:08.090885Z","iopub.status.idle":"2023-04-03T11:17:09.194179Z","shell.execute_reply.started":"2023-04-03T11:17:08.090844Z","shell.execute_reply":"2023-04-03T11:17:09.189348Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Cohort Study","metadata":{}},{"cell_type":"code","source":"ik = np.log(q_inf.groupby(['session_id'])['elapsed_time'].max()+2)\n\npx.histogram(ik)","metadata":{"execution":{"iopub.status.busy":"2023-04-03T11:17:09.199393Z","iopub.execute_input":"2023-04-03T11:17:09.206958Z","iopub.status.idle":"2023-04-03T11:17:13.624130Z","shell.execute_reply.started":"2023-04-03T11:17:09.206434Z","shell.execute_reply":"2023-04-03T11:17:13.622917Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Consider this group of users. Since they stand out sharply on the graph\n<img src=\"https://i.ibb.co/3kKFRDV/What-happens-in-this-interval.png\">","metadata":{}},{"cell_type":"code","source":"session_length = q_inf.groupby(['session_id'])['elapsed_time'].max()\nsession_length = session_length.to_frame()\nunclear_time = session_length[(session_length.elapsed_time > 53757835) & (session_length.elapsed_time < 119640264)]\nunclear_time.head()\n","metadata":{"execution":{"iopub.status.busy":"2023-04-03T11:17:13.625685Z","iopub.execute_input":"2023-04-03T11:17:13.626168Z","iopub.status.idle":"2023-04-03T11:17:14.331131Z","shell.execute_reply.started":"2023-04-03T11:17:13.626125Z","shell.execute_reply":"2023-04-03T11:17:14.329737Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"For etolon users, we will take this hill\n\n<img src=\"https://i.ibb.co/PQW742Z/how-much-is-spent-on-the-game-as-a-whole.png\">","metadata":{}},{"cell_type":"code","source":"clear_time = session_length[(session_length.elapsed_time < 14650719)]\nclear_time.head()","metadata":{"execution":{"iopub.status.busy":"2023-04-03T11:17:50.634684Z","iopub.execute_input":"2023-04-03T11:17:50.635470Z","iopub.status.idle":"2023-04-03T11:17:55.110940Z","shell.execute_reply.started":"2023-04-03T11:17:50.635426Z","shell.execute_reply":"2023-04-03T11:17:55.109902Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"session_length_clear = q_inf[q_inf.session_id.isin(clear_time.index)]\nsession_length_clear[\"elapsed_time\"] = np.log(session_length_clear['elapsed_time']+1)\nsession_length_clear.head()","metadata":{"execution":{"iopub.status.busy":"2023-04-03T11:17:48.912585Z","iopub.execute_input":"2023-04-03T11:17:48.913654Z","iopub.status.idle":"2023-04-03T11:17:48.927349Z","shell.execute_reply.started":"2023-04-03T11:17:48.913599Z","shell.execute_reply":"2023-04-03T11:17:48.926229Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ik = np.log(session_length_clear.groupby(['level_group'])['time_from_previous'].mean()+2)\nik","metadata":{"execution":{"iopub.status.busy":"2023-04-03T11:18:02.205634Z","iopub.execute_input":"2023-04-03T11:18:02.206105Z","iopub.status.idle":"2023-04-03T11:18:02.504664Z","shell.execute_reply.started":"2023-04-03T11:18:02.206066Z","shell.execute_reply":"2023-04-03T11:18:02.502954Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"session_length_sussy = q_inf[q_inf.session_id.isin(unclear_time.index)]\nsession_length_sussy[\"elapsed_time\"] = np.log(session_length_sussy['elapsed_time']+1)\nsession_length_sussy.head()","metadata":{"execution":{"iopub.status.busy":"2023-04-03T11:18:04.534133Z","iopub.execute_input":"2023-04-03T11:18:04.534595Z","iopub.status.idle":"2023-04-03T11:18:04.858919Z","shell.execute_reply.started":"2023-04-03T11:18:04.534555Z","shell.execute_reply":"2023-04-03T11:18:04.857697Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ik = np.log(session_length_sussy.groupby(['level_group'])['time_from_previous'].mean()+2)\nik","metadata":{"execution":{"iopub.status.busy":"2023-04-03T11:18:07.441466Z","iopub.execute_input":"2023-04-03T11:18:07.441908Z","iopub.status.idle":"2023-04-03T11:18:07.467961Z","shell.execute_reply.started":"2023-04-03T11:18:07.441872Z","shell.execute_reply":"2023-04-03T11:18:07.466643Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Consider the behavior of suspicious users. After all, it is not clear whether they went to afk, or in general they take longer to answer","metadata":{}},{"cell_type":"markdown","source":"\nTo do this, look at the average and maximum","metadata":{}},{"cell_type":"code","source":"session_length_sussy_standard_deviation = session_length_sussy.groupby(['session_id'],observed=True)['time_from_previous'].max()\nsession_length_sussy_standard_deviation","metadata":{"execution":{"iopub.status.busy":"2023-04-03T11:40:40.984851Z","iopub.execute_input":"2023-04-03T11:40:40.985387Z","iopub.status.idle":"2023-04-03T11:40:41.047664Z","shell.execute_reply.started":"2023-04-03T11:40:40.985329Z","shell.execute_reply":"2023-04-03T11:40:41.046211Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"session_length_sussy_mean = session_length_sussy.groupby(['session_id'],observed=True)['time_from_previous_tream'].mean()\nsession_length_sussy_mean","metadata":{"execution":{"iopub.status.busy":"2023-04-03T12:09:42.865199Z","iopub.execute_input":"2023-04-03T12:09:42.865654Z","iopub.status.idle":"2023-04-03T12:09:42.922525Z","shell.execute_reply.started":"2023-04-03T12:09:42.865616Z","shell.execute_reply":"2023-04-03T12:09:42.921288Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Summing up the micro-summary, we can make an approximate conclusion that all users that we have identified as suspicious are afk players. And also those who are to the right of them on our histogram. It is difficult to deduce any definite criterion when we can say that a player has definitely gone into afk. However, during the research, some questions arose. For example, why does the data jump so much in the first graph? Express your guesses and assumptions","metadata":{}}]}