{"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":"code","source":"# Import libraries\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport os\nimport matplotlib.pyplot as plt\nimport seaborn as sns","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-10-27T04:04:10.387417Z","iopub.execute_input":"2022-10-27T04:04:10.387901Z","iopub.status.idle":"2022-10-27T04:04:10.393940Z","shell.execute_reply.started":"2022-10-27T04:04:10.387859Z","shell.execute_reply":"2022-10-27T04:04:10.392624Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 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\nimport os\nfor 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-27T04:04:16.094243Z","iopub.execute_input":"2022-10-27T04:04:16.094676Z","iopub.status.idle":"2022-10-27T04:04:16.102947Z","shell.execute_reply.started":"2022-10-27T04:04:16.094641Z","shell.execute_reply":"2022-10-27T04:04:16.101691Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Read files - Will user train_1.csv file \ntrain_1 = pd.read_csv(\"/kaggle/input/tabular-playground-series-oct-2022/train_1.csv\")\ntrain_1","metadata":{"execution":{"iopub.status.busy":"2022-10-27T04:06:04.739738Z","iopub.execute_input":"2022-10-27T04:06:04.740776Z","iopub.status.idle":"2022-10-27T04:06:22.430638Z","shell.execute_reply.started":"2022-10-27T04:06:04.740718Z","shell.execute_reply":"2022-10-27T04:06:22.429519Z"},"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-27T04:06:30.434353Z","iopub.execute_input":"2022-10-27T04:06:30.434732Z","iopub.status.idle":"2022-10-27T04:06:58.770701Z","shell.execute_reply.started":"2022-10-27T04:06:30.434698Z","shell.execute_reply":"2022-10-27T04:06:58.769555Z"},"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-27T04:07:09.345977Z","iopub.execute_input":"2022-10-27T04:07:09.346407Z","iopub.status.idle":"2022-10-27T04:07:18.547826Z","shell.execute_reply.started":"2022-10-27T04:07:09.346371Z","shell.execute_reply":"2022-10-27T04:07:18.546807Z"},"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-27T04:07:25.365379Z","iopub.execute_input":"2022-10-27T04:07:25.365819Z","iopub.status.idle":"2022-10-27T04:07:25.564839Z","shell.execute_reply.started":"2022-10-27T04:07:25.365774Z","shell.execute_reply":"2022-10-27T04:07:25.563799Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(train_list[0].shape)\nprint(test.shape)\nprint(submission.shape)","metadata":{"execution":{"iopub.status.busy":"2022-10-27T04:07:31.244093Z","iopub.execute_input":"2022-10-27T04:07:31.244487Z","iopub.status.idle":"2022-10-27T04:07:31.250106Z","shell.execute_reply.started":"2022-10-27T04:07:31.244454Z","shell.execute_reply":"2022-10-27T04:07:31.249029Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"display(train_list[0].head(3))\ndisplay(test.head(3))","metadata":{"execution":{"iopub.status.busy":"2022-10-27T04:07:36.662398Z","iopub.execute_input":"2022-10-27T04:07:36.662760Z","iopub.status.idle":"2022-10-27T04:07:36.711090Z","shell.execute_reply.started":"2022-10-27T04:07:36.662715Z","shell.execute_reply":"2022-10-27T04:07:36.709912Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(20,5))\nplt.subplot(1,2,1)\nsns.countplot(data=train_list[0], x='team_A_scoring_within_10sec')\nplt.title('Target A')\n\nplt.subplot(1,2,2)\nsns.countplot(data=train_list[0], x='team_B_scoring_within_10sec')\nplt.title('Target B')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-10-27T04:07:41.847313Z","iopub.execute_input":"2022-10-27T04:07:41.847709Z","iopub.status.idle":"2022-10-27T04:07:42.464797Z","shell.execute_reply.started":"2022-10-27T04:07:41.847678Z","shell.execute_reply":"2022-10-27T04:07:42.463766Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Team A target mean', train_list[0]['team_A_scoring_within_10sec'].mean())\nprint('Team B target mean', train_list[0]['team_B_scoring_within_10sec'].mean())","metadata":{"execution":{"iopub.status.busy":"2022-10-27T04:07:47.494515Z","iopub.execute_input":"2022-10-27T04:07:47.495491Z","iopub.status.idle":"2022-10-27T04:07:47.504165Z","shell.execute_reply.started":"2022-10-27T04:07:47.495430Z","shell.execute_reply":"2022-10-27T04:07:47.503348Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(20,5))\nplt.subplot(1,2,1)\nsns.countplot(data=train_list[0], x='team_scoring_next')\nplt.title('Team scoring next')\n\nplt.subplot(1,2,2)\nsns.countplot(data=train_list[0], x='player_scoring_next')\nplt.title('Player scoring next')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-10-27T04:07:54.192547Z","iopub.execute_input":"2022-10-27T04:07:54.193036Z","iopub.status.idle":"2022-10-27T04:07:55.701474Z","shell.execute_reply.started":"2022-10-27T04:07:54.192994Z","shell.execute_reply":"2022-10-27T04:07:55.700313Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_a = train_list[0]['team_A_scoring_within_10sec']\ny_b = train_list[0]['team_B_scoring_within_10sec']\n\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_a.shape,y_b.shape, X_test.shape","metadata":{"execution":{"iopub.status.busy":"2022-10-27T04:08:02.288450Z","iopub.execute_input":"2022-10-27T04:08:02.288847Z","iopub.status.idle":"2022-10-27T04:08:02.525317Z","shell.execute_reply.started":"2022-10-27T04:08:02.288814Z","shell.execute_reply":"2022-10-27T04:08:02.524088Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\nXa_train, Xa_val, ya_train, ya_val = train_test_split(X, y_a, test_size=0.1, random_state=42, shuffle=True)\nXb_train, Xb_val, yb_train, yb_val = train_test_split(X, y_b, test_size=0.1, random_state=42, shuffle=True)\n\nprint(Xa_train.shape, Xa_val.shape, ya_train.shape, ya_val.shape)\nprint(Xb_train.shape, Xb_val.shape, yb_train.shape, yb_val.shape)","metadata":{"execution":{"iopub.status.busy":"2022-10-27T04:08:07.237364Z","iopub.execute_input":"2022-10-27T04:08:07.237769Z","iopub.status.idle":"2022-10-27T04:08:09.768305Z","shell.execute_reply.started":"2022-10-27T04:08:07.237710Z","shell.execute_reply":"2022-10-27T04:08:09.767323Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import lightgbm as lgb\n\nmodel_A = lgb.LGBMClassifier()\nmodel_B = lgb.LGBMClassifier()\n\n#model_A = lgb.LGBMClassifier(object=\"binary\", max_depth=3, num_leaves=25)\n#model_B = lgb.LGBMClassifier(object=\"binary\",max_depth=3, num_leaves=25)\nhistory_A = model_A.fit(Xa_train,ya_train,eval_set = [(Xa_val,ya_val),(Xa_train,ya_train)])\nhistory_B = model_B.fit(Xb_train,yb_train,eval_set = [(Xb_val,yb_val),(Xb_train,yb_train)])\n\n#del train_1,Xa_train,ya_train,Xb_train,yb_train,Xa_val,ya_val,Xb_val,yb_val","metadata":{"execution":{"iopub.status.busy":"2022-10-27T04:08:14.988328Z","iopub.execute_input":"2022-10-27T04:08:14.989008Z","iopub.status.idle":"2022-10-27T04:09:54.263634Z","shell.execute_reply.started":"2022-10-27T04:08:14.988970Z","shell.execute_reply":"2022-10-27T04:09:54.262641Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(model_A.score(Xa_val,ya_val))\nprint(model_B.score(Xb_val,yb_val))","metadata":{"execution":{"iopub.status.busy":"2022-10-27T04:10:07.092606Z","iopub.execute_input":"2022-10-27T04:10:07.093002Z","iopub.status.idle":"2022-10-27T04:10:08.203516Z","shell.execute_reply.started":"2022-10-27T04:10:07.092971Z","shell.execute_reply":"2022-10-27T04:10:08.202287Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predict_A = model_A.predict_proba(X_test)[:,1]\npredict_B = model_B.predict_proba(X_test)[:,1]\n\nprint(predict_A)\nprint(predict_B)","metadata":{"execution":{"iopub.status.busy":"2022-10-12T21:23:37.449181Z","iopub.execute_input":"2022-10-12T21:23:37.449647Z","iopub.status.idle":"2022-10-12T21:23:41.557919Z","shell.execute_reply.started":"2022-10-12T21:23:37.449607Z","shell.execute_reply":"2022-10-12T21:23:41.557017Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission[\"team_A_scoring_within_10sec\"] = predict_A\nsubmission[\"team_B_scoring_within_10sec\"] = predict_B\n\ndisplay(submission)\n\nsubmission.to_csv(\"submission.csv\",index=False)","metadata":{"execution":{"iopub.status.busy":"2022-10-11T22:57:37.027846Z","iopub.execute_input":"2022-10-11T22:57:37.028272Z","iopub.status.idle":"2022-10-11T22:57:39.625414Z","shell.execute_reply.started":"2022-10-11T22:57:37.028243Z","shell.execute_reply":"2022-10-11T22:57:39.624401Z"},"trusted":true},"execution_count":null,"outputs":[]}]}