{"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 dask.dataframe as dd\nimport numpy as np\nimport matplotlib.pyplot as plt\nfrom sklearn.linear_model import LogisticRegression\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.preprocessing import StandardScaler","metadata":{"execution":{"iopub.status.busy":"2023-05-10T07:26:08.817066Z","iopub.execute_input":"2023-05-10T07:26:08.817635Z","iopub.status.idle":"2023-05-10T07:26:10.151195Z","shell.execute_reply.started":"2023-05-10T07:26:08.817526Z","shell.execute_reply":"2023-05-10T07:26:10.149878Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_labels = pd.read_csv('/kaggle/input/predict-student-performance-from-game-play/train_labels.csv')","metadata":{"execution":{"iopub.status.busy":"2023-05-10T07:26:10.153245Z","iopub.execute_input":"2023-05-10T07:26:10.153787Z","iopub.status.idle":"2023-05-10T07:26:10.667116Z","shell.execute_reply.started":"2023-05-10T07:26:10.153748Z","shell.execute_reply":"2023-05-10T07:26:10.665902Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dtypes = {\n    'elapsed_time': np.int32,\n    'event_name': 'category',\n    'name': 'category',\n    'level': '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'\n}\n# Load the data using dask\ndf_train = dd.read_csv(\"/kaggle/input/predict-student-performance-from-game-play/train.csv\",\n                       dtype=dtypes)\nframe = df_train.compute()\n# frame = frame.drop(columns=['fullscreen', 'hq', 'music'])","metadata":{"execution":{"iopub.status.busy":"2023-05-10T07:26:10.668620Z","iopub.execute_input":"2023-05-10T07:26:10.669023Z","iopub.status.idle":"2023-05-10T07:27:31.965141Z","shell.execute_reply.started":"2023-05-10T07:26:10.668987Z","shell.execute_reply":"2023-05-10T07:27:31.964168Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def count_string_occurrences(s, string):\n    return (s == string).sum()\n\ndef get_features(frame):\n    cols = ['session_id',\n     'tunic.historicalsociety.cage',\n     'tunic.historicalsociety.closet_dirty',\n     'tunic.historicalsociety.entry',\n     'tunic.historicalsociety.frontdesk',\n     'tunic.historicalsociety.stacks',\n     'tunic.humanecology.frontdesk',\n     'tunic.kohlcenter.halloffame',\n     'tunic.library.frontdesk',\n     'tunic.library.microfiche',\n     'tunic.wildlife.center',\n     'event_nb',\n     'cutscene',\n     'navigate']\n    out = pd.DataFrame(columns=cols)\n    frame = frame.drop(columns=['fullscreen', 'hq', 'music'])\n    cut_nav_events = frame\n    cut_nav_events['cutscene'] = cut_nav_events['event_name']\n    cut_nav_events['navigate'] = cut_nav_events['event_name']\n\n    cut_nav_events = frame.groupby('session_id').agg({'cutscene': lambda x: count_string_occurrences(x, 'cutscene_click'), 'navigate': lambda x: count_string_occurrences(x, 'navigate_click')})\n    session_room_events = frame.groupby(['session_id', 'room_fqid']).agg({'session_id': ['count']})\n    session_room_events.columns=['event_nb']\n    session_room_events = frame.pivot_table(index=['session_id'], columns='room_fqid', values='event_name', aggfunc='count', fill_value=0)\n    useless = ['tunic.capitol_0.hall', 'tunic.capitol_1.hall','tunic.capitol_2.hall', 'tunic.drycleaner.frontdesk', 'tunic.flaghouse.entry', 'tunic.historicalsociety.basement', 'tunic.historicalsociety.closet', 'tunic.historicalsociety.collection_flag', 'tunic.historicalsociety.collection']\n    session_room_events = session_room_events.drop([col for col in useless if col in session_room_events], axis=1)\n    session_events = frame.groupby('session_id').agg({'session_id': ['count']})\n    session_events.columns=['event_nb']\n    room_total_events = session_room_events.merge(session_events, on='session_id', how='inner')\n    room_total_events = room_total_events.reset_index()\n    data = room_total_events.merge(cut_nav_events, on='session_id', how='inner')\n    out = out.append(data)\n    out = out.fillna(0)\n    return out","metadata":{"execution":{"iopub.status.busy":"2023-05-10T07:27:31.967708Z","iopub.execute_input":"2023-05-10T07:27:31.968386Z","iopub.status.idle":"2023-05-10T07:27:31.982121Z","shell.execute_reply.started":"2023-05-10T07:27:31.968348Z","shell.execute_reply":"2023-05-10T07:27:31.979698Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"get_features(frame)","metadata":{"execution":{"iopub.status.busy":"2023-05-10T07:27:31.983614Z","iopub.execute_input":"2023-05-10T07:27:31.984057Z","iopub.status.idle":"2023-05-10T07:27:53.338561Z","shell.execute_reply.started":"2023-05-10T07:27:31.984021Z","shell.execute_reply":"2023-05-10T07:27:53.337139Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_labels['session'] = df_labels.session_id.apply(lambda x: int(x.split('_')[0]))\ndf_labels['q'] = df_labels.session_id.apply(lambda x: int(x.split('_')[-1][1:]))\ndf_labels = df_labels.drop('session_id', axis=1)\ndf_labels = df_labels.rename(columns={'session': 'session_id'})","metadata":{"execution":{"iopub.status.busy":"2023-05-10T07:27:53.341045Z","iopub.execute_input":"2023-05-10T07:27:53.341457Z","iopub.status.idle":"2023-05-10T07:27:54.161326Z","shell.execute_reply.started":"2023-05-10T07:27:53.341414Z","shell.execute_reply":"2023-05-10T07:27:54.156098Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_labels_q_score = df_labels.pivot_table(index='session_id', columns='q', values='correct', aggfunc='sum', fill_value=0)\n\n\n# Rename the columns to indicate they represent questions\ndf_labels_q_score.columns = ['q' + str(col) for col in df_labels_q_score.columns]\n\n# Reset the index to include the session_id column\ndf_labels_q_score = df_labels_q_score.reset_index()","metadata":{"execution":{"iopub.status.busy":"2023-05-10T07:27:54.166747Z","iopub.execute_input":"2023-05-10T07:27:54.167404Z","iopub.status.idle":"2023-05-10T07:27:54.442170Z","shell.execute_reply.started":"2023-05-10T07:27:54.167291Z","shell.execute_reply":"2023-05-10T07:27:54.441040Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# data = room_total_events.merge(df_labels_q_score, on='session_id', how='inner')\nfeats = get_features(frame)\ncols = feats.columns\ndata = df_labels_q_score.merge(feats, on='session_id', how='inner')\n\n# data = df_q_score_merge\ndata","metadata":{"execution":{"iopub.status.busy":"2023-05-10T07:27:54.443654Z","iopub.execute_input":"2023-05-10T07:27:54.444010Z","iopub.status.idle":"2023-05-10T07:28:16.327776Z","shell.execute_reply.started":"2023-05-10T07:27:54.443979Z","shell.execute_reply":"2023-05-10T07:28:16.326685Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cols.tolist()","metadata":{"execution":{"iopub.status.busy":"2023-05-10T07:28:16.329330Z","iopub.execute_input":"2023-05-10T07:28:16.329908Z","iopub.status.idle":"2023-05-10T07:28:16.336246Z","shell.execute_reply.started":"2023-05-10T07:28:16.329871Z","shell.execute_reply":"2023-05-10T07:28:16.335352Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Preprocess data\nresult = []\nqs = ['q1', 'q2', 'q3', 'q4', 'q5', 'q6', 'q7', 'q8', 'q9', 'q10', 'q11', 'q12', 'q13', 'q14', 'q15', 'q16', 'q17', 'q18']\nmodels = []\nfor q in qs:\n    X = data.drop(qs, axis=1)\n    y = data[q]\n    scaler = StandardScaler()\n    X_train = scaler.fit_transform(X)\n    X_test = scaler.transform(X)\n    y_train = y\n    y_test = y\n\n    # Create logistic regression model\n    clf = LogisticRegression(random_state=32)\n\n    # Train the model\n    clf.fit(X_train, y_train)\n    \n    models.append(clf)\n\n    # Test the model\n    y_pred = clf.predict(X_test)\n\n    # Evaluate the performance of the model\n    accuracy = clf.score(X_test, y_test)\n    # print(\"Accuracy:\", accuracy)\n    result.append(accuracy)\nprint(\"Average accuracy:\", sum(result) / len(result))","metadata":{"execution":{"iopub.status.busy":"2023-05-10T07:51:39.545732Z","iopub.execute_input":"2023-05-10T07:51:39.546224Z","iopub.status.idle":"2023-05-10T07:51:40.761238Z","shell.execute_reply.started":"2023-05-10T07:51:39.546186Z","shell.execute_reply":"2023-05-10T07:51:40.760318Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import jo_wilder\nenv = jo_wilder.make_env()\niter_test = env.iter_test()\nfor (test, sample_submission) in iter_test:\n    \n    test_df = test\n    features = get_features(test_df)\n    d = []\n    \n    session_ids = sample_submission['session_id'].values\n    \n    for row in features.itertuples():\n        print(row[1])\n        sesh = row[1]\n        for i in range(1,19):\n            if not (f'{sesh}_q{i}' in session_ids):\n                continue\n            d.append(\n                {\n                    \"session_id\": f'{sesh}_q{i}',\n                    \"correct\": models[i-1].predict(np.array([row[1:]]))[0]\n                }        \n            )\n\n\n    predict = pd.DataFrame(d)\n#     print(predict)\n    env.predict(predict)","metadata":{"execution":{"iopub.status.busy":"2023-05-10T07:28:17.402323Z","iopub.execute_input":"2023-05-10T07:28:17.403206Z","iopub.status.idle":"2023-05-10T07:28:17.818636Z","shell.execute_reply.started":"2023-05-10T07:28:17.403157Z","shell.execute_reply":"2023-05-10T07:28:17.816041Z"},"trusted":true},"execution_count":null,"outputs":[]}]}