{"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","_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-08-13T08:29:35.277040Z","iopub.execute_input":"2022-08-13T08:29:35.278149Z","iopub.status.idle":"2022-08-13T08:29:35.287843Z","shell.execute_reply.started":"2022-08-13T08:29:35.278098Z","shell.execute_reply":"2022-08-13T08:29:35.286574Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **1. Introduction**\n\nInjury prevention is a critical component in modern athletics. Sports that involve animals, such as horse racing, are no different than human sport. Typically, efficiency in movement correlates to both improvements in performance and injury prevention.\n\nA wealth of data is now collected, including measures for heart rate, EKG, longitudinal movement, dorsal/ventral movement, medial/lateral deviation, total power and total landing vibration. Your data science skills and analysis are needed to decipher what makes the most positive impact.\n\nIn this competition, you will create a model to interpret one aspect of this new data. You’ll be among the first to access X/Y coordinate mapping of horses during races. Using the data, you might analyze jockey decision making, compare race surfaces, or measure the relative importance of drafting. With considerable data, contestants can flex their creativity problem solving skills.\n\nData for year 2019\n\n#### **Goals**\n\nYour challenge is to generate actionable, practical, and novel insights from horse tracking data that devises innovative and data-driven approaches to analyzing racing tactics, drafting strategies and path efficiency. There are several potential topics for participants to analyze.\n\nThese include, but are not limited to:\n\n* Create a horse rating measuring expected finish position versus actual finish position. How does a horse’s expected finish position change through the running of a race? Does this metric rely solely on a horse’s own position or is it influenced by the position of competitors?\n* What are optimal racing strategies? Considering different venues, surfaces and race distances. Create a jockey rating based upon path efficiency?\n* Create a surface measure model which would rate the fairness of different paths on a racecourse that may be beneficial or harmful to finish position based. This may be a result of unknown barometric, weather or maintenance factors.\n* Create a model measuring the existence (or not) and relevance of a drafting benefit.\n* Create a model reveal optimal gait patterns. Does the model differ for such factors as age, distance, race section or surface?\n\n**WOULD APPRECIATE IF YOU COULD LEAVE AN UPVOTE! :)**","metadata":{}},{"cell_type":"markdown","source":"#### Establishing Environment","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport seaborn as sns","metadata":{"execution":{"iopub.status.busy":"2022-08-13T08:29:35.314131Z","iopub.execute_input":"2022-08-13T08:29:35.315255Z","iopub.status.idle":"2022-08-13T08:29:36.498130Z","shell.execute_reply.started":"2022-08-13T08:29:35.315206Z","shell.execute_reply":"2022-08-13T08:29:36.496815Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **2. Understanding the Shape of Data**","metadata":{}},{"cell_type":"code","source":"derby_df = pd.read_csv(\"../input/big-data-derby-2022/nyra_2019_complete.csv\")\nderby_df['race_date']= pd.to_datetime(derby_df['race_date'],format='%Y-%m-%d')\nderby_df['month']= derby_df['race_date'].dt.month\nderby_df['day']= derby_df['race_date'].dt.day\n\nderby_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-13T09:23:30.698310Z","iopub.execute_input":"2022-08-13T09:23:30.698852Z","iopub.status.idle":"2022-08-13T09:23:45.118223Z","shell.execute_reply.started":"2022-08-13T09:23:30.698816Z","shell.execute_reply":"2022-08-13T09:23:45.116889Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"derby_df.info()","metadata":{"execution":{"iopub.status.busy":"2022-08-13T08:29:57.000637Z","iopub.execute_input":"2022-08-13T08:29:57.001296Z","iopub.status.idle":"2022-08-13T08:29:57.024138Z","shell.execute_reply.started":"2022-08-13T08:29:57.001256Z","shell.execute_reply":"2022-08-13T08:29:57.022549Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"derby_df.describe()","metadata":{"execution":{"iopub.status.busy":"2022-08-13T08:29:57.027796Z","iopub.execute_input":"2022-08-13T08:29:57.028319Z","iopub.status.idle":"2022-08-13T08:29:59.605408Z","shell.execute_reply.started":"2022-08-13T08:29:57.028273Z","shell.execute_reply":"2022-08-13T08:29:59.603616Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"derby_df.isna().sum()","metadata":{"execution":{"iopub.status.busy":"2022-08-13T08:29:59.607263Z","iopub.execute_input":"2022-08-13T08:29:59.607745Z","iopub.status.idle":"2022-08-13T08:30:01.065876Z","shell.execute_reply.started":"2022-08-13T08:29:59.607705Z","shell.execute_reply":"2022-08-13T08:30:01.064810Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **3. Data Cleaning and Exploration**","metadata":{}},{"cell_type":"markdown","source":"## **3.1 Basic Exploration**","metadata":{}},{"cell_type":"code","source":"fig = plt.figure(figsize = (30,20))\nax1 = fig.add_subplot(3,3,1)\nax2 = fig.add_subplot(3,3,2)\nax3 = fig.add_subplot(3,3,3)\nax4 = fig.add_subplot(3,3,4)\nax5 = fig.add_subplot(3,3,5)\nax6 = fig.add_subplot(3,3,6)\n\nsns.countplot(x=derby_df[\"track_id\"],ax=ax1)\nax1.set_title(\"Track Usage\")\n\nsns.countplot(y=derby_df[\"course_type\"],ax=ax2)\nax2.set_title(\"Type of Race Course\")\n\nderby_month_gb = derby_df.groupby(\"month\")\nmonthly_race_number=derby_month_gb[\"race_number\"].count()\ntotal_race_number=derby_df[\"race_number\"].count()\npercentage_race_number=monthly_race_number/total_race_number\nlabels = list(derby_month_gb.groups)\n# months = [\"Jan\",\"Feb\",\"Mar\",\"Apr\",\"May\",\"Jun\",\"Jul\",\"Aug\",\"Sep\",\"Oct\",\"Nov\",\"Dec\"]\nax3.pie(percentage_race_number,labels=labels, autopct='%.2f',\n        explode=[0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1])\nax3.set_title(\"Percentage of Races Conducted by Month\")\n\nsns.countplot(x=derby_df[\"track_condition\"],ax=ax4)\nax4.set_title(\"Track Condition\")\n\nsns.countplot(y=derby_df[\"race_type\"],ax=ax5)\nax5.set_title(\"Type of Race\")\n\nsns.distplot(derby_df[\"weight_carried\"],ax=ax6)\nax6.set_title(\"Distirbution of Weight Carried\")\n\nfig.suptitle(\"Basic Exploration\")\nplt.tight_layout\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-13T09:18:08.470909Z","iopub.execute_input":"2022-08-13T09:18:08.471408Z","iopub.status.idle":"2022-08-13T09:18:43.449904Z","shell.execute_reply.started":"2022-08-13T09:18:08.471368Z","shell.execute_reply":"2022-08-13T09:18:43.448646Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"* Can consder grouping instead of months, by seasons\n* Subsample required? Conditions very skewed (Track used, track condition, type of course, type of race)","metadata":{}},{"cell_type":"code","source":"# Number of races\nrace_bar = sns.countplot(x=derby_df[\"race_number\"])\nrace_bar","metadata":{"execution":{"iopub.status.busy":"2022-08-13T08:30:10.705219Z","iopub.execute_input":"2022-08-13T08:30:10.705761Z","iopub.status.idle":"2022-08-13T08:30:11.500378Z","shell.execute_reply.started":"2022-08-13T08:30:10.705711Z","shell.execute_reply":"2022-08-13T08:30:11.499173Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"derby_df1 = derby_df.copy()","metadata":{"execution":{"iopub.status.busy":"2022-08-13T08:30:11.502002Z","iopub.execute_input":"2022-08-13T08:30:11.502393Z","iopub.status.idle":"2022-08-13T08:30:12.622686Z","shell.execute_reply.started":"2022-08-13T08:30:11.502360Z","shell.execute_reply":"2022-08-13T08:30:12.621460Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **3.2 Track AQU**","metadata":{}},{"cell_type":"code","source":"cond1 = derby_df1[\"track_id\"] == \"AQU\"\nderby_df1_AQU = derby_df1[cond1]\nderby_df1_AQU.head()\nderby_df1_AQU[\"track_id\"].unique()","metadata":{"execution":{"iopub.status.busy":"2022-08-13T08:30:12.626095Z","iopub.execute_input":"2022-08-13T08:30:12.626507Z","iopub.status.idle":"2022-08-13T08:30:13.507681Z","shell.execute_reply.started":"2022-08-13T08:30:12.626473Z","shell.execute_reply":"2022-08-13T08:30:13.506405Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig=plt.figure(figsize=(30,10))\nax1 = fig.add_subplot(111)\nsns.scatterplot(data=derby_df1_AQU,x = \"longitude\", y = \"latitude\", hue = \"race_number\",label = \"race_number\", palette = \"Set2\")\nax1.set_title(\"Track: AQU\")\nplt.legend()","metadata":{"execution":{"iopub.status.busy":"2022-08-13T08:30:13.509019Z","iopub.execute_input":"2022-08-13T08:30:13.509376Z","iopub.status.idle":"2022-08-13T08:31:13.675825Z","shell.execute_reply.started":"2022-08-13T08:30:13.509344Z","shell.execute_reply":"2022-08-13T08:31:13.674732Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **3.3 Track BEL**","metadata":{}},{"cell_type":"code","source":"cond1 = derby_df1[\"track_id\"] == \"BEL\"\nderby_df1_BEL = derby_df1[cond1]\nderby_df1_BEL.head()\nderby_df1_BEL[\"track_id\"].unique()","metadata":{"execution":{"iopub.status.busy":"2022-08-13T08:31:13.677280Z","iopub.execute_input":"2022-08-13T08:31:13.678750Z","iopub.status.idle":"2022-08-13T08:31:14.876206Z","shell.execute_reply.started":"2022-08-13T08:31:13.678703Z","shell.execute_reply":"2022-08-13T08:31:14.874847Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig=plt.figure(figsize=(30,10))\nax1 = fig.add_subplot(111)\nsns.scatterplot(data=derby_df1_BEL,x = \"longitude\", y = \"latitude\", hue = \"race_number\",label = \"race_number\", palette = \"Set2\")\nax1.set_title(\"Track: BEL\")\nplt.legend()","metadata":{"execution":{"iopub.status.busy":"2022-08-13T08:31:14.877766Z","iopub.execute_input":"2022-08-13T08:31:14.878326Z","iopub.status.idle":"2022-08-13T08:32:04.710684Z","shell.execute_reply.started":"2022-08-13T08:31:14.878287Z","shell.execute_reply":"2022-08-13T08:32:04.709380Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **3.3 Track SAR**\n\n","metadata":{}},{"cell_type":"code","source":"cond1 = derby_df1[\"track_id\"] == \"SAR\"\nderby_df1_SAR = derby_df1[cond1]\nderby_df1_SAR.head()\nderby_df1_SAR[\"track_id\"].unique()","metadata":{"execution":{"iopub.status.busy":"2022-08-13T08:32:04.713479Z","iopub.execute_input":"2022-08-13T08:32:04.714254Z","iopub.status.idle":"2022-08-13T08:32:05.400594Z","shell.execute_reply.started":"2022-08-13T08:32:04.714171Z","shell.execute_reply":"2022-08-13T08:32:05.398929Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig=plt.figure(figsize=(30,10))\nax1 = fig.add_subplot(111)\nsns.scatterplot(data=derby_df1_SAR,x = \"longitude\", y = \"latitude\", hue = \"race_number\",label = \"race_number\", palette = \"Set2\")\nax1.set_title(\"Track: SAR\")\nplt.legend()","metadata":{"execution":{"iopub.status.busy":"2022-08-13T08:32:05.402154Z","iopub.execute_input":"2022-08-13T08:32:05.402564Z","iopub.status.idle":"2022-08-13T08:32:37.102324Z","shell.execute_reply.started":"2022-08-13T08:32:05.402528Z","shell.execute_reply":"2022-08-13T08:32:37.101361Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **3.4 Correlation Matrix**","metadata":{}},{"cell_type":"code","source":"fig = plt.figure(figsize = (18,20))\nax1 = fig.add_subplot(211)\n\nsns.heatmap(derby_df1.corr(),cmap = \"coolwarm\",vmin = -1, vmax = 1,annot=True)\nax1.set_title(\"Correlation of features to winnning odds\")\n\nplt.tight_layout\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-13T08:32:37.103755Z","iopub.execute_input":"2022-08-13T08:32:37.104985Z","iopub.status.idle":"2022-08-13T08:32:42.373305Z","shell.execute_reply.started":"2022-08-13T08:32:37.104918Z","shell.execute_reply":"2022-08-13T08:32:42.372161Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}