{"cells":[{"metadata":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","execution":{"iopub.execute_input":"2021-01-05T03:45:45.120223Z","iopub.status.busy":"2021-01-05T03:45:45.119161Z","iopub.status.idle":"2021-01-05T03:45:45.121699Z","shell.execute_reply":"2021-01-05T03:45:45.122345Z"},"papermill":{"duration":0.040039,"end_time":"2021-01-05T03:45:45.122513","exception":false,"start_time":"2021-01-05T03:45:45.082474","status":"completed"},"tags":[],"trusted":false},"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\n#import os\n#for dirname, _, filenames in os.walk('/kaggle/input'):\n#    for filename in filenames:\n#        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2021-01-05T03:45:45.195141Z","iopub.status.busy":"2021-01-05T03:45:45.194322Z","iopub.status.idle":"2021-01-05T03:45:51.675206Z","shell.execute_reply":"2021-01-05T03:45:51.673727Z"},"papermill":{"duration":6.524061,"end_time":"2021-01-05T03:45:51.675325","exception":false,"start_time":"2021-01-05T03:45:45.151264","status":"completed"},"tags":[],"trusted":false},"cell_type":"code","source":"import sys\n!cp ../input/rapids/rapids.0.15.0 /opt/conda/envs/rapids.tar.gz\n!cd /opt/conda/envs/ && tar -xzvf rapids.tar.gz > /dev/null\nsys.path = [\"/opt/conda/envs/rapids/lib/python3.7/site-packages\"] + sys.path\nsys.path = [\"/opt/conda/envs/rapids/lib/python3.7\"] + sys.path\nsys.path = [\"/opt/conda/envs/rapids/lib\"] + sys.path \n!cp /opt/conda/envs/rapids/lib/libxgboost.so /opt/conda/lib/\n\n# Rapids Imports\nimport cudf\nimport cupy # CuPy is an open-source array library accelerated with NVIDIA CUDA.","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","execution":{"iopub.execute_input":"2021-01-05T03:45:51.742090Z","iopub.status.busy":"2021-01-05T03:45:51.741472Z","iopub.status.idle":"2021-01-05T03:53:57.827350Z","shell.execute_reply":"2021-01-05T03:53:57.828132Z"},"papermill":{"duration":486.12417,"end_time":"2021-01-05T03:53:57.828296","exception":false,"start_time":"2021-01-05T03:45:51.704126","status":"completed"},"tags":[],"trusted":false},"cell_type":"code","source":"%%time\n#get the data\nimport numpy as np\nimport pandas as pd\ndtypes = {\n    \"row_id\": \"int64\",\n    \"timestamp\": \"int64\",\n    \"user_id\": \"int32\",\n    \"content_id\": \"int16\",\n    \"content_type_id\": \"boolean\",\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\ntrain = pd.read_csv('/kaggle/input/riiid-test-answer-prediction/train.csv',dtype=dtypes)\ntrain = train.iloc[:1500000]\nprint('Loaded dataset!')","execution_count":null,"outputs":[]},{"metadata":{"papermill":{"duration":0.069869,"end_time":"2021-01-05T03:53:57.928291","exception":false,"start_time":"2021-01-05T03:53:57.858422","status":"completed"},"tags":[]},"cell_type":"markdown","source":"## look at the values in answered_correctly\n### reasons will be explained later"},{"metadata":{"execution":{"iopub.execute_input":"2021-01-05T03:53:58.006057Z","iopub.status.busy":"2021-01-05T03:53:58.005139Z","iopub.status.idle":"2021-01-05T03:53:58.032447Z","shell.execute_reply":"2021-01-05T03:53:58.031950Z"},"papermill":{"duration":0.071717,"end_time":"2021-01-05T03:53:58.032546","exception":false,"start_time":"2021-01-05T03:53:57.960829","status":"completed"},"tags":[],"trusted":false},"cell_type":"code","source":"train.answered_correctly.value_counts()","execution_count":null,"outputs":[]},{"metadata":{"papermill":{"duration":0.030444,"end_time":"2021-01-05T03:53:58.092903","exception":false,"start_time":"2021-01-05T03:53:58.062459","status":"completed"},"tags":[]},"cell_type":"markdown","source":"## do the same for user_answer "},{"metadata":{"execution":{"iopub.execute_input":"2021-01-05T03:53:58.159038Z","iopub.status.busy":"2021-01-05T03:53:58.158173Z","iopub.status.idle":"2021-01-05T03:53:58.179643Z","shell.execute_reply":"2021-01-05T03:53:58.178966Z"},"papermill":{"duration":0.056493,"end_time":"2021-01-05T03:53:58.179763","exception":false,"start_time":"2021-01-05T03:53:58.123270","status":"completed"},"tags":[],"trusted":false},"cell_type":"code","source":"train.user_answer.value_counts()","execution_count":null,"outputs":[]},{"metadata":{"papermill":{"duration":0.031165,"end_time":"2021-01-05T03:53:58.241899","exception":false,"start_time":"2021-01-05T03:53:58.210734","status":"completed"},"tags":[]},"cell_type":"markdown","source":"### how many nan values are there in prior_question_elapsed_time"},{"metadata":{"execution":{"iopub.execute_input":"2021-01-05T03:53:58.310052Z","iopub.status.busy":"2021-01-05T03:53:58.309092Z","iopub.status.idle":"2021-01-05T03:53:58.316939Z","shell.execute_reply":"2021-01-05T03:53:58.317486Z"},"papermill":{"duration":0.043539,"end_time":"2021-01-05T03:53:58.317614","exception":false,"start_time":"2021-01-05T03:53:58.274075","status":"completed"},"tags":[],"trusted":false},"cell_type":"code","source":"train.prior_question_elapsed_time.isna().sum()","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2021-01-05T03:53:58.385079Z","iopub.status.busy":"2021-01-05T03:53:58.384385Z","iopub.status.idle":"2021-01-05T03:53:58.424497Z","shell.execute_reply":"2021-01-05T03:53:58.425726Z"},"papermill":{"duration":0.076885,"end_time":"2021-01-05T03:53:58.425926","exception":false,"start_time":"2021-01-05T03:53:58.349041","status":"completed"},"tags":[],"trusted":false},"cell_type":"code","source":"train.isna().sum()","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2021-01-05T03:53:58.529283Z","iopub.status.busy":"2021-01-05T03:53:58.527605Z","iopub.status.idle":"2021-01-05T03:53:58.567432Z","shell.execute_reply":"2021-01-05T03:53:58.568230Z"},"papermill":{"duration":0.093805,"end_time":"2021-01-05T03:53:58.568428","exception":false,"start_time":"2021-01-05T03:53:58.474623","status":"completed"},"tags":[],"trusted":false},"cell_type":"code","source":"del_ids = ((train.user_answer == -1) & (train.answered_correctly == -1) & \n (train['prior_question_elapsed_time'].isna() == True) ).sum()","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2021-01-05T03:53:58.672453Z","iopub.status.busy":"2021-01-05T03:53:58.671592Z","iopub.status.idle":"2021-01-05T03:53:58.675390Z","shell.execute_reply":"2021-01-05T03:53:58.674590Z"},"papermill":{"duration":0.059564,"end_time":"2021-01-05T03:53:58.675541","exception":false,"start_time":"2021-01-05T03:53:58.615977","status":"completed"},"tags":[],"trusted":false},"cell_type":"code","source":"print('percentage Ids that have been erased {:.4f}%'.format(del_ids/len(train)*100))","execution_count":null,"outputs":[]},{"metadata":{"papermill":{"duration":0.046063,"end_time":"2021-01-05T03:53:58.770597","exception":false,"start_time":"2021-01-05T03:53:58.724534","status":"completed"},"tags":[]},"cell_type":"markdown","source":"### -1 values are lectures (do not need them for trainning)\n### therefore drop them "},{"metadata":{"execution":{"iopub.execute_input":"2021-01-05T03:53:58.882834Z","iopub.status.busy":"2021-01-05T03:53:58.881868Z","iopub.status.idle":"2021-01-05T03:53:59.124991Z","shell.execute_reply":"2021-01-05T03:53:59.123905Z"},"papermill":{"duration":0.30674,"end_time":"2021-01-05T03:53:59.125193","exception":false,"start_time":"2021-01-05T03:53:58.818453","status":"completed"},"tags":[],"trusted":false},"cell_type":"code","source":"print('Train length : ', len(train))\n#remove the -1 values in the data it affects the algorithms ability to learn\ntrain = train.drop(train[(train.user_answer == -1) & (train.answered_correctly == -1) & \n (train['prior_question_elapsed_time'].isna() == True)].index)\nprint(\"We erased  {:.3}% of all data.\".format(del_ids/len(train)*100))\n","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2021-01-05T03:53:59.206139Z","iopub.status.busy":"2021-01-05T03:53:59.205226Z","iopub.status.idle":"2021-01-05T03:53:59.210937Z","shell.execute_reply":"2021-01-05T03:53:59.210261Z"},"papermill":{"duration":0.043982,"end_time":"2021-01-05T03:53:59.211048","exception":false,"start_time":"2021-01-05T03:53:59.167066","status":"completed"},"tags":[],"trusted":false},"cell_type":"code","source":"train.shape  ","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2021-01-05T03:53:59.294605Z","iopub.status.busy":"2021-01-05T03:53:59.293733Z","iopub.status.idle":"2021-01-05T03:53:59.324530Z","shell.execute_reply":"2021-01-05T03:53:59.323842Z"},"papermill":{"duration":0.079447,"end_time":"2021-01-05T03:53:59.324647","exception":false,"start_time":"2021-01-05T03:53:59.245200","status":"completed"},"tags":[],"trusted":false},"cell_type":"code","source":"train.user_answer.value_counts()","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2021-01-05T03:53:59.402697Z","iopub.status.busy":"2021-01-05T03:53:59.401129Z","iopub.status.idle":"2021-01-05T03:53:59.405253Z","shell.execute_reply":"2021-01-05T03:53:59.404614Z"},"papermill":{"duration":0.045873,"end_time":"2021-01-05T03:53:59.405415","exception":false,"start_time":"2021-01-05T03:53:59.359542","status":"completed"},"tags":[],"trusted":false},"cell_type":"code","source":"print('Train shape: ' ,train.shape)","execution_count":null,"outputs":[]},{"metadata":{"papermill":{"duration":0.037076,"end_time":"2021-01-05T03:53:59.477468","exception":false,"start_time":"2021-01-05T03:53:59.440392","status":"completed"},"tags":[]},"cell_type":"markdown","source":"# preprocessing the data"},{"metadata":{"execution":{"iopub.execute_input":"2021-01-05T03:53:59.561564Z","iopub.status.busy":"2021-01-05T03:53:59.560606Z","iopub.status.idle":"2021-01-05T03:53:59.591165Z","shell.execute_reply":"2021-01-05T03:53:59.591764Z"},"papermill":{"duration":0.073979,"end_time":"2021-01-05T03:53:59.591915","exception":false,"start_time":"2021-01-05T03:53:59.517936","status":"completed"},"tags":[],"trusted":false},"cell_type":"code","source":"# Find Missing Data if any\ntotal = len(train)\n\nfor column in train.columns:\n    if train[column].isna().sum() != 0:\n        print(\"{} has: {:,} ({:.2}%) missing values.\".format(column, train[column].isna().sum(), \n                                                             (train[column].isna().sum()/total)*100))","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2021-01-05T03:53:59.672971Z","iopub.status.busy":"2021-01-05T03:53:59.672071Z","iopub.status.idle":"2021-01-05T03:54:00.067810Z","shell.execute_reply":"2021-01-05T03:54:00.068301Z"},"papermill":{"duration":0.440413,"end_time":"2021-01-05T03:54:00.068439","exception":false,"start_time":"2021-01-05T03:53:59.628026","status":"completed"},"tags":[],"trusted":false},"cell_type":"code","source":"# Fill in missing values \ntrain[\"prior_question_elapsed_time\"] = train[\"prior_question_elapsed_time\"].fillna(np.float32(train[\"prior_question_elapsed_time\"].mean()))\ntrain[\"prior_question_had_explanation\"] = train[\"prior_question_had_explanation\"].fillna(train[\"prior_question_had_explanation\"].value_counts().index[0])\ntrain[\"prior_question_had_explanation\"] = train[\"prior_question_had_explanation\"].astype(int)\ntrain[\"content_type_id\"] = train[\"content_type_id\"].astype(int)\ntrain.head()","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2021-01-05T03:54:00.145425Z","iopub.status.busy":"2021-01-05T03:54:00.144445Z","iopub.status.idle":"2021-01-05T03:54:00.181534Z","shell.execute_reply":"2021-01-05T03:54:00.181047Z"},"papermill":{"duration":0.078026,"end_time":"2021-01-05T03:54:00.181642","exception":false,"start_time":"2021-01-05T03:54:00.103616","status":"completed"},"tags":[],"trusted":false},"cell_type":"code","source":"print(\"{} has: {:,} unique user ids,\\n{:.0f} is the average number of times user_id appears.\".format(train.columns[2], (train['user_id'].value_counts().unique()).sum(), \n                                                             train['user_id'].value_counts().mean()))\n#see if dropping users with < 15 appearences is better than dropping users with < mean_id\n#mean_id = train['user_id'].value_counts().mean()","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2021-01-05T03:54:00.261746Z","iopub.status.busy":"2021-01-05T03:54:00.260839Z","iopub.status.idle":"2021-01-05T03:54:00.421332Z","shell.execute_reply":"2021-01-05T03:54:00.421857Z"},"papermill":{"duration":0.204442,"end_time":"2021-01-05T03:54:00.422008","exception":false,"start_time":"2021-01-05T03:54:00.217566","status":"completed"},"tags":[],"trusted":false},"cell_type":"code","source":"# Select ids to erase\n# user_ids with less than 5 appearences where most-likely jerking off so, we remove them\nids_to_erase = train[\"user_id\"].value_counts().reset_index()[train[\"user_id\"].value_counts().reset_index()[\"user_id\"] < 15]\\\n                                                                                                                [\"index\"].values\n# Erase the ids\nnew_train = train[~train['user_id'].isin(ids_to_erase)]\n\nprint(\"We erased {} rows meaning {:.3}% of all data.\".format(len(train)-len(new_train), (1 - len(new_train)/len(train))*100))\ndel ids_to_erase\n# del train\n","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2021-01-05T03:54:00.502611Z","iopub.status.busy":"2021-01-05T03:54:00.501305Z","iopub.status.idle":"2021-01-05T03:54:00.505341Z","shell.execute_reply":"2021-01-05T03:54:00.505825Z"},"papermill":{"duration":0.04715,"end_time":"2021-01-05T03:54:00.505958","exception":false,"start_time":"2021-01-05T03:54:00.458808","status":"completed"},"tags":[],"trusted":false},"cell_type":"code","source":"new_train.shape","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2021-01-05T03:54:00.596950Z","iopub.status.busy":"2021-01-05T03:54:00.595138Z","iopub.status.idle":"2021-01-05T03:54:00.597628Z","shell.execute_reply":"2021-01-05T03:54:00.598144Z"},"papermill":{"duration":0.044648,"end_time":"2021-01-05T03:54:00.598270","exception":false,"start_time":"2021-01-05T03:54:00.553622","status":"completed"},"tags":[],"trusted":false},"cell_type":"code","source":"del train","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2021-01-05T03:54:00.680811Z","iopub.status.busy":"2021-01-05T03:54:00.679313Z","iopub.status.idle":"2021-01-05T03:54:00.683188Z","shell.execute_reply":"2021-01-05T03:54:00.683673Z"},"papermill":{"duration":0.048156,"end_time":"2021-01-05T03:54:00.683805","exception":false,"start_time":"2021-01-05T03:54:00.635649","status":"completed"},"tags":[],"trusted":false},"cell_type":"code","source":"new_train.columns","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2021-01-05T03:54:00.775074Z","iopub.status.busy":"2021-01-05T03:54:00.774267Z","iopub.status.idle":"2021-01-05T03:54:00.780852Z","shell.execute_reply":"2021-01-05T03:54:00.780264Z"},"papermill":{"duration":0.056997,"end_time":"2021-01-05T03:54:00.780964","exception":false,"start_time":"2021-01-05T03:54:00.723967","status":"completed"},"tags":[],"trusted":false},"cell_type":"code","source":"new_train.head()","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2021-01-05T03:54:00.922826Z","iopub.status.busy":"2021-01-05T03:54:00.920806Z","iopub.status.idle":"2021-01-05T03:54:00.923514Z","shell.execute_reply":"2021-01-05T03:54:00.924013Z"},"papermill":{"duration":0.104524,"end_time":"2021-01-05T03:54:00.924199","exception":false,"start_time":"2021-01-05T03:54:00.819675","status":"completed"},"tags":[],"trusted":false},"cell_type":"code","source":"new_train.drop('row_id',1,inplace=True)\nnew_train.drop('user_answer',1,inplace=True)\nnew_train.drop('user_id',1,inplace=True)","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2021-01-05T03:54:01.022888Z","iopub.status.busy":"2021-01-05T03:54:01.020943Z","iopub.status.idle":"2021-01-05T03:54:01.023676Z","shell.execute_reply":"2021-01-05T03:54:01.024214Z"},"papermill":{"duration":0.061391,"end_time":"2021-01-05T03:54:01.024365","exception":false,"start_time":"2021-01-05T03:54:00.962974","status":"completed"},"tags":[],"trusted":false},"cell_type":"code","source":"y = new_train['answered_correctly']\nnew_train.drop('answered_correctly',1,inplace=True)","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2021-01-05T03:54:01.106676Z","iopub.status.busy":"2021-01-05T03:54:01.105940Z","iopub.status.idle":"2021-01-05T03:54:01.109228Z","shell.execute_reply":"2021-01-05T03:54:01.109684Z"},"papermill":{"duration":0.046716,"end_time":"2021-01-05T03:54:01.109816","exception":false,"start_time":"2021-01-05T03:54:01.063100","status":"completed"},"tags":[],"trusted":false},"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\n","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2021-01-05T03:54:01.194964Z","iopub.status.busy":"2021-01-05T03:54:01.194065Z","iopub.status.idle":"2021-01-05T03:54:01.362320Z","shell.execute_reply":"2021-01-05T03:54:01.361616Z"},"papermill":{"duration":0.213609,"end_time":"2021-01-05T03:54:01.362450","exception":false,"start_time":"2021-01-05T03:54:01.148841","status":"completed"},"tags":[],"trusted":false},"cell_type":"code","source":"x_train,x_test,y_train,y_test = train_test_split(new_train,y,test_size=0.3)","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2021-01-05T03:54:01.446477Z","iopub.status.busy":"2021-01-05T03:54:01.445834Z","iopub.status.idle":"2021-01-05T03:54:01.556912Z","shell.execute_reply":"2021-01-05T03:54:01.554318Z"},"papermill":{"duration":0.154135,"end_time":"2021-01-05T03:54:01.557027","exception":false,"start_time":"2021-01-05T03:54:01.402892","status":"completed"},"tags":[],"trusted":false},"cell_type":"code","source":"import lightgbm as lgb\nfrom sklearn.metrics import roc_auc_score","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2021-01-05T03:54:01.646027Z","iopub.status.busy":"2021-01-05T03:54:01.644403Z","iopub.status.idle":"2021-01-05T03:54:01.646798Z","shell.execute_reply":"2021-01-05T03:54:01.647273Z"},"papermill":{"duration":0.047126,"end_time":"2021-01-05T03:54:01.647392","exception":false,"start_time":"2021-01-05T03:54:01.600266","status":"completed"},"tags":[],"trusted":false},"cell_type":"code","source":"from sklearn.model_selection import TimeSeriesSplit, KFold\n\nn_folds = 5\nfolds = TimeSeriesSplit(n_splits=n_folds)\nfolds = KFold(n_splits=5)","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2021-01-05T03:54:01.734625Z","iopub.status.busy":"2021-01-05T03:54:01.733735Z","iopub.status.idle":"2021-01-05T03:54:01.742221Z","shell.execute_reply":"2021-01-05T03:54:01.741645Z"},"papermill":{"duration":0.055821,"end_time":"2021-01-05T03:54:01.742327","exception":false,"start_time":"2021-01-05T03:54:01.686506","status":"completed"},"tags":[],"trusted":false},"cell_type":"code","source":"columns = x_train.columns\nsplits = folds.split(x_train,y_train)\n\ny_preds = np.zeros(x_test.shape[0])\ny_oof = np.zeros(x_train.shape[0])\n\nscore_auc = 0\n \nfeature_importances = pd.DataFrame()\nfeature_importances['feature'] = columns","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2021-01-05T03:54:01.828084Z","iopub.status.busy":"2021-01-05T03:54:01.827214Z","iopub.status.idle":"2021-01-05T03:54:01.830022Z","shell.execute_reply":"2021-01-05T03:54:01.829500Z"},"papermill":{"duration":0.048403,"end_time":"2021-01-05T03:54:01.830125","exception":false,"start_time":"2021-01-05T03:54:01.781722","status":"completed"},"tags":[],"trusted":false},"cell_type":"code","source":"params = {\n        'num_leaves': 64,\n        'min_child_weight' : 0.03,\n        'max_depth': -1,\n        'feature_fraction': 0.04,\n        'learning_rate': 0.006,\n        'min_data_in_leaf': 80,\n        'metric': 'auc',\n        'bagging_fraction': 0.33,\n        'boosting_type': 'gbdt',\n        'reg_alpha' : 0.3,\n        'reg_lambda' : 0.6,\n        'verbosity': -1,\n        'random_sate' : 0\n        }","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2021-01-05T03:54:01.917896Z","iopub.status.busy":"2021-01-05T03:54:01.916959Z","iopub.status.idle":"2021-01-05T05:46:13.602580Z","shell.execute_reply":"2021-01-05T05:46:13.603227Z"},"papermill":{"duration":6731.733932,"end_time":"2021-01-05T05:46:13.603394","exception":false,"start_time":"2021-01-05T03:54:01.869462","status":"completed"},"tags":[],"trusted":false},"cell_type":"code","source":"%%time\nfor fold_n, (train_index, valid_index) in enumerate(splits):\n    x_tr , x_val = x_train[columns].iloc[train_index], x_train[columns].iloc[valid_index]\n    y_tr, y_val = y_train.iloc[train_index], y_train.iloc[valid_index]\n    \n    dtrain = lgb.Dataset(x_tr, label=y_tr)\n    dvalid = lgb.Dataset(x_val, label=y_val)\n    \n    clf = lgb.train(params, dtrain,10000,valid_sets = [dtrain, dvalid], verbose_eval=200, early_stopping_rounds=100)\n    \n    feature_importances[f'fold_{fold_n + 1} '] = clf.feature_importance()\n    \n    y_pred_val = clf.predict(x_val)\n    y_oof[valid_index] = y_pred_val\n    print(f\"Fold {fold_n + 1} | AUC : {roc_auc_score(y_val,y_pred_val)}\")\n    \n    score_auc += roc_auc_score(y_val, y_pred_val) / n_folds\n    \n    y_preds += clf.predict(x_test)  / n_folds\n    \n    del x_tr, x_val, y_tr, y_val","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2021-01-05T05:46:13.813192Z","iopub.status.busy":"2021-01-05T05:46:13.812563Z","iopub.status.idle":"2021-01-05T05:46:14.220887Z","shell.execute_reply":"2021-01-05T05:46:14.221649Z"},"papermill":{"duration":0.517321,"end_time":"2021-01-05T05:46:14.221812","exception":false,"start_time":"2021-01-05T05:46:13.704491","status":"completed"},"tags":[],"trusted":false},"cell_type":"code","source":"%%time\nprint('Loading riiid!')\nimport riiideducation\n#Create the env\nenv = riiideducation.make_env()\nprint('Loaded riiideducation!')\n\n#Create the iterator\niter_test = env.iter_test()\n\n#Iter and predict\nfor (test_df, sample_prediction_df) in iter_test:\n    test_df['prior_question_elapsed_time'] = test_df['prior_question_elapsed_time'].fillna(np.float32(test_df['prior_question_elapsed_time'].mean()))\n    test_df['prior_question_had_explanation'] = test_df['prior_question_had_explanation'].fillna(test_df['prior_question_had_explanation'].value_counts().index[0])\n    test_df['prior_question_had_explanation'] = test_df['prior_question_had_explanation'].astype(int)\n    test_df['answered_correctly'] = clf.predict(np.array(test_df[['timestamp', 'content_id', 'content_type_id',\n       'task_container_id', 'prior_question_elapsed_time',\n       'prior_question_had_explanation']]))\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}