{"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":"Problem statement\n\nThis may be one of the most challenging Tabular Playground competitions to date! It just so happens that one of Kaggle's software engineers is an avid Rocket League player and he's assembled a dataset of Rocket League gameplay for this month's TPS.\n\nThis month's challenge is to, given a snapshot from a Rocket League match, predict the probability of each team scoring within the next 10 seconds of the game. Sounds awesome, right?","metadata":{}},{"cell_type":"markdown","source":"Import libraries","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport os\nimport matplotlib.pyplot as plt\nimport seaborn as sns","metadata":{"execution":{"iopub.status.busy":"2022-10-01T08:50:17.559969Z","iopub.execute_input":"2022-10-01T08:50:17.560968Z","iopub.status.idle":"2022-10-01T08:50:18.553633Z","shell.execute_reply.started":"2022-10-01T08:50:17.560875Z","shell.execute_reply":"2022-10-01T08:50:18.552388Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Load files","metadata":{}},{"cell_type":"code","source":"for dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))","metadata":{"execution":{"iopub.status.busy":"2022-10-01T08:50:18.555677Z","iopub.execute_input":"2022-10-01T08:50:18.556042Z","iopub.status.idle":"2022-10-01T08:50:18.563182Z","shell.execute_reply.started":"2022-10-01T08:50:18.555991Z","shell.execute_reply":"2022-10-01T08:50:18.562072Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Read files","metadata":{}},{"cell_type":"code","source":"train_0 = pd.read_csv(\"/kaggle/input/tabular-playground-series-oct-2022/train_0.csv\")\ntrain_0","metadata":{"execution":{"iopub.status.busy":"2022-10-01T08:50:18.564487Z","iopub.execute_input":"2022-10-01T08:50:18.564833Z","iopub.status.idle":"2022-10-01T08:50:49.291130Z","shell.execute_reply.started":"2022-10-01T08:50:18.564779Z","shell.execute_reply":"2022-10-01T08:50:49.290061Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tqdm import tqdm\n\nINPUT = '../input/tabular-playground-series-oct-2022/'\n\ndf_train_dtypes = pd.read_csv(INPUT + 'train_dtypes.csv')\ndf_test_dtypes = pd.read_csv(INPUT + 'test_dtypes.csv')\ntrain_dtypes = {k: v for (k, v) in zip(df_train_dtypes.column, df_train_dtypes.dtype)}\ntest_dtypes = {k: v for (k, v) in zip(df_test_dtypes.column, df_test_dtypes.dtype)}\n\ntrain_list = []\n\nfor i in tqdm(range(1)):\n    train_list.append(pd.read_csv(INPUT + f'train_{i}.csv', dtype = train_dtypes))\n    \ntrain_list","metadata":{"execution":{"iopub.status.busy":"2022-10-01T09:08:55.037404Z","iopub.execute_input":"2022-10-01T09:08:55.038525Z","iopub.status.idle":"2022-10-01T09:09:15.214775Z","shell.execute_reply.started":"2022-10-01T09:08:55.038483Z","shell.execute_reply":"2022-10-01T09:09:15.213832Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = pd.read_csv(INPUT + 'test.csv', dtype = test_dtypes)\ntest","metadata":{"execution":{"iopub.status.busy":"2022-10-01T09:12:03.438892Z","iopub.execute_input":"2022-10-01T09:12:03.439430Z","iopub.status.idle":"2022-10-01T09:12:10.082168Z","shell.execute_reply.started":"2022-10-01T09:12:03.439390Z","shell.execute_reply":"2022-10-01T09:12:10.081097Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.read_csv(INPUT + 'sample_submission.csv')\nsubmission","metadata":{"execution":{"iopub.status.busy":"2022-10-01T09:13:05.830313Z","iopub.execute_input":"2022-10-01T09:13:05.830732Z","iopub.status.idle":"2022-10-01T09:13:05.950411Z","shell.execute_reply.started":"2022-10-01T09:13:05.830697Z","shell.execute_reply":"2022-10-01T09:13:05.949354Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(train_list[0].shape)\nprint(test.shape)\nprint(submission.shape)\n","metadata":{"execution":{"iopub.status.busy":"2022-10-01T09:14:53.675387Z","iopub.execute_input":"2022-10-01T09:14:53.675841Z","iopub.status.idle":"2022-10-01T09:14:53.681896Z","shell.execute_reply.started":"2022-10-01T09:14:53.675792Z","shell.execute_reply":"2022-10-01T09:14:53.680938Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Define X and y variables","metadata":{}},{"cell_type":"code","source":"y = train_list[0]['team_A_scoring_within_10sec']\nX = train_list[0].drop(['game_num', 'event_id', 'event_time', 'player_scoring_next', \\\n                        'team_scoring_next', 'team_A_scoring_within_10sec', \\\n                        'team_B_scoring_within_10sec'], axis = 1)\nX_test = test.drop(['id'], axis = 1)\n\nX.shape, y.shape,X_test.shape","metadata":{"execution":{"iopub.status.busy":"2022-10-01T09:19:12.368522Z","iopub.execute_input":"2022-10-01T09:19:12.369006Z","iopub.status.idle":"2022-10-01T09:19:12.624336Z","shell.execute_reply.started":"2022-10-01T09:19:12.368966Z","shell.execute_reply":"2022-10-01T09:19:12.623091Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Split into training and validating sets","metadata":{}},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\nX_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.1, random_state=42, shuffle=True)\n\nX_train.shape, X_val.shape, y_train.shape, y_val.shape","metadata":{"execution":{"iopub.status.busy":"2022-10-01T09:22:15.714652Z","iopub.execute_input":"2022-10-01T09:22:15.715104Z","iopub.status.idle":"2022-10-01T09:22:17.105642Z","shell.execute_reply.started":"2022-10-01T09:22:15.715045Z","shell.execute_reply":"2022-10-01T09:22:17.104313Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Select model","metadata":{}},{"cell_type":"code","source":"import lightgbm as lgb\n\nparams = {'objective': 'binary',\n          'seed': 42\n         }\n\ntrain_data = lgb.Dataset(X_train, label = y_train)\nval_data = lgb.Dataset(X_val, label = y_val, reference = train_data)\n\nmodel = lgb.train({**params},\n                  train_data, \n                  1000,\n                  valid_sets = [train_data, val_data],\n                  early_stopping_rounds=10,\n                  verbose_eval=1000)","metadata":{"execution":{"iopub.status.busy":"2022-10-01T09:27:23.959594Z","iopub.execute_input":"2022-10-01T09:27:23.960277Z","iopub.status.idle":"2022-10-01T09:32:09.756898Z","shell.execute_reply.started":"2022-10-01T09:27:23.960236Z","shell.execute_reply":"2022-10-01T09:32:09.756056Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Make predictions","metadata":{}},{"cell_type":"code","source":"predictions = model.predict(X_test)\npredictions","metadata":{"execution":{"iopub.status.busy":"2022-10-01T09:32:40.408802Z","iopub.execute_input":"2022-10-01T09:32:40.409892Z","iopub.status.idle":"2022-10-01T09:32:55.311075Z","shell.execute_reply.started":"2022-10-01T09:32:40.409837Z","shell.execute_reply":"2022-10-01T09:32:55.309973Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Prepare submission","metadata":{}},{"cell_type":"code","source":"submission['team_A_scoring_within_10sec'] = predictions\nsubmission['team_B_scoring_within_10sec'] = 1 - predictions\nsubmission.to_csv('submission.csv', index = False)\nsubmission","metadata":{"execution":{"iopub.status.busy":"2022-10-01T09:33:15.377505Z","iopub.execute_input":"2022-10-01T09:33:15.378284Z","iopub.status.idle":"2022-10-01T09:33:18.048080Z","shell.execute_reply.started":"2022-10-01T09:33:15.378244Z","shell.execute_reply":"2022-10-01T09:33:18.047058Z"},"trusted":true},"execution_count":null,"outputs":[]}]}