{"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":"# 1️⃣Import Libraries","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nimport matplotlib.pyplot as plt\nimport numpy as np\nimport seaborn as sns\nimport warnings\nwarnings.filterwarnings('ignore')","metadata":{"execution":{"iopub.status.busy":"2022-10-05T03:44:19.747556Z","iopub.execute_input":"2022-10-05T03:44:19.748037Z","iopub.status.idle":"2022-10-05T03:44:20.540303Z","shell.execute_reply.started":"2022-10-05T03:44:19.747875Z","shell.execute_reply":"2022-10-05T03:44:20.539505Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 2️⃣Data Cleaning","metadata":{}},{"cell_type":"markdown","source":"Train data is too big. So sample some data from orginal data. RAM is limited.","metadata":{}},{"cell_type":"markdown","source":"I tried 3 train data but failed because it's too big.","metadata":{}},{"cell_type":"code","source":"df_1 = pd.read_csv(\"../input/tabular-playground-series-oct-2022/train_1.csv\")\ndf_1 = df_1.sample(frac=0.5)\ndf_5 = pd.read_csv(\"../input/tabular-playground-series-oct-2022/train_5.csv\")\ndf_5 = df_5.sample(frac=0.5)","metadata":{"execution":{"iopub.status.busy":"2022-10-05T03:44:20.543122Z","iopub.execute_input":"2022-10-05T03:44:20.544093Z","iopub.status.idle":"2022-10-05T03:45:21.323158Z","shell.execute_reply.started":"2022-10-05T03:44:20.544054Z","shell.execute_reply":"2022-10-05T03:45:21.322289Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = pd.concat([df_1, df_5])","metadata":{"execution":{"iopub.status.busy":"2022-10-05T03:45:21.324474Z","iopub.execute_input":"2022-10-05T03:45:21.324743Z","iopub.status.idle":"2022-10-05T03:45:21.668506Z","shell.execute_reply.started":"2022-10-05T03:45:21.324709Z","shell.execute_reply":"2022-10-05T03:45:21.667686Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Now we have data to train.","metadata":{}},{"cell_type":"code","source":"data.head()","metadata":{"execution":{"iopub.status.busy":"2022-10-05T03:45:21.670813Z","iopub.execute_input":"2022-10-05T03:45:21.671337Z","iopub.status.idle":"2022-10-05T03:45:21.726715Z","shell.execute_reply.started":"2022-10-05T03:45:21.671296Z","shell.execute_reply":"2022-10-05T03:45:21.725346Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.describe()","metadata":{"execution":{"iopub.status.busy":"2022-10-05T03:45:21.731805Z","iopub.execute_input":"2022-10-05T03:45:21.732832Z","iopub.status.idle":"2022-10-05T03:45:26.196679Z","shell.execute_reply.started":"2022-10-05T03:45:21.732786Z","shell.execute_reply":"2022-10-05T03:45:26.195896Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.info()","metadata":{"execution":{"iopub.status.busy":"2022-10-05T03:45:26.198075Z","iopub.execute_input":"2022-10-05T03:45:26.198726Z","iopub.status.idle":"2022-10-05T03:45:26.217286Z","shell.execute_reply.started":"2022-10-05T03:45:26.198687Z","shell.execute_reply":"2022-10-05T03:45:26.216344Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Let's check NaN value in data.","metadata":{}},{"cell_type":"code","source":"data.isna().sum()","metadata":{"execution":{"iopub.status.busy":"2022-10-05T03:45:26.219367Z","iopub.execute_input":"2022-10-05T03:45:26.219653Z","iopub.status.idle":"2022-10-05T03:45:26.522379Z","shell.execute_reply.started":"2022-10-05T03:45:26.219617Z","shell.execute_reply":"2022-10-05T03:45:26.521376Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Check test data","metadata":{}},{"cell_type":"code","source":"test_data = pd.read_csv(\"../input/tabular-playground-series-oct-2022/test.csv\")\ntest_data.head()","metadata":{"execution":{"iopub.status.busy":"2022-10-05T03:45:26.523942Z","iopub.execute_input":"2022-10-05T03:45:26.524230Z","iopub.status.idle":"2022-10-05T03:45:35.579772Z","shell.execute_reply.started":"2022-10-05T03:45:26.524195Z","shell.execute_reply":"2022-10-05T03:45:35.579020Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data.info()","metadata":{"execution":{"iopub.status.busy":"2022-10-05T03:45:35.581320Z","iopub.execute_input":"2022-10-05T03:45:35.581609Z","iopub.status.idle":"2022-10-05T03:45:35.669983Z","shell.execute_reply.started":"2022-10-05T03:45:35.581573Z","shell.execute_reply":"2022-10-05T03:45:35.668823Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data.isna().mean()","metadata":{"execution":{"iopub.status.busy":"2022-10-05T03:45:35.673840Z","iopub.execute_input":"2022-10-05T03:45:35.674092Z","iopub.status.idle":"2022-10-05T03:45:35.737526Z","shell.execute_reply.started":"2022-10-05T03:45:35.674060Z","shell.execute_reply":"2022-10-05T03:45:35.736674Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"train data, test data have nan values. But we will use LightGBM which is based on deicision tree. So we don't have to deal with those!","metadata":{}},{"cell_type":"markdown","source":"# 3️⃣EDA","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(12,10))\nsns.heatmap(data.corr())\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-10-05T03:45:35.738725Z","iopub.execute_input":"2022-10-05T03:45:35.739311Z","iopub.status.idle":"2022-10-05T03:45:55.665626Z","shell.execute_reply.started":"2022-10-05T03:45:35.739281Z","shell.execute_reply":"2022-10-05T03:45:55.664969Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(12,10))\nsns.countplot(x=\"team_A_scoring_within_10sec\", data=data)\nplt.title(\"team A score\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-10-05T03:45:55.666629Z","iopub.execute_input":"2022-10-05T03:45:55.666883Z","iopub.status.idle":"2022-10-05T03:45:56.105510Z","shell.execute_reply.started":"2022-10-05T03:45:55.666839Z","shell.execute_reply":"2022-10-05T03:45:56.104802Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(12,10))\nsns.countplot(x=\"team_B_scoring_within_10sec\", data=data)\nplt.title(\"team B score\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-10-05T03:45:56.106646Z","iopub.execute_input":"2022-10-05T03:45:56.109114Z","iopub.status.idle":"2022-10-05T03:45:56.427671Z","shell.execute_reply.started":"2022-10-05T03:45:56.109073Z","shell.execute_reply":"2022-10-05T03:45:56.426896Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"team A score number : \",len(data[data[\"team_A_scoring_within_10sec\"] == 1]))\nprint(\"team B score number : \",len(data[data[\"team_B_scoring_within_10sec\"] == 1]))","metadata":{"execution":{"iopub.status.busy":"2022-10-05T03:45:56.428986Z","iopub.execute_input":"2022-10-05T03:45:56.430110Z","iopub.status.idle":"2022-10-05T03:45:56.600219Z","shell.execute_reply.started":"2022-10-05T03:45:56.430072Z","shell.execute_reply":"2022-10-05T03:45:56.599434Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(12,10))\nplt.bar(0, len(data[data[\"team_A_scoring_within_10sec\"] == 1]), label=\"A\")\nplt.bar(1, len(data[data[\"team_B_scoring_within_10sec\"] == 1]), label=\"B\")\nplt.legend()\nplt.title(\"score number\")\nplt.ylabel(\"Score\", fontsize=20)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-10-05T03:45:56.601814Z","iopub.execute_input":"2022-10-05T03:45:56.602387Z","iopub.status.idle":"2022-10-05T03:45:56.994661Z","shell.execute_reply.started":"2022-10-05T03:45:56.602346Z","shell.execute_reply":"2022-10-05T03:45:56.993856Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"A more scored than B","metadata":{}},{"cell_type":"markdown","source":"let's think about ball position","metadata":{}},{"cell_type":"code","source":"data[\"ball_position\"] = data[\"ball_pos_x\"] +data[\"ball_pos_y\"] + data[\"ball_pos_z\"]","metadata":{"execution":{"iopub.status.busy":"2022-10-05T03:45:56.995989Z","iopub.execute_input":"2022-10-05T03:45:56.996251Z","iopub.status.idle":"2022-10-05T03:45:57.015204Z","shell.execute_reply.started":"2022-10-05T03:45:56.996221Z","shell.execute_reply":"2022-10-05T03:45:57.014461Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"posA = len(data[(data['ball_position'] > 90 ) & (data['team_A_scoring_within_10sec']==1)])\nposB = len(data[(data['ball_position'] > 90 ) & (data['team_B_scoring_within_10sec']==1)])\nprint(posA), print(posB)","metadata":{"execution":{"iopub.status.busy":"2022-10-05T03:45:57.016577Z","iopub.execute_input":"2022-10-05T03:45:57.016850Z","iopub.status.idle":"2022-10-05T03:45:57.693333Z","shell.execute_reply.started":"2022-10-05T03:45:57.016815Z","shell.execute_reply":"2022-10-05T03:45:57.692598Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(12,10))\nplt.bar(0, posA, label=\"A\")\nplt.bar(1, posB, label=\"B\")\nplt.legend()\nplt.ylabel(\"Score\", fontsize=20)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-10-05T03:45:57.694669Z","iopub.execute_input":"2022-10-05T03:45:57.694939Z","iopub.status.idle":"2022-10-05T03:45:58.054682Z","shell.execute_reply.started":"2022-10-05T03:45:57.694903Z","shell.execute_reply":"2022-10-05T03:45:58.053995Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**As you can see, if ball_position is high value, team A scored probability is higher than team B!**","metadata":{}},{"cell_type":"markdown","source":"let's think about velocity of ball","metadata":{}},{"cell_type":"code","source":"data['ball_velocity'] =data['ball_vel_x']+data['ball_vel_y']+data['ball_vel_z']","metadata":{"execution":{"iopub.status.busy":"2022-10-05T03:45:58.056014Z","iopub.execute_input":"2022-10-05T03:45:58.056260Z","iopub.status.idle":"2022-10-05T03:45:58.074171Z","shell.execute_reply.started":"2022-10-05T03:45:58.056228Z","shell.execute_reply":"2022-10-05T03:45:58.073442Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"velA = len(data[(data['ball_velocity'] < 90 ) & (data['team_A_scoring_within_10sec']==1)])\nvelB = len(data[(data['ball_velocity'] < 90)& (data['team_B_scoring_within_10sec']==1)])\nprint(velA, velB)","metadata":{"execution":{"iopub.status.busy":"2022-10-05T03:45:58.075567Z","iopub.execute_input":"2022-10-05T03:45:58.075869Z","iopub.status.idle":"2022-10-05T03:45:58.856821Z","shell.execute_reply.started":"2022-10-05T03:45:58.075834Z","shell.execute_reply":"2022-10-05T03:45:58.856020Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.drop([\"ball_position\", \"ball_velocity\"], axis=1, inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-10-05T03:45:58.858267Z","iopub.execute_input":"2022-10-05T03:45:58.859049Z","iopub.status.idle":"2022-10-05T03:45:59.169365Z","shell.execute_reply.started":"2022-10-05T03:45:58.859010Z","shell.execute_reply":"2022-10-05T03:45:59.168549Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 4️⃣Modeling for team A","metadata":{}},{"cell_type":"code","source":"xA = data.iloc[:, 3:-4]\nyA = data[\"team_A_scoring_within_10sec\"]","metadata":{"execution":{"iopub.status.busy":"2022-10-05T03:45:59.170771Z","iopub.execute_input":"2022-10-05T03:45:59.171319Z","iopub.status.idle":"2022-10-05T03:45:59.440060Z","shell.execute_reply.started":"2022-10-05T03:45:59.171279Z","shell.execute_reply":"2022-10-05T03:45:59.439194Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import lightgbm as lgb","metadata":{"execution":{"iopub.status.busy":"2022-10-05T03:45:59.441459Z","iopub.execute_input":"2022-10-05T03:45:59.441722Z","iopub.status.idle":"2022-10-05T03:46:01.738192Z","shell.execute_reply.started":"2022-10-05T03:45:59.441689Z","shell.execute_reply":"2022-10-05T03:46:01.737270Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_A_pre = lgb.LGBMClassifier()","metadata":{"execution":{"iopub.status.busy":"2022-10-05T03:46:01.739587Z","iopub.execute_input":"2022-10-05T03:46:01.739829Z","iopub.status.idle":"2022-10-05T03:46:01.744985Z","shell.execute_reply.started":"2022-10-05T03:46:01.739797Z","shell.execute_reply":"2022-10-05T03:46:01.744045Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_A_pre.fit(xA, yA)","metadata":{"execution":{"iopub.status.busy":"2022-10-05T03:46:01.746554Z","iopub.execute_input":"2022-10-05T03:46:01.746827Z","iopub.status.idle":"2022-10-05T03:47:36.071545Z","shell.execute_reply.started":"2022-10-05T03:46:01.746785Z","shell.execute_reply":"2022-10-05T03:47:36.070906Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"let's see what is the most important feature of model_A","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(15,10))\nA_imp = pd.DataFrame({\"columns\" : xA.columns, \"importance\" : model_A_pre.feature_importances_}).sort_values(\"importance\", ascending=False)\nsns.barplot(x=\"importance\", y=\"columns\", data=A_imp)\nplt.title(\"model_A feature importance\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-10-05T03:47:36.074721Z","iopub.execute_input":"2022-10-05T03:47:36.076541Z","iopub.status.idle":"2022-10-05T03:47:37.066643Z","shell.execute_reply.started":"2022-10-05T03:47:36.076509Z","shell.execute_reply":"2022-10-05T03:47:37.065874Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 5️⃣Modeling for team B","metadata":{}},{"cell_type":"code","source":"xB = data.iloc[:, 3:-4]\nyB = data[\"team_B_scoring_within_10sec\"]","metadata":{"execution":{"iopub.status.busy":"2022-10-05T03:47:37.068121Z","iopub.execute_input":"2022-10-05T03:47:37.068996Z","iopub.status.idle":"2022-10-05T03:47:37.344000Z","shell.execute_reply.started":"2022-10-05T03:47:37.068943Z","shell.execute_reply":"2022-10-05T03:47:37.343135Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_B_pre = lgb.LGBMClassifier()","metadata":{"execution":{"iopub.status.busy":"2022-10-05T03:47:37.348303Z","iopub.execute_input":"2022-10-05T03:47:37.348539Z","iopub.status.idle":"2022-10-05T03:47:37.353656Z","shell.execute_reply.started":"2022-10-05T03:47:37.348512Z","shell.execute_reply":"2022-10-05T03:47:37.352712Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_B_pre.fit(xB, yB)","metadata":{"execution":{"iopub.status.busy":"2022-10-05T03:47:37.356037Z","iopub.execute_input":"2022-10-05T03:47:37.356614Z","iopub.status.idle":"2022-10-05T03:49:11.312870Z","shell.execute_reply.started":"2022-10-05T03:47:37.356573Z","shell.execute_reply":"2022-10-05T03:49:11.312252Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_B_pre.feature_importances_","metadata":{"execution":{"iopub.status.busy":"2022-10-05T03:49:11.316692Z","iopub.execute_input":"2022-10-05T03:49:11.318359Z","iopub.status.idle":"2022-10-05T03:49:11.325781Z","shell.execute_reply.started":"2022-10-05T03:49:11.318323Z","shell.execute_reply":"2022-10-05T03:49:11.324854Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(15,10))\nB_imp = pd.DataFrame({\"columns\" : xB.columns, \"importance\" : model_B_pre.feature_importances_}).sort_values(\"importance\", ascending=False)\nsns.barplot(x=\"importance\", y=\"columns\", data=B_imp)\nplt.title(\"model_B feature importance\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-10-05T03:49:11.327200Z","iopub.execute_input":"2022-10-05T03:49:11.328236Z","iopub.status.idle":"2022-10-05T03:49:12.311324Z","shell.execute_reply.started":"2022-10-05T03:49:11.328196Z","shell.execute_reply":"2022-10-05T03:49:12.310522Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"We can see the priority of feature importance if different between model_A and model_B.","metadata":{}},{"cell_type":"markdown","source":"# 6️⃣Predict","metadata":{}},{"cell_type":"code","source":"x_test = test_data.iloc[:, 1:]","metadata":{"execution":{"iopub.status.busy":"2022-10-05T03:49:12.312809Z","iopub.execute_input":"2022-10-05T03:49:12.313096Z","iopub.status.idle":"2022-10-05T03:49:12.405260Z","shell.execute_reply.started":"2022-10-05T03:49:12.313062Z","shell.execute_reply":"2022-10-05T03:49:12.404369Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"We want to know the probability of score. Not True of False.","metadata":{}},{"cell_type":"code","source":"predA = model_A_pre.predict_proba(x_test)[:, 1]\npredB = model_B_pre.predict_proba(x_test)[:, 1]","metadata":{"execution":{"iopub.status.busy":"2022-10-05T03:49:12.406920Z","iopub.execute_input":"2022-10-05T03:49:12.407457Z","iopub.status.idle":"2022-10-05T03:49:19.933125Z","shell.execute_reply.started":"2022-10-05T03:49:12.407417Z","shell.execute_reply":"2022-10-05T03:49:19.932442Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 7️⃣Submit","metadata":{}},{"cell_type":"code","source":"submission = pd.read_csv(\"../input/tabular-playground-series-oct-2022/sample_submission.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-10-05T03:49:19.934362Z","iopub.execute_input":"2022-10-05T03:49:19.934916Z","iopub.status.idle":"2022-10-05T03:49:20.122150Z","shell.execute_reply.started":"2022-10-05T03:49:19.934869Z","shell.execute_reply":"2022-10-05T03:49:20.121298Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission[\"team_A_scoring_within_10sec\"] = predA\nsubmission[\"team_B_scoring_within_10sec\"] = predB\nsubmission.to_csv(\"submission.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2022-10-05T03:49:20.123461Z","iopub.execute_input":"2022-10-05T03:49:20.123721Z","iopub.status.idle":"2022-10-05T03:49:22.434638Z","shell.execute_reply.started":"2022-10-05T03:49:20.123687Z","shell.execute_reply":"2022-10-05T03:49:22.433627Z"},"trusted":true},"execution_count":null,"outputs":[]}]}