{"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":"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\nimport os\nfor 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","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-10-21T15:23:33.514733Z","iopub.execute_input":"2022-10-21T15:23:33.515475Z","iopub.status.idle":"2022-10-21T15:23:33.524419Z","shell.execute_reply.started":"2022-10-21T15:23:33.515436Z","shell.execute_reply":"2022-10-21T15:23:33.523174Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.set_option('display.max_rows', 500)\npd.set_option('display.max_columns', 500)\npd.set_option('display.width', 1000)\n","metadata":{"execution":{"iopub.status.busy":"2022-10-21T15:23:33.526701Z","iopub.execute_input":"2022-10-21T15:23:33.527169Z","iopub.status.idle":"2022-10-21T15:23:33.535657Z","shell.execute_reply.started":"2022-10-21T15:23:33.527133Z","shell.execute_reply":"2022-10-21T15:23:33.534723Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dtypes_df = pd.read_csv('/kaggle/input/tabular-playground-series-oct-2022/train_dtypes.csv')\ndtypes = {k: v for (k, v) in zip(dtypes_df.column, dtypes_df.dtype)}\ntrain0_df = pd.read_csv('/kaggle/input/tabular-playground-series-oct-2022/train_0.csv', dtype=dtypes)","metadata":{"execution":{"iopub.status.busy":"2022-10-21T15:23:33.537763Z","iopub.execute_input":"2022-10-21T15:23:33.538484Z","iopub.status.idle":"2022-10-21T15:23:47.340911Z","shell.execute_reply.started":"2022-10-21T15:23:33.538448Z","shell.execute_reply":"2022-10-21T15:23:47.339921Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# print head \ntrain0_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-10-21T15:23:47.342971Z","iopub.execute_input":"2022-10-21T15:23:47.343447Z","iopub.status.idle":"2022-10-21T15:23:47.397656Z","shell.execute_reply.started":"2022-10-21T15:23:47.343408Z","shell.execute_reply":"2022-10-21T15:23:47.396834Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# print shape\ntrain0_df.shape","metadata":{"execution":{"iopub.status.busy":"2022-10-21T15:23:47.398958Z","iopub.execute_input":"2022-10-21T15:23:47.399270Z","iopub.status.idle":"2022-10-21T15:23:47.405661Z","shell.execute_reply.started":"2022-10-21T15:23:47.399244Z","shell.execute_reply":"2022-10-21T15:23:47.404699Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# print information about all datatypes\ntrain0_df.info(verbose=True)","metadata":{"execution":{"iopub.status.busy":"2022-10-21T15:23:47.408230Z","iopub.execute_input":"2022-10-21T15:23:47.409382Z","iopub.status.idle":"2022-10-21T15:23:47.426411Z","shell.execute_reply.started":"2022-10-21T15:23:47.409326Z","shell.execute_reply":"2022-10-21T15:23:47.425579Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train0_df.describe()","metadata":{"execution":{"iopub.status.busy":"2022-10-21T15:23:47.428378Z","iopub.execute_input":"2022-10-21T15:23:47.429291Z","iopub.status.idle":"2022-10-21T15:23:52.896852Z","shell.execute_reply.started":"2022-10-21T15:23:47.429255Z","shell.execute_reply":"2022-10-21T15:23:52.895895Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Train columns with null values:\\n', train0_df.isnull().sum())","metadata":{"execution":{"iopub.status.busy":"2022-10-21T15:23:52.901326Z","iopub.execute_input":"2022-10-21T15:23:52.903724Z","iopub.status.idle":"2022-10-21T15:23:53.265396Z","shell.execute_reply.started":"2022-10-21T15:23:52.903685Z","shell.execute_reply":"2022-10-21T15:23:53.264356Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train0_df[\"team_A_scoring_within_10sec\"] = train0_df[\"team_A_scoring_within_10sec\"].astype(\"category\")\ntrain0_df[\"team_B_scoring_within_10sec\"] = train0_df[\"team_B_scoring_within_10sec\"].astype(\"category\")\ntrain0_df[\"boost0_timer\"] = train0_df[\"boost0_timer\"].astype(\"float32\")\ntrain0_df[\"boost1_timer\"] = train0_df[\"boost1_timer\"].astype(\"float32\")\ntrain0_df[\"boost2_timer\"] = train0_df[\"boost2_timer\"].astype(\"float32\")\ntrain0_df[\"boost3_timer\"] = train0_df[\"boost3_timer\"].astype(\"float32\")\ntrain0_df[\"boost4_timer\"] = train0_df[\"boost4_timer\"].astype(\"float32\")\ntrain0_df[\"boost5_timer\"] = train0_df[\"boost5_timer\"].astype(\"float32\")\ntrain0_df[\"p0_boost\"] = train0_df[\"p0_boost\"].astype(\"float32\")\ntrain0_df[\"p1_boost\"] = train0_df[\"p1_boost\"].astype(\"float32\")\ntrain0_df[\"p2_boost\"] = train0_df[\"p2_boost\"].astype(\"float32\")\ntrain0_df[\"p3_boost\"] = train0_df[\"p3_boost\"].astype(\"float32\")\ntrain0_df[\"p4_boost\"] = train0_df[\"p4_boost\"].astype(\"float32\")\ntrain0_df[\"p5_boost\"] = train0_df[\"p5_boost\"].astype(\"float32\")\n\n# y matters for ball, for p0 too most","metadata":{"execution":{"iopub.status.busy":"2022-10-21T15:23:53.268457Z","iopub.execute_input":"2022-10-21T15:23:53.268735Z","iopub.status.idle":"2022-10-21T15:23:53.474040Z","shell.execute_reply.started":"2022-10-21T15:23:53.268709Z","shell.execute_reply":"2022-10-21T15:23:53.473112Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from matplotlib import pyplot as plt\nimport seaborn as sns\ndef plot_boxplots(feature, team):\n    fig, axes = plt.subplots(2, 3, figsize=(15, 5))\n    sns.boxplot(ax= axes[0,0],data=train0_df, x=feature+\"_pos_x\", y=team, hue=\"team_scoring_next\")\n    sns.boxplot(ax= axes[0,1],data=train0_df, x=feature+\"_pos_y\", y=team, hue=\"team_scoring_next\")\n    sns.boxplot(ax= axes[0,2],data=train0_df, x=feature+\"_pos_z\", y=team, hue=\"team_scoring_next\")\n    sns.boxplot(ax= axes[1,0],data=train0_df, x=feature+\"_vel_x\", y=team, hue=\"team_scoring_next\")\n    sns.boxplot(ax= axes[1,1],data=train0_df, x=feature+\"_vel_y\", y=team, hue=\"team_scoring_next\")\n    sns.boxplot(ax= axes[1,2],data=train0_df, x=feature+\"_vel_z\", y=team, hue=\"team_scoring_next\")","metadata":{"execution":{"iopub.status.busy":"2022-10-21T15:23:53.475592Z","iopub.execute_input":"2022-10-21T15:23:53.476017Z","iopub.status.idle":"2022-10-21T15:23:53.485516Z","shell.execute_reply.started":"2022-10-21T15:23:53.475976Z","shell.execute_reply":"2022-10-21T15:23:53.484546Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_boxplots(\"p3\",\"team_A_scoring_within_10sec\")","metadata":{"execution":{"iopub.status.busy":"2022-10-21T15:23:53.486986Z","iopub.execute_input":"2022-10-21T15:23:53.488008Z","iopub.status.idle":"2022-10-21T15:24:06.560637Z","shell.execute_reply.started":"2022-10-21T15:23:53.487970Z","shell.execute_reply":"2022-10-21T15:24:06.559708Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_boxplots(\"p3\",\"team_B_scoring_within_10sec\")","metadata":{"execution":{"iopub.status.busy":"2022-10-21T15:24:06.564888Z","iopub.execute_input":"2022-10-21T15:24:06.566924Z","iopub.status.idle":"2022-10-21T15:24:19.128951Z","shell.execute_reply.started":"2022-10-21T15:24:06.566892Z","shell.execute_reply":"2022-10-21T15:24:19.127929Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_boxplots(\"p5\",\"team_B_scoring_within_10sec\")","metadata":{"execution":{"iopub.status.busy":"2022-10-21T15:24:19.130354Z","iopub.execute_input":"2022-10-21T15:24:19.130815Z","iopub.status.idle":"2022-10-21T15:24:32.691734Z","shell.execute_reply.started":"2022-10-21T15:24:19.130776Z","shell.execute_reply":"2022-10-21T15:24:32.682182Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_boxplots(\"p5\",\"team_A_scoring_within_10sec\")","metadata":{"execution":{"iopub.status.busy":"2022-10-21T15:24:32.693173Z","iopub.execute_input":"2022-10-21T15:24:32.693536Z","iopub.status.idle":"2022-10-21T15:24:45.787548Z","shell.execute_reply.started":"2022-10-21T15:24:32.693501Z","shell.execute_reply":"2022-10-21T15:24:45.786471Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_boxplots(\"p2\",\"team_A_scoring_within_10sec\")","metadata":{"execution":{"iopub.status.busy":"2022-10-21T15:24:45.789160Z","iopub.execute_input":"2022-10-21T15:24:45.789529Z","iopub.status.idle":"2022-10-21T15:24:58.998540Z","shell.execute_reply.started":"2022-10-21T15:24:45.789495Z","shell.execute_reply":"2022-10-21T15:24:58.996032Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_boxplots(\"p2\",\"team_B_scoring_within_10sec\")","metadata":{"execution":{"iopub.status.busy":"2022-10-21T15:24:59.000035Z","iopub.execute_input":"2022-10-21T15:24:59.000457Z","iopub.status.idle":"2022-10-21T15:25:11.941733Z","shell.execute_reply.started":"2022-10-21T15:24:59.000414Z","shell.execute_reply":"2022-10-21T15:25:11.940815Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_boost(team):\n    fig, axes = plt.subplots(2, 3, figsize=(15, 5))\n    sns.boxplot(ax= axes[0,0],data=train0_df, x=\"p0_boost\", y=team, hue=\"team_scoring_next\")\n    sns.boxplot(ax= axes[0,1],data=train0_df, x=\"p1_boost\", y=team, hue=\"team_scoring_next\")\n    sns.boxplot(ax= axes[0,2],data=train0_df, x=\"p2_boost\", y=team, hue=\"team_scoring_next\")\n    sns.boxplot(ax= axes[1,0],data=train0_df, x=\"p3_boost\", y=team, hue=\"team_scoring_next\")\n    sns.boxplot(ax= axes[1,1],data=train0_df, x=\"p4_boost\", y=team, hue=\"team_scoring_next\")\n    sns.boxplot(ax= axes[1,2],data=train0_df, x=\"p5_boost\", y=team, hue=\"team_scoring_next\")","metadata":{"execution":{"iopub.status.busy":"2022-10-21T15:25:11.943162Z","iopub.execute_input":"2022-10-21T15:25:11.943495Z","iopub.status.idle":"2022-10-21T15:25:11.950967Z","shell.execute_reply.started":"2022-10-21T15:25:11.943468Z","shell.execute_reply":"2022-10-21T15:25:11.950032Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_boost(\"team_A_scoring_within_10sec\")","metadata":{"execution":{"iopub.status.busy":"2022-10-21T15:25:11.952210Z","iopub.execute_input":"2022-10-21T15:25:11.952773Z","iopub.status.idle":"2022-10-21T15:25:16.970697Z","shell.execute_reply.started":"2022-10-21T15:25:11.952736Z","shell.execute_reply":"2022-10-21T15:25:16.969766Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_boost(\"team_B_scoring_within_10sec\")","metadata":{"execution":{"iopub.status.busy":"2022-10-21T15:25:16.972197Z","iopub.execute_input":"2022-10-21T15:25:16.973078Z","iopub.status.idle":"2022-10-21T15:25:22.154689Z","shell.execute_reply.started":"2022-10-21T15:25:16.973036Z","shell.execute_reply":"2022-10-21T15:25:22.153772Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_boost_timer(team):\n    fig, axes = plt.subplots(2, 3, figsize=(15, 5))\n    sns.boxplot(ax= axes[0,0],data=train0_df, x=\"boost0_timer\", y=team, hue=\"team_scoring_next\")\n    sns.boxplot(ax= axes[0,1],data=train0_df, x=\"boost1_timer\", y=team, hue=\"team_scoring_next\")\n    sns.boxplot(ax= axes[0,2],data=train0_df, x=\"boost2_timer\", y=team, hue=\"team_scoring_next\")\n    sns.boxplot(ax= axes[1,0],data=train0_df, x=\"boost3_timer\", y=team, hue=\"team_scoring_next\")\n    sns.boxplot(ax= axes[1,1],data=train0_df, x=\"boost4_timer\", y=team, hue=\"team_scoring_next\")\n    sns.boxplot(ax= axes[1,2],data=train0_df, x=\"boost5_timer\", y=team, hue=\"team_scoring_next\")","metadata":{"execution":{"iopub.status.busy":"2022-10-21T15:25:22.156343Z","iopub.execute_input":"2022-10-21T15:25:22.157030Z","iopub.status.idle":"2022-10-21T15:25:22.165346Z","shell.execute_reply.started":"2022-10-21T15:25:22.156989Z","shell.execute_reply":"2022-10-21T15:25:22.164295Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_boost_timer(\"team_A_scoring_within_10sec\")","metadata":{"execution":{"iopub.status.busy":"2022-10-21T15:25:22.166722Z","iopub.execute_input":"2022-10-21T15:25:22.167253Z","iopub.status.idle":"2022-10-21T15:25:27.030997Z","shell.execute_reply.started":"2022-10-21T15:25:22.167215Z","shell.execute_reply":"2022-10-21T15:25:27.029880Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_boost_timer(\"team_B_scoring_within_10sec\")","metadata":{"execution":{"iopub.status.busy":"2022-10-21T15:25:27.032522Z","iopub.execute_input":"2022-10-21T15:25:27.033137Z","iopub.status.idle":"2022-10-21T15:25:32.358235Z","shell.execute_reply.started":"2022-10-21T15:25:27.033096Z","shell.execute_reply":"2022-10-21T15:25:32.357322Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.boxplot(data=train0_df, x=\"event_time\", y=\"team_A_scoring_within_10sec\", hue=\"team_scoring_next\")","metadata":{"execution":{"iopub.status.busy":"2022-10-21T15:25:32.359765Z","iopub.execute_input":"2022-10-21T15:25:32.360156Z","iopub.status.idle":"2022-10-21T15:25:33.562761Z","shell.execute_reply.started":"2022-10-21T15:25:32.360118Z","shell.execute_reply":"2022-10-21T15:25:33.561806Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.boxplot(data=train0_df, x=\"event_time\", y=\"team_B_scoring_within_10sec\", hue=\"team_scoring_next\")","metadata":{"execution":{"iopub.status.busy":"2022-10-21T15:25:33.564136Z","iopub.execute_input":"2022-10-21T15:25:33.565239Z","iopub.status.idle":"2022-10-21T15:25:34.951349Z","shell.execute_reply.started":"2022-10-21T15:25:33.565188Z","shell.execute_reply":"2022-10-21T15:25:34.950450Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(train0_df[train0_df[\"event_time\"]==0])","metadata":{"execution":{"iopub.status.busy":"2022-10-21T15:25:34.955552Z","iopub.execute_input":"2022-10-21T15:25:34.958275Z","iopub.status.idle":"2022-10-21T15:25:35.281593Z","shell.execute_reply.started":"2022-10-21T15:25:34.958234Z","shell.execute_reply":"2022-10-21T15:25:35.280715Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train0_df = train0_df.drop([\"ball_vel_x\",\"ball_vel_z\",\"p0_vel_x\",\"p0_vel_z\",\"p1_vel_x\",\"p1_vel_z\",\"p2_vel_x\",\"p2_vel_z\",\"p3_vel_x\",\n               \"p3_vel_z\",\"p4_vel_x\",\"p4_vel_z\",\"p5_vel_x\",\"p5_vel_z\"], axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-10-21T15:25:35.285595Z","iopub.execute_input":"2022-10-21T15:25:35.288051Z","iopub.status.idle":"2022-10-21T15:25:35.503969Z","shell.execute_reply.started":"2022-10-21T15:25:35.288012Z","shell.execute_reply":"2022-10-21T15:25:35.502982Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train0_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-10-21T15:25:35.505621Z","iopub.execute_input":"2022-10-21T15:25:35.506024Z","iopub.status.idle":"2022-10-21T15:25:35.548565Z","shell.execute_reply.started":"2022-10-21T15:25:35.505988Z","shell.execute_reply":"2022-10-21T15:25:35.547551Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train0_extra = train0_df[train0_df[\"team_scoring_next\"].isnull()]","metadata":{"execution":{"iopub.status.busy":"2022-10-21T15:25:35.550003Z","iopub.execute_input":"2022-10-21T15:25:35.550821Z","iopub.status.idle":"2022-10-21T15:25:35.711358Z","shell.execute_reply.started":"2022-10-21T15:25:35.550784Z","shell.execute_reply":"2022-10-21T15:25:35.710350Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train0_extra.head()","metadata":{"execution":{"iopub.status.busy":"2022-10-21T15:25:35.712972Z","iopub.execute_input":"2022-10-21T15:25:35.713352Z","iopub.status.idle":"2022-10-21T15:25:35.758883Z","shell.execute_reply.started":"2022-10-21T15:25:35.713313Z","shell.execute_reply":"2022-10-21T15:25:35.757890Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train0_df['team_scoring_next'] = train0_df['team_scoring_next'].fillna(value=\"None\")\nprint(train0_df['team_scoring_next'].unique())","metadata":{"execution":{"iopub.status.busy":"2022-10-21T15:25:35.765150Z","iopub.execute_input":"2022-10-21T15:25:35.765450Z","iopub.status.idle":"2022-10-21T15:25:35.982626Z","shell.execute_reply.started":"2022-10-21T15:25:35.765423Z","shell.execute_reply":"2022-10-21T15:25:35.980925Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train0_extra_2 = train0_df[train0_df[\"p0_pos_x\"].isnull()]","metadata":{"execution":{"iopub.status.busy":"2022-10-21T15:25:35.984706Z","iopub.execute_input":"2022-10-21T15:25:35.985749Z","iopub.status.idle":"2022-10-21T15:25:35.999798Z","shell.execute_reply.started":"2022-10-21T15:25:35.985707Z","shell.execute_reply":"2022-10-21T15:25:35.998919Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train0_extra_2.head()\n","metadata":{"execution":{"iopub.status.busy":"2022-10-21T15:25:36.001121Z","iopub.execute_input":"2022-10-21T15:25:36.001645Z","iopub.status.idle":"2022-10-21T15:25:36.043168Z","shell.execute_reply.started":"2022-10-21T15:25:36.001608Z","shell.execute_reply":"2022-10-21T15:25:36.042146Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train0_df['p0_pos_x'] = train0_df['p0_pos_x'].fillna(value=0)\ntrain0_df['p0_pos_y'] = train0_df['p0_pos_y'].fillna(value=0)\ntrain0_df['p0_pos_z'] = train0_df['p0_pos_z'].fillna(value=0)\ntrain0_df['p0_vel_y'] = train0_df['p0_vel_y'].fillna(value=0)\ntrain0_df['p0_boost'] = train0_df['p0_boost'].fillna(value=0)\n\ntrain0_df['p1_pos_x'] = train0_df['p1_pos_x'].fillna(value=0)\ntrain0_df['p1_pos_y'] = train0_df['p1_pos_y'].fillna(value=0)\ntrain0_df['p1_pos_z'] = train0_df['p1_pos_z'].fillna(value=0)\ntrain0_df['p1_vel_y'] = train0_df['p1_vel_y'].fillna(value=0)\ntrain0_df['p1_boost'] = train0_df['p1_boost'].fillna(value=0)\n\ntrain0_df['p3_pos_x'] = train0_df['p3_pos_x'].fillna(value=0)\ntrain0_df['p3_pos_y'] = train0_df['p3_pos_y'].fillna(value=0)\ntrain0_df['p3_pos_z'] = train0_df['p3_pos_z'].fillna(value=0)\ntrain0_df['p3_vel_y'] = train0_df['p3_vel_y'].fillna(value=0)\ntrain0_df['p3_boost'] = train0_df['p3_boost'].fillna(value=0)\n\ntrain0_df['p2_pos_x'] = train0_df['p2_pos_x'].fillna(value=0)\ntrain0_df['p2_pos_y'] = train0_df['p2_pos_y'].fillna(value=0)\ntrain0_df['p2_pos_z'] = train0_df['p2_pos_z'].fillna(value=0)\ntrain0_df['p2_vel_y'] = train0_df['p2_vel_y'].fillna(value=0)\ntrain0_df['p2_boost'] = train0_df['p2_boost'].fillna(value=0)\n\ntrain0_df['p4_pos_x'] = train0_df['p4_pos_x'].fillna(value=0)\ntrain0_df['p4_pos_y'] = train0_df['p4_pos_y'].fillna(value=0)\ntrain0_df['p4_pos_z'] = train0_df['p4_pos_z'].fillna(value=0)\ntrain0_df['p4_vel_y'] = train0_df['p4_vel_y'].fillna(value=0)\ntrain0_df['p4_boost'] = train0_df['p4_boost'].fillna(value=0)\n\ntrain0_df['p5_pos_x'] = train0_df['p5_pos_x'].fillna(value=0)\ntrain0_df['p5_pos_y'] = train0_df['p5_pos_y'].fillna(value=0)\ntrain0_df['p5_pos_z'] = train0_df['p5_pos_z'].fillna(value=0)\ntrain0_df['p5_vel_y'] = train0_df['p5_vel_y'].fillna(value=0)\ntrain0_df['p5_boost'] = train0_df['p5_boost'].fillna(value=0)\n\n","metadata":{"execution":{"iopub.status.busy":"2022-10-21T15:25:36.044634Z","iopub.execute_input":"2022-10-21T15:25:36.045566Z","iopub.status.idle":"2022-10-21T15:25:36.224362Z","shell.execute_reply.started":"2022-10-21T15:25:36.045531Z","shell.execute_reply":"2022-10-21T15:25:36.223379Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Train columns with null values:\\n', train0_df.isnull().sum())","metadata":{"execution":{"iopub.status.busy":"2022-10-21T15:25:36.225599Z","iopub.execute_input":"2022-10-21T15:25:36.225965Z","iopub.status.idle":"2022-10-21T15:25:36.445146Z","shell.execute_reply.started":"2022-10-21T15:25:36.225930Z","shell.execute_reply":"2022-10-21T15:25:36.444079Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(train0_df['p0_boost'].unique())\nprint(train0_df['boost0_timer'].unique())\n","metadata":{"execution":{"iopub.status.busy":"2022-10-21T15:25:36.446693Z","iopub.execute_input":"2022-10-21T15:25:36.447089Z","iopub.status.idle":"2022-10-21T15:25:36.485067Z","shell.execute_reply.started":"2022-10-21T15:25:36.447051Z","shell.execute_reply":"2022-10-21T15:25:36.484058Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train0_df.describe()","metadata":{"execution":{"iopub.status.busy":"2022-10-21T15:25:36.486393Z","iopub.execute_input":"2022-10-21T15:25:36.486942Z","iopub.status.idle":"2022-10-21T15:25:38.833688Z","shell.execute_reply.started":"2022-10-21T15:25:36.486906Z","shell.execute_reply":"2022-10-21T15:25:38.832766Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train0_df.info(verbose=True)","metadata":{"execution":{"iopub.status.busy":"2022-10-21T15:25:38.835296Z","iopub.execute_input":"2022-10-21T15:25:38.835969Z","iopub.status.idle":"2022-10-21T15:25:38.850259Z","shell.execute_reply.started":"2022-10-21T15:25:38.835930Z","shell.execute_reply":"2022-10-21T15:25:38.849123Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"features=['p0_boost','boost0_timer','p1_boost','boost1_timer','p2_boost','boost2_timer','p3_boost','boost3_timer'\n         ,'p4_boost','boost4_timer','p5_boost','boost5_timer']\n\nX = train0_df.copy()\ny = X.pop('team_A_scoring_within_10sec')\nX = X.loc[:, features]\n\n# Standardize\nX_scaled = (X - X.mean(axis=0)) / X.std(axis=0)","metadata":{"execution":{"iopub.status.busy":"2022-10-21T15:25:38.851940Z","iopub.execute_input":"2022-10-21T15:25:38.852291Z","iopub.status.idle":"2022-10-21T15:25:39.380548Z","shell.execute_reply.started":"2022-10-21T15:25:38.852256Z","shell.execute_reply":"2022-10-21T15:25:39.379565Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.decomposition import PCA\n\n# Create principal components\npca = PCA()\nX_pca = pca.fit_transform(X_scaled)\n\n# Convert to dataframe\ncomponent_names = [f\"PC{i+1}\" for i in range(X_pca.shape[1])]\nX_pca = pd.DataFrame(X_pca, columns=component_names)\n\n\nX_pca.head()","metadata":{"execution":{"iopub.status.busy":"2022-10-21T15:25:39.381976Z","iopub.execute_input":"2022-10-21T15:25:39.382591Z","iopub.status.idle":"2022-10-21T15:25:40.083075Z","shell.execute_reply.started":"2022-10-21T15:25:39.382549Z","shell.execute_reply":"2022-10-21T15:25:40.082163Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"loadings = pd.DataFrame(\n    pca.components_.T,  # transpose the matrix of loadings\n    columns=component_names,  # so the columns are the principal components\n    index=X.columns,  # and the rows are the original features\n)\nloadings","metadata":{"execution":{"iopub.status.busy":"2022-10-21T15:25:40.085020Z","iopub.execute_input":"2022-10-21T15:25:40.085563Z","iopub.status.idle":"2022-10-21T15:25:40.106853Z","shell.execute_reply.started":"2022-10-21T15:25:40.085525Z","shell.execute_reply":"2022-10-21T15:25:40.105935Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport numpy as np\nimport pandas as pd\nimport seaborn as sns\nfrom IPython.display import display\nfrom sklearn.feature_selection import mutual_info_regression\n\n\nplt.style.use(\"seaborn-whitegrid\")\nplt.rc(\"figure\", autolayout=True)\nplt.rc(\n    \"axes\",\n    labelweight=\"bold\",\n    labelsize=\"large\",\n    titleweight=\"bold\",\n    titlesize=14,\n    titlepad=10,\n)\n\n\ndef plot_variance(pca, width=8, dpi=100):\n    # Create figure\n    fig, axs = plt.subplots(1, 2)\n    n = pca.n_components_\n    grid = np.arange(1, n + 1)\n    # Explained variance\n    evr = pca.explained_variance_ratio_\n    axs[0].bar(grid, evr)\n    axs[0].set(\n        xlabel=\"Component\", title=\"% Explained Variance\", ylim=(0.0, 1.0)\n    )\n    # Cumulative Variance\n    cv = np.cumsum(evr)\n    axs[1].plot(np.r_[0, grid], np.r_[0, cv], \"o-\")\n    axs[1].set(\n        xlabel=\"Component\", title=\"% Cumulative Variance\", ylim=(0.0, 1.0)\n    )\n    # Set up figure\n    fig.set(figwidth=8, dpi=100)\n    return axs","metadata":{"execution":{"iopub.status.busy":"2022-10-21T15:25:40.108411Z","iopub.execute_input":"2022-10-21T15:25:40.109120Z","iopub.status.idle":"2022-10-21T15:25:40.119325Z","shell.execute_reply.started":"2022-10-21T15:25:40.109083Z","shell.execute_reply":"2022-10-21T15:25:40.118453Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_variance(pca)","metadata":{"execution":{"iopub.status.busy":"2022-10-21T15:25:40.120813Z","iopub.execute_input":"2022-10-21T15:25:40.121186Z","iopub.status.idle":"2022-10-21T15:25:40.520653Z","shell.execute_reply.started":"2022-10-21T15:25:40.121152Z","shell.execute_reply":"2022-10-21T15:25:40.519758Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def make_mi_scores(X, y, discrete_features):\n    mi_scores = mutual_info_regression(X, y, discrete_features=discrete_features)\n    mi_scores = pd.Series(mi_scores, name=\"MI Scores\", index=X.columns)\n    mi_scores = mi_scores.sort_values(ascending=False)\n    return mi_scores","metadata":{"execution":{"iopub.status.busy":"2022-10-21T15:25:40.522250Z","iopub.execute_input":"2022-10-21T15:25:40.522904Z","iopub.status.idle":"2022-10-21T15:25:40.528342Z","shell.execute_reply.started":"2022-10-21T15:25:40.522851Z","shell.execute_reply":"2022-10-21T15:25:40.527442Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mi_scores = make_mi_scores(X_pca, y, discrete_features=False)\nmi_scores","metadata":{"execution":{"iopub.status.busy":"2022-10-21T15:26:12.881741Z","iopub.execute_input":"2022-10-21T15:26:12.883182Z","iopub.status.idle":"2022-10-21T15:26:13.950597Z","shell.execute_reply.started":"2022-10-21T15:26:12.883133Z","shell.execute_reply":"2022-10-21T15:26:13.949258Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(train0_df.corrwith(train0_df['team_A_scoring_within_10sec']))","metadata":{"execution":{"iopub.status.busy":"2022-10-21T15:26:09.118636Z","iopub.status.idle":"2022-10-21T15:26:09.119128Z","shell.execute_reply.started":"2022-10-21T15:26:09.118885Z","shell.execute_reply":"2022-10-21T15:26:09.118909Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(train0_df.corrwith(train0_df['team_B_scoring_within_10sec']))","metadata":{"execution":{"iopub.status.busy":"2022-10-21T15:26:09.121144Z","iopub.status.idle":"2022-10-21T15:26:09.121627Z","shell.execute_reply.started":"2022-10-21T15:26:09.121385Z","shell.execute_reply":"2022-10-21T15:26:09.121407Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from lightgbm import LGBMClassifier\nfrom sklearn.model_selection import train_test_split\nX_all = train0_df[['ball_pos_x','ball_pos_y','ball_pos_z','ball_vel_y','p0_pos_x','p0_pos_y','p0_pos_z','p1_pos_x','p1_pos_y','p1_pos_z','p2_pos_x','p2_pos_y','p2_pos_z','p3_pos_x', 'p3_pos_y', 'p3_pos_z', 'p3_vel_y',\n'p4_pos_x', 'p4_pos_y', 'p4_pos_z','p5_pos_x', 'p5_pos_y','p5_pos_z',\"p0_vel_y\",\"p1_vel_y\"]]\ny_B = train0_df['team_B_scoring_within_10sec']\ny_A = train0_df['team_A_scoring_within_10sec']\n\nX_train_B, X_test_B, y_train_B, y_test_B = train_test_split(X_all, y_B, test_size=0.2, random_state=0)\nX_train_A, X_test_A, y_train_A, y_test_A = train_test_split(X_all, y_A, test_size=0.2, random_state=0)\n\nmodel = LGBMClassifier()\nmodel_2 = LGBMClassifier()","metadata":{"execution":{"iopub.status.busy":"2022-10-21T15:26:17.428416Z","iopub.execute_input":"2022-10-21T15:26:17.428783Z","iopub.status.idle":"2022-10-21T15:26:18.738989Z","shell.execute_reply.started":"2022-10-21T15:26:17.428752Z","shell.execute_reply":"2022-10-21T15:26:18.738010Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Fit\nmodel.fit(X_train_B, y_train_B)\nmodel_2.fit(X_train_A, y_train_A)\n\n# Predict\npreds_B = model.predict_proba(X_test_B)\npreds_A = model.predict_proba(X_test_A)\n#print(preds)","metadata":{"execution":{"iopub.status.busy":"2022-10-21T15:26:20.209277Z","iopub.execute_input":"2022-10-21T15:26:20.209638Z","iopub.status.idle":"2022-10-21T15:27:35.114944Z","shell.execute_reply.started":"2022-10-21T15:26:20.209606Z","shell.execute_reply":"2022-10-21T15:27:35.114036Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import accuracy_score, log_loss\nprint(\"Score B: \" + str(log_loss(y_test_B, preds_B)))\nprint(\"Score A: \" + str(log_loss(y_test_A, preds_A)))\n# Team A: 0.018523272329936634\n# Team B: 0.01772910624692792\n# Team B log loss: ","metadata":{"execution":{"iopub.status.busy":"2022-10-21T15:27:35.118787Z","iopub.execute_input":"2022-10-21T15:27:35.121192Z","iopub.status.idle":"2022-10-21T15:27:35.268268Z","shell.execute_reply.started":"2022-10-21T15:27:35.121159Z","shell.execute_reply":"2022-10-21T15:27:35.267117Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dtypes_df_test = pd.read_csv('/kaggle/input/tabular-playground-series-oct-2022/test_dtypes.csv')\ndtypes = {k: v for (k, v) in zip(dtypes_df_test.column, dtypes_df_test.dtype)}\ntest_df = pd.read_csv('/kaggle/input/tabular-playground-series-oct-2022/test.csv', dtype=dtypes)","metadata":{"execution":{"iopub.status.busy":"2022-10-21T15:27:35.269975Z","iopub.execute_input":"2022-10-21T15:27:35.270370Z","iopub.status.idle":"2022-10-21T15:27:39.698288Z","shell.execute_reply.started":"2022-10-21T15:27:35.270332Z","shell.execute_reply":"2022-10-21T15:27:39.697134Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#test_df[\"team_A_scoring_within_10sec\"] = test_df[\"team_A_scoring_within_10sec\"].astype(\"category\")\n#test_df[\"team_B_scoring_within_10sec\"] = test_df[\"team_B_scoring_within_10sec\"].astype(\"category\")\ntest_df[\"boost0_timer\"] = test_df[\"boost0_timer\"].astype(\"float32\")\ntest_df[\"boost1_timer\"] = test_df[\"boost1_timer\"].astype(\"float32\")\ntest_df[\"boost2_timer\"] = test_df[\"boost2_timer\"].astype(\"float32\")\ntest_df[\"boost3_timer\"] = test_df[\"boost3_timer\"].astype(\"float32\")\ntest_df[\"boost4_timer\"] = test_df[\"boost4_timer\"].astype(\"float32\")\ntest_df[\"boost5_timer\"] = test_df[\"boost5_timer\"].astype(\"float32\")\ntest_df[\"p0_boost\"] = test_df[\"p0_boost\"].astype(\"float32\")\ntest_df[\"p1_boost\"] = test_df[\"p1_boost\"].astype(\"float32\")\ntest_df[\"p2_boost\"] = test_df[\"p2_boost\"].astype(\"float32\")\ntest_df[\"p3_boost\"] = test_df[\"p3_boost\"].astype(\"float32\")\ntest_df[\"p4_boost\"] = test_df[\"p4_boost\"].astype(\"float32\")\ntest_df[\"p5_boost\"] = test_df[\"p5_boost\"].astype(\"float32\")","metadata":{"execution":{"iopub.status.busy":"2022-10-21T15:27:39.706019Z","iopub.execute_input":"2022-10-21T15:27:39.706865Z","iopub.status.idle":"2022-10-21T15:27:39.819148Z","shell.execute_reply.started":"2022-10-21T15:27:39.706820Z","shell.execute_reply":"2022-10-21T15:27:39.818062Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df = test_df.drop([\"ball_vel_x\",\"ball_vel_z\",\"p0_vel_x\",\"p0_vel_z\",\"p1_vel_x\",\"p1_vel_z\",\"p2_vel_x\",\"p2_vel_z\",\"p3_vel_x\",\n               \"p3_vel_z\",\"p4_vel_x\",\"p4_vel_z\",\"p5_vel_x\",\"p5_vel_z\"], axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-10-21T15:27:39.823911Z","iopub.execute_input":"2022-10-21T15:27:39.826510Z","iopub.status.idle":"2022-10-21T15:27:40.010067Z","shell.execute_reply.started":"2022-10-21T15:27:39.826470Z","shell.execute_reply":"2022-10-21T15:27:40.008976Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n#test_df['team_scoring_next'] = test_df['team_scoring_next'].fillna(value=\"None\")\ntest_df['p0_pos_x'] = test_df['p0_pos_x'].fillna(value=0)\ntest_df['p0_pos_y'] = test_df['p0_pos_y'].fillna(value=0)\ntest_df['p0_pos_z'] = test_df['p0_pos_z'].fillna(value=0)\ntest_df['p0_vel_y'] = test_df['p0_vel_y'].fillna(value=0)\ntest_df['p0_boost'] = test_df['p0_boost'].fillna(value=0)\n\ntest_df['p1_pos_x'] = test_df['p1_pos_x'].fillna(value=0)\ntest_df['p1_pos_y'] = test_df['p1_pos_y'].fillna(value=0)\ntest_df['p1_pos_z'] = test_df['p1_pos_z'].fillna(value=0)\ntest_df['p1_vel_y'] = test_df['p1_vel_y'].fillna(value=0)\ntest_df['p1_boost'] = test_df['p1_boost'].fillna(value=0)\n\ntest_df['p3_pos_x'] = test_df['p3_pos_x'].fillna(value=0)\ntest_df['p3_pos_y'] = test_df['p3_pos_y'].fillna(value=0)\ntest_df['p3_pos_z'] = test_df['p3_pos_z'].fillna(value=0)\ntest_df['p3_vel_y'] = test_df['p3_vel_y'].fillna(value=0)\ntest_df['p3_boost'] = test_df['p3_boost'].fillna(value=0)\n\ntest_df['p2_pos_x'] = test_df['p2_pos_x'].fillna(value=0)\ntest_df['p2_pos_y'] = test_df['p2_pos_y'].fillna(value=0)\ntest_df['p2_pos_z'] = test_df['p2_pos_z'].fillna(value=0)\ntest_df['p2_vel_y'] = test_df['p2_vel_y'].fillna(value=0)\ntest_df['p2_boost'] = test_df['p2_boost'].fillna(value=0)\n\ntest_df['p4_pos_x'] = test_df['p4_pos_x'].fillna(value=0)\ntest_df['p4_pos_y'] = test_df['p4_pos_y'].fillna(value=0)\ntest_df['p4_pos_z'] = test_df['p4_pos_z'].fillna(value=0)\ntest_df['p4_vel_y'] = test_df['p4_vel_y'].fillna(value=0)\ntest_df['p4_boost'] = test_df['p4_boost'].fillna(value=0)\n\ntest_df['p5_pos_x'] = test_df['p5_pos_x'].fillna(value=0)\ntest_df['p5_pos_y'] = test_df['p5_pos_y'].fillna(value=0)\ntest_df['p5_pos_z'] = test_df['p5_pos_z'].fillna(value=0)\ntest_df['p5_vel_y'] = test_df['p5_vel_y'].fillna(value=0)\ntest_df['p5_boost'] = test_df['p5_boost'].fillna(value=0)\n","metadata":{"execution":{"iopub.status.busy":"2022-10-21T15:27:54.993892Z","iopub.execute_input":"2022-10-21T15:27:54.994242Z","iopub.status.idle":"2022-10-21T15:27:55.066186Z","shell.execute_reply.started":"2022-10-21T15:27:54.994214Z","shell.execute_reply":"2022-10-21T15:27:55.065208Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import log_loss","metadata":{"execution":{"iopub.status.busy":"2022-10-21T15:26:09.138624Z","iopub.status.idle":"2022-10-21T15:26:09.139706Z","shell.execute_reply.started":"2022-10-21T15:26:09.139416Z","shell.execute_reply":"2022-10-21T15:26:09.139445Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_all_test = test_df[['ball_pos_x','ball_pos_y','ball_pos_z','ball_vel_y','p0_pos_x','p0_pos_y','p0_pos_z','p1_pos_x','p1_pos_y','p1_pos_z','p2_pos_x','p2_pos_y','p2_pos_z','p3_pos_x', 'p3_pos_y', 'p3_pos_z', 'p3_vel_y',\n'p4_pos_x', 'p4_pos_y', 'p4_pos_z','p5_pos_x', 'p5_pos_y','p5_pos_z',\"p0_vel_y\",\"p1_vel_y\"]]\ny_B_test = model.predict_proba(X_all_test)\ny_A_test = model_2.predict_proba(X_all_test)","metadata":{"execution":{"iopub.status.busy":"2022-10-21T15:27:59.542305Z","iopub.execute_input":"2022-10-21T15:27:59.542654Z","iopub.status.idle":"2022-10-21T15:28:04.061243Z","shell.execute_reply.started":"2022-10-21T15:27:59.542624Z","shell.execute_reply":"2022-10-21T15:28:04.060387Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.read_csv('../input/tabular-playground-series-oct-2022/sample_submission.csv', usecols = ['id'])\nsubmission['team_A_scoring_within_10sec'] = y_A_test[:,1]\nsubmission['team_B_scoring_within_10sec'] = y_B_test[:,1]\nsubmission.to_csv('submission.csv', index = False)\n\nprint('Submission saved')","metadata":{"execution":{"iopub.status.busy":"2022-10-21T15:28:08.369122Z","iopub.execute_input":"2022-10-21T15:28:08.369477Z","iopub.status.idle":"2022-10-21T15:28:10.588981Z","shell.execute_reply.started":"2022-10-21T15:28:08.369447Z","shell.execute_reply":"2022-10-21T15:28:10.587946Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}