{"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":"# In this example notebook, the quantity and frequency of actions taken by a student in the past is used to predict their score on every problem within their unit test. This method does not take into account the differences between problems, which is not a good idea, and therefore makes the same prediction for every problem within a students' unit test.","metadata":{}},{"cell_type":"code","source":"# Import packages\n\nimport numpy as np\nimport pandas as pd\nfrom sklearn.linear_model import LogisticRegression\nfrom sklearn.model_selection import cross_val_score","metadata":{"execution":{"iopub.status.busy":"2023-03-30T14:06:33.018512Z","iopub.execute_input":"2023-03-30T14:06:33.018978Z","iopub.status.idle":"2023-03-30T14:06:33.692250Z","shell.execute_reply.started":"2023-03-30T14:06:33.018930Z","shell.execute_reply":"2023-03-30T14:06:33.690908Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Load the necessary data\n\ntuts = pd.read_csv('/kaggle/input/edm-cup-2023/training_unit_test_scores.csv')\neuts = pd.read_csv('/kaggle/input/edm-cup-2023/evaluation_unit_test_scores.csv')\nar = pd.read_csv('/kaggle/input/edm-cup-2023/assignment_relationships.csv')\nal = pd.read_csv('/kaggle/input/edm-cup-2023/action_logs.csv')","metadata":{"execution":{"iopub.status.busy":"2023-03-30T14:06:33.694242Z","iopub.execute_input":"2023-03-30T14:06:33.694595Z","iopub.status.idle":"2023-03-30T14:07:16.421819Z","shell.execute_reply.started":"2023-03-30T14:06:33.694562Z","shell.execute_reply":"2023-03-30T14:07:16.420793Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Associate the action logs for each in unit assignment with their unit test assignment\ndf = ar.merge(al, how='left', left_on='in_unit_assignment_log_id', right_on='assignment_log_id')\ndf = df[['unit_test_assignment_log_id', 'action']]\n\n# Get the total number of times each action was taken within the in unit assignments corresponding to each unit test assignment\ndf = pd.get_dummies(df, columns=['action'])\ndf = df.groupby('unit_test_assignment_log_id').sum()\n\n# Create a feature for the total action count, then scale it between 0 and 1\naction_count = df.sum(axis=1)\n\n# Convert the individual action counts into a fraction of total actions taken\ndf = df.div(action_count, axis=0)\n\n# Add the scaled total action count to the dataframe\ndf['action_count'] = (action_count - action_count.min()) / (action_count.max() - action_count.min())\n\n# Merge action count features with the training unit test scores\ntuts = tuts.merge(df, how='left', left_on='assignment_log_id', right_index=True)\n\n# Merge action count features with the evaluation unit test scores\neuts = euts.merge(df, how='left', left_on='assignment_log_id', right_index=True)","metadata":{"execution":{"iopub.status.busy":"2023-03-30T14:07:16.424108Z","iopub.execute_input":"2023-03-30T14:07:16.425354Z","iopub.status.idle":"2023-03-30T14:08:04.107399Z","shell.execute_reply.started":"2023-03-30T14:07:16.425301Z","shell.execute_reply":"2023-03-30T14:08:04.106202Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Collect the input and target columns for the regression\ninput_cols = [c for c in tuts.columns if 'action' in c]\ntarget_col = 'score'\n\n# Initialize a logistic regression\nlr = LogisticRegression(max_iter=1000)\n# Fit the regression on all the training data\nlr = lr.fit(tuts[input_cols], tuts[target_col])\n# Predict the score for each evaluation problem\neuts[target_col] = lr.predict_proba(euts[input_cols])[:,1]\n\n# Export the id and score columns of the evaluation unit test scores file for uploading to Kaggle\neuts[['id', 'score']].to_csv('/kaggle/working/example_submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2023-03-30T14:08:22.484927Z","iopub.execute_input":"2023-03-30T14:08:22.485373Z","iopub.status.idle":"2023-03-30T14:08:37.751562Z","shell.execute_reply.started":"2023-03-30T14:08:22.485336Z","shell.execute_reply":"2023-03-30T14:08:37.750206Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}