{"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":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport plotly.express as px #graphing\nimport plotly.graph_objects as go #graphing\nfrom plotly.subplots import make_subplots #graphing\nimport plotly.figure_factory as ff #graphing\nimport matplotlib.pyplot as plt #graphing\nimport seaborn as sns #graphing\nimport missingno as msno #describe data\nimport os\n\ncolors = [\"#FFFFFF\", \"#6CD4FF\", \"#F7DF00\", \"#E60000\"]\n\nfrom plotly.offline import plot, iplot, init_notebook_mode\nimport plotly.graph_objs as go\ninit_notebook_mode(connected=True)\n\nplt.rcParams[\"figure.figsize\"] = (12, 8)\n\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-08-21T03:53:57.235413Z","iopub.execute_input":"2022-08-21T03:53:57.235873Z","iopub.status.idle":"2022-08-21T03:54:00.332502Z","shell.execute_reply.started":"2022-08-21T03:53:57.235764Z","shell.execute_reply":"2022-08-21T03:54:00.331833Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Goal of the Competition 🐴 \n\nThe goal of this competition is to analyze horse racing tactics, drafting strategies, and path efficiency. You will develop a model using never-before-released coordinate data along with basic race information.\n\nYour work will help racing horse owners, trainers, and veterinarians better understand how equine performance and welfare fit together. With better data analysis, equine welfare could significantly improve.\n\n## Context 🐴\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\nThe New York Racing Association (NYRA) and the New York Thoroughbred Horsemen's Association (NYTHA) conduct world class thoroughbred racing at Aqueduct Racetrack, Belmont Park and Saratoga Race Course.\n\nWith your help, NYRA and NYTHA will better understand their vast data set, which could lead to new ways of racing and training in a highly traditional industry. With improved use of horse tracking data, you could help improve equine welfare, performance and rider decision making.\n\n## Columns in dataset 🐴\n***\n\n- `track_id` \n- `race_date`\n- `race_number` \n- `program_number`\n- `trakus_index`:  Trakus is a speed-distance curve measurement system for thoroughbred horseracing that measures the speed of each horse in the race with respect to its distance travelled in the race and from that measurement, the distance between horses at the time of POC (point of call) can be measured.\n- `latitude` \n- `longitude` \n- `distance_id` \n- `course_type`\n- `track_condition` \n- `run_up_distance`: “Run-up” is the distance from where the gate is placed and the timing of the race begins – that is, the point at which the horses reach the published distance of the race.\n- `race_type` \n- `purse`: In horse racing, the term purse refers to the total amount of money paid out to the owners of horses racing at a particular track over a given period of time, or to the percentages of a race's total purse that are awarded to each of the highest finishers. \n- `post_time`: Post time is the designated starting time of a horse race.\n- `weight_carried` \n- `jockey`: Name of jockey.\n- `odds`","metadata":{}},{"cell_type":"code","source":"nyra_tracking = pd.read_csv(\"/kaggle/input/big-data-derby-2022/nyra_tracking_table.csv\")\nnyra_start = pd.read_csv(\"/kaggle/input/big-data-derby-2022/nyra_start_table.csv\")\nnyra_race = pd.read_csv(\"/kaggle/input/big-data-derby-2022/nyra_race_table.csv\")\nnyra_2019 = pd.read_csv(\"/kaggle/input/big-data-derby-2022/nyra_2019_complete.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-08-21T03:54:00.334032Z","iopub.execute_input":"2022-08-21T03:54:00.334496Z","iopub.status.idle":"2022-08-21T03:54:16.881338Z","shell.execute_reply.started":"2022-08-21T03:54:00.334468Z","shell.execute_reply":"2022-08-21T03:54:16.880407Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nyra_2019.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-16T17:37:59.957743Z","iopub.execute_input":"2022-08-16T17:37:59.958091Z","iopub.status.idle":"2022-08-16T17:37:59.981203Z","shell.execute_reply.started":"2022-08-16T17:37:59.958058Z","shell.execute_reply":"2022-08-16T17:37:59.980040Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nyra_2019.describe().style.background_gradient(cmap = \"Purples\")","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nyra_tracking.track_id.value_counts()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nyra_tracking.race_number.value_counts()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nyra_tracking.program_number.value_counts()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nyra_tracking['race_date'] = pd.to_datetime(nyra_tracking['race_date'])\nnyra_tracking['day'], nyra_tracking['month'] = nyra_tracking['race_date'].dt.day, nyra_tracking['race_date'].dt.month","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-08-21T03:54:16.882490Z","iopub.execute_input":"2022-08-21T03:54:16.882741Z","iopub.status.idle":"2022-08-21T03:54:17.854515Z","shell.execute_reply.started":"2022-08-21T03:54:16.882716Z","shell.execute_reply":"2022-08-21T03:54:17.853561Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# track_id = AQU","metadata":{}},{"cell_type":"code","source":"nyra_tracking0 = nyra_tracking[nyra_tracking[\"track_id\"] == \"AQU\"]\nnyra_tracking1 = nyra_tracking0[nyra_tracking0[\"month\"] == 11]\n\nplt.style.use(\"dark_background\")\nplt.figure(figsize = (16, 8))\nsns.scatterplot(data = nyra_tracking1, x = \"longitude\", y = \"latitude\", hue = \"race_number\", palette = \"Paired\")\n\nplt.title(\"track_id = AQU, Longitude and Latitude by Race Number\")\nplt.legend()","metadata":{"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# track_id = BEL","metadata":{}},{"cell_type":"code","source":"nyra_tracking0 = nyra_tracking[nyra_tracking[\"track_id\"] == \"BEL\"]\nnyra_tracking1 = nyra_tracking0[nyra_tracking0[\"month\"] == 5]\n\nplt.style.use(\"dark_background\")\nplt.figure(figsize = (16, 8))\nsns.scatterplot(data = nyra_tracking1, x = \"longitude\", y = \"latitude\", hue = \"race_number\", palette = \"Paired\")\n\nplt.title(\"track_id = BEL, Longitude and Latitude by Race Number\")\nplt.legend()","metadata":{"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# track_id = SAR","metadata":{}},{"cell_type":"code","source":"nyra_tracking0 = nyra_tracking[nyra_tracking[\"track_id\"] == \"SAR\"]\nnyra_tracking1 = nyra_tracking0[nyra_tracking0[\"month\"] == 9]\n\nplt.style.use(\"dark_background\")\nplt.figure(figsize = (16, 8))\nsns.scatterplot(data = nyra_tracking1, x = \"longitude\", y = \"latitude\", hue = \"race_number\", palette = \"Paired\")\n\nplt.title(\"track_id = SAR, Longitude and Latitude by Race Number\")\nplt.legend()","metadata":{"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nyra_2019.race_number.hist(bins = 12)","metadata":{"execution":{"iopub.status.busy":"2022-08-21T03:54:45.059031Z","iopub.execute_input":"2022-08-21T03:54:45.059942Z","iopub.status.idle":"2022-08-21T03:54:45.297208Z","shell.execute_reply.started":"2022-08-21T03:54:45.059907Z","shell.execute_reply":"2022-08-21T03:54:45.295760Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Jockey Samuel Camacho Jr. - Trakus Index Race Animation 🏁","metadata":{}},{"cell_type":"code","source":"jockey = nyra_2019[nyra_2019[\"jockey\"] == \"Samuel Camacho Jr.\"]\njockey6 = jockey[jockey[\"race_number\"] == 6]\njockeyBEL = jockey6[jockey6[\"track_id\"] == \"BEL\"]\njockeyALW = jockeyBEL[jockeyBEL[\"race_type\"] == \"ALW\"]\njockeyCLM = jockeyBEL[jockeyBEL[\"race_type\"] == \"CLM\"]\njockeyALW = jockeyALW.sort_values(by = \"trakus_index\", ascending = True)\n\nfig = px.scatter(jockeyALW, x = \"longitude\", y = \"latitude\", hover_data = [\"race_type\", \"track_id\"],\n                 animation_frame = \"longitude\",\n                 range_y = (40.7122, 40.718),\n                 range_x = (-73.719, -73.73),\n                 #size = \"trakus_index\",\n                 color = \"trakus_index\", color_continuous_scale = \"Turbo\", range_color = (0, 299),\n                 title = \"Samuel Camacho Jr. - Trakus Index,  Race Number = 6,  track_id = BEL\")\n\nfig.layout.updatemenus[0].buttons[0].args[1]['frame']['duration'] = 30\nfig.layout.updatemenus[0].buttons[0].args[1]['transition']['duration'] = 5\nfig.update_traces(marker = dict(symbol = \"circle-open\", size = 20)) # scaling the markers\nfig.update_layout(template = \"plotly_dark\", font = dict(family = \"PT Sans\", size = 12))\nfig.show()","metadata":{"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Jockey Flavien Prat - Trakus Index Race Animation 🏁","metadata":{}},{"cell_type":"code","source":"jockey = nyra_2019[nyra_2019[\"jockey\"] == \"Flavien Prat\"]\njockey6 = jockey[jockey[\"race_number\"] == 6]\njockeyBEL = jockey6[jockey6[\"track_id\"] == \"BEL\"]\njockeyALW = jockeyBEL[jockeyBEL[\"race_type\"] == \"ALW\"]\njockeyCLM = jockeyBEL[jockeyBEL[\"race_type\"] == \"CLM\"]\njockeyALW = jockeyALW.sort_values(by = \"trakus_index\", ascending = True)\n\nfig = px.scatter(jockeyALW, x = \"longitude\", y = \"latitude\", hover_data = [\"race_type\", \"track_id\"],\n                 animation_frame = \"longitude\",\n                 range_y = (40.7122, 40.718),\n                 range_x = (-73.719, -73.73),\n                # size = \"trakus_index\",\n                 color = \"trakus_index\", color_continuous_scale = \"Turbo\", range_color = (0, 299),\n                 title = \"Flavien Prat - Trakus Index,  Race Number = 6,  track_id = BEL\")\n\nfig.layout.updatemenus[0].buttons[0].args[1]['frame']['duration'] = 30\nfig.layout.updatemenus[0].buttons[0].args[1]['transition']['duration'] = 5\nfig.update_traces(marker = dict(symbol = \"circle-open\", size = 20)) # scaling the markers\nfig.update_layout(template = \"plotly_dark\", font = dict(family = \"PT Sans\", size = 12))\nfig.show()","metadata":{"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Jockey Flavien Prat - Trakus Index Race Pathing by Odds of Winning 🎲","metadata":{}},{"cell_type":"code","source":"jockey = nyra_2019[nyra_2019[\"jockey\"] == \"Flavien Prat\"]\nraces_bel = jockey[jockey[\"track_id\"] == 'BEL']\n\nfor odds in sorted([690, 650, 1840, 70, 520, 2140, 400, 2775, 700, 160, 1910, 1030, 660]):\n    \n    df0 = races_bel[races_bel[\"odds\"] == odds]\n    \n    fig = px.scatter(df0, x = \"longitude\", y = \"latitude\", opacity = 0.8,\n                     hover_data = [\"race_date\", \"distance_id\", \"purse\"],\n                     color = \"trakus_index\", color_continuous_scale = \"Turbo\", #range_color = (0, 399),\n                     title = f\"Flavien Prat Trakus Index Race Pathing,  Odds: {odds}  track_id = BEL\")\n\n    fig.update_traces(marker = dict(symbol = \"square-open\", size = 20)) # scaling the markers\n    fig.update_layout(template = \"plotly_dark\", font = dict(family = \"PT Sans\", size = 12))\n    fig.show()","metadata":{"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nyra_2019.head()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nyra_start.head()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nyra_start['race_date'] = pd.to_datetime(nyra_start['race_date'])\nnyra_start['day'], nyra_start['month'] = nyra_start['race_date'].dt.day, nyra_start['race_date'].dt.month","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Odds of winning race 🐴","metadata":{}},{"cell_type":"code","source":"fig = px.scatter(nyra_start, x = \"weight_carried\", y = \"odds\", hover_data = [\"race_number\", \"jockey\"],\n                 color = \"odds\", color_continuous_scale = colors, range_color = (0, 9999),\n                 title = \"Odds by Weight Carried\")\n\nfig.update_traces(marker = dict(size = 8, symbol = \"circle\")) # scaling the markers\nfig.update_layout(template = \"plotly_dark\", font = dict(family = \"PT Sans\", size = 20))\nfig.show()","metadata":{"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = px.scatter(nyra_start, x = \"weight_carried\", y = \"day\", hover_data = [\"race_number\", \"jockey\"],\n                 color = \"odds\", color_continuous_scale = colors, range_color = (0, 9999),\n                 title = \"Odds by Day of Event and Weight Carried\")\n\nfig.update_traces(marker = dict(size = 9, symbol = \"square\")) # scaling the markers\nfig.update_layout(template = \"plotly_dark\", font = dict(family = \"PT Sans\", size = 20))\nfig.show()","metadata":{"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = px.scatter(nyra_start, x = \"weight_carried\", y = \"month\", hover_data = [\"race_number\", \"jockey\"],\n                 color = \"odds\", color_continuous_scale = colors, range_color = (0, 9999),\n                 title = \"Odds by Month of Event and Weight Carried\")\n\nfig.update_traces(marker = dict(size = 11, symbol = \"circle\")) # scaling the markers\nfig.update_layout(template = \"plotly_dark\", font = dict(family = \"PT Sans\", size = 20))\nfig.show()","metadata":{"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nyra_race.head()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nyra_race.track_condition.value_counts()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = px.scatter(nyra_race, x = \"post_time\", y = \"run_up_distance\", hover_data = [\"race_number\", \"course_type\"],\n                 color = \"race_number\", color_discrete_sequence = colors, #range_color = (0, 9999),\n                 title = \"Post Time by Run Up Distance\")\n\nfig.update_traces(marker = dict(size = 4, symbol = \"circle\")) # scaling the markers\nfig.update_layout(template = \"plotly_dark\", font = dict(family = \"PT Sans\", size = 20))\nfig.show()","metadata":{"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Thank you for viewing this notebook. Please feel free to provide any feedback**","metadata":{}}]}