{"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":"# Big Data Derby 2022","metadata":{}},{"cell_type":"markdown","source":"Analyze horse racing data to improve the health of the horse and strategy of competition","metadata":{}},{"cell_type":"markdown","source":"ID's","metadata":{}},{"cell_type":"markdown","source":"\ntrack_id - 3 character id for the track the race took place at. AQU -Aqueduct, BEL - Belmont, SAR - Saratoga.\n\nrace_date - date the race took place. YYYY-MM-DD.\n\nrace_number - Number of the race. Passed as 3 characters but can be cast or converted to int for this data set.\n\nprogram_number - Program number of the horse in the race passed as 3 characters. Should remain 3 characters as it isn't limited to just numbers. Is essentially the unique identifier of the horse in the race.\n\ntrakus_index - The common collection of point of the lat / long of the horse in the race passed as an integer. From what we can tell, it's collected every 0.25 seconds.\n\nlatitude - The latitude of the horse in the race passed as a float.\n\nlongitude - The longitude of the horse in the race passed as a float.\n\ndistance_id - Distance of the race in furlongs passed as an integer. Example - 600 would be 6 furlongs.\n\ncourse_type - The course the race was run over passed as one character. M - Hurdle, D - Dirt, O - Outer turf, I - Inner turf, T - turf.\n\ntrack_condition - The condition of the course the race was run on passed as three characters. YL - Yielding, FM - Firm, SY - Sloppy, GD - Good, FT - Fast, MY - Muddy, SF - Soft.\n\nrun_up_distance - Distance in feet of the gate to the start of the race passed as an integer.\n\nrace_type - The classification of the race passed as as five characters. STK - Stakes, WCL - Waiver Claiming, WMC - Waiver Maiden Claiming, SST - Starter Stakes, SHP - Starter Handicap, CLM - Claiming, STR - Starter Allowance, AOC - Allowance Optionl Claimer, SOC - Starter Optional Claimer, MCL - Maiden Claiming, ALW - Allowance, MSW - Maiden Special Weight.\n\npurse - Purse in US dollars of the race passed as an money with two decimal places.\n\npost_time - Time of day the race began passed as 5 character. Example - 01220 would be 12:20.\n\nweight_carried - An integer of the weight carried by the horse in the race.\n\njockey - Name of the jockey on the horse in the race. 50 character max.\n\nodds - Odds to win the race passed as an integer. Divide by 100 to derive the odds to 1. Example - 1280 would be 12.8-1.\n","metadata":{}},{"cell_type":"markdown","source":"## Importing libraries","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport plotly.express as px\nimport plotly.graph_objects as go","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Reading data","metadata":{}},{"cell_type":"code","source":"path = '/kaggle/input/big-data-derby-2022/'\nnyra_2019 = pd.read_csv(path+'nyra_2019_complete.csv', low_memory = False)\nnyra_race = pd.read_csv(path+'nyra_race_table.csv', low_memory = False)\nnyra_start = pd.read_csv(path+'nyra_start_table.csv', low_memory = False)\nnyra_tracking = pd.read_csv(path+'nyra_tracking_table.csv', low_memory = False)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nyra_2019.head()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nyra_tracking.head()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nyra_race.head()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nyra_start.head()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nyra_2019.describe().style.background_gradient(cmap = \"Purples\")","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nyra_tracking.track_id.value_counts()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nyra_tracking.race_number.value_counts()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nyra_tracking.program_number.value_counts()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nyra_2019['race_date'] = pd.to_datetime(nyra_2019['race_date'])\nnyra_2019['day'], nyra_2019['month'] = nyra_2019['race_date'].dt.day, nyra_2019['race_date'].dt.month\n","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nyra_2019","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Working with race data","metadata":{}},{"cell_type":"code","source":"class RaceAnalysis:\n    \n    def __init__(self, track, date, race_number):\n        self.track = track\n        self.date = date\n        self.race_number = race_number\n\n    def get_race_data(self):\n        '''\n        Get data of 1 race\n        '''\n\n        race_data = nyra_2019.loc[(nyra_tracking.track_id == self.track) \n                          & (nyra_tracking.race_date == self.date)\n                          & (nyra_tracking.race_number == self.race_number)\n                          ]\n        return race_data\n\n    def get_plot_race_way(self, race_data):\n        '''\n        Get plot\n        '''\n        plt.style.use(\"dark_background\")\n        plt.figure(figsize = (16, 8))\n        sns.scatterplot(data = race_data, \n                        x = \"longitude\", \n                        y = \"latitude\", \n                        hue = \"program_number\", \n                        palette = \"Paired\"\n                       )\n\n        plt.title(f'Race date {date}\\nRace number {race_number}')\n        plt.legend()\n        \n    def get_animated_race(self, race_data):\n\n        \n        lat = race_data.latitude.sort_values()\n        min_lat = lat.iloc[0]\n        max_lat = lat.iloc[-1]\n\n        long = race_data.longitude.sort_values()\n        min_long = long.iloc[0]\n        max_long = long.iloc[-1]\n\n            \n        fig = px.scatter(race_data.sort_values('trakus_index'), \n                   x=\"longitude\", \n                   y=\"latitude\", \n                   animation_frame=\"trakus_index\", \n                   animation_group=\"program_number\",\n                   color = \"program_number\",\n                   range_x = [min_long, max_long],\n                   range_y = [min_lat, max_lat]\n                  )\n        fig.show()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"track = 'AQU'\ndate = '2019-01-01'\nrace_number = 1\n\nrace_1 = RaceAnalysis(track, date, race_number)\n\nrace_1_data = race_1.get_race_data()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Track way","metadata":{}},{"cell_type":"code","source":"race_1.get_plot_race_way(race_1_data)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Racing visualization","metadata":{}},{"cell_type":"code","source":"race_1.get_animated_race(race_1_data)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Coming soon...","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}