{"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":"# Injury Analysis","metadata":{}},{"cell_type":"markdown","source":"# Import libraries","metadata":{}},{"cell_type":"code","source":"!pip install utm\nimport numpy as np\nimport pandas as pd\nimport utm\nimport math\nfrom tqdm.auto import tqdm\ntqdm.pandas()\n%matplotlib inline\nimport matplotlib \nimport matplotlib.pyplot as plt\nfrom openpyxl import Workbook\nfrom openpyxl import load_workbook\nimport plotly.express as px\nimport warnings\nwarnings.filterwarnings(\"ignore\")","metadata":{"execution":{"iopub.status.busy":"2022-11-10T05:20:25.869577Z","iopub.execute_input":"2022-11-10T05:20:25.870395Z","iopub.status.idle":"2022-11-10T05:20:44.495257Z","shell.execute_reply.started":"2022-11-10T05:20:25.870265Z","shell.execute_reply":"2022-11-10T05:20:44.493845Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def tqdm_pandas(t):\n    from pandas.core.frame import DataFrame\n    def inner(df, func, *args, **kwargs):\n        t.total = groups.size // len(groups)\n        def wrapper(*args, **kwargs):\n            t.update(1)\n            return func(*args, **kwargs)\n        result = df.transform(wrapper, *args, **kwargs)\n        t.close()\n        return result\n    DataFrame.progress_transform = inner","metadata":{"execution":{"iopub.status.busy":"2022-11-10T05:20:44.498652Z","iopub.execute_input":"2022-11-10T05:20:44.500122Z","iopub.status.idle":"2022-11-10T05:20:44.509351Z","shell.execute_reply.started":"2022-11-10T05:20:44.500063Z","shell.execute_reply":"2022-11-10T05:20:44.507589Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Reading data","metadata":{}},{"cell_type":"code","source":"# Reading Injured animal \"Mybestfriend\" data run on \"2019-01-06\" ,race_number =7,program_number=4,and ridden by \"Reylu Gutierrez\"\ndf = pd.read_excel(\"/kaggle/input/derby-2022/My_Best_Friend.xlsx\")\n\n# Reading Aqueduct-2019 Six furlongs Muddy underfoot conditions data\ndf1 = pd.read_excel(\"/kaggle/input/derby-2022/AQU_600_DIRT_MY.xlsx\")","metadata":{"execution":{"iopub.status.busy":"2022-11-10T05:20:44.510975Z","iopub.execute_input":"2022-11-10T05:20:44.511433Z","iopub.status.idle":"2022-11-10T05:20:58.892274Z","shell.execute_reply.started":"2022-11-10T05:20:44.511396Z","shell.execute_reply":"2022-11-10T05:20:58.890836Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# \"My Best Friend\" Analysis","metadata":{}},{"cell_type":"code","source":"df = df.sort_values(['track_id', 'race_date', 'race_number', 'program_number', 'trakus_index']).reset_index(drop=True)\n# Reading Injured animal \"Mybestfriend\" data run on \"2019-01-06\" ,race_number =7,program_number=4,and ridden by \"Reylu Gutierrez\"\ndf.drop([\"Unnamed: 0\"], axis=1, inplace=True)\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2022-11-10T05:20:58.895109Z","iopub.execute_input":"2022-11-10T05:20:58.895529Z","iopub.status.idle":"2022-11-10T05:20:58.956676Z","shell.execute_reply.started":"2022-11-10T05:20:58.895489Z","shell.execute_reply":"2022-11-10T05:20:58.955387Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['xy'] = np.array(utm.from_latlon(df['latitude'].values, df['longitude'].values)[:2]).T.tolist()","metadata":{"execution":{"iopub.status.busy":"2022-11-10T05:20:58.958637Z","iopub.execute_input":"2022-11-10T05:20:58.959457Z","iopub.status.idle":"2022-11-10T05:20:58.969737Z","shell.execute_reply.started":"2022-11-10T05:20:58.959406Z","shell.execute_reply":"2022-11-10T05:20:58.968270Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_speed(points):\n    speed = [0]\n    points = points.values\n    for i in range(1, len(points)):\n        speed.append(4 * np.linalg.norm(np.array(points[i]) - np.array(points[i-1])))\n    return speed\n\ndf['speed'] = df.groupby(['track_id', 'race_date', 'race_number', 'program_number'])['xy'].progress_transform(get_speed)","metadata":{"execution":{"iopub.status.busy":"2022-11-10T05:20:58.971401Z","iopub.execute_input":"2022-11-10T05:20:58.971980Z","iopub.status.idle":"2022-11-10T05:20:59.035897Z","shell.execute_reply.started":"2022-11-10T05:20:58.971930Z","shell.execute_reply":"2022-11-10T05:20:59.034671Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def getAngle(a, b, c, thresh=5):\n    ang = 180 - math.degrees(math.atan2(c[1]-b[1], c[0]-b[0]) - math.atan2(a[1]-b[1], a[0]-b[0]))\n    if ang < -180:\n        ang += 360\n    if ang > 180:\n        ang -= 360\n    if abs(ang) > thresh:\n        return np.nan\n    return ang\n\ndef get_turning_angle(points):\n    turning_angle = [0]\n    for i in range(1, len(points.values) - 1):\n        turning_angle.append(getAngle(points.values[i-1], points.values[i], points.values[i+1]))\n    turning_angle.append(0)\n    return turning_angle\n\ndf['turning_angle'] = df.groupby(['track_id', 'race_date', 'race_number', 'program_number'])['xy'].progress_transform(get_turning_angle)","metadata":{"execution":{"iopub.status.busy":"2022-11-10T05:20:59.037714Z","iopub.execute_input":"2022-11-10T05:20:59.038232Z","iopub.status.idle":"2022-11-10T05:20:59.091590Z","shell.execute_reply.started":"2022-11-10T05:20:59.038184Z","shell.execute_reply":"2022-11-10T05:20:59.090571Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_prev = df[['track_id', 'race_date', 'race_number', 'trakus_index', 'program_number', 'xy']].copy()\ndf_prev['trakus_index'] = df_prev['trakus_index'] + 1\ndfa = pd.merge(df, df_prev, on=['track_id', 'race_date', 'race_number', 'program_number', 'trakus_index'], suffixes=['', '_prev'], how='left')","metadata":{"execution":{"iopub.status.busy":"2022-11-10T05:20:59.093065Z","iopub.execute_input":"2022-11-10T05:20:59.093399Z","iopub.status.idle":"2022-11-10T05:20:59.112228Z","shell.execute_reply.started":"2022-11-10T05:20:59.093369Z","shell.execute_reply":"2022-11-10T05:20:59.110746Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dfa['xy_pair'] = dfa.progress_apply(lambda row: [np.array(row['xy_prev']), np.array(row['xy'])], axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-11-10T05:20:59.113509Z","iopub.execute_input":"2022-11-10T05:20:59.113848Z","iopub.status.idle":"2022-11-10T05:20:59.164673Z","shell.execute_reply.started":"2022-11-10T05:20:59.113818Z","shell.execute_reply":"2022-11-10T05:20:59.163498Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def lateral_distance(p1, p2, p3):\n    return round(np.cross(p2-p1, p1-p3) / np.linalg.norm(p2-p1), 2)\n\ndef longitudinal_distance(p1, p2, p3):\n    return round(np.dot(p2-p1, p3-p2) / np.linalg.norm(p2-p1), 2)\n\ndef get_lateral_distances(points):\n    return [[lateral_distance(point1[0], point1[1], point2[1]) for point2 in points.values] for point1 in points.values]\n\ndef get_longitudinal_distances(points):\n    return [[longitudinal_distance(point1[0], point1[1], point2[1]) for point2 in points.values] for point1 in points.values]\n\ndfa['lateral_distances'] = dfa.groupby(['track_id', 'race_date', 'race_number', 'trakus_index'])['xy_pair'].progress_transform(get_lateral_distances)\ndfa['longitudinal_distances'] = dfa.groupby(['track_id', 'race_date', 'race_number', 'trakus_index'])['xy_pair'].progress_transform(get_longitudinal_distances)","metadata":{"execution":{"iopub.status.busy":"2022-11-10T05:20:59.168611Z","iopub.execute_input":"2022-11-10T05:20:59.169016Z","iopub.status.idle":"2022-11-10T05:20:59.399679Z","shell.execute_reply.started":"2022-11-10T05:20:59.168978Z","shell.execute_reply":"2022-11-10T05:20:59.398360Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_position(longitudinal_distances):\n    position = (np.array(longitudinal_distances) >= 0).sum()\n    return np.nan if position == 0 else position\n\ndfa['position'] = dfa['longitudinal_distances'].progress_apply(get_position)","metadata":{"execution":{"iopub.status.busy":"2022-11-10T05:20:59.401326Z","iopub.execute_input":"2022-11-10T05:20:59.401934Z","iopub.status.idle":"2022-11-10T05:20:59.449561Z","shell.execute_reply.started":"2022-11-10T05:20:59.401895Z","shell.execute_reply":"2022-11-10T05:20:59.448092Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### \"My Best Friend\" speed analysis","metadata":{}},{"cell_type":"code","source":"fig = px.line(dfa, x= \"trakus_index\", y=\"speed\",title='', markers=True)\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2022-11-10T05:20:59.451670Z","iopub.execute_input":"2022-11-10T05:20:59.452229Z","iopub.status.idle":"2022-11-10T05:21:00.826935Z","shell.execute_reply.started":"2022-11-10T05:20:59.452176Z","shell.execute_reply":"2022-11-10T05:21:00.825671Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### \"My Best Friend\" angle analysis","metadata":{}},{"cell_type":"code","source":"fig = px.line(dfa, x= \"trakus_index\", y=\"turning_angle\",title='', markers=True)\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2022-11-10T05:21:00.828570Z","iopub.execute_input":"2022-11-10T05:21:00.829070Z","iopub.status.idle":"2022-11-10T05:21:00.895996Z","shell.execute_reply.started":"2022-11-10T05:21:00.829001Z","shell.execute_reply":"2022-11-10T05:21:00.894671Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Mean Analysis\nMean analysis of Aqueduct 1200m dirt muddy track winners","metadata":{}},{"cell_type":"code","source":"df1 # Reading Aqueduct-2019 Six furlongs Muddy underfoot conditions data","metadata":{"execution":{"iopub.status.busy":"2022-11-10T05:21:00.897517Z","iopub.execute_input":"2022-11-10T05:21:00.897888Z","iopub.status.idle":"2022-11-10T05:21:00.948024Z","shell.execute_reply.started":"2022-11-10T05:21:00.897852Z","shell.execute_reply":"2022-11-10T05:21:00.946741Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df1 = df1[df1[\"final position\"] == 1]# Filtering out winners from Aqueduct-2019 Six furlongs Muddy underfoot conditions data\ndf1.drop([\"Unnamed: 0\", \"Unnamed: 0.1\"], axis=1, inplace=True)\ndf1","metadata":{"execution":{"iopub.status.busy":"2022-11-10T05:21:00.950243Z","iopub.execute_input":"2022-11-10T05:21:00.950632Z","iopub.status.idle":"2022-11-10T05:21:01.002035Z","shell.execute_reply.started":"2022-11-10T05:21:00.950599Z","shell.execute_reply":"2022-11-10T05:21:01.000505Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df1['program_number'].unique()","metadata":{"execution":{"iopub.status.busy":"2022-11-10T05:21:01.003700Z","iopub.execute_input":"2022-11-10T05:21:01.004132Z","iopub.status.idle":"2022-11-10T05:21:01.013834Z","shell.execute_reply.started":"2022-11-10T05:21:01.004086Z","shell.execute_reply":"2022-11-10T05:21:01.012339Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df1['program_number'] = df1['program_number'].astype(str)\npg_dict = {'1': '01','1.0': '01', '1  ': '01', '10': '10', '10 ': '10', '11': '11', '11 ': '11', '12': '12', '12 ': '12',\n           '13': '13', '13 ': '13', '14': '14', '14 ': '14', '15': '15', '15 ': '15', '16': '16', '16 ': '16',\n           '1A ': '01A', '1X ': '01X', '2': '02', '2  ': '02', '2B ': '02B', '3': '03', '3  ': '03', '3X ': '03X',\n           '4': '04', '4  ': '04', '5': '05', '5  ': '05', '6': '06', '6.0': '06', '7': '07', '7  ': '07',\n           '8': '08', '8  ': '08', '9': '09', '9  ': '09', '1A': '01A', '2B': '02B', '3X': '03X', '1X': '01X'}\ndf1['pg'] = df1['program_number'].map(pg_dict)","metadata":{"execution":{"iopub.status.busy":"2022-11-10T05:21:01.015417Z","iopub.execute_input":"2022-11-10T05:21:01.015809Z","iopub.status.idle":"2022-11-10T05:21:01.029581Z","shell.execute_reply.started":"2022-11-10T05:21:01.015773Z","shell.execute_reply":"2022-11-10T05:21:01.028060Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df1 = df1.sort_values(['track_id', 'race_date', 'race_number', 'pg', 'trakus_index']).reset_index(drop=True)","metadata":{"execution":{"iopub.status.busy":"2022-11-10T05:21:01.031492Z","iopub.execute_input":"2022-11-10T05:21:01.032301Z","iopub.status.idle":"2022-11-10T05:21:01.054442Z","shell.execute_reply.started":"2022-11-10T05:21:01.032166Z","shell.execute_reply":"2022-11-10T05:21:01.052967Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df1['xy'] = np.array(utm.from_latlon(df1['latitude'].values, df1['longitude'].values)[:2]).T.tolist()","metadata":{"execution":{"iopub.status.busy":"2022-11-10T05:21:01.056122Z","iopub.execute_input":"2022-11-10T05:21:01.056640Z","iopub.status.idle":"2022-11-10T05:21:01.071644Z","shell.execute_reply.started":"2022-11-10T05:21:01.056591Z","shell.execute_reply":"2022-11-10T05:21:01.070149Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df1['speed'] = df1.groupby(['track_id', 'race_date', 'race_number', 'pg'])['xy'].progress_transform(get_speed)","metadata":{"execution":{"iopub.status.busy":"2022-11-10T05:21:01.073556Z","iopub.execute_input":"2022-11-10T05:21:01.074097Z","iopub.status.idle":"2022-11-10T05:21:01.156166Z","shell.execute_reply.started":"2022-11-10T05:21:01.074024Z","shell.execute_reply":"2022-11-10T05:21:01.154882Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df1['turning_angle'] = df1.groupby(['track_id', 'race_date', 'race_number', 'pg'])['xy'].progress_transform(get_turning_angle)","metadata":{"execution":{"iopub.status.busy":"2022-11-10T05:21:01.157600Z","iopub.execute_input":"2022-11-10T05:21:01.157957Z","iopub.status.idle":"2022-11-10T05:21:01.232360Z","shell.execute_reply.started":"2022-11-10T05:21:01.157923Z","shell.execute_reply":"2022-11-10T05:21:01.230800Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_prev = df1[['track_id', 'race_date', 'race_number', 'trakus_index', 'pg', 'xy']].copy()\ndf_prev['trakus_index'] = df_prev['trakus_index'] + 1\ndf1 = pd.merge(df1, df_prev, on=['track_id', 'race_date', 'race_number', 'pg', 'trakus_index'], suffixes=['', '_prev'], how='left')","metadata":{"execution":{"iopub.status.busy":"2022-11-10T05:21:01.234437Z","iopub.execute_input":"2022-11-10T05:21:01.234812Z","iopub.status.idle":"2022-11-10T05:21:01.262179Z","shell.execute_reply.started":"2022-11-10T05:21:01.234779Z","shell.execute_reply":"2022-11-10T05:21:01.261162Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df1['xy_pair'] = df1.progress_apply(lambda row: [np.array(row['xy_prev']), np.array(row['xy'])], axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-11-10T05:21:01.263730Z","iopub.execute_input":"2022-11-10T05:21:01.264575Z","iopub.status.idle":"2022-11-10T05:21:01.408726Z","shell.execute_reply.started":"2022-11-10T05:21:01.264531Z","shell.execute_reply":"2022-11-10T05:21:01.407786Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df1['lateral_distances'] = df1.groupby(['track_id', 'race_date', 'race_number', 'trakus_index'])['xy_pair'].progress_transform(get_lateral_distances)\ndf1['longitudinal_distances'] = df1.groupby(['track_id', 'race_date', 'race_number', 'trakus_index'])['xy_pair'].progress_transform(get_longitudinal_distances)","metadata":{"execution":{"iopub.status.busy":"2022-11-10T05:21:01.409684Z","iopub.execute_input":"2022-11-10T05:21:01.410016Z","iopub.status.idle":"2022-11-10T05:21:03.665094Z","shell.execute_reply.started":"2022-11-10T05:21:01.409985Z","shell.execute_reply":"2022-11-10T05:21:03.663190Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df1['position'] = df1['longitudinal_distances'].progress_apply(get_position)","metadata":{"execution":{"iopub.status.busy":"2022-11-10T05:21:03.667953Z","iopub.execute_input":"2022-11-10T05:21:03.668648Z","iopub.status.idle":"2022-11-10T05:21:03.760484Z","shell.execute_reply.started":"2022-11-10T05:21:03.668583Z","shell.execute_reply":"2022-11-10T05:21:03.759167Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df1","metadata":{"execution":{"iopub.status.busy":"2022-11-10T05:21:03.763178Z","iopub.execute_input":"2022-11-10T05:21:03.763628Z","iopub.status.idle":"2022-11-10T05:21:03.841479Z","shell.execute_reply.started":"2022-11-10T05:21:03.763593Z","shell.execute_reply":"2022-11-10T05:21:03.840078Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df1a = df1.groupby(\"trakus_index\").mean()\ndf1a = df1a.reset_index()\ndf1a.head()","metadata":{"execution":{"iopub.status.busy":"2022-11-10T05:21:03.843032Z","iopub.execute_input":"2022-11-10T05:21:03.843405Z","iopub.status.idle":"2022-11-10T05:21:03.878757Z","shell.execute_reply.started":"2022-11-10T05:21:03.843372Z","shell.execute_reply":"2022-11-10T05:21:03.877236Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Optimal speed analysis","metadata":{}},{"cell_type":"code","source":"fig = px.line(df1a, x= \"trakus_index\", y=\"speed\",title='Mean optimal speed', markers=True)\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2022-11-10T05:21:03.887496Z","iopub.execute_input":"2022-11-10T05:21:03.887971Z","iopub.status.idle":"2022-11-10T05:21:03.956416Z","shell.execute_reply.started":"2022-11-10T05:21:03.887935Z","shell.execute_reply":"2022-11-10T05:21:03.955220Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Optimal angle analysis","metadata":{}},{"cell_type":"code","source":"fig = px.line(df1a, x= \"trakus_index\", y=\"turning_angle\",title='Mean optimal angle', markers=True)\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2022-11-10T05:21:03.958827Z","iopub.execute_input":"2022-11-10T05:21:03.959254Z","iopub.status.idle":"2022-11-10T05:21:04.019856Z","shell.execute_reply.started":"2022-11-10T05:21:03.959218Z","shell.execute_reply":"2022-11-10T05:21:04.018608Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# \"My Best Friend\" vs Mean Analysis ","metadata":{}},{"cell_type":"code","source":"dfa[\"Horse Name\"] = \"Mybestfriend\"\ndf1a[\"Horse Name\"] = \"Mean\"","metadata":{"execution":{"iopub.status.busy":"2022-11-10T05:21:04.021421Z","iopub.execute_input":"2022-11-10T05:21:04.021781Z","iopub.status.idle":"2022-11-10T05:21:04.031368Z","shell.execute_reply.started":"2022-11-10T05:21:04.021751Z","shell.execute_reply":"2022-11-10T05:21:04.030229Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df2 = pd.concat([df1a,dfa],axis=0)\nfig = px.line(df2, x= \"trakus_index\", y=\"speed\",color='Horse Name',title='', markers=True)\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2022-11-10T05:21:04.032854Z","iopub.execute_input":"2022-11-10T05:21:04.033337Z","iopub.status.idle":"2022-11-10T05:21:04.118544Z","shell.execute_reply.started":"2022-11-10T05:21:04.033300Z","shell.execute_reply":"2022-11-10T05:21:04.117274Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":" # Conclusion\nThe optimal speed to be maintained is 36 mph as the horse is on uneven keel at the \"BEND\". Whereas the rider Reylu Gutierrez unleashed a burst of speed up to 40 mph at bend which resulted in the injury of the animal and subsequently was not able to win. Hence always It is strictly to be followed not to exert the Horse at bends and maintain average speed not exceeding 37 mph and in the straight the Max speed can be unleashed when the Horse is flat and firm on four legs  ","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2022-11-10T05:21:23.183477Z","iopub.execute_input":"2022-11-10T05:21:23.183921Z","iopub.status.idle":"2022-11-10T05:21:23.190112Z","shell.execute_reply.started":"2022-11-10T05:21:23.183887Z","shell.execute_reply":"2022-11-10T05:21:23.188705Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Predictions","metadata":{}},{"cell_type":"markdown","source":"# Import libraries","metadata":{}},{"cell_type":"code","source":"import pandas as pd               \nimport numpy as np\nimport pickle\n\nfrom sklearn import preprocessing\nfrom sklearn import linear_model\nfrom sklearn.model_selection import train_test_split\nfrom sklearn import metrics\nfrom sklearn.metrics import confusion_matrix, classification_report, roc_auc_score, recall_score\nfrom sklearn.preprocessing import StandardScaler\nfrom sklearn.linear_model import LinearRegression\nfrom sklearn.preprocessing import MinMaxScaler\nfrom sklearn.linear_model import Ridge\nfrom sklearn.linear_model import RidgeCV\nfrom sklearn.metrics import confusion_matrix, classification_report, roc_auc_score, recall_score\nfrom sklearn.ensemble import RandomForestRegressor\nfrom sklearn.svm import SVR\nimport xgboost as xgb\nfrom xgboost import plot_importance, plot_tree\nfrom sklearn.metrics import mean_squared_error, mean_absolute_error\nfrom sklearn.model_selection import RandomizedSearchCV ,cross_val_score, KFold\nfrom sklearn.tree import DecisionTreeRegressor\nfrom sklearn.linear_model import Ridge\nfrom sklearn import linear_model\nfrom sklearn.linear_model import ElasticNet\nfrom sklearn.ensemble import AdaBoostRegressor\nfrom sklearn.neighbors import KNeighborsRegressor\n\nmodel_print={}\n\nimport seaborn as sns\nimport matplotlib.pyplot as plt\nimport plotly.express as px\n%matplotlib inline     \nsns.set(color_codes=True)\n\nimport warnings\nwarnings.filterwarnings('ignore')","metadata":{"execution":{"iopub.status.busy":"2022-11-10T05:26:27.564022Z","iopub.execute_input":"2022-11-10T05:26:27.564571Z","iopub.status.idle":"2022-11-10T05:26:28.700660Z","shell.execute_reply.started":"2022-11-10T05:26:27.564531Z","shell.execute_reply":"2022-11-10T05:26:28.699138Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Read data","metadata":{}},{"cell_type":"code","source":"df = pd.read_excel(\"/kaggle/input/derbypreds/AQU.xlsx\")\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2022-11-10T05:26:51.588409Z","iopub.execute_input":"2022-11-10T05:26:51.588952Z","iopub.status.idle":"2022-11-10T05:27:02.352716Z","shell.execute_reply.started":"2022-11-10T05:26:51.588910Z","shell.execute_reply":"2022-11-10T05:27:02.351164Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Process data","metadata":{}},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"markdown","source":"## Filter data","metadata":{}},{"cell_type":"code","source":"df = df[df['TYPE OF TRACK'] == \"dirt\"]\ndf.reset_index(inplace=True, drop=True)\ndf = df.sort_values(['DATE','RNO','SNO'],ascending=True)","metadata":{"execution":{"iopub.status.busy":"2022-11-10T05:27:34.827532Z","iopub.execute_input":"2022-11-10T05:27:34.827987Z","iopub.status.idle":"2022-11-10T05:27:34.858097Z","shell.execute_reply.started":"2022-11-10T05:27:34.827948Z","shell.execute_reply":"2022-11-10T05:27:34.856318Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Feature Engineering","metadata":{}},{"cell_type":"code","source":"def normalize_distance(inp):\n  try:\n    data, distance = inp\n    return data/distance\n  except:\n    return np.nan\n\ndef convert_to_seconds(time):\n  time = str(time)\n  try:\n    time = time.split(\":\")\n    return float(time[0]) * 60 + float(time[1])\n  except:\n    return float(time[0])","metadata":{"execution":{"iopub.status.busy":"2022-11-10T05:29:50.729863Z","iopub.execute_input":"2022-11-10T05:29:50.730371Z","iopub.status.idle":"2022-11-10T05:29:50.739072Z","shell.execute_reply.started":"2022-11-10T05:29:50.730333Z","shell.execute_reply":"2022-11-10T05:29:50.737130Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df[\"TIMES\"] = df[\"TIME\"].apply(convert_to_seconds)\ndf[\"TIMES1\"] = df[\"TIME1\"].apply(convert_to_seconds)\n\ndf[\"NTIMES\"] = df[[\"TIMES\", \"DISTANCE\"]].apply(normalize_distance, axis=1)\ndf[\"NTIMES1\"] = df[[\"TIMES1\", \"DISTANCE\"]].apply(normalize_distance, axis=1)\ndf[\"NTravel\"] = df[[\"Travel\", \"DISTANCE\"]].apply(normalize_distance, axis=1)\n\ndf['MEAN NTIMES'] = df.groupby('HORSE NAME')['NTIMES'].apply(lambda x: x.shift().expanding().mean())\ndf['MEAN NTIMES1'] = df.groupby('HORSE NAME')['NTIMES1'].apply(lambda x: x.shift().expanding().mean())\ndf['MEAN NTravel'] = df.groupby('HORSE NAME')['NTravel'].apply(lambda x: x.shift().expanding().mean())\n\ndf[\"SHIFT_NTIMES\"] = df.groupby('HORSE NAME')['NTIMES'].shift()\ndf[\"SHIFT_NTIMES1\"] = df.groupby('HORSE NAME')['NTIMES1'].shift()\ndf[\"SHIFT_NTravel\"] = df.groupby('HORSE NAME')['NTravel'].shift()","metadata":{"execution":{"iopub.status.busy":"2022-11-10T05:30:05.315345Z","iopub.execute_input":"2022-11-10T05:30:05.315869Z","iopub.status.idle":"2022-11-10T05:30:10.303380Z","shell.execute_reply.started":"2022-11-10T05:30:05.315830Z","shell.execute_reply":"2022-11-10T05:30:10.301950Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = df.drop_duplicates()\ndf_og = df\ndf = df[['WT','AGE', \"SHIFT_NTIMES\", \"MEAN NTIMES\", \"SHIFT_NTIMES1\", \"MEAN NTIMES1\", \"SHIFT_NTravel\", \"MEAN NTravel\", \"TIME IN SEC\", \"DISTANCE\"]]","metadata":{"execution":{"iopub.status.busy":"2022-11-10T05:30:19.307729Z","iopub.execute_input":"2022-11-10T05:30:19.308228Z","iopub.status.idle":"2022-11-10T05:30:19.376012Z","shell.execute_reply.started":"2022-11-10T05:30:19.308192Z","shell.execute_reply":"2022-11-10T05:30:19.375012Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Remove Outliers","metadata":{}},{"cell_type":"code","source":"fig = px.box(df, y=\"TIME IN SEC\")\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2022-11-10T05:30:50.817025Z","iopub.execute_input":"2022-11-10T05:30:50.817526Z","iopub.status.idle":"2022-11-10T05:30:50.914335Z","shell.execute_reply.started":"2022-11-10T05:30:50.817490Z","shell.execute_reply":"2022-11-10T05:30:50.912842Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def Remove_Outlier_Indices(df, feature):\n    Q1 = df[feature].quantile(0.25)\n    Q3 = df[feature].quantile(0.75)\n    IQR = Q3 - Q1\n    trueList = ~((df[feature] < (Q1 - 1.5 * IQR)) |(df[feature] > (Q3 + 1.5 * IQR)))\n    return df[trueList]\n\ndf = Remove_Outlier_Indices(df, 'TIME IN SEC')","metadata":{"execution":{"iopub.status.busy":"2022-11-10T05:30:59.294638Z","iopub.execute_input":"2022-11-10T05:30:59.295175Z","iopub.status.idle":"2022-11-10T05:30:59.311625Z","shell.execute_reply.started":"2022-11-10T05:30:59.295124Z","shell.execute_reply":"2022-11-10T05:30:59.309804Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Drop Null values","metadata":{}},{"cell_type":"code","source":"df.dropna(inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-11-10T05:31:16.651957Z","iopub.execute_input":"2022-11-10T05:31:16.652476Z","iopub.status.idle":"2022-11-10T05:31:16.667686Z","shell.execute_reply.started":"2022-11-10T05:31:16.652428Z","shell.execute_reply":"2022-11-10T05:31:16.666105Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Descriptive statistics of data","metadata":{}},{"cell_type":"code","source":"df[\"DISTANCE\"] = df[\"DISTANCE\"].astype(\"float64\")\ndf.info()","metadata":{"execution":{"iopub.status.busy":"2022-11-10T05:31:44.965752Z","iopub.execute_input":"2022-11-10T05:31:44.966187Z","iopub.status.idle":"2022-11-10T05:31:44.989194Z","shell.execute_reply.started":"2022-11-10T05:31:44.966154Z","shell.execute_reply":"2022-11-10T05:31:44.988102Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.describe()","metadata":{"execution":{"iopub.status.busy":"2022-11-10T05:31:56.688826Z","iopub.execute_input":"2022-11-10T05:31:56.689297Z","iopub.status.idle":"2022-11-10T05:31:56.738022Z","shell.execute_reply.started":"2022-11-10T05:31:56.689259Z","shell.execute_reply":"2022-11-10T05:31:56.736405Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df=df[df['WT']<200]\ndf = df.reset_index(drop=True)\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2022-11-10T05:32:16.714617Z","iopub.execute_input":"2022-11-10T05:32:16.715090Z","iopub.status.idle":"2022-11-10T05:32:16.738189Z","shell.execute_reply.started":"2022-11-10T05:32:16.715034Z","shell.execute_reply":"2022-11-10T05:32:16.736805Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Correlation exploration","metadata":{}},{"cell_type":"code","source":"sns.pairplot(df)","metadata":{"execution":{"iopub.status.busy":"2022-11-10T05:33:02.633957Z","iopub.execute_input":"2022-11-10T05:33:02.634418Z","iopub.status.idle":"2022-11-10T05:33:27.049632Z","shell.execute_reply.started":"2022-11-10T05:33:02.634381Z","shell.execute_reply":"2022-11-10T05:33:27.048143Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"corr = df.corr()\nsns.heatmap(corr, annot=True)","metadata":{"execution":{"iopub.status.busy":"2022-11-10T05:33:27.052129Z","iopub.execute_input":"2022-11-10T05:33:27.052547Z","iopub.status.idle":"2022-11-10T05:33:27.956337Z","shell.execute_reply.started":"2022-11-10T05:33:27.052513Z","shell.execute_reply":"2022-11-10T05:33:27.955111Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Split data for training","metadata":{}},{"cell_type":"code","source":"X = df.drop([\"TIME IN SEC\"],axis=1)\nX = df[['WT','AGE', \"SHIFT_NTIMES\", \"MEAN NTIMES\", \"SHIFT_NTIMES1\", \"MEAN NTIMES1\", \"SHIFT_NTravel\", \"MEAN NTravel\", \"DISTANCE\"]]\ny = df[[\"TIME IN SEC\"]]\n\nX_train, X_test, y_train, y_test = train_test_split(X,y,random_state=12345)\nX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)","metadata":{"execution":{"iopub.status.busy":"2022-11-10T05:33:27.958690Z","iopub.execute_input":"2022-11-10T05:33:27.959800Z","iopub.status.idle":"2022-11-10T05:33:27.975054Z","shell.execute_reply.started":"2022-11-10T05:33:27.959749Z","shell.execute_reply":"2022-11-10T05:33:27.974035Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Normalize data for training","metadata":{}},{"cell_type":"code","source":"scaler = MinMaxScaler()\nX_train_scaled = scaler.fit_transform(X_train)\nX_test_scaled = scaler.transform(X_test)\nX_train_scaled = pd.DataFrame(X_train_scaled, columns=X_train.columns)\nX_test_scaled = pd.DataFrame(X_test_scaled, columns=X_test.columns)","metadata":{"execution":{"iopub.status.busy":"2022-11-10T05:33:42.044173Z","iopub.execute_input":"2022-11-10T05:33:42.044592Z","iopub.status.idle":"2022-11-10T05:33:42.060601Z","shell.execute_reply.started":"2022-11-10T05:33:42.044558Z","shell.execute_reply":"2022-11-10T05:33:42.059536Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Build models","metadata":{}},{"cell_type":"code","source":"reg =linear_model.LinearRegression()\nreg.fit(X_train_scaled,y_train)\n\ny_pred=reg.predict(X_test_scaled)\n\nmodel_name='Linear_Regerrision'\n\nmabs=metrics.mean_absolute_error(y_test, y_pred)\nmse= metrics.mean_squared_error(y_test, y_pred)\nrmse_val=np.sqrt(metrics.mean_squared_error(y_test, y_pred))\n\naccur=reg.score(X_test_scaled, y_test)\nmodel_print[model_name]=[mabs,mse,rmse_val,accur]\n\nprint('Mean Absolute Error   :',mabs)  \nprint('Mean Squared Error    :',mse )  \nprint('Root Mean Squared     :',rmse_val )\nprint('R2 Score        :',accur)","metadata":{"execution":{"iopub.status.busy":"2022-11-10T05:33:58.812380Z","iopub.execute_input":"2022-11-10T05:33:58.812848Z","iopub.status.idle":"2022-11-10T05:33:58.873381Z","shell.execute_reply.started":"2022-11-10T05:33:58.812811Z","shell.execute_reply":"2022-11-10T05:33:58.871724Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"null_pred = [y_test.mean()] * len(y_test)\nnull_mse=metrics.mean_squared_error(null_pred,y_test)\nnull_mae=metrics.mean_absolute_error(y_test,null_pred)\nprint('Null mae: {}'.format(null_mae))\nprint('Null mse: {}'.format(null_mse))\nprint('Null rmse: {}'.format(np.sqrt(null_mse)))","metadata":{"execution":{"iopub.status.busy":"2022-11-10T05:34:05.774101Z","iopub.execute_input":"2022-11-10T05:34:05.774503Z","iopub.status.idle":"2022-11-10T05:34:05.801269Z","shell.execute_reply.started":"2022-11-10T05:34:05.774471Z","shell.execute_reply":"2022-11-10T05:34:05.799916Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ridgeReg = RidgeCV(alphas=[1e-3, 1e-2, 1e-1, 1])\nridgeReg.fit(X_train_scaled,y_train)\n\ny_pred = ridgeReg.predict(X_test_scaled)\n\nmodel_name='Ridge Classifier'\nmabs=metrics.mean_absolute_error(y_test, y_pred)\nmse= metrics.mean_squared_error(y_test, y_pred)\nrmse_val=np.sqrt(metrics.mean_squared_error(y_test, y_pred))\n\naccur=ridgeReg.score(X_test_scaled, y_test)\nmodel_print[model_name]=[mabs,mse,rmse_val,accur]\n\nprint('Mean Absolute Error    :',mabs)  \nprint('Mean Squared Error     :',mse)  \nprint('Root Mean Squared Error:',rmse_val)\nprint('R2 Score         :',accur)","metadata":{"execution":{"iopub.status.busy":"2022-11-10T05:34:12.149131Z","iopub.execute_input":"2022-11-10T05:34:12.149567Z","iopub.status.idle":"2022-11-10T05:34:12.221448Z","shell.execute_reply.started":"2022-11-10T05:34:12.149534Z","shell.execute_reply":"2022-11-10T05:34:12.219445Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = xgb.XGBRegressor()\nmodel.fit(X_train_scaled,y_train)\ny_pred = model.predict(X_test_scaled)\n\nmodel_name='xgboost'\nmabs=metrics.mean_absolute_error(y_test, y_pred)\nmse= metrics.mean_squared_error(y_test, y_pred)\nrmse_val=np.sqrt(metrics.mean_squared_error(y_test, y_pred))\naccur=model.score(X_test_scaled, y_test)\n\nmodel_print[model_name]=[mabs,mse,rmse_val,accur]\n\n\nprint('Mean Absolute Error    :',mabs)  \nprint('Mean Squared Error     :',mse)  \nprint('Root Mean Squared Error:',rmse_val)\nprint('R2 Score         :',accur)","metadata":{"execution":{"iopub.status.busy":"2022-11-10T05:34:22.480206Z","iopub.execute_input":"2022-11-10T05:34:22.480677Z","iopub.status.idle":"2022-11-10T05:34:25.138721Z","shell.execute_reply.started":"2022-11-10T05:34:22.480642Z","shell.execute_reply":"2022-11-10T05:34:25.137632Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rf=RandomForestRegressor(criterion='mse',)\nrf.fit(X_train_scaled,y_train)\ny_pred=rf.predict(X_test_scaled)\n\nfeatures = df.columns\nimportances = rf.feature_importances_\n\nmodel_name=\"RandomForestRegressor\"\nmabs=metrics.mean_absolute_error(y_test, y_pred)\nmse= metrics.mean_squared_error(y_test, y_pred)\nrmse=np.sqrt(metrics.mean_squared_error(y_test, y_pred))\n\naccur=rf.score(X_test_scaled, y_test)\nmodel_print[model_name]=[mabs,mse,rmse,accur]\n\nprint('Mean Absolute Error    :',mabs)  \nprint('Mean Squared Error     :',mse )  \nprint('Root Mean Squared Error:',rmse )\nprint('R2 Score         :',accur)\n","metadata":{"execution":{"iopub.status.busy":"2022-11-10T05:34:25.143713Z","iopub.execute_input":"2022-11-10T05:34:25.146653Z","iopub.status.idle":"2022-11-10T05:34:28.312482Z","shell.execute_reply.started":"2022-11-10T05:34:25.146593Z","shell.execute_reply":"2022-11-10T05:34:28.311294Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dt=DecisionTreeRegressor()\ndt.fit(X_train_scaled,y_train)\ny_pred=dt.predict(X_test_scaled)\n\n\nmodel_name=\"DecisionTreeRegressor\"\nmabs=metrics.mean_absolute_error(y_test, y_pred)\nmse= metrics.mean_squared_error(y_test, y_pred)\nrmse=np.sqrt(metrics.mean_squared_error(y_test, y_pred))\n\n\naccur=dt.score(X_test_scaled, y_test)\nmodel_print[model_name]=[mabs,mse,rmse,accur]\n\nprint('Mean Absolute Error    :',mabs)  \nprint('Mean Squared Error     :',mse )  \nprint('Root Mean Squared Error:',rmse )\nprint('R2 Score         :',accur)\n","metadata":{"execution":{"iopub.status.busy":"2022-11-10T05:34:32.409592Z","iopub.execute_input":"2022-11-10T05:34:32.410086Z","iopub.status.idle":"2022-11-10T05:34:32.483489Z","shell.execute_reply.started":"2022-11-10T05:34:32.410033Z","shell.execute_reply":"2022-11-10T05:34:32.481884Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lasso_reg = linear_model.Lasso(alpha=1)\nlasso_reg.fit(X_train_scaled , y_train)\n\ny_pred=lasso_reg.predict(X_test_scaled)\n\nmodel_name='lasso'\nmabs=metrics.mean_absolute_error(y_test, y_pred)\nmse= metrics.mean_squared_error(y_test, y_pred)\nrmse=np.sqrt(metrics.mean_squared_error(y_test, y_pred))\n\naccur=lasso_reg.score(X_test_scaled, y_test)\nmodel_print[model_name]=[mabs,mse,rmse,accur]\n\n\nprint('Mean Absolute Error    :',mabs)  \nprint('Mean Squared Error     :',mse )  \nprint('Root Mean Squared Error:',rmse )\nprint('R2 Score         :',accur)","metadata":{"execution":{"iopub.status.busy":"2022-11-10T05:34:39.462734Z","iopub.execute_input":"2022-11-10T05:34:39.463207Z","iopub.status.idle":"2022-11-10T05:34:39.522087Z","shell.execute_reply.started":"2022-11-10T05:34:39.463173Z","shell.execute_reply":"2022-11-10T05:34:39.520528Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"elasticnet_reg = ElasticNet(alpha=1)\nelasticnet_reg.fit(X_train_scaled , y_train)\ncross_validation_accuracy = cross_val_score(estimator = elasticnet_reg, X = X_train_scaled, y = y_train, cv = 5,verbose = 1)\ny_pred = elasticnet_reg.predict(X_test_scaled)\n\nmodel_name='ElasticNet'\nmabs=metrics.mean_absolute_error(y_test, y_pred)\nmse= metrics.mean_squared_error(y_test, y_pred)\nrmse=np.sqrt(metrics.mean_squared_error(y_test, y_pred))\n\naccur=elasticnet_reg.score(X_test_scaled, y_test)\nmodel_print[model_name]=[mabs,mse,rmse,accur]\n\n\nprint('Mean Absolute Error    :',mabs)  \nprint('Mean Squared Error     :',mse )  \nprint('Root Mean Squared Error:',rmse )\nprint('R2 Score         :',accur)","metadata":{"execution":{"iopub.status.busy":"2022-11-10T05:34:45.978200Z","iopub.execute_input":"2022-11-10T05:34:45.978647Z","iopub.status.idle":"2022-11-10T05:34:46.112916Z","shell.execute_reply.started":"2022-11-10T05:34:45.978611Z","shell.execute_reply":"2022-11-10T05:34:46.111506Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ada_reg = AdaBoostRegressor(n_estimators=500)\nada_reg.fit(X_train_scaled , y_train)\ncross_validation_accuracy = cross_val_score(estimator = ada_reg, X = X_train_scaled, y = y_train, cv = 5,verbose = 1)\ny_pred = ada_reg.predict(X_test_scaled)\n\nmodel_name='AdaBoost'\n\naccur=ada_reg.score(X_test_scaled, y_test)\nmabs=metrics.mean_absolute_error(y_test, y_pred)\nmse= metrics.mean_squared_error(y_test, y_pred)\nrmse=np.sqrt(metrics.mean_squared_error(y_test, y_pred))\n\nmodel_print[model_name]=[mabs,mse,rmse,accur]\n\n\nprint('Mean Absolute Error    :',mabs)  \nprint('Mean Squared Error     :',mse )  \nprint('Root Mean Squared Error:',rmse )\nprint('R2 Score         :',accur)\n","metadata":{"execution":{"iopub.status.busy":"2022-11-10T05:34:53.678382Z","iopub.execute_input":"2022-11-10T05:34:53.678779Z","iopub.status.idle":"2022-11-10T05:35:02.251032Z","shell.execute_reply.started":"2022-11-10T05:34:53.678739Z","shell.execute_reply":"2022-11-10T05:35:02.249856Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"knn_reg = KNeighborsRegressor(n_neighbors=1)\nknn_reg.fit(X_train_scaled , y_train)\ncross_validation_accuracy = cross_val_score(estimator = knn_reg, X = X_train_scaled, y = y_train, cv = 5,verbose = 1)\ny_pred = knn_reg.predict(X_test_scaled)\n\nmodel_name='KNN'\nmabs=metrics.mean_absolute_error(y_test, y_pred)\nmse= metrics.mean_squared_error(y_test, y_pred)\nrmse=np.sqrt(metrics.mean_squared_error(y_test, y_pred))\naccur=knn_reg.score(X_test_scaled, y_test)\nmodel_print[model_name]=[mabs,mse,rmse,accur]\n\n\nprint('Mean Absolute Error    :',mabs)  \nprint('Mean Squared Error     :',mse )  \nprint('Root Mean Squared Error:',rmse )\nprint('R2 Score         :',accur)","metadata":{"execution":{"iopub.status.busy":"2022-11-10T05:35:02.252938Z","iopub.execute_input":"2022-11-10T05:35:02.253332Z","iopub.status.idle":"2022-11-10T05:35:02.459782Z","shell.execute_reply.started":"2022-11-10T05:35:02.253298Z","shell.execute_reply":"2022-11-10T05:35:02.458448Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Compare models","metadata":{}},{"cell_type":"code","source":"df_new = pd.DataFrame(list(model_print.values()),columns = ['Mean Absolute Err','Mean Square Err','RMSE','R2 Score'],index=model_print.keys()) \ndf_new.reset_index(level=0, drop=False,inplace=True)\ndf_new.rename(columns={'index':'Algo_Name'},inplace=True)\ndf_new.sort_values(by=['RMSE', 'R2 Score'],ascending=True)","metadata":{"execution":{"iopub.status.busy":"2022-11-10T05:35:21.674830Z","iopub.execute_input":"2022-11-10T05:35:21.675301Z","iopub.status.idle":"2022-11-10T05:35:21.694760Z","shell.execute_reply.started":"2022-11-10T05:35:21.675264Z","shell.execute_reply":"2022-11-10T05:35:21.693858Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Ridge Classifier has turned out to be the best model based on R2 score hence we proceed with this model for further predictions","metadata":{}},{"cell_type":"markdown","source":"## Test model on real race data","metadata":{}},{"cell_type":"code","source":"def process_testdf(df, date):\n  df = df[df['TYPE OF TRACK'] == \"dirt\"]\n  df.reset_index(inplace=True, drop=True)\n  \n  df[\"TIMES\"] = df[\"TIME\"].apply(convert_to_seconds)\n  df[\"TIMES1\"] = df[\"TIME1\"].apply(convert_to_seconds)\n\n  df[\"NTIMES\"] = df[[\"TIMES\", \"DISTANCE\"]].apply(normalize_distance, axis=1)\n  df[\"NTIMES1\"] = df[[\"TIMES1\", \"DISTANCE\"]].apply(normalize_distance, axis=1)\n  df[\"NTravel\"] = df[[\"Travel\", \"DISTANCE\"]].apply(normalize_distance, axis=1)\n\n  df['MEAN NTIMES'] = df.groupby('HORSE NAME')['NTIMES'].apply(lambda x: x.shift().expanding().mean())\n  df['MEAN NTIMES1'] = df.groupby('HORSE NAME')['NTIMES1'].apply(lambda x: x.shift().expanding().mean())\n  df['MEAN NTravel'] = df.groupby('HORSE NAME')['NTravel'].apply(lambda x: x.shift().expanding().mean())\n\n  df[\"SHIFT_NTIMES\"] = df.groupby('HORSE NAME')['NTIMES'].shift()\n  df[\"SHIFT_NTIMES1\"] = df.groupby('HORSE NAME')['NTIMES1'].shift()\n  df[\"SHIFT_NTravel\"] = df.groupby('HORSE NAME')['NTravel'].shift()\n\n  df = df[df[\"DATE\"] == date]\n\n  return df\n\ndef normalize_testdf(df):\n  X_test = df[['WT','AGE', \"SHIFT_NTIMES\", \"MEAN NTIMES\", \"SHIFT_NTIMES1\", \"MEAN NTIMES1\", \"SHIFT_NTravel\", \"MEAN NTravel\", \"DISTANCE\"]]\n  X_test = scaler.transform(X_test)\n  return X_test","metadata":{"execution":{"iopub.status.busy":"2022-11-10T05:35:51.249760Z","iopub.execute_input":"2022-11-10T05:35:51.250291Z","iopub.status.idle":"2022-11-10T05:35:51.263544Z","shell.execute_reply.started":"2022-11-10T05:35:51.250256Z","shell.execute_reply":"2022-11-10T05:35:51.262260Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df = pd.read_excel(\"/kaggle/input/derbypreds/TEST_RACE.xlsx\")","metadata":{"execution":{"iopub.status.busy":"2022-11-10T05:36:00.419140Z","iopub.execute_input":"2022-11-10T05:36:00.419662Z","iopub.status.idle":"2022-11-10T05:36:00.566997Z","shell.execute_reply.started":"2022-11-10T05:36:00.419624Z","shell.execute_reply":"2022-11-10T05:36:00.565652Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ptest_df = process_testdf(test_df, \"2022/10OCT/15\")\n\ntest_points = normalize_testdf(ptest_df)\n\npreds = ridgeReg.predict(test_points).flatten()\n\nptest_df[\"TIME_PRED\"] = preds\nptest_df.sort_values([\"TIME_PRED\"])[[\"SNO\", \"HORSE NAME\", \"TIME_PRED\"]]","metadata":{"execution":{"iopub.status.busy":"2022-11-10T05:36:06.350839Z","iopub.execute_input":"2022-11-10T05:36:06.352322Z","iopub.status.idle":"2022-11-10T05:36:06.412526Z","shell.execute_reply.started":"2022-11-10T05:36:06.352265Z","shell.execute_reply":"2022-11-10T05:36:06.410909Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Read data","metadata":{}},{"cell_type":"code","source":"df = pd.read_excel(\"/kaggle/input/derbypreds/BEL.xlsx\")\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2022-11-10T05:42:09.759013Z","iopub.execute_input":"2022-11-10T05:42:09.759585Z","iopub.status.idle":"2022-11-10T05:42:21.018110Z","shell.execute_reply.started":"2022-11-10T05:42:09.759538Z","shell.execute_reply":"2022-11-10T05:42:21.016901Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Process data","metadata":{}},{"cell_type":"markdown","source":"## Filter data","metadata":{}},{"cell_type":"code","source":"df = df[df['TYPE OF TRACK'] == \"dirt\"]\ndf.reset_index(inplace=True, drop=True)\ndf = df.sort_values(['DATE','RNO','SNO'],ascending=True)","metadata":{"execution":{"iopub.status.busy":"2022-11-10T05:42:21.020477Z","iopub.execute_input":"2022-11-10T05:42:21.021494Z","iopub.status.idle":"2022-11-10T05:42:21.041492Z","shell.execute_reply.started":"2022-11-10T05:42:21.021450Z","shell.execute_reply":"2022-11-10T05:42:21.040486Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Feature Engineering","metadata":{}},{"cell_type":"code","source":"def normalize_distance(inp):\n  try:\n    data, distance = inp\n    return data/distance\n  except:\n    return np.nan\n\ndef convert_to_seconds(time):\n  time = str(time)\n  try:\n    time = time.split(\":\")\n    return float(time[0]) * 60 + float(time[1])\n  except:\n    return float(time[0])","metadata":{"execution":{"iopub.status.busy":"2022-11-10T05:42:21.042924Z","iopub.execute_input":"2022-11-10T05:42:21.043846Z","iopub.status.idle":"2022-11-10T05:42:21.049898Z","shell.execute_reply.started":"2022-11-10T05:42:21.043808Z","shell.execute_reply":"2022-11-10T05:42:21.049016Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df[\"TIMES\"] = df[\"TIME\"].apply(convert_to_seconds)\ndf[\"TIMES1\"] = df[\"TIME1\"].apply(convert_to_seconds)\n\ndf[\"NTIMES\"] = df[[\"TIMES\", \"DISTANCE\"]].apply(normalize_distance, axis=1)\ndf[\"NTIMES1\"] = df[[\"TIMES1\", \"DISTANCE\"]].apply(normalize_distance, axis=1)\ndf[\"NTravel\"] = df[[\"Travel\", \"DISTANCE\"]].apply(normalize_distance, axis=1)\n\ndf['MEAN NTIMES'] = df.groupby('HORSE NAME')['NTIMES'].apply(lambda x: x.shift().expanding().mean())\ndf['MEAN NTIMES1'] = df.groupby('HORSE NAME')['NTIMES1'].apply(lambda x: x.shift().expanding().mean())\ndf['MEAN NTravel'] = df.groupby('HORSE NAME')['NTravel'].apply(lambda x: x.shift().expanding().mean())\n\ndf[\"SHIFT_NTIMES\"] = df.groupby('HORSE NAME')['NTIMES'].shift()\ndf[\"SHIFT_NTIMES1\"] = df.groupby('HORSE NAME')['NTIMES1'].shift()\ndf[\"SHIFT_NTravel\"] = df.groupby('HORSE NAME')['NTravel'].shift()","metadata":{"execution":{"iopub.status.busy":"2022-11-10T05:42:21.052500Z","iopub.execute_input":"2022-11-10T05:42:21.052969Z","iopub.status.idle":"2022-11-10T05:42:24.936421Z","shell.execute_reply.started":"2022-11-10T05:42:21.052932Z","shell.execute_reply":"2022-11-10T05:42:24.935125Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = df.drop_duplicates()\ndf_og = df\ndf = df[['WT','AGE', \"SHIFT_NTIMES\", \"MEAN NTIMES\", \"SHIFT_NTIMES1\", \"MEAN NTIMES1\", \"SHIFT_NTravel\", \"MEAN NTravel\", \"TIME IN SEC\", \"DISTANCE\"]]","metadata":{"execution":{"iopub.status.busy":"2022-11-10T05:42:24.938269Z","iopub.execute_input":"2022-11-10T05:42:24.939415Z","iopub.status.idle":"2022-11-10T05:42:24.984482Z","shell.execute_reply.started":"2022-11-10T05:42:24.939363Z","shell.execute_reply":"2022-11-10T05:42:24.983126Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Remove Outliers","metadata":{}},{"cell_type":"code","source":"fig = px.box(df, y=\"TIME IN SEC\")\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2022-11-10T05:42:24.986356Z","iopub.execute_input":"2022-11-10T05:42:24.986870Z","iopub.status.idle":"2022-11-10T05:42:25.048648Z","shell.execute_reply.started":"2022-11-10T05:42:24.986821Z","shell.execute_reply":"2022-11-10T05:42:25.047392Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def Remove_Outlier_Indices(df, feature):\n    Q1 = df[feature].quantile(0.25)\n    Q3 = df[feature].quantile(0.75)\n    IQR = Q3 - Q1\n    trueList = ~((df[feature] < (Q1 - 1.5 * IQR)) |(df[feature] > (Q3 + 1.5 * IQR)))\n    return df[trueList]\n\ndf = Remove_Outlier_Indices(df, 'TIME IN SEC')","metadata":{"execution":{"iopub.status.busy":"2022-11-10T05:42:25.050421Z","iopub.execute_input":"2022-11-10T05:42:25.051201Z","iopub.status.idle":"2022-11-10T05:42:25.064467Z","shell.execute_reply.started":"2022-11-10T05:42:25.051153Z","shell.execute_reply":"2022-11-10T05:42:25.062830Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Drop Null values","metadata":{}},{"cell_type":"code","source":"df.dropna(inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-11-10T05:42:25.066616Z","iopub.execute_input":"2022-11-10T05:42:25.067160Z","iopub.status.idle":"2022-11-10T05:42:25.078801Z","shell.execute_reply.started":"2022-11-10T05:42:25.067111Z","shell.execute_reply":"2022-11-10T05:42:25.077520Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Descriptive statistics of data","metadata":{}},{"cell_type":"code","source":"df[\"DISTANCE\"] = df[\"DISTANCE\"].astype(\"float64\")\ndf.info()","metadata":{"execution":{"iopub.status.busy":"2022-11-10T05:42:25.080689Z","iopub.execute_input":"2022-11-10T05:42:25.081560Z","iopub.status.idle":"2022-11-10T05:42:25.099754Z","shell.execute_reply.started":"2022-11-10T05:42:25.081515Z","shell.execute_reply":"2022-11-10T05:42:25.098113Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.describe()","metadata":{"execution":{"iopub.status.busy":"2022-11-10T05:42:25.102734Z","iopub.execute_input":"2022-11-10T05:42:25.103135Z","iopub.status.idle":"2022-11-10T05:42:25.150374Z","shell.execute_reply.started":"2022-11-10T05:42:25.103102Z","shell.execute_reply":"2022-11-10T05:42:25.149132Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df=df[df['WT']<200]\ndf = df.reset_index(drop=True)\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2022-11-10T05:42:25.151807Z","iopub.execute_input":"2022-11-10T05:42:25.153360Z","iopub.status.idle":"2022-11-10T05:42:25.173936Z","shell.execute_reply.started":"2022-11-10T05:42:25.153296Z","shell.execute_reply":"2022-11-10T05:42:25.172148Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Correlation exploration","metadata":{}},{"cell_type":"code","source":"sns.pairplot(df)","metadata":{"execution":{"iopub.status.busy":"2022-11-10T05:42:25.176420Z","iopub.execute_input":"2022-11-10T05:42:25.176938Z","iopub.status.idle":"2022-11-10T05:42:51.955262Z","shell.execute_reply.started":"2022-11-10T05:42:25.176897Z","shell.execute_reply":"2022-11-10T05:42:51.953809Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"corr = df.corr()\nsns.heatmap(corr, annot=True)","metadata":{"execution":{"iopub.status.busy":"2022-11-10T05:42:51.956583Z","iopub.execute_input":"2022-11-10T05:42:51.956935Z","iopub.status.idle":"2022-11-10T05:42:52.873113Z","shell.execute_reply.started":"2022-11-10T05:42:51.956902Z","shell.execute_reply":"2022-11-10T05:42:52.871910Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Split data for training","metadata":{}},{"cell_type":"code","source":"X = df.drop([\"TIME IN SEC\"],axis=1)\nX = df[['WT','AGE', \"SHIFT_NTIMES\", \"MEAN NTIMES\", \"SHIFT_NTIMES1\", \"MEAN NTIMES1\", \"SHIFT_NTravel\", \"MEAN NTravel\", \"DISTANCE\"]]\ny = df[[\"TIME IN SEC\"]]\n\nX_train, X_test, y_train, y_test = train_test_split(X,y,random_state=12345)\nX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)","metadata":{"execution":{"iopub.status.busy":"2022-11-10T05:42:52.874764Z","iopub.execute_input":"2022-11-10T05:42:52.875152Z","iopub.status.idle":"2022-11-10T05:42:52.890369Z","shell.execute_reply.started":"2022-11-10T05:42:52.875119Z","shell.execute_reply":"2022-11-10T05:42:52.888941Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Normalize data for training","metadata":{}},{"cell_type":"code","source":"scaler = MinMaxScaler()\nX_train_scaled = scaler.fit_transform(X_train)\nX_test_scaled = scaler.transform(X_test)\nX_train_scaled = pd.DataFrame(X_train_scaled, columns=X_train.columns)\nX_test_scaled = pd.DataFrame(X_test_scaled, columns=X_test.columns)","metadata":{"execution":{"iopub.status.busy":"2022-11-10T05:42:52.895846Z","iopub.execute_input":"2022-11-10T05:42:52.896238Z","iopub.status.idle":"2022-11-10T05:42:52.910324Z","shell.execute_reply.started":"2022-11-10T05:42:52.896205Z","shell.execute_reply":"2022-11-10T05:42:52.909113Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Build models","metadata":{}},{"cell_type":"code","source":"reg =linear_model.LinearRegression()\nreg.fit(X_train_scaled,y_train)\n\ny_pred=reg.predict(X_test_scaled)\n\nmodel_name='Linear_Regerrision'\n\nmabs=metrics.mean_absolute_error(y_test, y_pred)\nmse= metrics.mean_squared_error(y_test, y_pred)\nrmse_val=np.sqrt(metrics.mean_squared_error(y_test, y_pred))\n\naccur=reg.score(X_test_scaled, y_test)\nmodel_print[model_name]=[mabs,mse,rmse_val,accur]\n\nprint('Mean Absolute Error   :',mabs)  \nprint('Mean Squared Error    :',mse )  \nprint('Root Mean Squared     :',rmse_val )\nprint('R2 Score        :',accur)","metadata":{"execution":{"iopub.status.busy":"2022-11-10T05:42:52.911756Z","iopub.execute_input":"2022-11-10T05:42:52.912129Z","iopub.status.idle":"2022-11-10T05:42:52.942159Z","shell.execute_reply.started":"2022-11-10T05:42:52.912084Z","shell.execute_reply":"2022-11-10T05:42:52.941283Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"null_pred = [y_test.mean()] * len(y_test)\nnull_mse=metrics.mean_squared_error(null_pred,y_test)\nnull_mae=metrics.mean_absolute_error(y_test,null_pred)\nprint('Null mae: {}'.format(null_mae))\nprint('Null mse: {}'.format(null_mse))\nprint('Null rmse: {}'.format(np.sqrt(null_mse)))","metadata":{"execution":{"iopub.status.busy":"2022-11-10T05:42:52.943137Z","iopub.execute_input":"2022-11-10T05:42:52.943477Z","iopub.status.idle":"2022-11-10T05:42:52.966945Z","shell.execute_reply.started":"2022-11-10T05:42:52.943445Z","shell.execute_reply":"2022-11-10T05:42:52.965397Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ridgeReg = RidgeCV(alphas=[1e-3, 1e-2, 1e-1, 1])\nridgeReg.fit(X_train_scaled,y_train)\n\ny_pred = ridgeReg.predict(X_test_scaled)\n\nmodel_name='Ridge Classifier'\nmabs=metrics.mean_absolute_error(y_test, y_pred)\nmse= metrics.mean_squared_error(y_test, y_pred)\nrmse_val=np.sqrt(metrics.mean_squared_error(y_test, y_pred))\n\naccur=ridgeReg.score(X_test_scaled, y_test)\nmodel_print[model_name]=[mabs,mse,rmse_val,accur]\n\nprint('Mean Absolute Error    :',mabs)  \nprint('Mean Squared Error     :',mse)  \nprint('Root Mean Squared Error:',rmse_val)\nprint('R2 Score         :',accur)","metadata":{"execution":{"iopub.status.busy":"2022-11-10T05:42:52.969725Z","iopub.execute_input":"2022-11-10T05:42:52.970970Z","iopub.status.idle":"2022-11-10T05:42:53.013833Z","shell.execute_reply.started":"2022-11-10T05:42:52.970904Z","shell.execute_reply":"2022-11-10T05:42:53.012185Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = xgb.XGBRegressor()\nmodel.fit(X_train_scaled,y_train)\ny_pred = model.predict(X_test_scaled)\n\nmodel_name='xgboost'\nmabs=metrics.mean_absolute_error(y_test, y_pred)\nmse= metrics.mean_squared_error(y_test, y_pred)\nrmse_val=np.sqrt(metrics.mean_squared_error(y_test, y_pred))\naccur=model.score(X_test_scaled, y_test)\n\nmodel_print[model_name]=[mabs,mse,rmse_val,accur]\n\n\nprint('Mean Absolute Error    :',mabs)  \nprint('Mean Squared Error     :',mse)  \nprint('Root Mean Squared Error:',rmse_val)\nprint('R2 Score         :',accur)","metadata":{"execution":{"iopub.status.busy":"2022-11-10T05:42:53.016727Z","iopub.execute_input":"2022-11-10T05:42:53.017920Z","iopub.status.idle":"2022-11-10T05:42:53.594731Z","shell.execute_reply.started":"2022-11-10T05:42:53.017851Z","shell.execute_reply":"2022-11-10T05:42:53.593812Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rf=RandomForestRegressor(criterion='mse',)\nrf.fit(X_train_scaled,y_train)\ny_pred=rf.predict(X_test_scaled)\n\nfeatures = df.columns\nimportances = rf.feature_importances_\n\nmodel_name=\"RandomForestRegressor\"\nmabs=metrics.mean_absolute_error(y_test, y_pred)\nmse= metrics.mean_squared_error(y_test, y_pred)\nrmse=np.sqrt(metrics.mean_squared_error(y_test, y_pred))\n\naccur=rf.score(X_test_scaled, y_test)\nmodel_print[model_name]=[mabs,mse,rmse,accur]\n\nprint('Mean Absolute Error    :',mabs)  \nprint('Mean Squared Error     :',mse )  \nprint('Root Mean Squared Error:',rmse )\nprint('R2 Score         :',accur)\n","metadata":{"execution":{"iopub.status.busy":"2022-11-10T05:42:53.596561Z","iopub.execute_input":"2022-11-10T05:42:53.597367Z","iopub.status.idle":"2022-11-10T05:42:54.777701Z","shell.execute_reply.started":"2022-11-10T05:42:53.597327Z","shell.execute_reply":"2022-11-10T05:42:54.776458Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dt=DecisionTreeRegressor()\ndt.fit(X_train_scaled,y_train)\ny_pred=dt.predict(X_test_scaled)\n\n\nmodel_name=\"DecisionTreeRegressor\"\nmabs=metrics.mean_absolute_error(y_test, y_pred)\nmse= metrics.mean_squared_error(y_test, y_pred)\nrmse=np.sqrt(metrics.mean_squared_error(y_test, y_pred))\n\n\naccur=dt.score(X_test_scaled, y_test)\nmodel_print[model_name]=[mabs,mse,rmse,accur]\n\nprint('Mean Absolute Error    :',mabs)  \nprint('Mean Squared Error     :',mse )  \nprint('Root Mean Squared Error:',rmse )\nprint('R2 Score         :',accur)\n","metadata":{"execution":{"iopub.status.busy":"2022-11-10T05:42:54.779274Z","iopub.execute_input":"2022-11-10T05:42:54.779732Z","iopub.status.idle":"2022-11-10T05:42:54.818882Z","shell.execute_reply.started":"2022-11-10T05:42:54.779688Z","shell.execute_reply":"2022-11-10T05:42:54.817744Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lasso_reg = linear_model.Lasso(alpha=1)\nlasso_reg.fit(X_train_scaled , y_train)\n\ny_pred=lasso_reg.predict(X_test_scaled)\n\nmodel_name='lasso'\nmabs=metrics.mean_absolute_error(y_test, y_pred)\nmse= metrics.mean_squared_error(y_test, y_pred)\nrmse=np.sqrt(metrics.mean_squared_error(y_test, y_pred))\n\naccur=lasso_reg.score(X_test_scaled, y_test)\nmodel_print[model_name]=[mabs,mse,rmse,accur]\n\n\nprint('Mean Absolute Error    :',mabs)  \nprint('Mean Squared Error     :',mse )  \nprint('Root Mean Squared Error:',rmse )\nprint('R2 Score         :',accur)","metadata":{"execution":{"iopub.status.busy":"2022-11-10T05:42:54.820707Z","iopub.execute_input":"2022-11-10T05:42:54.821031Z","iopub.status.idle":"2022-11-10T05:42:54.855410Z","shell.execute_reply.started":"2022-11-10T05:42:54.821001Z","shell.execute_reply":"2022-11-10T05:42:54.853613Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"elasticnet_reg = ElasticNet(alpha=1)\nelasticnet_reg.fit(X_train_scaled , y_train)\ncross_validation_accuracy = cross_val_score(estimator = elasticnet_reg, X = X_train_scaled, y = y_train, cv = 5,verbose = 1)\ny_pred = elasticnet_reg.predict(X_test_scaled)\n\nmodel_name='ElasticNet'\nmabs=metrics.mean_absolute_error(y_test, y_pred)\nmse= metrics.mean_squared_error(y_test, y_pred)\nrmse=np.sqrt(metrics.mean_squared_error(y_test, y_pred))\n\naccur=elasticnet_reg.score(X_test_scaled, y_test)\nmodel_print[model_name]=[mabs,mse,rmse,accur]\n\n\nprint('Mean Absolute Error    :',mabs)  \nprint('Mean Squared Error     :',mse )  \nprint('Root Mean Squared Error:',rmse )\nprint('R2 Score         :',accur)","metadata":{"execution":{"iopub.status.busy":"2022-11-10T05:42:54.857149Z","iopub.execute_input":"2022-11-10T05:42:54.858128Z","iopub.status.idle":"2022-11-10T05:42:54.971453Z","shell.execute_reply.started":"2022-11-10T05:42:54.858080Z","shell.execute_reply":"2022-11-10T05:42:54.970276Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ada_reg = AdaBoostRegressor(n_estimators=500)\nada_reg.fit(X_train_scaled , y_train)\ncross_validation_accuracy = cross_val_score(estimator = ada_reg, X = X_train_scaled, y = y_train, cv = 5,verbose = 1)\ny_pred = ada_reg.predict(X_test_scaled)\n\nmodel_name='AdaBoost'\n\naccur=ada_reg.score(X_test_scaled, y_test)\nmabs=metrics.mean_absolute_error(y_test, y_pred)\nmse= metrics.mean_squared_error(y_test, y_pred)\nrmse=np.sqrt(metrics.mean_squared_error(y_test, y_pred))\n\nmodel_print[model_name]=[mabs,mse,rmse,accur]\n\n\nprint('Mean Absolute Error    :',mabs)  \nprint('Mean Squared Error     :',mse )  \nprint('Root Mean Squared Error:',rmse )\nprint('R2 Score         :',accur)\n","metadata":{"execution":{"iopub.status.busy":"2022-11-10T05:42:54.973437Z","iopub.execute_input":"2022-11-10T05:42:54.974348Z","iopub.status.idle":"2022-11-10T05:42:55.539322Z","shell.execute_reply.started":"2022-11-10T05:42:54.974300Z","shell.execute_reply":"2022-11-10T05:42:55.537933Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"knn_reg = KNeighborsRegressor(n_neighbors=1)\nknn_reg.fit(X_train_scaled , y_train)\ncross_validation_accuracy = cross_val_score(estimator = knn_reg, X = X_train_scaled, y = y_train, cv = 5,verbose = 1)\ny_pred = knn_reg.predict(X_test_scaled)\n\nmodel_name='KNN'\nmabs=metrics.mean_absolute_error(y_test, y_pred)\nmse= metrics.mean_squared_error(y_test, y_pred)\nrmse=np.sqrt(metrics.mean_squared_error(y_test, y_pred))\naccur=knn_reg.score(X_test_scaled, y_test)\nmodel_print[model_name]=[mabs,mse,rmse,accur]\n\n\nprint('Mean Absolute Error    :',mabs)  \nprint('Mean Squared Error     :',mse )  \nprint('Root Mean Squared Error:',rmse )\nprint('R2 Score         :',accur)","metadata":{"execution":{"iopub.status.busy":"2022-11-10T05:42:55.541334Z","iopub.execute_input":"2022-11-10T05:42:55.541807Z","iopub.status.idle":"2022-11-10T05:42:55.650067Z","shell.execute_reply.started":"2022-11-10T05:42:55.541760Z","shell.execute_reply":"2022-11-10T05:42:55.648794Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Compare models","metadata":{}},{"cell_type":"code","source":"df_new = pd.DataFrame(list(model_print.values()),columns = ['Mean Absolute Err','Mean Square Err','RMSE','R2 Score'],index=model_print.keys()) \ndf_new.reset_index(level=0, drop=False,inplace=True)\ndf_new.rename(columns={'index':'Algo_Name'},inplace=True)\ndf_new.sort_values(by=['RMSE', 'R2 Score'],ascending=True)","metadata":{"execution":{"iopub.status.busy":"2022-11-10T05:42:55.652252Z","iopub.execute_input":"2022-11-10T05:42:55.652735Z","iopub.status.idle":"2022-11-10T05:42:55.675398Z","shell.execute_reply.started":"2022-11-10T05:42:55.652687Z","shell.execute_reply":"2022-11-10T05:42:55.673792Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"RandomForestRegressor has turned out to be the best model based on R2 score hence we proceed with this model for further predictions","metadata":{}},{"cell_type":"markdown","source":"## Test model on real race data","metadata":{}},{"cell_type":"code","source":"def process_testdf(df, date):\n  df = df[df['TYPE OF TRACK'] == \"dirt\"]\n  df.reset_index(inplace=True, drop=True)\n  \n  df[\"TIMES\"] = df[\"TIME\"].apply(convert_to_seconds)\n  df[\"TIMES1\"] = df[\"TIME1\"].apply(convert_to_seconds)\n\n  df[\"NTIMES\"] = df[[\"TIMES\", \"DISTANCE\"]].apply(normalize_distance, axis=1)\n  df[\"NTIMES1\"] = df[[\"TIMES1\", \"DISTANCE\"]].apply(normalize_distance, axis=1)\n  df[\"NTravel\"] = df[[\"Travel\", \"DISTANCE\"]].apply(normalize_distance, axis=1)\n\n  df['MEAN NTIMES'] = df.groupby('HORSE NAME')['NTIMES'].apply(lambda x: x.shift().expanding().mean())\n  df['MEAN NTIMES1'] = df.groupby('HORSE NAME')['NTIMES1'].apply(lambda x: x.shift().expanding().mean())\n  df['MEAN NTravel'] = df.groupby('HORSE NAME')['NTravel'].apply(lambda x: x.shift().expanding().mean())\n\n  df[\"SHIFT_NTIMES\"] = df.groupby('HORSE NAME')['NTIMES'].shift()\n  df[\"SHIFT_NTIMES1\"] = df.groupby('HORSE NAME')['NTIMES1'].shift()\n  df[\"SHIFT_NTravel\"] = df.groupby('HORSE NAME')['NTravel'].shift()\n\n  df = df[df[\"DATE\"] == date]\n\n  return df\n\ndef normalize_testdf(df):\n  X_test = df[['WT','AGE', \"SHIFT_NTIMES\", \"MEAN NTIMES\", \"SHIFT_NTIMES1\", \"MEAN NTIMES1\", \"SHIFT_NTravel\", \"MEAN NTravel\", \"DISTANCE\"]]\n  X_test = scaler.transform(X_test)\n  return X_test","metadata":{"execution":{"iopub.status.busy":"2022-11-10T05:43:56.836574Z","iopub.execute_input":"2022-11-10T05:43:56.837089Z","iopub.status.idle":"2022-11-10T05:43:56.850588Z","shell.execute_reply.started":"2022-11-10T05:43:56.837052Z","shell.execute_reply":"2022-11-10T05:43:56.849447Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df = pd.read_excel(\"/kaggle/input/derbypreds/TEST_RACE.xlsx\")","metadata":{"execution":{"iopub.status.busy":"2022-11-10T05:43:57.449311Z","iopub.execute_input":"2022-11-10T05:43:57.449939Z","iopub.status.idle":"2022-11-10T05:43:57.587250Z","shell.execute_reply.started":"2022-11-10T05:43:57.449905Z","shell.execute_reply":"2022-11-10T05:43:57.586405Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ptest_df = process_testdf(test_df, \"2022/10OCT/15\")\n\ntest_points = normalize_testdf(ptest_df)\n\npreds = rf.predict(test_points).flatten()\n\nptest_df[\"TIME_PRED\"] = preds\nptest_df.sort_values([\"TIME_PRED\"])[[\"SNO\", \"HORSE NAME\", \"TIME_PRED\"]]","metadata":{"execution":{"iopub.status.busy":"2022-11-10T05:43:58.827972Z","iopub.execute_input":"2022-11-10T05:43:58.828969Z","iopub.status.idle":"2022-11-10T05:43:58.892775Z","shell.execute_reply.started":"2022-11-10T05:43:58.828926Z","shell.execute_reply":"2022-11-10T05:43:58.891617Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Read data","metadata":{}},{"cell_type":"code","source":"df = pd.read_excel(\"/kaggle/input/derbypreds/SAR.xlsx\")\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2022-11-10T05:44:54.326885Z","iopub.execute_input":"2022-11-10T05:44:54.328516Z","iopub.status.idle":"2022-11-10T05:45:01.022277Z","shell.execute_reply.started":"2022-11-10T05:44:54.328428Z","shell.execute_reply":"2022-11-10T05:45:01.021191Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Process data","metadata":{}},{"cell_type":"markdown","source":"## Filter data","metadata":{}},{"cell_type":"code","source":"df = df[df['TYPE OF TRACK'] == \"dirt\"]\ndf.reset_index(inplace=True, drop=True)\ndf = df.sort_values(['DATE','RNO','SNO'],ascending=True)","metadata":{"execution":{"iopub.status.busy":"2022-11-10T05:45:02.328997Z","iopub.execute_input":"2022-11-10T05:45:02.330328Z","iopub.status.idle":"2022-11-10T05:45:02.348684Z","shell.execute_reply.started":"2022-11-10T05:45:02.330278Z","shell.execute_reply":"2022-11-10T05:45:02.347324Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Feature Engineering","metadata":{}},{"cell_type":"code","source":"def normalize_distance(inp):\n  try:\n    data, distance = inp\n    return data/distance\n  except:\n    return np.nan\n\ndef convert_to_seconds(time):\n  time = str(time)\n  try:\n    time = time.split(\":\")\n    return float(time[0]) * 60 + float(time[1])\n  except:\n    return float(time[0])","metadata":{"execution":{"iopub.status.busy":"2022-11-10T05:45:03.263275Z","iopub.execute_input":"2022-11-10T05:45:03.263680Z","iopub.status.idle":"2022-11-10T05:45:03.270947Z","shell.execute_reply.started":"2022-11-10T05:45:03.263647Z","shell.execute_reply":"2022-11-10T05:45:03.269896Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df[\"TIMES\"] = df[\"TIME\"].apply(convert_to_seconds)\ndf[\"TIMES1\"] = df[\"TIME1\"].apply(convert_to_seconds)\n\ndf[\"NTIMES\"] = df[[\"TIMES\", \"DISTANCE\"]].apply(normalize_distance, axis=1)\ndf[\"NTIMES1\"] = df[[\"TIMES1\", \"DISTANCE\"]].apply(normalize_distance, axis=1)\ndf[\"NTravel\"] = df[[\"Travel\", \"DISTANCE\"]].apply(normalize_distance, axis=1)\n\ndf['MEAN NTIMES'] = df.groupby('HORSE NAME')['NTIMES'].apply(lambda x: x.shift().expanding().mean())\ndf['MEAN NTIMES1'] = df.groupby('HORSE NAME')['NTIMES1'].apply(lambda x: x.shift().expanding().mean())\ndf['MEAN NTravel'] = df.groupby('HORSE NAME')['NTravel'].apply(lambda x: x.shift().expanding().mean())\n\ndf[\"SHIFT_NTIMES\"] = df.groupby('HORSE NAME')['NTIMES'].shift()\ndf[\"SHIFT_NTIMES1\"] = df.groupby('HORSE NAME')['NTIMES1'].shift()\ndf[\"SHIFT_NTravel\"] = df.groupby('HORSE NAME')['NTravel'].shift()","metadata":{"execution":{"iopub.status.busy":"2022-11-10T05:45:04.155428Z","iopub.execute_input":"2022-11-10T05:45:04.155832Z","iopub.status.idle":"2022-11-10T05:45:07.205851Z","shell.execute_reply.started":"2022-11-10T05:45:04.155799Z","shell.execute_reply":"2022-11-10T05:45:07.204683Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = df.drop_duplicates()\ndf_og = df\ndf = df[['WT','AGE', \"SHIFT_NTIMES\", \"MEAN NTIMES\", \"SHIFT_NTIMES1\", \"MEAN NTIMES1\", \"SHIFT_NTravel\", \"MEAN NTravel\", \"TIME IN SEC\", \"DISTANCE\"]]","metadata":{"execution":{"iopub.status.busy":"2022-11-10T05:45:07.207853Z","iopub.execute_input":"2022-11-10T05:45:07.208202Z","iopub.status.idle":"2022-11-10T05:45:07.241804Z","shell.execute_reply.started":"2022-11-10T05:45:07.208172Z","shell.execute_reply":"2022-11-10T05:45:07.240598Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Remove Outliers","metadata":{}},{"cell_type":"code","source":"fig = px.box(df, y=\"TIME IN SEC\")\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2022-11-10T05:45:07.243213Z","iopub.execute_input":"2022-11-10T05:45:07.243587Z","iopub.status.idle":"2022-11-10T05:45:07.302838Z","shell.execute_reply.started":"2022-11-10T05:45:07.243554Z","shell.execute_reply":"2022-11-10T05:45:07.301677Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def Remove_Outlier_Indices(df, feature):\n    Q1 = df[feature].quantile(0.25)\n    Q3 = df[feature].quantile(0.75)\n    IQR = Q3 - Q1\n    trueList = ~((df[feature] < (Q1 - 1.5 * IQR)) |(df[feature] > (Q3 + 1.5 * IQR)))\n    return df[trueList]\n\ndf = Remove_Outlier_Indices(df, 'TIME IN SEC')","metadata":{"execution":{"iopub.status.busy":"2022-11-10T05:45:07.305102Z","iopub.execute_input":"2022-11-10T05:45:07.305658Z","iopub.status.idle":"2022-11-10T05:45:07.315607Z","shell.execute_reply.started":"2022-11-10T05:45:07.305624Z","shell.execute_reply":"2022-11-10T05:45:07.314340Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Drop Null values","metadata":{}},{"cell_type":"code","source":"df.dropna(inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-11-10T05:45:07.317140Z","iopub.execute_input":"2022-11-10T05:45:07.317523Z","iopub.status.idle":"2022-11-10T05:45:07.330863Z","shell.execute_reply.started":"2022-11-10T05:45:07.317490Z","shell.execute_reply":"2022-11-10T05:45:07.329765Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Descriptive statistics of data","metadata":{}},{"cell_type":"code","source":"df[\"DISTANCE\"] = df[\"DISTANCE\"].astype(\"float64\")\ndf.info()","metadata":{"execution":{"iopub.status.busy":"2022-11-10T05:45:07.332172Z","iopub.execute_input":"2022-11-10T05:45:07.332780Z","iopub.status.idle":"2022-11-10T05:45:07.354277Z","shell.execute_reply.started":"2022-11-10T05:45:07.332740Z","shell.execute_reply":"2022-11-10T05:45:07.352963Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.describe()","metadata":{"execution":{"iopub.status.busy":"2022-11-10T05:45:07.355927Z","iopub.execute_input":"2022-11-10T05:45:07.357088Z","iopub.status.idle":"2022-11-10T05:45:07.407396Z","shell.execute_reply.started":"2022-11-10T05:45:07.357017Z","shell.execute_reply":"2022-11-10T05:45:07.406151Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df=df[df['WT']<200]\ndf = df.reset_index(drop=True)\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2022-11-10T05:45:08.084273Z","iopub.execute_input":"2022-11-10T05:45:08.084705Z","iopub.status.idle":"2022-11-10T05:45:08.109529Z","shell.execute_reply.started":"2022-11-10T05:45:08.084657Z","shell.execute_reply":"2022-11-10T05:45:08.108127Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Correlation exploration","metadata":{}},{"cell_type":"code","source":"sns.pairplot(df)","metadata":{"execution":{"iopub.status.busy":"2022-11-10T05:45:08.111583Z","iopub.execute_input":"2022-11-10T05:45:08.111969Z","iopub.status.idle":"2022-11-10T05:45:33.547355Z","shell.execute_reply.started":"2022-11-10T05:45:08.111935Z","shell.execute_reply":"2022-11-10T05:45:33.546228Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"corr = df.corr()\nsns.heatmap(corr, annot=True)","metadata":{"execution":{"iopub.status.busy":"2022-11-10T05:45:33.549263Z","iopub.execute_input":"2022-11-10T05:45:33.550425Z","iopub.status.idle":"2022-11-10T05:45:34.444385Z","shell.execute_reply.started":"2022-11-10T05:45:33.550382Z","shell.execute_reply":"2022-11-10T05:45:34.443176Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Split data for training","metadata":{}},{"cell_type":"code","source":"X = df.drop([\"TIME IN SEC\"],axis=1)\nX = df[['WT','AGE', \"SHIFT_NTIMES\", \"MEAN NTIMES\", \"SHIFT_NTIMES1\", \"MEAN NTIMES1\", \"SHIFT_NTravel\", \"MEAN NTravel\", \"DISTANCE\"]]\ny = df[[\"TIME IN SEC\"]]\n\nX_train, X_test, y_train, y_test = train_test_split(X,y,random_state=12345)\nX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)","metadata":{"execution":{"iopub.status.busy":"2022-11-10T05:45:34.446258Z","iopub.execute_input":"2022-11-10T05:45:34.446981Z","iopub.status.idle":"2022-11-10T05:45:34.458662Z","shell.execute_reply.started":"2022-11-10T05:45:34.446941Z","shell.execute_reply":"2022-11-10T05:45:34.457396Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Normalize data for training","metadata":{}},{"cell_type":"code","source":"scaler = MinMaxScaler()\nX_train_scaled = scaler.fit_transform(X_train)\nX_test_scaled = scaler.transform(X_test)\nX_train_scaled = pd.DataFrame(X_train_scaled, columns=X_train.columns)\nX_test_scaled = pd.DataFrame(X_test_scaled, columns=X_test.columns)","metadata":{"execution":{"iopub.status.busy":"2022-11-10T05:45:34.461383Z","iopub.execute_input":"2022-11-10T05:45:34.461739Z","iopub.status.idle":"2022-11-10T05:45:34.481569Z","shell.execute_reply.started":"2022-11-10T05:45:34.461708Z","shell.execute_reply":"2022-11-10T05:45:34.480415Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Build models","metadata":{}},{"cell_type":"code","source":"reg =linear_model.LinearRegression()\nreg.fit(X_train_scaled,y_train)\n\ny_pred=reg.predict(X_test_scaled)\n\nmodel_name='Linear_Regerrision'\n\nmabs=metrics.mean_absolute_error(y_test, y_pred)\nmse= metrics.mean_squared_error(y_test, y_pred)\nrmse_val=np.sqrt(metrics.mean_squared_error(y_test, y_pred))\n\naccur=reg.score(X_test_scaled, y_test)\nmodel_print[model_name]=[mabs,mse,rmse_val,accur]\n\nprint('Mean Absolute Error   :',mabs)  \nprint('Mean Squared Error    :',mse )  \nprint('Root Mean Squared     :',rmse_val )\nprint('R2 Score        :',accur)","metadata":{"execution":{"iopub.status.busy":"2022-11-10T05:45:34.483184Z","iopub.execute_input":"2022-11-10T05:45:34.484328Z","iopub.status.idle":"2022-11-10T05:45:34.507017Z","shell.execute_reply.started":"2022-11-10T05:45:34.484279Z","shell.execute_reply":"2022-11-10T05:45:34.505976Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"null_pred = [y_test.mean()] * len(y_test)\nnull_mse=metrics.mean_squared_error(null_pred,y_test)\nnull_mae=metrics.mean_absolute_error(y_test,null_pred)\nprint('Null mae: {}'.format(null_mae))\nprint('Null mse: {}'.format(null_mse))\nprint('Null rmse: {}'.format(np.sqrt(null_mse)))","metadata":{"execution":{"iopub.status.busy":"2022-11-10T05:45:34.508342Z","iopub.execute_input":"2022-11-10T05:45:34.508778Z","iopub.status.idle":"2022-11-10T05:45:34.528365Z","shell.execute_reply.started":"2022-11-10T05:45:34.508734Z","shell.execute_reply":"2022-11-10T05:45:34.527102Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ridgeReg = RidgeCV(alphas=[1e-3, 1e-2, 1e-1, 1])\nridgeReg.fit(X_train_scaled,y_train)\n\ny_pred = ridgeReg.predict(X_test_scaled)\n\nmodel_name='Ridge Classifier'\nmabs=metrics.mean_absolute_error(y_test, y_pred)\nmse= metrics.mean_squared_error(y_test, y_pred)\nrmse_val=np.sqrt(metrics.mean_squared_error(y_test, y_pred))\n\naccur=ridgeReg.score(X_test_scaled, y_test)\nmodel_print[model_name]=[mabs,mse,rmse_val,accur]\n\nprint('Mean Absolute Error    :',mabs)  \nprint('Mean Squared Error     :',mse)  \nprint('Root Mean Squared Error:',rmse_val)\nprint('R2 Score         :',accur)","metadata":{"execution":{"iopub.status.busy":"2022-11-10T05:45:34.529746Z","iopub.execute_input":"2022-11-10T05:45:34.530974Z","iopub.status.idle":"2022-11-10T05:45:34.589777Z","shell.execute_reply.started":"2022-11-10T05:45:34.530925Z","shell.execute_reply":"2022-11-10T05:45:34.588526Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = xgb.XGBRegressor()\nmodel.fit(X_train_scaled,y_train)\ny_pred = model.predict(X_test_scaled)\n\nmodel_name='xgboost'\nmabs=metrics.mean_absolute_error(y_test, y_pred)\nmse= metrics.mean_squared_error(y_test, y_pred)\nrmse_val=np.sqrt(metrics.mean_squared_error(y_test, y_pred))\naccur=model.score(X_test_scaled, y_test)\n\nmodel_print[model_name]=[mabs,mse,rmse_val,accur]\n\n\nprint('Mean Absolute Error    :',mabs)  \nprint('Mean Squared Error     :',mse)  \nprint('Root Mean Squared Error:',rmse_val)\nprint('R2 Score         :',accur)","metadata":{"execution":{"iopub.status.busy":"2022-11-10T05:45:34.591412Z","iopub.execute_input":"2022-11-10T05:45:34.592236Z","iopub.status.idle":"2022-11-10T05:45:35.049238Z","shell.execute_reply.started":"2022-11-10T05:45:34.592177Z","shell.execute_reply":"2022-11-10T05:45:35.048136Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rf=RandomForestRegressor(criterion='mse',)\nrf.fit(X_train_scaled,y_train)\ny_pred=rf.predict(X_test_scaled)\n\nfeatures = df.columns\nimportances = rf.feature_importances_\n\nmodel_name=\"RandomForestRegressor\"\nmabs=metrics.mean_absolute_error(y_test, y_pred)\nmse= metrics.mean_squared_error(y_test, y_pred)\nrmse=np.sqrt(metrics.mean_squared_error(y_test, y_pred))\n\naccur=rf.score(X_test_scaled, y_test)\nmodel_print[model_name]=[mabs,mse,rmse,accur]\n\nprint('Mean Absolute Error    :',mabs)  \nprint('Mean Squared Error     :',mse )  \nprint('Root Mean Squared Error:',rmse )\nprint('R2 Score         :',accur)\n","metadata":{"execution":{"iopub.status.busy":"2022-11-10T05:45:35.052976Z","iopub.execute_input":"2022-11-10T05:45:35.055903Z","iopub.status.idle":"2022-11-10T05:45:35.645097Z","shell.execute_reply.started":"2022-11-10T05:45:35.055851Z","shell.execute_reply":"2022-11-10T05:45:35.643914Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dt=DecisionTreeRegressor()\ndt.fit(X_train_scaled,y_train)\ny_pred=dt.predict(X_test_scaled)\n\n\nmodel_name=\"DecisionTreeRegressor\"\nmabs=metrics.mean_absolute_error(y_test, y_pred)\nmse= metrics.mean_squared_error(y_test, y_pred)\nrmse=np.sqrt(metrics.mean_squared_error(y_test, y_pred))\n\n\naccur=dt.score(X_test_scaled, y_test)\nmodel_print[model_name]=[mabs,mse,rmse,accur]\n\nprint('Mean Absolute Error    :',mabs)  \nprint('Mean Squared Error     :',mse )  \nprint('Root Mean Squared Error:',rmse )\nprint('R2 Score         :',accur)\n","metadata":{"execution":{"iopub.status.busy":"2022-11-10T05:45:35.648868Z","iopub.execute_input":"2022-11-10T05:45:35.649251Z","iopub.status.idle":"2022-11-10T05:45:35.677005Z","shell.execute_reply.started":"2022-11-10T05:45:35.649217Z","shell.execute_reply":"2022-11-10T05:45:35.675649Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lasso_reg = linear_model.Lasso(alpha=1)\nlasso_reg.fit(X_train_scaled , y_train)\n\ny_pred=lasso_reg.predict(X_test_scaled)\n\nmodel_name='lasso'\nmabs=metrics.mean_absolute_error(y_test, y_pred)\nmse= metrics.mean_squared_error(y_test, y_pred)\nrmse=np.sqrt(metrics.mean_squared_error(y_test, y_pred))\n\naccur=lasso_reg.score(X_test_scaled, y_test)\nmodel_print[model_name]=[mabs,mse,rmse,accur]\n\n\nprint('Mean Absolute Error    :',mabs)  \nprint('Mean Squared Error     :',mse )  \nprint('Root Mean Squared Error:',rmse )\nprint('R2 Score         :',accur)","metadata":{"execution":{"iopub.status.busy":"2022-11-10T05:45:35.678206Z","iopub.execute_input":"2022-11-10T05:45:35.678570Z","iopub.status.idle":"2022-11-10T05:45:35.703175Z","shell.execute_reply.started":"2022-11-10T05:45:35.678539Z","shell.execute_reply":"2022-11-10T05:45:35.701809Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"elasticnet_reg = ElasticNet(alpha=1)\nelasticnet_reg.fit(X_train_scaled , y_train)\ncross_validation_accuracy = cross_val_score(estimator = elasticnet_reg, X = X_train_scaled, y = y_train, cv = 5,verbose = 1)\ny_pred = elasticnet_reg.predict(X_test_scaled)\n\nmodel_name='ElasticNet'\nmabs=metrics.mean_absolute_error(y_test, y_pred)\nmse= metrics.mean_squared_error(y_test, y_pred)\nrmse=np.sqrt(metrics.mean_squared_error(y_test, y_pred))\n\naccur=elasticnet_reg.score(X_test_scaled, y_test)\nmodel_print[model_name]=[mabs,mse,rmse,accur]\n\n\nprint('Mean Absolute Error    :',mabs)  \nprint('Mean Squared Error     :',mse )  \nprint('Root Mean Squared Error:',rmse )\nprint('R2 Score         :',accur)","metadata":{"execution":{"iopub.status.busy":"2022-11-10T05:45:35.704842Z","iopub.execute_input":"2022-11-10T05:45:35.705375Z","iopub.status.idle":"2022-11-10T05:45:35.766803Z","shell.execute_reply.started":"2022-11-10T05:45:35.705339Z","shell.execute_reply":"2022-11-10T05:45:35.765652Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ada_reg = AdaBoostRegressor(n_estimators=500)\nada_reg.fit(X_train_scaled , y_train)\ncross_validation_accuracy = cross_val_score(estimator = ada_reg, X = X_train_scaled, y = y_train, cv = 5,verbose = 1)\ny_pred = ada_reg.predict(X_test_scaled)\n\nmodel_name='AdaBoost'\n\naccur=ada_reg.score(X_test_scaled, y_test)\nmabs=metrics.mean_absolute_error(y_test, y_pred)\nmse= metrics.mean_squared_error(y_test, y_pred)\nrmse=np.sqrt(metrics.mean_squared_error(y_test, y_pred))\n\nmodel_print[model_name]=[mabs,mse,rmse,accur]\n\n\nprint('Mean Absolute Error    :',mabs)  \nprint('Mean Squared Error     :',mse )  \nprint('Root Mean Squared Error:',rmse )\nprint('R2 Score         :',accur)\n","metadata":{"execution":{"iopub.status.busy":"2022-11-10T05:45:35.768766Z","iopub.execute_input":"2022-11-10T05:45:35.769623Z","iopub.status.idle":"2022-11-10T05:45:36.699132Z","shell.execute_reply.started":"2022-11-10T05:45:35.769568Z","shell.execute_reply":"2022-11-10T05:45:36.697830Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"knn_reg = KNeighborsRegressor(n_neighbors=1)\nknn_reg.fit(X_train_scaled , y_train)\ncross_validation_accuracy = cross_val_score(estimator = knn_reg, X = X_train_scaled, y = y_train, cv = 5,verbose = 1)\ny_pred = knn_reg.predict(X_test_scaled)\n\nmodel_name='KNN'\nmabs=metrics.mean_absolute_error(y_test, y_pred)\nmse= metrics.mean_squared_error(y_test, y_pred)\nrmse=np.sqrt(metrics.mean_squared_error(y_test, y_pred))\naccur=knn_reg.score(X_test_scaled, y_test)\nmodel_print[model_name]=[mabs,mse,rmse,accur]\n\n\nprint('Mean Absolute Error    :',mabs)  \nprint('Mean Squared Error     :',mse )  \nprint('Root Mean Squared Error:',rmse )\nprint('R2 Score         :',accur)","metadata":{"execution":{"iopub.status.busy":"2022-11-10T05:45:36.700934Z","iopub.execute_input":"2022-11-10T05:45:36.701374Z","iopub.status.idle":"2022-11-10T05:45:36.773616Z","shell.execute_reply.started":"2022-11-10T05:45:36.701338Z","shell.execute_reply":"2022-11-10T05:45:36.772468Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Compare models","metadata":{}},{"cell_type":"code","source":"df_new = pd.DataFrame(list(model_print.values()),columns = ['Mean Absolute Err','Mean Square Err','RMSE','R2 Score'],index=model_print.keys()) \ndf_new.reset_index(level=0, drop=False,inplace=True)\ndf_new.rename(columns={'index':'Algo_Name'},inplace=True)\ndf_new.sort_values(by=['RMSE', 'R2 Score'],ascending=True)","metadata":{"execution":{"iopub.status.busy":"2022-11-10T05:45:36.775282Z","iopub.execute_input":"2022-11-10T05:45:36.775844Z","iopub.status.idle":"2022-11-10T05:45:36.796451Z","shell.execute_reply.started":"2022-11-10T05:45:36.775811Z","shell.execute_reply":"2022-11-10T05:45:36.794885Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"RandomForestRegressor has turned out to be the best model based on R2 score hence we proceed with this model for further predictions","metadata":{}},{"cell_type":"markdown","source":"## Test model on real race data","metadata":{}},{"cell_type":"code","source":"def process_testdf(df, date):\n  df = df[df['TYPE OF TRACK'] == \"dirt\"]\n  df.reset_index(inplace=True, drop=True)\n  \n  df[\"TIMES\"] = df[\"TIME\"].apply(convert_to_seconds)\n  df[\"TIMES1\"] = df[\"TIME1\"].apply(convert_to_seconds)\n\n  df[\"NTIMES\"] = df[[\"TIMES\", \"DISTANCE\"]].apply(normalize_distance, axis=1)\n  df[\"NTIMES1\"] = df[[\"TIMES1\", \"DISTANCE\"]].apply(normalize_distance, axis=1)\n  df[\"NTravel\"] = df[[\"Travel\", \"DISTANCE\"]].apply(normalize_distance, axis=1)\n\n  df['MEAN NTIMES'] = df.groupby('HORSE NAME')['NTIMES'].apply(lambda x: x.shift().expanding().mean())\n  df['MEAN NTIMES1'] = df.groupby('HORSE NAME')['NTIMES1'].apply(lambda x: x.shift().expanding().mean())\n  df['MEAN NTravel'] = df.groupby('HORSE NAME')['NTravel'].apply(lambda x: x.shift().expanding().mean())\n\n  df[\"SHIFT_NTIMES\"] = df.groupby('HORSE NAME')['NTIMES'].shift()\n  df[\"SHIFT_NTIMES1\"] = df.groupby('HORSE NAME')['NTIMES1'].shift()\n  df[\"SHIFT_NTravel\"] = df.groupby('HORSE NAME')['NTravel'].shift()\n\n  df = df[df[\"DATE\"] == date]\n\n  return df\n\ndef normalize_testdf(df):\n  X_test = df[['WT','AGE', \"SHIFT_NTIMES\", \"MEAN NTIMES\", \"SHIFT_NTIMES1\", \"MEAN NTIMES1\", \"SHIFT_NTravel\", \"MEAN NTravel\", \"DISTANCE\"]]\n  X_test = scaler.transform(X_test)\n  return X_test","metadata":{"execution":{"iopub.status.busy":"2022-11-10T05:46:29.955013Z","iopub.execute_input":"2022-11-10T05:46:29.955503Z","iopub.status.idle":"2022-11-10T05:46:29.968695Z","shell.execute_reply.started":"2022-11-10T05:46:29.955459Z","shell.execute_reply":"2022-11-10T05:46:29.967370Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df = pd.read_excel(\"/kaggle/input/derbypreds/TEST_RACE.xlsx\")","metadata":{"execution":{"iopub.status.busy":"2022-11-10T05:46:30.567490Z","iopub.execute_input":"2022-11-10T05:46:30.567976Z","iopub.status.idle":"2022-11-10T05:46:30.712011Z","shell.execute_reply.started":"2022-11-10T05:46:30.567937Z","shell.execute_reply":"2022-11-10T05:46:30.710777Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ptest_df = process_testdf(test_df, \"2022/10OCT/15\")\n\ntest_points = normalize_testdf(ptest_df)\n\npreds = rf.predict(test_points).flatten()\n\nptest_df[\"TIME_PRED\"] = preds\nptest_df.sort_values([\"TIME_PRED\"])[[\"SNO\", \"HORSE NAME\", \"TIME_PRED\"]]","metadata":{"execution":{"iopub.status.busy":"2022-11-10T05:46:31.982339Z","iopub.execute_input":"2022-11-10T05:46:31.982758Z","iopub.status.idle":"2022-11-10T05:46:32.048572Z","shell.execute_reply.started":"2022-11-10T05:46:31.982722Z","shell.execute_reply":"2022-11-10T05:46:32.047275Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Points Analysis","metadata":{}},{"cell_type":"markdown","source":"Note: https://drive.google.com/drive/folders/13UEX1-qE3O7jhfIilmrI96JKQDOD5246?usp=share_link \n\n","metadata":{}},{"cell_type":"markdown","source":"# Import Libraries","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nimport plotly.express as px\nimport glob","metadata":{"execution":{"iopub.status.busy":"2022-11-10T06:09:22.384669Z","iopub.execute_input":"2022-11-10T06:09:22.385428Z","iopub.status.idle":"2022-11-10T06:09:22.391742Z","shell.execute_reply.started":"2022-11-10T06:09:22.385359Z","shell.execute_reply":"2022-11-10T06:09:22.390314Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Visualize Points","metadata":{}},{"cell_type":"code","source":"df_points = pd.DataFrame()\n\ndf_points1 = pd.read_excel(\"/kaggle/input/points-datasets/POINTS_AQU.xlsx\")\ndf_points2 = pd.read_excel(\"/kaggle/input/points-datasets/POINTS_BEL.xlsx\")\ndf_points3 = pd.read_excel(\"/kaggle/input/points-datasets/POINTS_SAR.xlsx\")\n\ndf_points = pd.concat([df_points, df_points1, df_points2, df_points3])\ndf_points","metadata":{"execution":{"iopub.status.busy":"2022-11-10T06:45:41.832734Z","iopub.execute_input":"2022-11-10T06:45:41.833441Z","iopub.status.idle":"2022-11-10T06:45:41.945924Z","shell.execute_reply.started":"2022-11-10T06:45:41.833371Z","shell.execute_reply":"2022-11-10T06:45:41.943982Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Overview of Jockey Points for 2019\nBy taking the cumulative of all the points as mentioned below, \"overview of jockey performance\" is obtained.","metadata":{}},{"cell_type":"code","source":"df_points = df_points.groupby([\"JOCKY\"]).sum()\ndf_points = df_points.reset_index()\ndf_points.loc[df_points['POINTS'] < 37000, 'JOCKY'] = 'Other'\nfig = px.pie(df_points, values='POINTS', names='JOCKY', title='JOCKY POINTS')\nfig.update_traces(hoverinfo=\"label+percent+name\")\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2022-11-10T06:45:50.038679Z","iopub.execute_input":"2022-11-10T06:45:50.039601Z","iopub.status.idle":"2022-11-10T06:45:50.106215Z","shell.execute_reply.started":"2022-11-10T06:45:50.039560Z","shell.execute_reply":"2022-11-10T06:45:50.104441Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Overview of Jockey Points Aqueduct, Belmont, Sartoga wise ","metadata":{}},{"cell_type":"markdown","source":"### Points Allocation Strategy\nFor the remaining data \"points are allocated based on win and place\".","metadata":{}},{"cell_type":"code","source":"df_points1 = df_points1.groupby([\"JOCKY\"]).sum()\ndf_points1 = df_points1.reset_index()\ndf_points1.loc[df_points1['POINTS'] < 17000, 'JOCKY'] = 'Other'\nfig = px.pie(df_points1, values='POINTS', names='JOCKY', title='JOCKY POINTS AQU')\nfig.update_traces(hoverinfo=\"label+percent+name\")\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2022-11-10T06:45:54.780803Z","iopub.execute_input":"2022-11-10T06:45:54.781301Z","iopub.status.idle":"2022-11-10T06:45:54.847164Z","shell.execute_reply.started":"2022-11-10T06:45:54.781262Z","shell.execute_reply":"2022-11-10T06:45:54.845721Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_points2 = df_points2.groupby([\"JOCKY\"]).sum()\ndf_points2 = df_points2.reset_index()\ndf_points2.loc[df_points2['POINTS'] < 17000, 'JOCKY'] = 'Other'\nfig = px.pie(df_points2, values='POINTS', names='JOCKY', title='JOCKY POINTS BEL')\nfig.update_traces(hoverinfo=\"label+percent+name\")\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2022-11-10T06:45:58.313481Z","iopub.execute_input":"2022-11-10T06:45:58.313934Z","iopub.status.idle":"2022-11-10T06:45:58.378575Z","shell.execute_reply.started":"2022-11-10T06:45:58.313900Z","shell.execute_reply":"2022-11-10T06:45:58.377641Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_points3 = df_points3.groupby([\"JOCKY\"]).sum()\ndf_points3 = df_points3.reset_index()\ndf_points3.loc[df_points3['POINTS'] < 7000, 'JOCKY'] = 'Other'\nfig = px.pie(df_points3, values='POINTS', names='JOCKY', title='JOCKY POINTS SAR')\nfig.update_traces(hoverinfo=\"label+percent+name\")\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2022-11-10T06:46:00.159482Z","iopub.execute_input":"2022-11-10T06:46:00.159987Z","iopub.status.idle":"2022-11-10T06:46:00.226545Z","shell.execute_reply.started":"2022-11-10T06:46:00.159948Z","shell.execute_reply":"2022-11-10T06:46:00.224819Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Overview of Jockey Points with distance and center\nA granular analysis was done across all centers with \"dirt course type\" and \"good & fast track conditions\". The data for rest of the conditions/tracks is insufficient to perform any useful analysis and the corner cases like \"Muddy\" and \"Sloppy\" doesn't allow us to take any inference.","metadata":{}},{"cell_type":"markdown","source":"### Points Allocation Strategy\nPoints are allocated to a jockey if he follows an optimal path at the bends. We are not allocating any points for drafting as it is dependent on the various factors such as dynamic position of the horse in a given situation, the kickback, the response of the horse to proper, lasikx.","metadata":{}},{"cell_type":"code","source":"for file_name in glob.glob('/kaggle/input/points-datasets/AQU_*.xlsx'):\n    df_points_temp = pd.read_excel(file_name)\n    distance = file_name.split(\"_\")[1].split(\".\")[0]\n    df_points_temp = df_points_temp.groupby([\"JOCKY\"]).sum()\n    df_points_temp = df_points_temp.reset_index()\n    df_points_temp.loc[df_points_temp['POINTS'] < 30, 'JOCKY'] = 'Other'\n    fig = px.pie(df_points_temp, values='POINTS', names='JOCKY', title='JOCKY POINTS AQU ' + distance)\n    fig.update_traces(hoverinfo=\"label+percent+name\")\n    fig.show()","metadata":{"execution":{"iopub.status.busy":"2022-11-10T07:03:30.436010Z","iopub.execute_input":"2022-11-10T07:03:30.436498Z","iopub.status.idle":"2022-11-10T07:03:35.816065Z","shell.execute_reply.started":"2022-11-10T07:03:30.436462Z","shell.execute_reply":"2022-11-10T07:03:35.814739Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for file_name in glob.glob('/kaggle/input/points-datasets/BEL_*.xlsx'):\n    df_points_temp = pd.read_excel(file_name)\n    distance = file_name.split(\"_\")[1].split(\".\")[0]\n    df_points_temp = df_points_temp.groupby([\"JOCKY\"]).sum()\n    df_points_temp = df_points_temp.reset_index()\n    df_points_temp.loc[df_points_temp['POINTS'] < 30, 'JOCKY'] = 'Other'\n    fig = px.pie(df_points_temp, values='POINTS', names='JOCKY', title='JOCKY POINTS AQU ' + distance)\n    fig.update_traces(hoverinfo=\"label+percent+name\")\n    fig.show()","metadata":{"execution":{"iopub.status.busy":"2022-11-10T07:03:35.818718Z","iopub.execute_input":"2022-11-10T07:03:35.819263Z","iopub.status.idle":"2022-11-10T07:03:42.433579Z","shell.execute_reply.started":"2022-11-10T07:03:35.819219Z","shell.execute_reply":"2022-11-10T07:03:42.431990Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for file_name in glob.glob('/kaggle/input/points-datasets/SAR_*.xlsx'):\n    df_points_temp = pd.read_excel(file_name)\n    distance = file_name.split(\"_\")[1].split(\".\")[0]\n    df_points_temp = df_points_temp.groupby([\"JOCKY\"]).sum()\n    df_points_temp = df_points_temp.reset_index()\n    df_points_temp.loc[df_points_temp['POINTS'] < 30, 'JOCKY'] = 'Other'\n    fig = px.pie(df_points_temp, values='POINTS', names='JOCKY', title='JOCKY POINTS SAR ' + distance)\n    fig.update_traces(hoverinfo=\"label+percent+name\")\n    fig.show()","metadata":{"execution":{"iopub.status.busy":"2022-11-10T07:03:42.435617Z","iopub.execute_input":"2022-11-10T07:03:42.436129Z","iopub.status.idle":"2022-11-10T07:03:46.108346Z","shell.execute_reply.started":"2022-11-10T07:03:42.436080Z","shell.execute_reply":"2022-11-10T07:03:46.106396Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}