{"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":"# Drill Down EDA is most Important things.\n## 1. Import Datas \n## 2. EDA all Data\n## 3. Let's See One race.\n## 4. Let's See One Jockey.","metadata":{}},{"cell_type":"markdown","source":"## 1. Import Datas\n### 1.1 Import Basic Lib.s","metadata":{}},{"cell_type":"code","source":"# basic Lib...\nimport numpy                as np\nimport pandas               as pd\nimport ipywidgets           as widgets\nimport matplotlib.pyplot    as plt\nimport os\nimport seaborn              as sns\n# import pymap3d              as pm\n\n# 시계열 데이터 처리용\n# Time Series Lib.\nfrom datetime        import datetime       as dt\nfrom IPython.display import display, HTML\n\n# To Calcurate distance Lib. Import\nfrom haversine       import haversine\n\n# To Compute running direction\nfrom scipy           import cos, sin, arctan, sqrt, arctan2 \n\n# To use Zoom/Pan in Graph\nimport plotly.offline       as plyo\nimport plotly.express       as px\nimport plotly.graph_objects as go\nimport cufflinks\n\n# Draw on Map\nimport folium\n\n# to compute OLS\nimport statsmodels.api      as sm","metadata":{"execution":{"iopub.status.busy":"2022-11-08T12:42:28.646769Z","iopub.execute_input":"2022-11-08T12:42:28.647376Z","iopub.status.idle":"2022-11-08T12:42:28.653595Z","shell.execute_reply.started":"2022-11-08T12:42:28.647340Z","shell.execute_reply":"2022-11-08T12:42:28.652837Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### 1.2 Data location","metadata":{}},{"cell_type":"code","source":"COMPLETE_TABLE = \"../input/big-data-derby-2022/nyra_2019_complete.csv\"\nRACE_TABLE = \"../input/big-data-derby-2022/nyra_race_table.csv\"\nSTART_TABLE = \"../input/big-data-derby-2022/nyra_start_table.csv\"\nTRACKING_TABLE = \"../input/big-data-derby-2022/nyra_tracking_table.csv\"","metadata":{"execution":{"iopub.status.busy":"2022-11-08T12:42:29.298317Z","iopub.execute_input":"2022-11-08T12:42:29.299358Z","iopub.status.idle":"2022-11-08T12:42:29.304553Z","shell.execute_reply.started":"2022-11-08T12:42:29.299318Z","shell.execute_reply":"2022-11-08T12:42:29.303451Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 1.3 Load data the complete data\n#### 2022-09-08 when position_at_finish is inclued, they omit header..\n#### I must write all var.s name.","metadata":{}},{"cell_type":"code","source":"nyra_2019_complete = pd.read_csv(COMPLETE_TABLE\n                                 , names=['track_id'\n                                        , 'race_date'\n                                        , 'race_number'\n                                        , 'program_number'\n                                        , 'trakus_index'\n                                        , 'latitude'\n                                        , 'longitude'\n                                        , 'distance_id'\n                                        , 'course_type'\n                                        , 'track_condition'\n                                        , 'run_up_distance'\n                                        , 'race_type'\n                                        , 'purse' # This Var. is omitted in discription.\n                                        , 'post_time'\n                                        , 'weight_carried'\n                                        , 'jockey'\n                                        , 'odds'\n                                        , 'position_at_finish'\n                                        ]\n                                 , header=None\n                                 , sep = ','\n                                 , dtype={'program_number':object} # if drop it, some errors occurs.\n                                )\n# nyra_2019_complete.head()","metadata":{"execution":{"iopub.status.busy":"2022-11-08T12:42:32.683059Z","iopub.execute_input":"2022-11-08T12:42:32.683447Z","iopub.status.idle":"2022-11-08T12:42:50.540075Z","shell.execute_reply.started":"2022-11-08T12:42:32.683415Z","shell.execute_reply":"2022-11-08T12:42:50.539010Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### 1.3.1 Remove redundant spaces","metadata":{}},{"cell_type":"code","source":"nyra_2019_complete.program_number = nyra_2019_complete.program_number.str.strip()","metadata":{"execution":{"iopub.status.busy":"2022-11-08T12:42:56.613133Z","iopub.execute_input":"2022-11-08T12:42:56.613545Z","iopub.status.idle":"2022-11-08T12:42:58.394722Z","shell.execute_reply.started":"2022-11-08T12:42:56.613509Z","shell.execute_reply":"2022-11-08T12:42:58.393413Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 1.4 Load data the racing information.","metadata":{}},{"cell_type":"code","source":"nyra_2019_race = pd.read_csv(RACE_TABLE\n                                 , sep = ','\n                                )\nnyra_2019_race","metadata":{"execution":{"iopub.status.busy":"2022-11-08T12:43:01.499496Z","iopub.execute_input":"2022-11-08T12:43:01.500262Z","iopub.status.idle":"2022-11-08T12:43:01.543556Z","shell.execute_reply.started":"2022-11-08T12:43:01.500221Z","shell.execute_reply":"2022-11-08T12:43:01.542549Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 1.5 Load data the start\n#### 2022-09-08 when position_at_finish is inclued, they omit header..\n#### I must write all var.s name.","metadata":{}},{"cell_type":"code","source":"nyra_2019_start = pd.read_csv(START_TABLE\n                                 , names=['track_id'\n                                        , 'race_date'\n                                        , 'race_number'\n                                        , 'program_number'\n                                        , 'weight_carried'\n                                        , 'jockey'\n                                        , 'odds'\n                                        , 'position_at_finish'\n                                        ]\n                                 , header=None\n                                 , sep = ','\n                                )\n# remove redundant spaces\nnyra_2019_start.program_number = nyra_2019_start.program_number.str.strip()\nnyra_2019_start","metadata":{"execution":{"iopub.status.busy":"2022-11-08T12:43:04.009244Z","iopub.execute_input":"2022-11-08T12:43:04.009992Z","iopub.status.idle":"2022-11-08T12:43:04.069472Z","shell.execute_reply.started":"2022-11-08T12:43:04.009942Z","shell.execute_reply":"2022-11-08T12:43:04.068130Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# remove redundant spaces\nnyra_2019_start.program_number = nyra_2019_start.program_number.str.strip()","metadata":{"execution":{"iopub.status.busy":"2022-11-08T12:43:05.325866Z","iopub.execute_input":"2022-11-08T12:43:05.326292Z","iopub.status.idle":"2022-11-08T12:43:05.337588Z","shell.execute_reply.started":"2022-11-08T12:43:05.326251Z","shell.execute_reply":"2022-11-08T12:43:05.336390Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 1.6 Load data the race tracking data","metadata":{}},{"cell_type":"code","source":"nyra_2019_tracking = pd.read_csv(TRACKING_TABLE\n                                 , sep = ','\n                                 , dtype={'program_number':object} \n                                 # front part is number, but include charaters...\n                                )\nnyra_2019_tracking","metadata":{"execution":{"iopub.status.busy":"2022-11-08T12:43:24.626294Z","iopub.execute_input":"2022-11-08T12:43:24.627376Z","iopub.status.idle":"2022-11-08T12:43:32.639674Z","shell.execute_reply.started":"2022-11-08T12:43:24.627325Z","shell.execute_reply":"2022-11-08T12:43:32.638530Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### 1.6.1 Remove redundant spaces","metadata":{}},{"cell_type":"code","source":"# remove redundant spaces\nnyra_2019_tracking.program_number = nyra_2019_tracking.program_number.str.strip()","metadata":{"execution":{"iopub.status.busy":"2022-11-08T12:43:36.332897Z","iopub.execute_input":"2022-11-08T12:43:36.333309Z","iopub.status.idle":"2022-11-08T12:43:38.062632Z","shell.execute_reply.started":"2022-11-08T12:43:36.333274Z","shell.execute_reply":"2022-11-08T12:43:38.061696Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 2. EDA all Data\n### Many players Done Well in EDA for all data","metadata":{}},{"cell_type":"markdown","source":"### 2.1.1 Define Function","metadata":{}},{"cell_type":"markdown","source":"#### 2.1.1.1 Compute distance with Earth's radius of curvature\n##### But, something differ from real because of radius vs. plat","metadata":{}},{"cell_type":"code","source":"def haversine1(lat1, lon1, lat2, lon2):\n      R = 3959.87433\n        # For Earth radius in miles      use 3959.87433 miles.  \n        # For Earth radius in kilometers use 6372.8 km\n      dLat = np.radians(lat2 - lat1)\n      dLon = np.radians(lon2 - lon1)\n      lat1 = np.radians(lat1)\n      lat2 = np.radians(lat2)\n      a = np.sin(dLat/2)**2 + np.cos(lat1)*np.cos(lat2)*np.sin(dLon/2)**2\n      c = 2*np.arcsin(np.sqrt(a))\n      return R * c","metadata":{"execution":{"iopub.status.busy":"2022-11-08T12:43:41.970738Z","iopub.execute_input":"2022-11-08T12:43:41.971142Z","iopub.status.idle":"2022-11-08T12:43:41.978077Z","shell.execute_reply.started":"2022-11-08T12:43:41.971107Z","shell.execute_reply":"2022-11-08T12:43:41.976924Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### 2.1.1.2 Compute running direction\n##### 0 means East.\n##### 90 means North.\n##### -90 means South.\n##### 180/-180 means West.","metadata":{}},{"cell_type":"code","source":"def Bearing(lat1, lon1, lat2, lon2):\n    Lat1 = np.radians(lat1)\n    Lat2 = np.radians(lat2)\n    Long1 = np.radians(lon1)\n    Long2 = np.radians(lon2) \n    y = np.sin(Long2-Long1)*np.cos(Lat2) \n    x = np.cos(Lat1)*np.sin(Lat2) - np.sin(Lat1)*np.cos(Lat2)*np.cos(Long2-Long1)\n    return np.degrees(np.arctan2(y, x))","metadata":{"execution":{"iopub.status.busy":"2022-11-08T12:43:46.792416Z","iopub.execute_input":"2022-11-08T12:43:46.792843Z","iopub.status.idle":"2022-11-08T12:43:46.799940Z","shell.execute_reply.started":"2022-11-08T12:43:46.792806Z","shell.execute_reply":"2022-11-08T12:43:46.798780Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 3. Let's See One race.","metadata":{}},{"cell_type":"markdown","source":"To select Long distance racing.<br>\nbecause I want to check the tracks.","metadata":{}},{"cell_type":"code","source":"nyra_2019_race[(nyra_2019_race['distance_id'] >= 1600)]","metadata":{"execution":{"iopub.status.busy":"2022-11-08T12:43:50.535401Z","iopub.execute_input":"2022-11-08T12:43:50.535812Z","iopub.status.idle":"2022-11-08T12:43:50.553030Z","shell.execute_reply.started":"2022-11-08T12:43:50.535763Z","shell.execute_reply":"2022-11-08T12:43:50.551862Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# race selected \nt_i_i = \"SAR\"\nr_d_i = \"2019-08-22\" #r_d_i = \"2019-08-28\"\nr_n_i = 1","metadata":{"execution":{"iopub.status.busy":"2022-11-08T12:43:55.189230Z","iopub.execute_input":"2022-11-08T12:43:55.189597Z","iopub.status.idle":"2022-11-08T12:43:55.194659Z","shell.execute_reply.started":"2022-11-08T12:43:55.189566Z","shell.execute_reply":"2022-11-08T12:43:55.193584Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# to get data only one race selected \ndef sel_race(track, date, race):\n    df_race = nyra_2019_race[\n            (nyra_2019_race['track_id'] == track)\n            & (nyra_2019_race['race_date'] == date)\n            & (nyra_2019_race['race_number'] == race)\n                                    ]\n    df_start = nyra_2019_start[\n            (nyra_2019_start['track_id'] == track)\n            & (nyra_2019_start['race_date'] == date)\n            & (nyra_2019_start['race_number'] == race)\n                                    ]\n    df_tracking = nyra_2019_tracking[\n            (nyra_2019_tracking['track_id'] == track)\n            & (nyra_2019_tracking['race_date'] == date)\n            & (nyra_2019_tracking['race_number'] == race)\n                                    ]\n    df_complete = nyra_2019_complete[\n            (nyra_2019_complete['track_id'] == track)\n            & (nyra_2019_complete['race_date'] == date)\n            & (nyra_2019_complete['race_number'] == race)\n                                    ]\n    return df_race, df_start, df_tracking, df_complete","metadata":{"execution":{"iopub.status.busy":"2022-11-08T12:43:58.709622Z","iopub.execute_input":"2022-11-08T12:43:58.710047Z","iopub.status.idle":"2022-11-08T12:43:58.719428Z","shell.execute_reply.started":"2022-11-08T12:43:58.710013Z","shell.execute_reply":"2022-11-08T12:43:58.718188Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"##### By Selected race, get data","metadata":{}},{"cell_type":"code","source":"df_race, df_start, df_tracking, df_complete = sel_race(t_i_i, r_d_i, r_n_i)","metadata":{"execution":{"iopub.status.busy":"2022-11-08T12:44:05.393755Z","iopub.execute_input":"2022-11-08T12:44:05.394143Z","iopub.status.idle":"2022-11-08T12:44:06.808317Z","shell.execute_reply.started":"2022-11-08T12:44:05.394112Z","shell.execute_reply":"2022-11-08T12:44:06.807406Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_race","metadata":{"execution":{"iopub.status.busy":"2022-11-08T12:44:08.272527Z","iopub.execute_input":"2022-11-08T12:44:08.272913Z","iopub.status.idle":"2022-11-08T12:44:08.285316Z","shell.execute_reply.started":"2022-11-08T12:44:08.272880Z","shell.execute_reply":"2022-11-08T12:44:08.284203Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_start","metadata":{"execution":{"iopub.status.busy":"2022-11-08T12:44:09.432293Z","iopub.execute_input":"2022-11-08T12:44:09.433265Z","iopub.status.idle":"2022-11-08T12:44:09.447593Z","shell.execute_reply.started":"2022-11-08T12:44:09.433223Z","shell.execute_reply":"2022-11-08T12:44:09.446360Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_tracking","metadata":{"execution":{"iopub.status.busy":"2022-11-08T12:44:10.631737Z","iopub.execute_input":"2022-11-08T12:44:10.632151Z","iopub.status.idle":"2022-11-08T12:44:10.654092Z","shell.execute_reply.started":"2022-11-08T12:44:10.632112Z","shell.execute_reply":"2022-11-08T12:44:10.652974Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_complete","metadata":{"execution":{"iopub.status.busy":"2022-11-08T12:44:11.993817Z","iopub.execute_input":"2022-11-08T12:44:11.994838Z","iopub.status.idle":"2022-11-08T12:44:12.023364Z","shell.execute_reply.started":"2022-11-08T12:44:11.994799Z","shell.execute_reply":"2022-11-08T12:44:12.022087Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Sorting data\n##### Sort this race program_number(jockey), trakus_index...\n##### If you use all tracking data, you need track_id, race_date, race_number more...","metadata":{}},{"cell_type":"code","source":"df_tracking = df_tracking.sort_values(by=['track_id'\n                                        ,'race_date'\n                                        ,'race_number'\n                                        ,'program_number'\n                                        ,'trakus_index'\n                                        ]\n                                    ,ignore_index=True\n                                   )","metadata":{"execution":{"iopub.status.busy":"2022-11-08T12:44:15.360413Z","iopub.execute_input":"2022-11-08T12:44:15.360828Z","iopub.status.idle":"2022-11-08T12:44:15.382922Z","shell.execute_reply.started":"2022-11-08T12:44:15.360789Z","shell.execute_reply":"2022-11-08T12:44:15.381718Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Making variables\nrunning distance, cumul. distance, heading degrees, direction <br>\nspeed is... <br> \n4 times distance is mile per second. <br>\n240 times distance is mile per minite. <br>\n14400 times distance is mile per hour.","metadata":{}},{"cell_type":"code","source":"%%time \n# time check\ndf_tracking['latitude_1']  = df_tracking['latitude'].shift(1)\ndf_tracking['longitude_1'] = df_tracking['longitude'].shift(1)\ndf_tracking.loc[(df_tracking.trakus_index == 1),'latitude_1']  = 0\ndf_tracking.loc[(df_tracking.trakus_index == 1),'longitude_1'] = 0\n\ndf_tracking['dista'] = haversine1(df_tracking['latitude_1']\n                                 ,df_tracking['longitude_1']\n                                 ,df_tracking['latitude']\n                                 ,df_tracking['longitude']\n                                 )\n\ndf_tracking.loc[(df_tracking.trakus_index == 1),'dista'] = 0\n\ndf_tracking['c_dis'] = df_tracking.groupby(['track_id'\n                                            ,'race_date'\n                                            ,'race_number'\n                                            ,'program_number'\n                                           ]\n                                          )['dista'].cumsum()\n\ndf_tracking['degre'] = Bearing( df_tracking['latitude_1']\n                               ,df_tracking['longitude_1']\n                               ,df_tracking['latitude']\n                               ,df_tracking['longitude']\n                              )\n\ndf_tracking['degre_1'] = df_tracking['degre'].shift(1)\n\ndf_tracking['direc'] = df_tracking['degre'] - df_tracking['degre_1']\n\ndf_tracking.loc[(df_tracking.trakus_index == 1),'degre'] = 0\n\ndf_tracking.loc[(df_tracking.trakus_index == 1),'direc'] = 0\ndf_tracking.loc[(df_tracking.trakus_index == 2),'direc'] = 0\n# It looks too big changing around 180(near the west side). \n# to reduce to real changing...\ndf_tracking.loc[(df_tracking.direc >= 300),'direc'] = df_tracking['direc'] - 360","metadata":{"execution":{"iopub.status.busy":"2022-11-08T12:44:17.858110Z","iopub.execute_input":"2022-11-08T12:44:17.858484Z","iopub.status.idle":"2022-11-08T12:44:17.896660Z","shell.execute_reply.started":"2022-11-08T12:44:17.858453Z","shell.execute_reply":"2022-11-08T12:44:17.895556Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# drop some vars.\ndf_tracking.drop(['latitude_1','longitude_1','degre_1'],axis=1,inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-11-08T12:44:20.336557Z","iopub.execute_input":"2022-11-08T12:44:20.337308Z","iopub.status.idle":"2022-11-08T12:44:20.344630Z","shell.execute_reply.started":"2022-11-08T12:44:20.337268Z","shell.execute_reply":"2022-11-08T12:44:20.343232Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_tracking","metadata":{"execution":{"iopub.status.busy":"2022-11-08T12:44:21.688208Z","iopub.execute_input":"2022-11-08T12:44:21.688633Z","iopub.status.idle":"2022-11-08T12:44:21.712882Z","shell.execute_reply.started":"2022-11-08T12:44:21.688596Z","shell.execute_reply":"2022-11-08T12:44:21.711627Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Let's see in Graph","metadata":{}},{"cell_type":"code","source":"# in graph running distance vs. cum.-running-distance\nplt.style.use(\"dark_background\")\nplt.figure(figsize = (16, 8))\nsns.lineplot(data = df_tracking\n                , x = \"c_dis\"\n                , y = \"dista\"\n                , hue = \"program_number\"\n                , palette = \"Set2\")\nplt.legend()","metadata":{"execution":{"iopub.status.busy":"2022-11-08T12:44:24.319927Z","iopub.execute_input":"2022-11-08T12:44:24.321198Z","iopub.status.idle":"2022-11-08T12:44:25.081022Z","shell.execute_reply.started":"2022-11-08T12:44:24.321147Z","shell.execute_reply":"2022-11-08T12:44:25.079905Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### * Around 1.0 and 2.0 miles(It's near the starting Point)\n####  their speeds write \"W\"\n### - There are some troubles nearby starting point.\n##### * Cumul.-running dist. is better then trakus_index to see details.\n##### * Cumul.-running dist. shows almost same spaces.\n##### * trakus_index shows same time.","metadata":{}},{"cell_type":"code","source":"#plt.style.use(\"dark_background\")\nplt.figure(figsize = (16, 8))\nsns.lineplot(data = df_tracking\n                , x = \"trakus_index\"\n                , y = \"dista\"\n                , hue = \"program_number\"\n                , palette = \"Set2\")\nplt.legend()","metadata":{"execution":{"iopub.status.busy":"2022-11-08T12:44:27.809117Z","iopub.execute_input":"2022-11-08T12:44:27.809535Z","iopub.status.idle":"2022-11-08T12:44:28.525459Z","shell.execute_reply.started":"2022-11-08T12:44:27.809499Z","shell.execute_reply":"2022-11-08T12:44:28.524399Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.style.use(\"dark_background\")\nplt.figure(figsize = (16, 8))\nsns.lineplot(data = df_tracking\n                , x = \"trakus_index\"\n                , y = \"degre\"\n                , hue = \"program_number\"\n                , palette = \"Set2\")\nplt.legend()","metadata":{"execution":{"iopub.status.busy":"2022-11-08T12:44:29.707975Z","iopub.execute_input":"2022-11-08T12:44:29.708436Z","iopub.status.idle":"2022-11-08T12:44:30.380705Z","shell.execute_reply.started":"2022-11-08T12:44:29.708395Z","shell.execute_reply":"2022-11-08T12:44:30.379574Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.style.use(\"dark_background\")\nplt.figure(figsize = (16, 8))\nsns.lineplot(data = df_tracking\n                , x = \"trakus_index\"\n                , y = \"direc\"\n                , hue = \"program_number\"\n                , palette = \"Set2\")\nplt.legend()","metadata":{"execution":{"iopub.status.busy":"2022-11-08T12:44:31.341701Z","iopub.execute_input":"2022-11-08T12:44:31.342087Z","iopub.status.idle":"2022-11-08T12:44:32.044917Z","shell.execute_reply.started":"2022-11-08T12:44:31.342056Z","shell.execute_reply":"2022-11-08T12:44:32.043713Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### when straight running, direction is about 0,\n#### when right-turning, over 0,\n#### when left-turning, below 0.","metadata":{}},{"cell_type":"code","source":"plt.style.use(\"dark_background\")\nplt.figure(figsize = (16, 8))\nsns.lineplot(data = df_tracking\n                , x = \"c_dis\"\n                , y = \"direc\"\n                , hue = \"program_number\"\n                , palette = \"Set2\")\nplt.legend()","metadata":{"execution":{"iopub.status.busy":"2022-11-08T12:44:34.409765Z","iopub.execute_input":"2022-11-08T12:44:34.410208Z","iopub.status.idle":"2022-11-08T12:44:35.095816Z","shell.execute_reply.started":"2022-11-08T12:44:34.410169Z","shell.execute_reply":"2022-11-08T12:44:35.094736Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### When Exit from round-running(east-north-east side), every jockeys turn to right and left very sharply.\n#### - There are some problems or something wrongs on senors or barriers.","metadata":{}},{"cell_type":"markdown","source":"## Let's See details with Zooming and Panning.","metadata":{}},{"cell_type":"code","source":"df_tracking_direc = df_tracking.pivot(index='trakus_index'\n                                    ,columns='program_number'\n                                    ,values='direc')\nplyo.iplot(df_tracking_direc.iplot(asFigure=True))","metadata":{"execution":{"iopub.status.busy":"2022-11-08T12:44:38.585633Z","iopub.execute_input":"2022-11-08T12:44:38.586065Z","iopub.status.idle":"2022-11-08T12:44:39.429677Z","shell.execute_reply.started":"2022-11-08T12:44:38.586031Z","shell.execute_reply":"2022-11-08T12:44:39.428409Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_tracking_dista = df_tracking.pivot(index='trakus_index'\n                                    ,columns='program_number'\n                                    ,values='dista')\nplyo.iplot(df_tracking_dista.iplot(asFigure=True))","metadata":{"execution":{"iopub.status.busy":"2022-11-08T12:44:42.257266Z","iopub.execute_input":"2022-11-08T12:44:42.258027Z","iopub.status.idle":"2022-11-08T12:44:42.317595Z","shell.execute_reply.started":"2022-11-08T12:44:42.257985Z","shell.execute_reply":"2022-11-08T12:44:42.316712Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_tracking_c_dis = df_tracking.pivot(index='trakus_index'\n                                    ,columns='program_number'\n                                    ,values='c_dis')\nplyo.iplot(df_tracking_c_dis.iplot(asFigure=True))","metadata":{"execution":{"iopub.status.busy":"2022-11-08T12:44:44.135037Z","iopub.execute_input":"2022-11-08T12:44:44.135489Z","iopub.status.idle":"2022-11-08T12:44:44.192896Z","shell.execute_reply.started":"2022-11-08T12:44:44.135449Z","shell.execute_reply":"2022-11-08T12:44:44.191815Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Show there running on the map.","metadata":{}},{"cell_type":"code","source":"color_options = [  'blue', 'green', 'purple', 'red'\n                 , 'darkblue', 'darkgreen', 'darkpurple', 'darkred'\n                 , 'orange', 'pink', 'gray', 'black'\n                 , 'lightblue', 'lightgreen', 'lightgray', 'lightred'\n                 , 'cadetblue', 'beige'\n                ]","metadata":{"execution":{"iopub.status.busy":"2022-11-08T12:44:48.135780Z","iopub.execute_input":"2022-11-08T12:44:48.136171Z","iopub.status.idle":"2022-11-08T12:44:48.141826Z","shell.execute_reply.started":"2022-11-08T12:44:48.136140Z","shell.execute_reply":"2022-11-08T12:44:48.140469Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"##### Each tracks have different center points.\n##### So, let's give every center point and zooming scales to each tracks.","metadata":{}},{"cell_type":"code","source":"# Aqueduct Race Track [40.672379, -73.829673]\n# Belmont Race Track  [40.715018, -73.722532]\n# Saratoga Race Track [43.071609, -73.768300]\nif t_i_i == \"AQU\":\n    map_sel = folium.Map(location = [40.672379, -73.829673]\n                         ,zoom_start=16.1\n                         ,tiles='cartodbpositron'\n                        )\nelif t_i_i == \"BEL\":\n    map_sel = folium.Map(location = [40.715018, -73.722532]\n                         ,zoom_start=16.7\n                         ,tiles='cartodbpositron'\n                        )\nelif t_i_i == \"SAR\":\n    map_sel = folium.Map(location = [43.071609, -73.768300]\n                         ,zoom_start=16.7\n                         ,tiles='cartodbpositron'\n                        )","metadata":{"execution":{"iopub.status.busy":"2022-11-08T12:44:50.535695Z","iopub.execute_input":"2022-11-08T12:44:50.536718Z","iopub.status.idle":"2022-11-08T12:44:50.551905Z","shell.execute_reply.started":"2022-11-08T12:44:50.536673Z","shell.execute_reply":"2022-11-08T12:44:50.550959Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pm_values = df_tracking[\"program_number\"].value_counts().index.tolist()\npm_values","metadata":{"execution":{"iopub.status.busy":"2022-11-08T12:44:52.059193Z","iopub.execute_input":"2022-11-08T12:44:52.059577Z","iopub.status.idle":"2022-11-08T12:44:52.068423Z","shell.execute_reply.started":"2022-11-08T12:44:52.059547Z","shell.execute_reply":"2022-11-08T12:44:52.067255Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"##### Dot every jockeys running point on the map.","metadata":{}},{"cell_type":"code","source":"for i in range(len(pm_values)-1):\n    df_sam = df_tracking[\n         (df_tracking['program_number'] == pm_values[i])\n        ].sort_values(by=['trakus_index']\n                     ).reset_index(drop=True)\n    latitude=list(df_sam[\"latitude\"])\n    longitude=list(df_sam[\"longitude\"])\n    location_sam=np.column_stack((latitude, longitude))\n    folium.PolyLine(\n        locations=location_sam\n        ,tooltip='Polyline'\n        ,color=color_options[i]\n    ).add_to(map_sel)","metadata":{"execution":{"iopub.status.busy":"2022-11-08T12:44:57.223092Z","iopub.execute_input":"2022-11-08T12:44:57.224196Z","iopub.status.idle":"2022-11-08T12:44:57.277889Z","shell.execute_reply.started":"2022-11-08T12:44:57.224145Z","shell.execute_reply":"2022-11-08T12:44:57.276895Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"map_sel","metadata":{"execution":{"iopub.status.busy":"2022-11-08T12:44:58.440373Z","iopub.execute_input":"2022-11-08T12:44:58.441500Z","iopub.status.idle":"2022-11-08T12:44:58.537699Z","shell.execute_reply.started":"2022-11-08T12:44:58.441449Z","shell.execute_reply":"2022-11-08T12:44:58.536551Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 4. Let's See One Jockey.","metadata":{}},{"cell_type":"markdown","source":"##### To analyze jockey's running...\n##### Select one jockey... and sort it by trakus_index.\n##### In This race, program_number = \"3\" is 1st.","metadata":{}},{"cell_type":"code","source":"df_sam = df_tracking[(df_tracking['program_number'] == \"3\")\n                    ].sort_values(by=['trakus_index']\n                                 ).reset_index(drop=True)\ndf_sam","metadata":{"execution":{"iopub.status.busy":"2022-11-08T12:45:02.800610Z","iopub.execute_input":"2022-11-08T12:45:02.801321Z","iopub.status.idle":"2022-11-08T12:45:02.827842Z","shell.execute_reply.started":"2022-11-08T12:45:02.801283Z","shell.execute_reply":"2022-11-08T12:45:02.826790Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Make trend like skeching line...","metadata":{}},{"cell_type":"code","source":"%%time\ntarget = df_sam[\"dista\"]     # Target Var.\ninterval = []  # gathering results\nstep = 1       # Steps to signal the Times\na1 = 0         # initial value of start\na2 = 3         # minimum intervar in every pireods\np_alpha = 0.8  # interval-estimating p-value ~ smaller, longer \n\ntt = target.iloc[a1:a2]\n\ntrend = range(len(tt))    # make it Dataframe\ntrend_t = pd.DataFrame({\"trend_0\": trend})\ntrend_line = sm.add_constant(trend_t, has_constant = \"add\")\nlin_scan = sm.OLS(tt,trend_line)\nfitted_lin_scan = lin_scan.fit()\npara_t = fitted_lin_scan.params\nconf_t = fitted_lin_scan.conf_int(alpha = p_alpha, cols=None)\npara_0 = para_t[\"trend_0\"]\nconf0_0 = conf_t[0][\"trend_0\"]\nconf1_0 = conf_t[1][\"trend_0\"]\nfor ii in range(0,len(target)-a2):\n    # 초기값 설정하기\n    b1 = a1\n    b2 = a2 + ii * step\n    if b2 > len(target):\n        break\n    \n    # Target...\n    btt = target.iloc[b1:b2].reset_index(drop = True) # to concate new trend, reset the index\n    btrend = range(len(btt))\n    btrend_t = pd.DataFrame({\"trend_0\": btrend})\n    btrend_line = sm.add_constant(btrend_t, has_constant = \"add\")\n    blin_scan = sm.OLS(btt,btrend_line)\n    bfitted_lin_scan = blin_scan.fit()\n    bpara_t = bfitted_lin_scan.params\n    bconf_t = bfitted_lin_scan.conf_int(alpha = p_alpha, cols=None)\n    bpara_0 = bpara_t[\"trend_0\"]\n    bconf0_0 = bconf_t[0][\"trend_0\"]\n    bconf1_0 = bconf_t[1][\"trend_0\"]\n    if (bpara_0 >= conf0_0) & (bpara_0 <= conf1_0):\n        b2 = b2 + ii * step\n    else :\n        interval.append(b2)\n        print(interval[-1],'This Time is (' + str(ii) + ')')\n        a1 = b2\n        a2 = a1 + 14\n        tt = target.iloc[a1:a2].reset_index(drop = True) # to concate new trend, reset the index\n        trend = range(len(tt))\n        trend_t = pd.DataFrame({\"trend_0\": trend})\n        trend_line = sm.add_constant(trend_t, has_constant = \"add\")\n        lin_scan = sm.OLS(tt,trend_line)\n        fitted_lin_scan = lin_scan.fit()\n        para_t = fitted_lin_scan.params\n        conf_t = fitted_lin_scan.conf_int(alpha = p_alpha, cols=None)\n        para_0 = para_t[\"trend_0\"]\n        conf0_0 = conf_t[0][\"trend_0\"]\n        conf1_0 = conf_t[1][\"trend_0\"]","metadata":{"execution":{"iopub.status.busy":"2022-11-08T12:45:05.587257Z","iopub.execute_input":"2022-11-08T12:45:05.587714Z","iopub.status.idle":"2022-11-08T12:45:05.814574Z","shell.execute_reply.started":"2022-11-08T12:45:05.587675Z","shell.execute_reply":"2022-11-08T12:45:05.813509Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Total Trend\ntrend = range(len(target))\n# Converse list to DataFlame\ntrend_1 = pd.DataFrame({\"trend_0\": trend})","metadata":{"execution":{"iopub.status.busy":"2022-11-08T12:45:06.462951Z","iopub.execute_input":"2022-11-08T12:45:06.463364Z","iopub.status.idle":"2022-11-08T12:45:06.470975Z","shell.execute_reply.started":"2022-11-08T12:45:06.463328Z","shell.execute_reply":"2022-11-08T12:45:06.469241Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"inte_0 = interval\naa = len(inte_0) + 1","metadata":{"execution":{"iopub.status.busy":"2022-11-08T12:45:08.337487Z","iopub.execute_input":"2022-11-08T12:45:08.337922Z","iopub.status.idle":"2022-11-08T12:45:08.343385Z","shell.execute_reply.started":"2022-11-08T12:45:08.337884Z","shell.execute_reply":"2022-11-08T12:45:08.342086Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trend_mul = trend_1\ntrend_mul = trend_mul.rename(columns = {\"trend_0\" : \"tren_1\"})\n\nfor ii in range(2,aa):\n    trend_mul = pd.concat([trend_mul\n                        ,trend_1\n                        ]\n                        ,axis=1\n                        )    \n    trend_mul = trend_mul.rename(columns = {'trend_0' : 'tren_' + str(ii)})\n# If you use script like below, every dummy vars' name are Alphabet, over 26, order charaters shows....\n# dum = pd.DataFrame(np.zeros((len(trend), aa-1)), columns=list(map(chr, range(65, 64 + aa))) )\n# So, Useing upper script, make the points to turn.\ninter_list = []\nfor i in range(len(interval)):\n    inter_list.append('int_' + str(interval[i])) \n\ndum = pd.DataFrame(np.zeros((len(trend), aa-1)), columns=inter_list )\n\nfor tt in range(aa-1):\n    dum.iloc[0:inte_0[tt],tt] = 1\n\ndff = pd.DataFrame(np.zeros((len(trend), aa-1)), columns=trend_mul.columns )\nfor tt in range(aa-1):\n    dff.iloc[0:inte_0[tt],tt] = 1\n# making multi. the dataflame, each dataflames' column names must be equal.\nrr = dff.rmul(trend_mul)\n# concate total trend, trend dummys, Intercept dummys...\ntrend_lin_t = pd.concat([trend_1\n                        ,dum\n                        ,rr\n                        ]\n                        ,axis=1\n                        )  \n\n# concate total intercept dummy..\ntrend_line = sm.add_constant(trend_lin_t, has_constant = \"add\")\n\n# OLS...\ntrend_lin_001 = sm.OLS(target,trend_line)\nfitted_trend_lin_001 = trend_lin_001.fit()\nprint(fitted_trend_lin_001.summary())\n# To make function, block the last line \"Print summary\"","metadata":{"execution":{"iopub.status.busy":"2022-11-08T12:45:09.937270Z","iopub.execute_input":"2022-11-08T12:45:09.937722Z","iopub.status.idle":"2022-11-08T12:45:10.063991Z","shell.execute_reply.started":"2022-11-08T12:45:09.937684Z","shell.execute_reply":"2022-11-08T12:45:10.062822Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"##### To concate fitting value, converte list to dataframe...","metadata":{}},{"cell_type":"code","source":"y1=pd.DataFrame(fitted_trend_lin_001.fittedvalues, columns=['fittedvalues'])\ntitan_c = pd.concat([df_sam,y1],axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-11-08T12:45:12.746753Z","iopub.execute_input":"2022-11-08T12:45:12.747167Z","iopub.status.idle":"2022-11-08T12:45:12.756682Z","shell.execute_reply.started":"2022-11-08T12:45:12.747132Z","shell.execute_reply":"2022-11-08T12:45:12.755288Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Let's make moving Averages...\ntitan_c['ma05_dista']  = (\n                            titan_c['dista']\n                          + titan_c['dista'].shift(1) \n                          + titan_c['dista'].shift(2) \n                          + titan_c['dista'].shift(-1) \n                          + titan_c['dista'].shift(-2) \n                         )/5\ntitan_c['ma09_dista']  = (\n                            titan_c['dista']\n                          + titan_c['dista'].shift(1) \n                          + titan_c['dista'].shift(2) \n                          + titan_c['dista'].shift(3) \n                          + titan_c['dista'].shift(4) \n                          + titan_c['dista'].shift(-1) \n                          + titan_c['dista'].shift(-2) \n                          + titan_c['dista'].shift(-3) \n                          + titan_c['dista'].shift(-4) \n                         )/9","metadata":{"execution":{"iopub.status.busy":"2022-11-08T12:45:14.208179Z","iopub.execute_input":"2022-11-08T12:45:14.208601Z","iopub.status.idle":"2022-11-08T12:45:14.221909Z","shell.execute_reply.started":"2022-11-08T12:45:14.208569Z","shell.execute_reply":"2022-11-08T12:45:14.220849Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Show it details by Graph with Zooming and panning.","metadata":{}},{"cell_type":"code","source":"fig = px.line(titan_c\n              , x='trakus_index'\n              , y=['dista','fittedvalues','ma09_dista']\n              , title='Distance per 0.25 sec.'\n             )\nfig.update_xaxes(rangeslider_visible=True)\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2022-11-08T12:45:18.825216Z","iopub.execute_input":"2022-11-08T12:45:18.825661Z","iopub.status.idle":"2022-11-08T12:45:19.202209Z","shell.execute_reply.started":"2022-11-08T12:45:18.825614Z","shell.execute_reply":"2022-11-08T12:45:19.201096Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"##### Sketching Line shows running intuitively.","metadata":{}},{"cell_type":"markdown","source":"### Making Function to get Trend like skeching line\n#### Input\n##### * df_input : Dataframe to get skeching line\n##### * target_input : Column name to get skeching line\n##### * initial_int : Minimum inteval\n#### Output : titan_c with fitted skeching line trend\n#### Use Like this : df_sam_3 = skeching(df_sam,\"dista\",8)","metadata":{}},{"cell_type":"code","source":"def skeching(df_input,target_input,initial_int):\n    target = df_input[target_input]     # Target Var.\n    interval = []            # gathering results\n    step = 1                 # Steps to signal the Times\n    a1 = 0                   # initial value of start\n    a2 = initial_int         # minimum intervar in every pireods\n    p_alpha = 0.8            # interval-estimating p-value ~ smaller, longer \n    tt = target.iloc[a1:a2]\n    trend = range(len(tt))    # make it Dataframe\n    trend_t = pd.DataFrame({\"trend_0\": trend})\n    trend_line = sm.add_constant(trend_t, has_constant = \"add\")\n    lin_scan = sm.OLS(tt,trend_line)\n    fitted_lin_scan = lin_scan.fit()\n    para_t = fitted_lin_scan.params\n    conf_t = fitted_lin_scan.conf_int(alpha = p_alpha, cols=None)\n    para_0 = para_t[\"trend_0\"]\n    conf0_0 = conf_t[0][\"trend_0\"]\n    conf1_0 = conf_t[1][\"trend_0\"]\n    for ii in range(0,len(target)-a2):\n        b1 = a1\n        b2 = a2 + ii * step\n        if b1 > len(target):\n            break\n        if b2 > len(target):\n            break\n        btt = target.iloc[b1:b2].reset_index(drop = True) # to concate new trend, reset the index\n        btrend = range(len(btt))\n        btrend_t = pd.DataFrame({\"trend_0\": btrend})\n        btrend_line = sm.add_constant(btrend_t, has_constant = \"add\")\n        blin_scan = sm.OLS(btt,btrend_line)\n        bfitted_lin_scan = blin_scan.fit()\n        bpara_t = bfitted_lin_scan.params\n        bconf_t = bfitted_lin_scan.conf_int(alpha = p_alpha, cols=None)\n        bpara_0 = bpara_t[\"trend_0\"]\n        bconf0_0 = bconf_t[0][\"trend_0\"]\n        bconf1_0 = bconf_t[1][\"trend_0\"]\n        if (bpara_0 >= conf0_0) & (bpara_0 <= conf1_0):\n            b2 = b2 + ii * step\n        else :\n            interval.append(b2)\n            a1 = b2\n            a2 = a1 + initial_int\n            tt = target.iloc[a1:a2].reset_index(drop = True) # to concate new trend, reset the index\n            trend = range(len(tt))\n            trend_t = pd.DataFrame({\"trend_0\": trend})\n            trend_line = sm.add_constant(trend_t, has_constant = \"add\")\n            lin_scan = sm.OLS(tt,trend_line)\n            fitted_lin_scan = lin_scan.fit()\n            para_t = fitted_lin_scan.params\n            conf_t = fitted_lin_scan.conf_int(alpha = p_alpha, cols=None)\n            para_0 = para_t[\"trend_0\"]\n            conf0_0 = conf_t[0][\"trend_0\"]\n            conf1_0 = conf_t[1][\"trend_0\"]\n    # Total Trend\n    trend = range(len(target))\n    # Converse list to DataFlame\n    trend_1 = pd.DataFrame({\"trend_0\": trend})\n    inte_0 = interval\n    aa = len(inte_0) + 1\n    trend_mul = trend_1\n    trend_mul = trend_mul.rename(columns = {\"trend_0\" : \"tren_1\"})\n\n    for ii in range(2,aa):\n        trend_mul = pd.concat([trend_mul\n                            ,trend_1\n                            ]\n                            ,axis=1\n                            )    \n        trend_mul = trend_mul.rename(columns = {'trend_0' : 'tren_' + str(ii)})\n    inter_list = []\n    for i in range(len(interval)):\n        inter_list.append('int_' + str(interval[i])) \n\n    dum = pd.DataFrame(np.zeros((len(trend), aa-1)), columns=inter_list )\n\n    for tt in range(aa-1):\n        dum.iloc[0:inte_0[tt],tt] = 1\n\n    dff = pd.DataFrame(np.zeros((len(trend), aa-1)), columns=trend_mul.columns )\n    for tt in range(aa-1):\n        dff.iloc[0:inte_0[tt],tt] = 1\n    # making multi. the dataflame, each dataflames' column names must be equal.\n    rr = dff.rmul(trend_mul)\n    # concate total trend, trend dummys, Intercept dummys...\n    trend_lin_t = pd.concat([trend_1\n                            ,dum\n                            ,rr\n                            ]\n                            ,axis=1\n                            )  \n    # concate total intercept dummy..\n    trend_line = sm.add_constant(trend_lin_t, has_constant = \"add\")\n    # OLS...\n    trend_lin_001        = sm.OLS(target,trend_line)\n    fitted_trend_lin_001 = trend_lin_001.fit()\n    y1      = pd.DataFrame(fitted_trend_lin_001.fittedvalues, columns=['fittedvalues'])\n    titan_c = pd.concat([df_input,y1],axis=1)\n    return titan_c","metadata":{"execution":{"iopub.status.busy":"2022-11-08T12:45:25.566418Z","iopub.execute_input":"2022-11-08T12:45:25.566871Z","iopub.status.idle":"2022-11-08T12:45:25.592024Z","shell.execute_reply.started":"2022-11-08T12:45:25.566832Z","shell.execute_reply":"2022-11-08T12:45:25.590624Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_sam_1st = skeching(df_sam,\"dista\",8)","metadata":{"execution":{"iopub.status.busy":"2022-11-08T12:45:28.241990Z","iopub.execute_input":"2022-11-08T12:45:28.242386Z","iopub.status.idle":"2022-11-08T12:45:28.834663Z","shell.execute_reply.started":"2022-11-08T12:45:28.242353Z","shell.execute_reply":"2022-11-08T12:45:28.832753Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = px.line(df_sam_1st\n#              , x='trakus_index'\n              , x='c_dis'\n              , y=['dista'\n                  ,'fittedvalues'\n                  ]\n              , title='Distance per 0.25 sec.'\n             )\nfig.update_xaxes(rangeslider_visible=True)\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2022-11-08T12:45:30.226873Z","iopub.execute_input":"2022-11-08T12:45:30.227295Z","iopub.status.idle":"2022-11-08T12:45:30.299994Z","shell.execute_reply.started":"2022-11-08T12:45:30.227259Z","shell.execute_reply":"2022-11-08T12:45:30.298937Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Get the Moving Average Line and residuals to show running pattern","metadata":{}},{"cell_type":"code","source":"# Let's make moving Averages...\ndef ma_line(df_input):\n    df_input['ma05_dista']  = (\n                            df_input['dista']\n                          + df_input['dista'].shift(1) \n                          + df_input['dista'].shift(2) \n                          + df_input['dista'].shift(-1) \n                          + df_input['dista'].shift(-2) \n                         )/5\n    df_input['ma09_dista']  = (\n                            df_input['dista']\n                          + df_input['dista'].shift(1) \n                          + df_input['dista'].shift(2) \n                          + df_input['dista'].shift(3) \n                          + df_input['dista'].shift(4) \n                          + df_input['dista'].shift(-1) \n                          + df_input['dista'].shift(-2) \n                          + df_input['dista'].shift(-3) \n                          + df_input['dista'].shift(-4) \n                         )/9\n    df_input['resid_ma05_dista']  = df_input['dista'] - df_input['ma05_dista']\n    df_input['resid_ma09_dista']  = df_input['dista'] - df_input['ma09_dista']\n    return df_input","metadata":{"execution":{"iopub.status.busy":"2022-11-08T12:45:34.561527Z","iopub.execute_input":"2022-11-08T12:45:34.562617Z","iopub.status.idle":"2022-11-08T12:45:34.570768Z","shell.execute_reply.started":"2022-11-08T12:45:34.562572Z","shell.execute_reply":"2022-11-08T12:45:34.569744Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Make One dataframe each jockeies In One Race.","metadata":{}},{"cell_type":"code","source":"program_number = df_start[\"program_number\"].reset_index(drop=True).tolist()","metadata":{"execution":{"iopub.status.busy":"2022-11-08T12:45:39.857280Z","iopub.execute_input":"2022-11-08T12:45:39.858381Z","iopub.status.idle":"2022-11-08T12:45:39.863535Z","shell.execute_reply.started":"2022-11-08T12:45:39.858336Z","shell.execute_reply":"2022-11-08T12:45:39.862407Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Make it like df_sam_1, df_sam_2, df_sam_3, ...","metadata":{}},{"cell_type":"code","source":"for i in program_number:\n    globals()['df_sam_{}'.format(i)] = df_tracking[(df_tracking['program_number'] == i )\n                                                ].sort_values(by=['trakus_index']\n                                                             ).reset_index(drop=True)","metadata":{"execution":{"iopub.status.busy":"2022-11-08T12:45:48.685599Z","iopub.execute_input":"2022-11-08T12:45:48.686034Z","iopub.status.idle":"2022-11-08T12:45:48.707121Z","shell.execute_reply.started":"2022-11-08T12:45:48.685997Z","shell.execute_reply":"2022-11-08T12:45:48.705927Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"##### Check it","metadata":{}},{"cell_type":"code","source":"df_sam_1","metadata":{"execution":{"iopub.status.busy":"2022-11-08T12:45:52.774570Z","iopub.execute_input":"2022-11-08T12:45:52.775008Z","iopub.status.idle":"2022-11-08T12:45:52.800124Z","shell.execute_reply.started":"2022-11-08T12:45:52.774973Z","shell.execute_reply":"2022-11-08T12:45:52.798710Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### If you are the program_number 9's trainer, See details.","metadata":{}},{"cell_type":"code","source":"df_sam_9 = skeching(df_sam_9,\"dista\",8)","metadata":{"execution":{"iopub.status.busy":"2022-11-08T12:45:56.207495Z","iopub.execute_input":"2022-11-08T12:45:56.207917Z","iopub.status.idle":"2022-11-08T12:45:56.699059Z","shell.execute_reply.started":"2022-11-08T12:45:56.207881Z","shell.execute_reply":"2022-11-08T12:45:56.697391Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_sam_9","metadata":{"execution":{"iopub.status.busy":"2022-11-08T12:46:00.082634Z","iopub.execute_input":"2022-11-08T12:46:00.083124Z","iopub.status.idle":"2022-11-08T12:46:00.107114Z","shell.execute_reply.started":"2022-11-08T12:46:00.083085Z","shell.execute_reply":"2022-11-08T12:46:00.106096Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_sam_9 = ma_line(df_sam_9)","metadata":{"execution":{"iopub.status.busy":"2022-11-08T12:46:01.272086Z","iopub.execute_input":"2022-11-08T12:46:01.272488Z","iopub.status.idle":"2022-11-08T12:46:01.284567Z","shell.execute_reply.started":"2022-11-08T12:46:01.272452Z","shell.execute_reply":"2022-11-08T12:46:01.283718Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### By Residual Analisys, running patterns shows.","metadata":{}},{"cell_type":"code","source":"fig = px.line(df_sam_9\n              , x='trakus_index'\n              , y=[ \n                   'resid_ma09_dista'\n                  ]\n              , title='Distance per 0.25 sec.'\n             )\nfig.update_xaxes(rangeslider_visible=True)\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2022-11-08T12:46:05.356678Z","iopub.execute_input":"2022-11-08T12:46:05.357800Z","iopub.status.idle":"2022-11-08T12:46:05.421231Z","shell.execute_reply.started":"2022-11-08T12:46:05.357742Z","shell.execute_reply":"2022-11-08T12:46:05.420084Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"##### Zooming between 0 and 200, residual pattern is repeated about 48.\n###### It means running cycle is 12/n sec.\n","metadata":{}},{"cell_type":"markdown","source":"### And compare with 1st jockey's running, training schedule.\n### If you dominate orther all jockeys, this analisys isn't needed.\n### But trian the jockey following the 1st jockey to save the power and last corner pass the 1st.\n### Taking after 1st jockey's Running pattern is helpful to save the power.","metadata":{}},{"cell_type":"code","source":"df_sam_9.to_excel('df_sam_9.xlsx')","metadata":{"execution":{"iopub.status.busy":"2022-11-08T12:46:16.195047Z","iopub.execute_input":"2022-11-08T12:46:16.195857Z","iopub.status.idle":"2022-11-08T12:46:16.924781Z","shell.execute_reply.started":"2022-11-08T12:46:16.195809Z","shell.execute_reply":"2022-11-08T12:46:16.923629Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}