{"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":"# Import Packages","metadata":{}},{"cell_type":"code","source":"import math\nimport numpy as np\nimport pandas as pd\nimport seaborn as sns\nimport matplotlib.pyplot as plt\npd.set_option('mode.chained_assignment',  None)","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-09-24T04:05:01.883943Z","iopub.execute_input":"2022-09-24T04:05:01.884747Z","iopub.status.idle":"2022-09-24T04:05:03.012787Z","shell.execute_reply.started":"2022-09-24T04:05:01.884641Z","shell.execute_reply":"2022-09-24T04:05:03.011557Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Read Data\nAfter receiving the data file for the first time, I was thinking about how to classify it.\nI decided to proceed by creating a variable for each date and jockey.\n\nIt took me a while to understand the words in the column.","metadata":{}},{"cell_type":"code","source":"der = pd.read_csv('../input/big-data-derby-2022/nyra_2019_complete.csv')","metadata":{"execution":{"iopub.status.busy":"2022-09-24T04:05:15.299231Z","iopub.execute_input":"2022-09-24T04:05:15.299635Z","iopub.status.idle":"2022-09-24T04:05:34.503588Z","shell.execute_reply.started":"2022-09-24T04:05:15.299593Z","shell.execute_reply":"2022-09-24T04:05:34.502381Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data preprocessing","metadata":{}},{"cell_type":"code","source":"der.columns = [\"track_id\",\"race_date\",\"race_number\",\"program_number\",\"trakus_index\",\"latitude\",\"longitude\",\n               \"distance_id\",\"course_type\",\"track_condition\",\"run_up_distance\",\"race_type\",\"purse\",\"post_time\",\"weight_carried\",\"jockey\",\"odds\",\"position\"]\n","metadata":{"execution":{"iopub.status.busy":"2022-09-24T04:05:43.864302Z","iopub.execute_input":"2022-09-24T04:05:43.864753Z","iopub.status.idle":"2022-09-24T04:05:43.870436Z","shell.execute_reply.started":"2022-09-24T04:05:43.864714Z","shell.execute_reply":"2022-09-24T04:05:43.869367Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"I tried to change the match date column to a date type,\nbut I chose to use a string without \"-\".\n\nAnd I also removed the space on the right of \"track_condition\"","metadata":{}},{"cell_type":"code","source":"der['race_date'] = der['race_date'].str.replace(\"-\", \"\")\nder['track_condition'] = der['track_condition'].str.rstrip()","metadata":{"execution":{"iopub.status.busy":"2022-09-24T04:05:46.717789Z","iopub.execute_input":"2022-09-24T04:05:46.718220Z","iopub.status.idle":"2022-09-24T04:05:51.648260Z","shell.execute_reply.started":"2022-09-24T04:05:46.718185Z","shell.execute_reply":"2022-09-24T04:05:51.647042Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Then, by sorting \"race_number\" and \"trakus_index\" in order, the speed calculation was efficiently sorted using the function.","metadata":{}},{"cell_type":"code","source":"der['race_date_name'] = der['race_date'].str[4:] + der['jockey'].str[:4]","metadata":{"execution":{"iopub.status.busy":"2022-09-24T04:05:55.738672Z","iopub.execute_input":"2022-09-24T04:05:55.739098Z","iopub.status.idle":"2022-09-24T04:06:00.057737Z","shell.execute_reply.started":"2022-09-24T04:05:55.739049Z","shell.execute_reply":"2022-09-24T04:06:00.056534Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"der = der.sort_values(by = ['race_number','trakus_index']).reset_index().drop(['index'],axis = 1)","metadata":{"execution":{"iopub.status.busy":"2022-09-24T04:06:05.314366Z","iopub.execute_input":"2022-09-24T04:06:05.314755Z","iopub.status.idle":"2022-09-24T04:06:13.601809Z","shell.execute_reply.started":"2022-09-24T04:06:05.314723Z","shell.execute_reply":"2022-09-24T04:06:13.600507Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Create Variable","metadata":{}},{"cell_type":"markdown","source":"I named the variable with \"race_date\" and the first 4 letters of\"jockey\".\n\nFom example : der0101John - Played by John R. Velazquez on January 1, 2019\n\n","metadata":{}},{"cell_type":"code","source":"#It takes about 15-20 minutes to work\nfor i in der['race_date_name'].drop_duplicates().sort_values().tolist():\n    globals()['der{}'.format(i)] = der[der['race_date_name'] == i].reset_index().drop(['index'],axis =1)","metadata":{"execution":{"iopub.status.busy":"2022-09-24T04:06:17.345714Z","iopub.execute_input":"2022-09-24T04:06:17.346142Z","iopub.status.idle":"2022-09-24T04:48:48.807318Z","shell.execute_reply.started":"2022-09-24T04:06:17.346105Z","shell.execute_reply":"2022-09-24T04:48:48.805528Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"If you want to create a variable for \"race_number\" only, run the code below","metadata":{}},{"cell_type":"code","source":"for i in der['race_date'].drop_duplicates().sort_values().str[4:8].tolist():\n    globals()['der{}'.format(i)] = der[der['race_date'].str[4:8] == i]","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# EDA","metadata":{}},{"cell_type":"markdown","source":"Since speed requires time and distance, the time is fixed at 0.25 seconds, and you just need to find the distance.\nDistances are easy to find because latitude and longitude data are available.\nBut the value is too small, so I'll multiply it by 10000 for ease of visualization.\nThis results in a relative value, not an absolute value.\nIn this case, the outlier was indicated as 0.\n\nIf you draw a graph, the velocity is the gradient.","metadata":{}},{"cell_type":"code","source":"def velocity_race_number(x):\n    vel = []\n    for i in range(len(x)):\n        try:\n            vel.append((((x.loc[i,'latitude'] - x.loc[i+1,'latitude'])*(x.loc[i,'latitude'] - x.loc[i+1,'latitude']) +\n            (x.loc[i,'longitude'] - x.loc[i+1,'longitude'])*(x.loc[i,'longitude'] - x.loc[i+1,'longitude']))**0.5)*1000000)\n        except:\n            vel.append(0)        \n    x['vel'] = vel\n    for i in x['race_number'].unique():\n        f,ax  = plt.subplots(1,1,figsize  = (10,10))\n        a =  x[(x['race_number']== i) & (x['vel'] <= 100)]\n        sns.lineplot(data =a , x = 'trakus_index', y = 'vel',hue = 'race_number',palette = 'Set2')\n        plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-09-24T04:49:26.972978Z","iopub.execute_input":"2022-09-24T04:49:26.973435Z","iopub.status.idle":"2022-09-24T04:49:26.984711Z","shell.execute_reply.started":"2022-09-24T04:49:26.973397Z","shell.execute_reply":"2022-09-24T04:49:26.983279Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def velocity_course_type(x):\n    vel = []\n    for i in range(len(x)):\n        try:\n            vel.append((((x.loc[i,'latitude'] - x.loc[i+1,'latitude'])*(x.loc[i,'latitude'] - x.loc[i+1,'latitude']) +\n            (x.loc[i,'longitude'] - x.loc[i+1,'longitude'])*(x.loc[i,'longitude'] - x.loc[i+1,'longitude']))**0.5)*1000000)\n        except:\n            vel.append(0)        \n    x['vel'] = vel\n    for i in x['race_number'].unique():\n        f,ax  = plt.subplots(1,1,figsize  = (10,10))\n        a =  x[(x['race_number']== i) & (x['vel'] <= 100)]\n        sns.lineplot(data =a , x = 'trakus_index', y = 'vel',hue = 'course_type',palette = 'Set2')\n        plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-09-24T04:49:28.851180Z","iopub.execute_input":"2022-09-24T04:49:28.851601Z","iopub.status.idle":"2022-09-24T04:49:28.862285Z","shell.execute_reply.started":"2022-09-24T04:49:28.851566Z","shell.execute_reply":"2022-09-24T04:49:28.860757Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def velocity_distance_id(x):\n    vel = []\n    for i in range(len(x)):\n        try:\n            vel.append((((x.loc[i,'latitude'] - x.loc[i+1,'latitude'])*(x.loc[i,'latitude'] - x.loc[i+1,'latitude']) +\n            (x.loc[i,'longitude'] - x.loc[i+1,'longitude'])*(x.loc[i,'longitude'] - x.loc[i+1,'longitude']))**0.5)*1000000)\n        except:\n            vel.append(0)        \n    x['vel'] = vel\n    for i in x['race_number'].unique():\n        f,ax  = plt.subplots(1,1,figsize  = (10,10))\n        a =  x[(x['race_number']== i) & (x['vel'] <= 100)]\n        sns.lineplot(data =a , x = 'trakus_index', y = 'vel',hue = 'distance_id',palette = 'Set2')\n        plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-09-24T04:49:30.952125Z","iopub.execute_input":"2022-09-24T04:49:30.952527Z","iopub.status.idle":"2022-09-24T04:49:30.963902Z","shell.execute_reply.started":"2022-09-24T04:49:30.952492Z","shell.execute_reply":"2022-09-24T04:49:30.962567Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def velocity_track_condition(x):\n    vel = []\n    for i in range(len(x)):\n        try:\n            vel.append((((x.loc[i,'latitude'] - x.loc[i+1,'latitude'])*(x.loc[i,'latitude'] - x.loc[i+1,'latitude']) +\n            (x.loc[i,'longitude'] - x.loc[i+1,'longitude'])*(x.loc[i,'longitude'] - x.loc[i+1,'longitude']))**0.5)*1000000)\n        except:\n            vel.append(0)        \n    x['vel'] = vel\n    for i in x['race_number'].unique():\n        f,ax  = plt.subplots(1,1,figsize  = (10,10))\n        a =  x[(x['race_number']== i) & (x['vel'] <= 100)]\n        sns.lineplot(data =a , x = 'trakus_index', y = 'vel',hue = 'track_condition',palette = 'Set2')\n        plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-09-24T04:49:33.069305Z","iopub.execute_input":"2022-09-24T04:49:33.069966Z","iopub.status.idle":"2022-09-24T04:49:33.080636Z","shell.execute_reply.started":"2022-09-24T04:49:33.069932Z","shell.execute_reply":"2022-09-24T04:49:33.078969Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# For example\nLet 'Dylan Davis' see the match on May 25th, 2019","metadata":{}},{"cell_type":"code","source":"velocity_race_number(der0525Dyla)","metadata":{"execution":{"iopub.status.busy":"2022-09-24T04:49:35.370164Z","iopub.execute_input":"2022-09-24T04:49:35.370590Z","iopub.status.idle":"2022-09-24T04:49:37.088398Z","shell.execute_reply.started":"2022-09-24T04:49:35.370551Z","shell.execute_reply":"2022-09-24T04:49:37.087172Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"velocity_course_type(der0525Dyla)","metadata":{"execution":{"iopub.status.busy":"2022-09-24T04:49:40.927829Z","iopub.execute_input":"2022-09-24T04:49:40.928269Z","iopub.status.idle":"2022-09-24T04:49:42.626476Z","shell.execute_reply.started":"2022-09-24T04:49:40.928226Z","shell.execute_reply":"2022-09-24T04:49:42.625018Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"velocity_distance_id(der0525Dyla)","metadata":{"execution":{"iopub.status.busy":"2022-09-24T04:49:45.216784Z","iopub.execute_input":"2022-09-24T04:49:45.217259Z","iopub.status.idle":"2022-09-24T04:49:46.930666Z","shell.execute_reply.started":"2022-09-24T04:49:45.217216Z","shell.execute_reply":"2022-09-24T04:49:46.929781Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"velocity_track_condition(der0525Dyla)","metadata":{"execution":{"iopub.status.busy":"2022-09-24T04:49:48.064561Z","iopub.execute_input":"2022-09-24T04:49:48.065135Z","iopub.status.idle":"2022-09-24T04:49:49.891834Z","shell.execute_reply.started":"2022-09-24T04:49:48.065079Z","shell.execute_reply":"2022-09-24T04:49:49.890593Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Result\nBecause of this, when the rider is weak in the race and\nIf you strengthen the complementary points for each course, distance, and track, you can overcome your weaknesses.","metadata":{}}]}