{"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":"# <h><strong><center>⭐️🐴🐎BIG DATA DERBY 2022🐎🐴⭐️</center></strong></h>\n\n<div>\n    <img class=\"marginauto\" src='https://storage.googleapis.com/kaggle-competitions/kaggle/37644/logos/header.png?t=2022-08-05-15-26-16' alt=\"centered image\" />\n</div>\n\n\n# **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: Important part of feature analysis**\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\n## ***Now, This competition approach new for me! So i am trying to use difference people notebook and how to approach this data analysis competition***\n\n## ***Full credit:***\n\n## 1. https://www.kaggle.com/code/mattop/big-data-derby-2022-eda\n## 2. https://www.kaggle.com/code/foocheechuan/horse-racing-analysis\n## 3. https://www.kaggle.com/code/lichtlab/race-detailed-observation-with-animation/notebook\n\n# <h><strong><center>⭐️⭐️Let's try it Guys! And learn EDA in this competition⭐️⭐️</center></strong></h>\n\n\n<div>\n    <img class=\"marginauto\" src='https://sportshub.cbsistatic.com/i/r/2019/04/26/85dd0c13-81ee-434c-8951-4a9c1117af1e/thumbnail/1200x675/229c62c30d7deb6c03cf79d64ca931de/kentucky-derby-1.jpg' alt=\"centered image\" />\n</div>\n","metadata":{}},{"cell_type":"markdown","source":"# **Import Necessary Library**","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\n\nimport plotly.express as px\nimport plotly.graph_objects as go\nfrom plotly.subplots import make_subplots\nimport plotly.figure_factory as ff\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport missingno as msno\n\nimport os\nsns.set_theme(style=\"darkgrid\")\n\ncolors = [\"#FFFFFF\",\"#6CD4FF\",\"#F7DF00\",\"#E60000\"]\n\nplt.rcParams[\"figure.figsize\"] = (12,8)","metadata":{"execution":{"iopub.status.busy":"2022-08-30T15:45:26.023822Z","iopub.execute_input":"2022-08-30T15:45:26.025055Z","iopub.status.idle":"2022-08-30T15:45:29.973129Z","shell.execute_reply.started":"2022-08-30T15:45:26.024942Z","shell.execute_reply":"2022-08-30T15:45:29.972098Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Load and read Data**","metadata":{}},{"cell_type":"code","source":"nyra_tracking = pd.read_csv(\"../input/big-data-derby-2022/nyra_2019_complete.csv\")\nnyra_race = pd.read_csv(\"../input/big-data-derby-2022/nyra_race_table.csv\")\nnyra_start= pd.read_csv(\"../input/big-data-derby-2022/nyra_start_table.csv\")\nnyra_2019 = pd.read_csv(\"../input/big-data-derby-2022/nyra_2019_complete.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-08-30T15:45:29.977295Z","iopub.execute_input":"2022-08-30T15:45:29.979122Z","iopub.status.idle":"2022-08-30T15:45:54.618778Z","shell.execute_reply.started":"2022-08-30T15:45:29.979091Z","shell.execute_reply":"2022-08-30T15:45:54.617702Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Start to analysis the nyra data**","metadata":{}},{"cell_type":"code","source":"nyra_tracking.columns","metadata":{"execution":{"iopub.status.busy":"2022-08-30T15:45:54.623451Z","iopub.execute_input":"2022-08-30T15:45:54.625811Z","iopub.status.idle":"2022-08-30T15:45:54.639715Z","shell.execute_reply.started":"2022-08-30T15:45:54.625772Z","shell.execute_reply":"2022-08-30T15:45:54.638688Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nyra_start.columns","metadata":{"execution":{"iopub.status.busy":"2022-08-30T15:45:54.643728Z","iopub.execute_input":"2022-08-30T15:45:54.644434Z","iopub.status.idle":"2022-08-30T15:45:54.655595Z","shell.execute_reply.started":"2022-08-30T15:45:54.644397Z","shell.execute_reply":"2022-08-30T15:45:54.654276Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nyra_race.columns","metadata":{"execution":{"iopub.status.busy":"2022-08-30T15:45:54.661837Z","iopub.execute_input":"2022-08-30T15:45:54.664122Z","iopub.status.idle":"2022-08-30T15:45:54.674331Z","shell.execute_reply.started":"2022-08-30T15:45:54.664086Z","shell.execute_reply":"2022-08-30T15:45:54.673149Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nyra_2019.columns","metadata":{"execution":{"iopub.status.busy":"2022-08-30T15:45:54.677677Z","iopub.execute_input":"2022-08-30T15:45:54.680306Z","iopub.status.idle":"2022-08-30T15:45:54.690509Z","shell.execute_reply.started":"2022-08-30T15:45:54.680270Z","shell.execute_reply":"2022-08-30T15:45:54.689557Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nyra_tracking.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-30T15:45:54.692081Z","iopub.execute_input":"2022-08-30T15:45:54.692749Z","iopub.status.idle":"2022-08-30T15:45:54.727194Z","shell.execute_reply.started":"2022-08-30T15:45:54.692702Z","shell.execute_reply":"2022-08-30T15:45:54.726073Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **nyra_tracking the each racer path**","metadata":{}},{"cell_type":"code","source":"nyra_tracking['track_id'].unique()","metadata":{"execution":{"iopub.status.busy":"2022-08-30T15:45:54.728578Z","iopub.execute_input":"2022-08-30T15:45:54.729209Z","iopub.status.idle":"2022-08-30T15:45:55.170793Z","shell.execute_reply.started":"2022-08-30T15:45:54.729173Z","shell.execute_reply":"2022-08-30T15:45:55.169761Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in ['AQU', 'BEL','SAR']:\n    if i == 'AQU':\n        track = nyra_2019[(nyra_2019[\"track_id\"]== i) & (nyra_2019[\"race_number\"]==1)]\n        plt.style.use(\"Solarize_Light2\")\n        plt.figure(figsize = (16,8))\n        sns.scatterplot(data = track, x = \"longitude\", y=\"latitude\", hue = \"jockey\", palette=\"Set2\")\n        \n        plt.title(f\"{i}\")\n        plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-30T15:45:55.172250Z","iopub.execute_input":"2022-08-30T15:45:55.172934Z","iopub.status.idle":"2022-08-30T15:46:07.335405Z","shell.execute_reply.started":"2022-08-30T15:45:55.172896Z","shell.execute_reply":"2022-08-30T15:46:07.334417Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Player - particular player trackus index and show the result**","metadata":{}},{"cell_type":"code","source":"df_select=nyra_2019[(nyra_2019.track_id == 'SAR') &\n            (nyra_2019.race_date == '2019-09-02') &\n            (nyra_2019.race_number == 1)]","metadata":{"execution":{"iopub.status.busy":"2022-08-30T15:46:07.336983Z","iopub.execute_input":"2022-08-30T15:46:07.337676Z","iopub.status.idle":"2022-08-30T15:46:08.506287Z","shell.execute_reply.started":"2022-08-30T15:46:07.337638Z","shell.execute_reply":"2022-08-30T15:46:08.505292Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('min: ', df_select['trakus_index'].min())\nprint('max: ', df_select['trakus_index'].max())","metadata":{"execution":{"iopub.status.busy":"2022-08-30T15:46:08.507957Z","iopub.execute_input":"2022-08-30T15:46:08.508657Z","iopub.status.idle":"2022-08-30T15:46:08.521271Z","shell.execute_reply.started":"2022-08-30T15:46:08.508617Z","shell.execute_reply":"2022-08-30T15:46:08.520336Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in [1,50,100,150,200,250,300,350,400]:\n    \n    plt.figure(figsize=(10,7))\n    sns.scatterplot(data=df_select[df_select.trakus_index>1], x = 'longitude', y='latitude',\n                   hue= 'jockey',\n                   size = 'trakus_index')\n    plt.title(f\"trakus_index >{i}\")\n    plt.grid()\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-30T15:46:08.523855Z","iopub.execute_input":"2022-08-30T15:46:08.524715Z","iopub.status.idle":"2022-08-30T15:46:14.409142Z","shell.execute_reply.started":"2022-08-30T15:46:08.524674Z","shell.execute_reply":"2022-08-30T15:46:14.405288Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Another Approach - Animation view - Trackin**","metadata":{}},{"cell_type":"code","source":"import plotly.express as px\nimport geopy.distance\nimport warnings\nwarnings.simplefilter('ignore')\n\ncolor_discrete_map = {\n    \"6\" : \"red\",\n    \"1\" : \"green\",\n    \"11\": \"blue\",\n    \"9\" : \"goldenrod\",\n    \"4\": \"magenta\",\n    \"7\": \"orange\",\n    \"10\": \"pink\",\n    \"3\": \"gray\",\n    \"2\": \"gray\",\n    \"8\": \"gray\"\n}\n\nsymbol_discrete_map = {\n     \"6\": \"star\",\n    \"1\": \"square\",\n    \"11\": \"x\",\n    \"9\": \"circle\",\n    \"4\": \"cross\",\n    \"7\": \"diamond\",\n    \"10\": \"triangle-up\",\n    \"3\": \"circle-x\",\n    \"2\": \"circle-x\",\n    \"8\": \"circle-x\"\n    \n}","metadata":{"execution":{"iopub.status.busy":"2022-08-30T15:46:14.411124Z","iopub.execute_input":"2022-08-30T15:46:14.411623Z","iopub.status.idle":"2022-08-30T15:46:14.564181Z","shell.execute_reply.started":"2022-08-30T15:46:14.411582Z","shell.execute_reply":"2022-08-30T15:46:14.563190Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nyra_2019 = pd.read_csv(\"../input/big-data-derby-2022/nyra_2019_complete.csv\", dtype={'post_time' : str, 'program_number': str})\nnyra_2019['program_number'] = nyra_2019['program_number'].str.strip()","metadata":{"execution":{"iopub.status.busy":"2022-08-30T15:46:14.570655Z","iopub.execute_input":"2022-08-30T15:46:14.571453Z","iopub.status.idle":"2022-08-30T15:46:24.144727Z","shell.execute_reply.started":"2022-08-30T15:46:14.571422Z","shell.execute_reply":"2022-08-30T15:46:24.143600Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#pick on saratoga race\n\none_race_sar = nyra_2019[(nyra_2019['race_date']=='2019-07-27') & (nyra_2019['distance_id']==850) & (nyra_2019['race_type']=='MSW')]\none_race_sar = one_race_sar.sort_values(by=['trakus_index','program_number'])","metadata":{"execution":{"iopub.status.busy":"2022-08-30T15:46:24.146405Z","iopub.execute_input":"2022-08-30T15:46:24.146809Z","iopub.status.idle":"2022-08-30T15:46:24.775030Z","shell.execute_reply.started":"2022-08-30T15:46:24.146752Z","shell.execute_reply":"2022-08-30T15:46:24.774049Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result = pd.read_csv(\"/kaggle/input/race-data/2019_Saratoga_Juveniles.csv\")\nresult = result[(result['DATE']=='7/27/19') & (result['RACE']==7)]\nresult[['minutes', 'seconds']] = result['TIME'].str.split(':',expand=True)\nresult['TIME_IN_SEC'] = result['minutes'].astype(float) * 60  + result['seconds'].astype(float)\nresult['TIME_DIFF'] = result['TIME_IN_SEC'].diff().fillna(0)\nresult['HORSE_NUM'] = np.array(['6','1','11','9','4','7','10','3','2','8']) # manual macthing by jockey name & odds\ndisplay(result[['DATE','FIN','JOCKEY','TIME','TIME_DIFF','STARTER NAME','HORSE_NUM','ODDS']].head(7))\npx.bar(result.head(7), x='FIN', y='TIME_DIFF',hover_data=['HORSE_NUM'],\n       color='HORSE_NUM',\n       color_discrete_map=color_discrete_map,\n       width=700,height=400)","metadata":{"execution":{"iopub.status.busy":"2022-08-30T15:46:24.776621Z","iopub.execute_input":"2022-08-30T15:46:24.776986Z","iopub.status.idle":"2022-08-30T15:46:25.567146Z","shell.execute_reply.started":"2022-08-30T15:46:24.776948Z","shell.execute_reply":"2022-08-30T15:46:25.565193Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Race observation\none_race_sar['resample_flg'] = one_race_sar['trakus_index'] % 4 == 0\none_race_sar_sec_sampling = one_race_sar[one_race_sar['resample_flg']==True]\npx.scatter(one_race_sar_sec_sampling,x='latitude',y='longitude',animation_frame=\"trakus_index\",\n          color='program_number',\n          color_discrete_map = color_discrete_map,\n          symbol = 'program_number',\n          symbol_map = symbol_discrete_map,\n          range_x = [one_race_sar['latitude'].min(), one_race_sar['latitude'].max()],\n          range_y =[one_race_sar['longitude'].min(),one_race_sar['longitude'].max()],\n          title = 'Race simulation lat/long scatter plot / 1sec sampling', width = 700,height=700 )","metadata":{"execution":{"iopub.status.busy":"2022-08-30T15:46:25.568770Z","iopub.execute_input":"2022-08-30T15:46:25.569177Z","iopub.status.idle":"2022-08-30T15:46:28.843272Z","shell.execute_reply.started":"2022-08-30T15:46:25.569140Z","shell.execute_reply":"2022-08-30T15:46:28.842316Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Horses run right around the oval track in a single-lap type race, running the last corner from about trakus_index=241 and the last sprint from about trakus_index=313.\n\nLet's dive in to the last 50 sec sprint. This time I moved the coordinate axes so that horse #6 would come to the origin.","metadata":{}},{"cell_type":"code","source":"centroid = one_race_sar[one_race_sar['program_number']=='6'].rename(columns={'latitude': 'centroid_lat', 'longitude': 'centroid_long'}).reset_index()\none_race_sar = pd.merge(\n    one_race_sar,\n    centroid[['trakus_index','centroid_lat','centroid_long']],\n    on='trakus_index',\n    how='left'\n)\n\ndef get_speed(x):\n    dist = geopy.distance.geodesic(\n        (x['lag_lat'], x['lag_long']), \n        (x['latitude'], x['longitude'])).m\n    x['m_per_sec'] = dist / 0.25\n    return x\none_race_sar['lag_lat'] = one_race_sar.groupby('program_number')[['latitude']].shift(1)\none_race_sar['lag_lat'] = one_race_sar.groupby('program_number')[['lag_lat']].fillna(method='bfill')\none_race_sar['lag_long'] = one_race_sar.groupby('program_number')[['longitude']].shift(1)\none_race_sar['lag_long'] = one_race_sar.groupby('program_number')[['lag_long']].fillna(method='bfill')\none_race_sar = one_race_sar.apply(get_speed, axis=1)\none_race_sar['x_horse6_centered'] = (one_race_sar['latitude'] - one_race_sar['centroid_lat']) * (10 ** 5)\none_race_sar['y_horse6_centered'] = (one_race_sar['longitude'] - one_race_sar['centroid_long']) * (10 ** 5)\n","metadata":{"execution":{"iopub.status.busy":"2022-08-30T15:46:28.844918Z","iopub.execute_input":"2022-08-30T15:46:28.845564Z","iopub.status.idle":"2022-08-30T15:46:32.336499Z","shell.execute_reply.started":"2022-08-30T15:46:28.845505Z","shell.execute_reply":"2022-08-30T15:46:32.335535Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"one_race_sar_lastSprint = one_race_sar[one_race_sar['trakus_index'] > one_race_sar['trakus_index'].max() - (50 / 0.25)]\npx.scatter(one_race_sar_lastSprint, x='x_horse6_centered', y='y_horse6_centered', animation_frame=\"trakus_index\", \n           color='program_number',\n           color_discrete_map=color_discrete_map,\n           symbol='program_number',\n           symbol_map=symbol_discrete_map,\n           range_x = [-15, 15],\n           range_y = [-15, 25],\n           title='Last sprint / .25sec sampling', width=800, height=800)\n","metadata":{"execution":{"iopub.status.busy":"2022-08-30T15:46:32.337811Z","iopub.execute_input":"2022-08-30T15:46:32.338390Z","iopub.status.idle":"2022-08-30T15:46:38.681678Z","shell.execute_reply.started":"2022-08-30T15:46:32.338354Z","shell.execute_reply":"2022-08-30T15:46:38.680867Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"trakus_index=241: Horse 11 was in last place as he approached the last turn.\n\ntrakus_index=283: Horse 4 and horse 7 were in the lead as they rounded the final corner. Horse 11 was in the lower position, but began to accelerate rapidly.\n\ntrakus_index=313: Horse 7 was in the lead when the group turned the corner and began to run the straightaway, but Horse 7 would slow down from this point.\n\ntrakus_index=350: Horse 6 and horse 1 caught up with horse 4 and 7. Horse 11 changed its trajectory to the left to overtake the group.\n\ntrakus_index=386: Around this time seems to be the point where the goal line was crossed. Reasons below.\n\nHorse 7 is ahead of horse 10 at this time but horse 7 is passed by horse 10 after this time. Since we know that horse 7 is ahead of horse 10 in the race result, we can assume that horse 7 has alread passed the final line.\nObservation - Horse #11\nHorse 11, the most popular horse at odds, had a remarkable acceleration at the final sprint, but was unable to catch up to horses 6 and 1 at the end, finishing third. It seems with a little more time, horse 11 would have been expected to overtake horses 6 and 1. That fact suggests that there were two possible reasons for his defeat.\n\nHorse 11 had to change its trajectory to the left to overtake the group(trakus_index=350). I believe that horse 11 thought it was risky to run into a group of horses.\nHorse 11 was in last place as he approached the last turn(trakus_index=241). He had to go around the outside of the corner to pass the other horses(trakus_index=241~313). The trajectory was not efficient.","metadata":{}},{"cell_type":"code","source":"one_race_sar_lastSprint['dist_to_horse6'] = np.sqrt(one_race_sar_lastSprint['x_horse6_centered'] ** 2 + one_race_sar_lastSprint['y_horse6_centered'] ** 2)\npx.line(one_race_sar_lastSprint[one_race_sar_lastSprint['program_number'].isin(['6','11','1'])], \n        x='trakus_index', y='dist_to_horse6', \n        color='program_number',\n        color_discrete_map=color_discrete_map,\n        width=700,height=400)\n","metadata":{"execution":{"iopub.status.busy":"2022-08-30T15:46:38.683123Z","iopub.execute_input":"2022-08-30T15:46:38.683666Z","iopub.status.idle":"2022-08-30T15:46:38.774633Z","shell.execute_reply.started":"2022-08-30T15:46:38.683630Z","shell.execute_reply":"2022-08-30T15:46:38.773555Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ttl_distance_df = one_race_sar_lastSprint[one_race_sar_lastSprint['trakus_index'] <= 390]\nttl_distance_df = ttl_distance_df.groupby('program_number')[['m_per_sec']].sum().reset_index().rename(columns={'m_per_sec': 'total_distance'})\nttl_distance_df['total_distance'] = ttl_distance_df['total_distance'] * 0.25\nttl_distance_df = ttl_distance_df[ttl_distance_df['program_number'].isin(['6','1','11','9','4'])]\npx.bar(\n    ttl_distance_df,\n    x='program_number',\n    y='total_distance',\n    color='program_number',color_discrete_map=color_discrete_map,\n    log_y=True,\n    width=700,height=400)","metadata":{"execution":{"iopub.status.busy":"2022-08-30T15:46:38.776315Z","iopub.execute_input":"2022-08-30T15:46:38.776729Z","iopub.status.idle":"2022-08-30T15:46:38.859602Z","shell.execute_reply.started":"2022-08-30T15:46:38.776691Z","shell.execute_reply":"2022-08-30T15:46:38.857944Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nyra_2019.describe().style.background_gradient(cmap=\"rainbow\")","metadata":{"execution":{"iopub.status.busy":"2022-08-30T15:46:38.861237Z","iopub.execute_input":"2022-08-30T15:46:38.861603Z","iopub.status.idle":"2022-08-30T15:46:40.348633Z","shell.execute_reply.started":"2022-08-30T15:46:38.861567Z","shell.execute_reply":"2022-08-30T15:46:40.347675Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nyra_tracking.track_id.value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-08-30T15:46:40.350031Z","iopub.execute_input":"2022-08-30T15:46:40.350692Z","iopub.status.idle":"2022-08-30T15:46:40.629493Z","shell.execute_reply.started":"2022-08-30T15:46:40.350651Z","shell.execute_reply":"2022-08-30T15:46:40.628356Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nyra_tracking.race_number.value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-08-30T15:46:40.632050Z","iopub.execute_input":"2022-08-30T15:46:40.632857Z","iopub.status.idle":"2022-08-30T15:46:40.681020Z","shell.execute_reply.started":"2022-08-30T15:46:40.632820Z","shell.execute_reply":"2022-08-30T15:46:40.680070Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nyra_tracking.program_number.value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-08-30T15:46:40.683915Z","iopub.execute_input":"2022-08-30T15:46:40.684283Z","iopub.status.idle":"2022-08-30T15:46:40.929365Z","shell.execute_reply.started":"2022-08-30T15:46:40.684256Z","shell.execute_reply":"2022-08-30T15:46:40.928295Z"},"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":{"execution":{"iopub.status.busy":"2022-08-30T15:46:40.930987Z","iopub.execute_input":"2022-08-30T15:46:40.931404Z","iopub.status.idle":"2022-08-30T15:46:42.482046Z","shell.execute_reply.started":"2022-08-30T15:46:40.931372Z","shell.execute_reply":"2022-08-30T15:46:42.481021Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Track_id = AQU\nnyra_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":{"execution":{"iopub.status.busy":"2022-08-30T15:46:42.483398Z","iopub.execute_input":"2022-08-30T15:46:42.483784Z","iopub.status.idle":"2022-08-30T15:47:08.105584Z","shell.execute_reply.started":"2022-08-30T15:46:42.483720Z","shell.execute_reply":"2022-08-30T15:47:08.104638Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#track_id = BEL\nnyra_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":{"execution":{"iopub.status.busy":"2022-08-30T15:47:08.107228Z","iopub.execute_input":"2022-08-30T15:47:08.107893Z","iopub.status.idle":"2022-08-30T15:47:27.445276Z","shell.execute_reply.started":"2022-08-30T15:47:08.107857Z","shell.execute_reply":"2022-08-30T15:47:27.444218Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#track_id=SAR\nnyra_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":{"execution":{"iopub.status.busy":"2022-08-30T15:47:27.446854Z","iopub.execute_input":"2022-08-30T15:47:27.447213Z","iopub.status.idle":"2022-08-30T15:47:31.173546Z","shell.execute_reply.started":"2022-08-30T15:47:27.447175Z","shell.execute_reply":"2022-08-30T15:47:31.172555Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nyra_2019.race_number.hist(bins=10)","metadata":{"execution":{"iopub.status.busy":"2022-08-30T15:47:32.652063Z","iopub.execute_input":"2022-08-30T15:47:32.652447Z","iopub.status.idle":"2022-08-30T15:47:33.110692Z","shell.execute_reply.started":"2022-08-30T15:47:32.652409Z","shell.execute_reply":"2022-08-30T15:47:33.109652Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#jockey samuel camacho jr. - trakus index race animation'\njockey = 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()\n","metadata":{"execution":{"iopub.status.busy":"2022-08-30T15:48:07.752314Z","iopub.execute_input":"2022-08-30T15:48:07.752688Z","iopub.status.idle":"2022-08-30T15:48:10.105790Z","shell.execute_reply.started":"2022-08-30T15:48:07.752657Z","shell.execute_reply":"2022-08-30T15:48:10.104756Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# jockey flavienprat - Trakus index race animation\n\njockey = 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()\n","metadata":{"execution":{"iopub.status.busy":"2022-08-30T15:49:13.118206Z","iopub.execute_input":"2022-08-30T15:49:13.118572Z","iopub.status.idle":"2022-08-30T15:49:14.750617Z","shell.execute_reply.started":"2022-08-30T15:49:13.118539Z","shell.execute_reply":"2022-08-30T15:49:14.749674Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#jockey flavien prat - Trakus index race pathing by odds of winning\n\njockey = 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":{"execution":{"iopub.status.busy":"2022-08-30T15:50:07.244576Z","iopub.execute_input":"2022-08-30T15:50:07.245511Z","iopub.status.idle":"2022-08-30T15:50:08.546363Z","shell.execute_reply.started":"2022-08-30T15:50:07.245477Z","shell.execute_reply":"2022-08-30T15:50:08.545451Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nyra_2019.tail()","metadata":{"execution":{"iopub.status.busy":"2022-08-30T15:51:04.216917Z","iopub.execute_input":"2022-08-30T15:51:04.217273Z","iopub.status.idle":"2022-08-30T15:51:04.243843Z","shell.execute_reply.started":"2022-08-30T15:51:04.217243Z","shell.execute_reply":"2022-08-30T15:51:04.242417Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nyra_start.sample(2)","metadata":{"execution":{"iopub.status.busy":"2022-08-30T15:51:28.076367Z","iopub.execute_input":"2022-08-30T15:51:28.076744Z","iopub.status.idle":"2022-08-30T15:51:28.094751Z","shell.execute_reply.started":"2022-08-30T15:51:28.076701Z","shell.execute_reply":"2022-08-30T15:51:28.093751Z"},"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":{"execution":{"iopub.status.busy":"2022-08-30T15:51:44.001451Z","iopub.execute_input":"2022-08-30T15:51:44.001942Z","iopub.status.idle":"2022-08-30T15:51:44.026050Z","shell.execute_reply.started":"2022-08-30T15:51:44.001900Z","shell.execute_reply":"2022-08-30T15:51:44.025000Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Odd of winning race\nfig = 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()\n","metadata":{"execution":{"iopub.status.busy":"2022-08-30T15:52:13.052506Z","iopub.execute_input":"2022-08-30T15:52:13.052975Z","iopub.status.idle":"2022-08-30T15:52:13.281748Z","shell.execute_reply.started":"2022-08-30T15:52:13.052930Z","shell.execute_reply":"2022-08-30T15:52:13.280702Z"},"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":{"execution":{"iopub.status.busy":"2022-08-30T15:52:31.669370Z","iopub.execute_input":"2022-08-30T15:52:31.669744Z","iopub.status.idle":"2022-08-30T15:52:31.878640Z","shell.execute_reply.started":"2022-08-30T15:52:31.669701Z","shell.execute_reply":"2022-08-30T15:52:31.877683Z"},"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()\n","metadata":{"execution":{"iopub.status.busy":"2022-08-30T15:52:47.268768Z","iopub.execute_input":"2022-08-30T15:52:47.269123Z","iopub.status.idle":"2022-08-30T15:52:47.465273Z","shell.execute_reply.started":"2022-08-30T15:52:47.269092Z","shell.execute_reply":"2022-08-30T15:52:47.464456Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nyra_race.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-30T15:53:06.640659Z","iopub.execute_input":"2022-08-30T15:53:06.641354Z","iopub.status.idle":"2022-08-30T15:53:06.658829Z","shell.execute_reply.started":"2022-08-30T15:53:06.641320Z","shell.execute_reply":"2022-08-30T15:53:06.657904Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nyra_race.track_condition.value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-08-30T15:53:20.191475Z","iopub.execute_input":"2022-08-30T15:53:20.191863Z","iopub.status.idle":"2022-08-30T15:53:20.204690Z","shell.execute_reply.started":"2022-08-30T15:53:20.191827Z","shell.execute_reply":"2022-08-30T15:53:20.203632Z"},"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":{"execution":{"iopub.status.busy":"2022-08-30T15:53:33.618210Z","iopub.execute_input":"2022-08-30T15:53:33.618575Z","iopub.status.idle":"2022-08-30T15:53:33.712567Z","shell.execute_reply.started":"2022-08-30T15:53:33.618545Z","shell.execute_reply":"2022-08-30T15:53:33.711651Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **⭐️Thanks for visiting guys!!⭐️**\n\n## ***⭐️Explore more together! - ComingSoon - Explore more together!⭐️***","metadata":{}}]}