{"cells":[{"metadata":{},"cell_type":"markdown","source":"# Riid! Correct Answers Prediction\n\nWe need pandas, numpy, datatable, and a few other libraries.\nWe also need riiideducation for this particular competititon."},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"#!pip install datatable\n\nimport riiideducation\nimport numpy as np\nimport pandas as pd\nfrom matplotlib import pyplot as plt\n#import datatable as dt\nfrom sklearn import metrics","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Get the training data. It's large, so this may take a while."},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"%%time\n\ndtypes ={'row_id': 'int64', \n         'timestamp': 'int64', \n         'user_id': 'int32', \n         'content_id': 'int16', \n         'content_type_id': 'int8',\n         'task_container_id': 'int16', \n         'user_answer': 'int8', \n         'answered_correctly': 'int8', \n         'prior_question_elapsed_time': 'float32', \n         'prior_question_had_explanation': 'boolean',\n        }\n\n#train_df = dt.fread('../input/riiid-test-answer-prediction/train.csv').to_pandas()\ndata_df = pd.read_csv('../input/riiid-test-answer-prediction/train.csv', nrows=150000, dtype=dtypes)\nprint(\"All data:\", data_df.shape)\n\ntrain_df = data_df[:100000]\nvalid_df = data_df[100000:]\nprint(\"Train:\", train_df.shape)\nprint(\"Valid:\", valid_df.shape)\n\ntrain_df = train_df.drop(train_df[train_df['answered_correctly']==-1].index)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Define some functions to get test data and labels."},{"metadata":{"trusted":true},"cell_type":"code","source":"def get_input(df):\n    return df[['timestamp', 'content_id', 'content_type_id', \n                       'task_container_id', 'prior_question_elapsed_time', \n                       'prior_question_had_explanation']].fillna(0)\n\ndef get_labels(df):\n    return df['answered_correctly']","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Random Forest Model"},{"metadata":{"trusted":true},"cell_type":"code","source":"from sklearn.ensemble import RandomForestClassifier\n\nmodel = RandomForestClassifier()\n\ndef train_model():\n    model.fit(get_input(train_df), get_labels(train_df))\n    train_score = model.score(get_input(train_df), get_labels(train_df))\n    valid_score = model.score(get_input(valid_df), get_labels(valid_df))\n    print('Train score:', train_score)\n    print('Valid score:', valid_score)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Now we train our model:"},{"metadata":{"trusted":true},"cell_type":"code","source":"model.fit(get_input(train_df), get_labels(train_df))","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"And we check the accuracy:"},{"metadata":{"trusted":true},"cell_type":"code","source":"train_score = model.score(get_input(train_df), get_labels(train_df))\nvalid_score = model.score(get_input(valid_df), get_labels(valid_df))\nprint('Train score:', train_score)\nprint('Valid score:', valid_score)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Test (Validate) Model\n\nWe can start an environment and go through iter_test exactly once in the notebook. This is a dummy prediction, to test a submission."},{"metadata":{"trusted":true},"cell_type":"code","source":"env = riiideducation.make_env()\niter_test = env.iter_test()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"for (test_df, sample_prediction_df) in iter_test:\n    test_df['answered_correctly'] = model.predict(get_input(test_df))\n    env.predict(test_df.loc[test_df['content_type_id'] == 0, ['row_id', 'answered_correctly']])","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":4}