{"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 pandas as pd\nimport numpy as np\nimport seaborn as sns","metadata":{"execution":{"iopub.status.busy":"2023-10-04T09:48:38.173728Z","iopub.execute_input":"2023-10-04T09:48:38.174170Z","iopub.status.idle":"2023-10-04T09:48:38.179601Z","shell.execute_reply.started":"2023-10-04T09:48:38.174140Z","shell.execute_reply":"2023-10-04T09:48:38.178661Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tmp = pd.read_csv(\"/kaggle/input/predict-student-performance-from-game-play/train.csv\",usecols=[0])\ntmp = tmp.groupby('session_id').session_id.agg('count')\n\nPIECES = 10\nCHUNK = int( np.ceil(len(tmp)/PIECES) )\n\nreads = []\nskips = [0]\nfor k in range(PIECES):\n    a = k*CHUNK\n    b = (k+1)*CHUNK\n    if b>len(tmp): b=len(tmp)\n    r = tmp.iloc[a:b].sum()\n    reads.append(r)\n    skips.append(skips[-1]+r)\n    \nprint(f'To avoid memory error, we will read train in {PIECES} pieces of sizes:')\nprint(reads)","metadata":{"execution":{"iopub.status.busy":"2023-10-04T09:48:38.181178Z","iopub.execute_input":"2023-10-04T09:48:38.182031Z","iopub.status.idle":"2023-10-04T09:49:15.461828Z","shell.execute_reply.started":"2023-10-04T09:48:38.182002Z","shell.execute_reply":"2023-10-04T09:49:15.460277Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv('/kaggle/input/predict-student-performance-from-game-play/train.csv', nrows=reads[0])\nprint('Train size of first piece:', train.shape )\ntrain.head()\n","metadata":{"execution":{"iopub.status.busy":"2023-10-04T09:49:15.463953Z","iopub.execute_input":"2023-10-04T09:49:15.464289Z","iopub.status.idle":"2023-10-04T09:49:23.967322Z","shell.execute_reply.started":"2023-10-04T09:49:15.464259Z","shell.execute_reply":"2023-10-04T09:49:23.966510Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"targets = pd.read_csv('/kaggle/input/predict-student-performance-from-game-play/train_labels.csv')\nprint(targets.shape)\ntargets.head()","metadata":{"execution":{"iopub.status.busy":"2023-10-04T09:49:23.968818Z","iopub.execute_input":"2023-10-04T09:49:23.969449Z","iopub.status.idle":"2023-10-04T09:49:24.342741Z","shell.execute_reply.started":"2023-10-04T09:49:23.969416Z","shell.execute_reply":"2023-10-04T09:49:24.342024Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.info()","metadata":{"execution":{"iopub.status.busy":"2023-10-04T09:49:24.344861Z","iopub.execute_input":"2023-10-04T09:49:24.345485Z","iopub.status.idle":"2023-10-04T09:49:24.356116Z","shell.execute_reply.started":"2023-10-04T09:49:24.345455Z","shell.execute_reply":"2023-10-04T09:49:24.355278Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['event_name'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2023-10-04T09:49:24.357854Z","iopub.execute_input":"2023-10-04T09:49:24.358472Z","iopub.status.idle":"2023-10-04T09:49:24.582687Z","shell.execute_reply.started":"2023-10-04T09:49:24.358441Z","shell.execute_reply":"2023-10-04T09:49:24.581868Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"event_count=train.groupby(['session_id','event_name']).size().unstack(fill_value=0)\nevent_count.reset_index(inplace=True)\ntrain=pd.merge(train,event_count,on='session_id',how='left')\ntrain.fillna(0,inplace=True)","metadata":{"execution":{"iopub.status.busy":"2023-10-04T09:49:24.584144Z","iopub.execute_input":"2023-10-04T09:49:24.584799Z","iopub.status.idle":"2023-10-04T09:49:27.535876Z","shell.execute_reply.started":"2023-10-04T09:49:24.584768Z","shell.execute_reply":"2023-10-04T09:49:27.534903Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"count of events for each session","metadata":{}},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"train","metadata":{"execution":{"iopub.status.busy":"2023-10-04T09:49:27.537345Z","iopub.execute_input":"2023-10-04T09:49:27.537646Z","iopub.status.idle":"2023-10-04T09:49:28.936636Z","shell.execute_reply.started":"2023-10-04T09:49:27.537620Z","shell.execute_reply":"2023-10-04T09:49:28.935472Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['elapsed_time']=pd.to_datetime(train['elapsed_time'],unit='s')\ntrain.sort_values(by=['session_id','elapsed_time'],inplace=True)\ntrain['event_duration']=train.groupby('session_id')['elapsed_time'].diff().fillna(pd.Timedelta(seconds=0))\nsession_agg = train.groupby('session_id').agg({\n    'event_name': 'count',             \n    'event_duration': 'sum'})\nsession_agg.rename(columns={'event_name':'total_event','event_duration':'total_duration'},inplace=True)\nsession_agg['avg_time_event']=session_agg['total_duration']/session_agg['total_event']\nsession_agg.reset_index(inplace=True)\ntrain=pd.merge(train,session_agg,on='session_id',how='left')","metadata":{"execution":{"iopub.status.busy":"2023-10-04T09:49:28.938143Z","iopub.execute_input":"2023-10-04T09:49:28.938465Z","iopub.status.idle":"2023-10-04T09:49:32.759548Z","shell.execute_reply.started":"2023-10-04T09:49:28.938438Z","shell.execute_reply":"2023-10-04T09:49:32.758584Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"total duration of events and the average time per event.","metadata":{"execution":{"iopub.status.busy":"2023-10-04T09:46:16.445459Z","iopub.execute_input":"2023-10-04T09:46:16.446058Z","iopub.status.idle":"2023-10-04T09:46:17.299297Z","shell.execute_reply.started":"2023-10-04T09:46:16.446026Z","shell.execute_reply":"2023-10-04T09:46:17.298542Z"}}},{"cell_type":"code","source":"train","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2023-10-04T09:49:32.771889Z","iopub.execute_input":"2023-10-04T09:49:32.772377Z","iopub.status.idle":"2023-10-04T09:49:33.949072Z","shell.execute_reply.started":"2023-10-04T09:49:32.772349Z","shell.execute_reply":"2023-10-04T09:49:33.948334Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}