{"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 numpy as np\nimport seaborn as sns\nimport matplotlib.pyplot as plt","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-03-14T07:02:14.959758Z","iopub.execute_input":"2023-03-14T07:02:14.961381Z","iopub.status.idle":"2023-03-14T07:02:14.966546Z","shell.execute_reply.started":"2023-03-14T07:02:14.961315Z","shell.execute_reply":"2023-03-14T07:02:14.965405Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Use Random Model to answer the questions.\n1. use the frequency of the correct answer if question is attempted by the user.\n\n2. for the question(qi) question_mean[i] = sum( correct(question[i]) )/count(question[i])\n\n3. During Evaluation on the train set and submission of the test set, generate a    random uniform number (r = U[0, 1] )\n4. if qi is the question number(i), then if r <= question_mean[i]; --> correct=1 else correct=0","metadata":{}},{"cell_type":"code","source":"train_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'] = train_label['session_id'].apply(lambda s: s.split(\"_\")[0])\n\ntrain_label.head()","metadata":{"execution":{"iopub.status.busy":"2023-03-14T07:02:14.982641Z","iopub.execute_input":"2023-03-14T07:02:14.983155Z","iopub.status.idle":"2023-03-14T07:02:15.516936Z","shell.execute_reply.started":"2023-03-14T07:02:14.983112Z","shell.execute_reply":"2023-03-14T07:02:15.515911Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# EDA ","metadata":{}},{"cell_type":"code","source":"nsessions = train_label.session.nunique()\nnquestions = train_label.q.nunique()\nntrain_labels = len(train_label)\n\nprint(\"number of train labels:\",ntrain_labels)\nprint(\"Number of sessions:\", nsessions)\nprint(\"Number of Questions:\", nquestions)\nprint()\nprint()\nprint(\"Proportion to Number ofquestions answered per student: \", (nsessions * nquestions)/ntrain_labels)","metadata":{"execution":{"iopub.status.busy":"2023-03-14T07:02:15.518641Z","iopub.execute_input":"2023-03-14T07:02:15.519316Z","iopub.status.idle":"2023-03-14T07:02:15.580349Z","shell.execute_reply.started":"2023-03-14T07:02:15.519272Z","shell.execute_reply":"2023-03-14T07:02:15.579173Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"In the given dataset for each session, each student answered all the questions","metadata":{}},{"cell_type":"markdown","source":"# Distribution of correct answers per student","metadata":{}},{"cell_type":"code","source":"qprob_df = train_label.groupby(\"q\")[['correct']].mean().reset_index().sort_values(\"correct\", ascending=False)\nqprob_map={}\n\nfor _,row in qprob_df.iterrows():\n    q=row.q\n    p = row.correct\n    qprob_map[q]=p\nprint(len(qprob_map))","metadata":{"execution":{"iopub.status.busy":"2023-03-14T07:02:15.581692Z","iopub.execute_input":"2023-03-14T07:02:15.582422Z","iopub.status.idle":"2023-03-14T07:02:15.668221Z","shell.execute_reply.started":"2023-03-14T07:02:15.582383Z","shell.execute_reply":"2023-03-14T07:02:15.667197Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"qprob_df.head()","metadata":{"execution":{"iopub.status.busy":"2023-03-14T07:02:15.670348Z","iopub.execute_input":"2023-03-14T07:02:15.671113Z","iopub.status.idle":"2023-03-14T07:02:15.681816Z","shell.execute_reply.started":"2023-03-14T07:02:15.671058Z","shell.execute_reply":"2023-03-14T07:02:15.680838Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# lets try on the train labels the random model","metadata":{}},{"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\n\ntrain_label['pred'] = train_label['q'].apply(lambda k: np.random.uniform() <= qprob_map[k]).astype(int)\ntrain_label['pred_correct'] = (train_label['correct'] == train_label['pred']).astype(int)\ntrain_label['group_level'] = train_label.q.apply(get_group_level)\n\ntrain_label.head()","metadata":{"execution":{"iopub.status.busy":"2023-03-14T07:04:58.928023Z","iopub.execute_input":"2023-03-14T07:04:58.928579Z","iopub.status.idle":"2023-03-14T07:04:59.727732Z","shell.execute_reply.started":"2023-03-14T07:04:58.928536Z","shell.execute_reply":"2023-03-14T07:04:59.726638Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.title(\"distriution of questions answered correctly\")\nsns.barplot(data=qprob_df, x='q', y='correct')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-03-14T07:02:16.374997Z","iopub.execute_input":"2023-03-14T07:02:16.375612Z","iopub.status.idle":"2023-03-14T07:02:16.674596Z","shell.execute_reply.started":"2023-03-14T07:02:16.375569Z","shell.execute_reply":"2023-03-14T07:02:16.673269Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_label.groupby(\"q\")[['pred_correct']].mean().sort_values(\"pred_correct\", ascending=False).plot(kind='bar', title=\"distriution of questions predicted correctly\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-03-14T07:02:16.676236Z","iopub.execute_input":"2023-03-14T07:02:16.676895Z","iopub.status.idle":"2023-03-14T07:02:17.012938Z","shell.execute_reply.started":"2023-03-14T07:02:16.676853Z","shell.execute_reply":"2023-03-14T07:02:17.011857Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_label.groupby('q')[['correct', 'pred_correct']].mean().sort_values(\"correct\", ascending=False).plot(kind='bar')","metadata":{"execution":{"iopub.status.busy":"2023-03-14T07:02:39.392556Z","iopub.execute_input":"2023-03-14T07:02:39.393753Z","iopub.status.idle":"2023-03-14T07:02:39.757361Z","shell.execute_reply.started":"2023-03-14T07:02:39.393692Z","shell.execute_reply":"2023-03-14T07:02:39.755838Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_label.groupby('group_level')[['pred_correct']].mean().plot(kind='bar')","metadata":{"execution":{"iopub.status.busy":"2023-03-14T07:05:48.705438Z","iopub.execute_input":"2023-03-14T07:05:48.705930Z","iopub.status.idle":"2023-03-14T07:05:48.950038Z","shell.execute_reply.started":"2023-03-14T07:05:48.705890Z","shell.execute_reply":"2023-03-14T07:05:48.949056Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import f1_score\n\ngroup_df1 = train_label[train_label.group_level == '0-4'].copy()\ngroup_df2 = train_label[train_label.group_level == '5-12'].copy()\ngroup_df3 = train_label[train_label.group_level == '13-22'].copy()\n\n\nprint(\"All fscore:\", f1_score(train_label['correct'], train_label['pred']))\nprint(\"group_level (0-4) fscore: \", f1_score(group_df1.correct, group_df1.pred) )\nprint(\"group_level (5-12) fscore: \", f1_score(group_df2.correct, group_df2.pred) )\nprint(\"group_level (13-22) fscore: \", f1_score(group_df3.correct, group_df3.pred) )","metadata":{"execution":{"iopub.status.busy":"2023-03-14T07:07:54.355438Z","iopub.execute_input":"2023-03-14T07:07:54.355937Z","iopub.status.idle":"2023-03-14T07:07:54.622094Z","shell.execute_reply.started":"2023-03-14T07:07:54.355898Z","shell.execute_reply":"2023-03-14T07:07:54.620657Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# submission","metadata":{}},{"cell_type":"code","source":"import jo_wilder","metadata":{"execution":{"iopub.status.busy":"2023-03-13T10:11:28.043632Z","iopub.execute_input":"2023-03-13T10:11:28.044418Z","iopub.status.idle":"2023-03-13T10:11:28.069255Z","shell.execute_reply.started":"2023-03-13T10:11:28.044377Z","shell.execute_reply":"2023-03-13T10:11:28.068122Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"env = jo_wilder.make_env()\niter_test = iter(env.iter_test())\n\nfor (submission, test) in iter_test:\n    q = submission.session_id.apply(lambda k: k.split(\"_\")[-1])\n    p = q.apply(lambda k: qprob_map[k])\n    r = np.random.uniform(0, 1, len(q))\n    correct = (r<=p).astype(int)\n    \n    submission['correct'] = correct\n    env.predict(submission)","metadata":{"execution":{"iopub.status.busy":"2023-03-13T10:11:28.074550Z","iopub.execute_input":"2023-03-13T10:11:28.075530Z","iopub.status.idle":"2023-03-13T10:11:28.176723Z","shell.execute_reply.started":"2023-03-13T10:11:28.075486Z","shell.execute_reply":"2023-03-13T10:11:28.175473Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.level_group.unique()","metadata":{"execution":{"iopub.status.busy":"2023-03-13T10:16:51.477667Z","iopub.execute_input":"2023-03-13T10:16:51.478642Z","iopub.status.idle":"2023-03-13T10:16:51.486648Z","shell.execute_reply.started":"2023-03-13T10:16:51.478584Z","shell.execute_reply":"2023-03-13T10:16:51.485772Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.read_csv(\"submission.csv\")","metadata":{"execution":{"iopub.status.busy":"2023-03-13T10:16:03.822380Z","iopub.execute_input":"2023-03-13T10:16:03.822810Z","iopub.status.idle":"2023-03-13T10:16:03.842238Z","shell.execute_reply.started":"2023-03-13T10:16:03.822774Z","shell.execute_reply":"2023-03-13T10:16:03.841258Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}