{"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":"<h2><center>Map to make decisions in horse racing</center></h2>","metadata":{}},{"cell_type":"markdown","source":"<p style='text-align: justify;'>This report will present a simple way to improve the chances of winning a horse race bet, through a study of the data provided. For this, the physical conditions that are present in the data will be taken into account, making a choice of the variables to be used in the machine learning model for future decision-making in the field in which it is circumstanced.</p>","metadata":{}},{"cell_type":"markdown","source":"### Artifacts & Data","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nfrom sklearn import preprocessing\nfrom sklearn.model_selection import train_test_split\nimport numpy as np\nfrom sklearn import metrics\nfrom sklearn.tree import DecisionTreeClassifier\nimport seaborn as sns\nimport matplotlib.pyplot as plt\nimport plotly.express as px\nimport joblib","metadata":{"execution":{"iopub.status.busy":"2022-11-10T20:43:48.393603Z","iopub.execute_input":"2022-11-10T20:43:48.394069Z","iopub.status.idle":"2022-11-10T20:43:48.402484Z","shell.execute_reply.started":"2022-11-10T20:43:48.394034Z","shell.execute_reply":"2022-11-10T20:43:48.400743Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_complete = pd.read_csv('/kaggle/input/big-data-derby-2022/nyra_2019_complete.csv', header=None, low_memory=False)\ndf_nyra_race_table = pd.read_csv('/kaggle/input/big-data-derby-2022/nyra_race_table.csv')\ndf_nyra_start_table = pd.read_csv('/kaggle/input/big-data-derby-2022/nyra_start_table.csv', header=None)","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-11-10T20:43:53.845379Z","iopub.execute_input":"2022-11-10T20:43:53.846332Z","iopub.status.idle":"2022-11-10T20:44:10.813440Z","shell.execute_reply.started":"2022-11-10T20:43:53.846295Z","shell.execute_reply":"2022-11-10T20:44:10.811937Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_complete.columns =['track_id', 'race_date', 'race_number', 'program_number', 'trakus_index', 'latitude', 'longitude', 'distance_id', 'course_type', 'track_condition', 'run_up_distance', 'race_type', 'post_time', 'purse', 'weight_carried', 'jockey', 'odds', 'position_at_finish']\ndf_complete.head(2)","metadata":{"execution":{"iopub.status.busy":"2022-11-10T20:44:10.816398Z","iopub.execute_input":"2022-11-10T20:44:10.817268Z","iopub.status.idle":"2022-11-10T20:44:10.844215Z","shell.execute_reply.started":"2022-11-10T20:44:10.817220Z","shell.execute_reply":"2022-11-10T20:44:10.842916Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"corr = df_complete.corr(method='pearson')\nplt.figure(figsize=(12,10))\nsns.heatmap(corr, linewidth=0.2,annot=True,cmap=\"cividis\").set(title='Correlations between variables in df_complete')","metadata":{"execution":{"iopub.status.busy":"2022-11-10T20:02:16.681268Z","iopub.execute_input":"2022-11-10T20:02:16.681860Z","iopub.status.idle":"2022-11-10T20:02:19.767125Z","shell.execute_reply.started":"2022-11-10T20:02:16.681824Z","shell.execute_reply":"2022-11-10T20:02:19.765989Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Variable selection\nBased on the dictionary of variables provided by the sponsors and a heuristic view of the correlations previously provided for a decision on physical conditions.\nThe following will be selected\n\nNumeric variables:\n- distance_id\n- purse\n-weight_carried\n- odds\n\nCategorical variables:\n- race_type\n- track_condition\n-course_type\n\nTarget: position_at_finish\"","metadata":{}},{"cell_type":"code","source":"df_complete[['track_condition','weight_carried','odds','distance_id','course_type','race_type','purse']].head(1)","metadata":{"execution":{"iopub.status.busy":"2022-11-10T20:02:19.769576Z","iopub.execute_input":"2022-11-10T20:02:19.769902Z","iopub.status.idle":"2022-11-10T20:02:19.961777Z","shell.execute_reply.started":"2022-11-10T20:02:19.769873Z","shell.execute_reply":"2022-11-10T20:02:19.960579Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Variables selected for further analysis together with the model and its behavior","metadata":{}},{"cell_type":"markdown","source":"* weight_carried - An integer of the weight carried by the horse in the race.\n* odds - Odds to win the race passed as an integer. Divide by 100 to derive the odds to 1. Example - 1280 would be 12.8-1.\n* distance_id - Distance of the race in furlongs passed as an integer. Example - 600 would be 6 furlongs.\n* course_type - The course the race was run over passed as one character. M - Hurdle, D - Dirt, O - Outer turf, I - Inner turf, T - turf.\n* track_condition - The condition of the course the race was run on passed as three characters. YL - Yielding, FM - Firm, SY - Sloppy, GD - Good, FT - Fast, MY - Muddy, SF - Soft.\n* race_type - The classification of the race passed as as five characters. STK - Stakes, WCL - Waiver Claiming, WMC - Waiver Maiden Claiming, SST - Starter Stakes, SHP - Starter Handicap, CLM - Claiming, STR - Starter Allowance, AOC - Allowance Optionl Claimer, SOC - Starter Optional Claimer, MCL - Maiden Claiming, ALW - Allowance, MSW - Maiden Special Weight.\n* purse - Purse in US dollars of the race passed as an money with two decimal places.\n* position_at_finish - An integer of the horse's finishing position. (added to the dataset 9/8/22)","metadata":{}},{"cell_type":"markdown","source":"#### Matrix of Numerical Variables","metadata":{}},{"cell_type":"code","source":"sns.pairplot(df_complete[['weight_carried','odds','distance_id','purse', 'position_at_finish']])\nplt.figure(figsize=(8,8))\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-11-10T20:02:19.963423Z","iopub.execute_input":"2022-11-10T20:02:19.963804Z","iopub.status.idle":"2022-11-10T20:05:38.625526Z","shell.execute_reply.started":"2022-11-10T20:02:19.963758Z","shell.execute_reply":"2022-11-10T20:05:38.624409Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Categorical Variables","metadata":{}},{"cell_type":"code","source":"fig = px.box(df_complete, x='race_type', y=\"position_at_finish\", title='Position at finish vs Race type',width=900,height=500)\nfig.update_traces(quartilemethod=\"exclusive\") \nfig.show()","metadata":{"execution":{"iopub.status.busy":"2022-11-10T20:17:14.191152Z","iopub.execute_input":"2022-11-10T20:17:14.191550Z","iopub.status.idle":"2022-11-10T20:17:36.766708Z","shell.execute_reply.started":"2022-11-10T20:17:14.191518Z","shell.execute_reply":"2022-11-10T20:17:36.762974Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = px.box(df_complete, x='track_condition', y=\"position_at_finish\", title='Position at finish vs Track condition',width=800,height=400)\nfig.update_traces(quartilemethod=\"exclusive\") \nfig.show()","metadata":{"execution":{"iopub.status.busy":"2022-11-10T20:11:12.374264Z","iopub.execute_input":"2022-11-10T20:11:12.375336Z","iopub.status.idle":"2022-11-10T20:11:32.693944Z","shell.execute_reply.started":"2022-11-10T20:11:12.375271Z","shell.execute_reply":"2022-11-10T20:11:32.691887Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = px.box(df_complete, x='course_type', y=\"position_at_finish\", title='Position at finish vs Course type',width=800,height=400)\nfig.update_traces(quartilemethod=\"exclusive\") \nfig.show()","metadata":{"execution":{"iopub.status.busy":"2022-11-10T20:20:43.914833Z","iopub.execute_input":"2022-11-10T20:20:43.915339Z","iopub.status.idle":"2022-11-10T20:21:04.512738Z","shell.execute_reply.started":"2022-11-10T20:20:43.915304Z","shell.execute_reply":"2022-11-10T20:21:04.511755Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Modeling with machine learning","metadata":{}},{"cell_type":"markdown","source":"<center><img src=\"https://i0.wp.com/robbreport.mx/wp-content/uploads/2019/04/webp-1-1.jpg?w=1152&ssl=1\" width=\"500\" ></center>","metadata":{}},{"cell_type":"code","source":"df_nyra_start_table.columns=['track_id', 'race_date', 'race_number', 'program_number', 'weight_carried', 'jockey', 'odds', 'position_at_finish']\ndf_complet = df_complete[['track_condition','weight_carried','odds','distance_id','course_type','race_type','purse','position_at_finish']]\ndf_total = df_nyra_start_table.merge(df_nyra_race_table, on=['track_id','race_date', 'race_number'], how='inner')\ndf_total = df_total[['track_condition','weight_carried','odds','distance_id','course_type','race_type','purse','position_at_finish']]\nframes = [df_complet, df_total]\ndf_result = pd.concat(frames, ignore_index = True)","metadata":{"execution":{"iopub.status.busy":"2022-11-10T20:56:29.836064Z","iopub.execute_input":"2022-11-10T20:56:29.836485Z","iopub.status.idle":"2022-11-10T20:56:30.942314Z","shell.execute_reply.started":"2022-11-10T20:56:29.836450Z","shell.execute_reply":"2022-11-10T20:56:30.940898Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = df_result[['track_condition','weight_carried','odds','distance_id','course_type','race_type','purse']].values\ny = df_result[\"position_at_finish\"]","metadata":{"execution":{"iopub.status.busy":"2022-11-10T20:45:00.188304Z","iopub.execute_input":"2022-11-10T20:45:00.188778Z","iopub.status.idle":"2022-11-10T20:45:03.136562Z","shell.execute_reply.started":"2022-11-10T20:45:00.188743Z","shell.execute_reply":"2022-11-10T20:45:03.134903Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"le_track_condition = preprocessing.LabelEncoder()\nle_track_condition.fit(['GD ', 'FM ', 'FT ', 'SY ', 'MY ', 'YL ', 'SF '])\nX[:,0] = le_track_condition.transform(X[:,0])\n\nle_course_type = preprocessing.LabelEncoder()\nle_course_type.fit(['D', 'O', 'I', 'T', 'M'])\nX[:,4] = le_course_type.transform(X[:,4]) \n\nle_race_type = preprocessing.LabelEncoder()\nle_race_type.fit(['CLM', 'AOC', 'ALW', 'SST', 'STK', 'WCL', 'STR', 'SOC', 'SHP', 'MCL', 'WMC', 'MSW'])\nX[:,5] = le_race_type.transform(X[:,5]) \n\nX[0:5]","metadata":{"execution":{"iopub.status.busy":"2022-11-10T20:45:04.312700Z","iopub.execute_input":"2022-11-10T20:45:04.313440Z","iopub.status.idle":"2022-11-10T20:45:08.165613Z","shell.execute_reply.started":"2022-11-10T20:45:04.313399Z","shell.execute_reply":"2022-11-10T20:45:08.164111Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_trainset, X_testset, y_trainset, y_testset = train_test_split(X, y, test_size=0.30, random_state=4)","metadata":{"execution":{"iopub.status.busy":"2022-11-10T20:45:08.168171Z","iopub.execute_input":"2022-11-10T20:45:08.168672Z","iopub.status.idle":"2022-11-10T20:45:18.344249Z","shell.execute_reply.started":"2022-11-10T20:45:08.168625Z","shell.execute_reply":"2022-11-10T20:45:18.342802Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"RaceTree = DecisionTreeClassifier(criterion=\"entropy\", max_depth = 100)\nRaceTree \nRaceTree.fit(X_trainset,y_trainset)\npredTree = RaceTree.predict(X_testset)","metadata":{"execution":{"iopub.status.busy":"2022-11-10T20:45:20.094430Z","iopub.execute_input":"2022-11-10T20:45:20.095683Z","iopub.status.idle":"2022-11-10T20:46:21.391116Z","shell.execute_reply.started":"2022-11-10T20:45:20.095626Z","shell.execute_reply":"2022-11-10T20:46:21.389305Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"DecisionTrees's Accuracy: \", metrics.accuracy_score(y_testset, predTree))","metadata":{"execution":{"iopub.status.busy":"2022-11-10T20:46:25.102101Z","iopub.execute_input":"2022-11-10T20:46:25.103029Z","iopub.status.idle":"2022-11-10T20:46:25.234897Z","shell.execute_reply.started":"2022-11-10T20:46:25.102989Z","shell.execute_reply":"2022-11-10T20:46:25.233549Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Related curiosities and useful information","metadata":{}},{"cell_type":"code","source":"df_pos1 = df_result[df_result[\"position_at_finish\"] == 1]\ndf_pos_t3 = df_result[df_result[\"position_at_finish\"] < 4]","metadata":{"execution":{"iopub.status.busy":"2022-11-10T20:57:37.346049Z","iopub.execute_input":"2022-11-10T20:57:37.346524Z","iopub.status.idle":"2022-11-10T20:57:38.566288Z","shell.execute_reply.started":"2022-11-10T20:57:37.346488Z","shell.execute_reply":"2022-11-10T20:57:38.564765Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Race Type**","metadata":{}},{"cell_type":"markdown","source":"It will be analyzed when it is the first position and within the first 3","metadata":{}},{"cell_type":"code","source":"df_result['race_type'].unique()","metadata":{"execution":{"iopub.status.busy":"2022-11-10T20:07:33.196839Z","iopub.execute_input":"2022-11-10T20:07:33.197190Z","iopub.status.idle":"2022-11-10T20:07:33.560571Z","shell.execute_reply.started":"2022-11-10T20:07:33.197157Z","shell.execute_reply":"2022-11-10T20:07:33.559378Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_race_type = df_pos1.groupby('race_type')['position_at_finish'].sum().sort_values(ascending=False)","metadata":{"execution":{"iopub.status.busy":"2022-11-10T20:07:33.562427Z","iopub.execute_input":"2022-11-10T20:07:33.563137Z","iopub.status.idle":"2022-11-10T20:07:33.627199Z","shell.execute_reply.started":"2022-11-10T20:07:33.563100Z","shell.execute_reply":"2022-11-10T20:07:33.626007Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_race_type","metadata":{"execution":{"iopub.status.busy":"2022-11-10T20:07:33.628535Z","iopub.execute_input":"2022-11-10T20:07:33.628897Z","iopub.status.idle":"2022-11-10T20:07:33.637730Z","shell.execute_reply.started":"2022-11-10T20:07:33.628850Z","shell.execute_reply":"2022-11-10T20:07:33.636374Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_race_type.plot(kind='bar')","metadata":{"execution":{"iopub.status.busy":"2022-11-10T20:07:33.639227Z","iopub.execute_input":"2022-11-10T20:07:33.639564Z","iopub.status.idle":"2022-11-10T20:07:33.905056Z","shell.execute_reply.started":"2022-11-10T20:07:33.639530Z","shell.execute_reply":"2022-11-10T20:07:33.903867Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Top 3 positions:","metadata":{}},{"cell_type":"code","source":"df_pos_t3.groupby('race_type')['position_at_finish'].sum().sort_values(ascending=False)","metadata":{"execution":{"iopub.status.busy":"2022-11-10T20:07:33.906422Z","iopub.execute_input":"2022-11-10T20:07:33.906885Z","iopub.status.idle":"2022-11-10T20:07:34.086199Z","shell.execute_reply.started":"2022-11-10T20:07:33.906852Z","shell.execute_reply":"2022-11-10T20:07:34.085111Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<p style='text-align: justify;'>In horse racing a maiden race is an event for horses that have not won a race. Horses that have not won a race are referred to as maidens. Maiden horse races are held over a variety of distances and under conditions with eligibility based on the sex or age of the horse. Races may be handicaps, set weights, or weight for age. In many countries, maiden races are the lowest level of class and represent an entry point into a racing career. In countries such as the United States, maiden special weight races rank above claiming races, while maiden claiming races allow the horse to be claimed (bought) by another owner.</p>\nhttps://en.wikipedia.org/wiki/Maiden_race","metadata":{}},{"cell_type":"markdown","source":"**Track Condition**","metadata":{}},{"cell_type":"code","source":"df_result['track_condition'].unique()","metadata":{"execution":{"iopub.status.busy":"2022-11-10T20:07:34.087519Z","iopub.execute_input":"2022-11-10T20:07:34.088381Z","iopub.status.idle":"2022-11-10T20:07:34.449222Z","shell.execute_reply.started":"2022-11-10T20:07:34.088346Z","shell.execute_reply":"2022-11-10T20:07:34.448069Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_track_condition = df_pos1.groupby('track_condition')['position_at_finish'].sum().sort_values(ascending=False)","metadata":{"execution":{"iopub.status.busy":"2022-11-10T20:07:34.450819Z","iopub.execute_input":"2022-11-10T20:07:34.451713Z","iopub.status.idle":"2022-11-10T20:07:34.515092Z","shell.execute_reply.started":"2022-11-10T20:07:34.451663Z","shell.execute_reply":"2022-11-10T20:07:34.513889Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_track_condition","metadata":{"execution":{"iopub.status.busy":"2022-11-10T20:07:34.517135Z","iopub.execute_input":"2022-11-10T20:07:34.517607Z","iopub.status.idle":"2022-11-10T20:07:34.525419Z","shell.execute_reply.started":"2022-11-10T20:07:34.517562Z","shell.execute_reply":"2022-11-10T20:07:34.524569Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_track_condition.plot(kind='bar')","metadata":{"execution":{"iopub.status.busy":"2022-11-10T20:07:34.526804Z","iopub.execute_input":"2022-11-10T20:07:34.527166Z","iopub.status.idle":"2022-11-10T20:07:34.756446Z","shell.execute_reply.started":"2022-11-10T20:07:34.527133Z","shell.execute_reply":"2022-11-10T20:07:34.755312Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Top 3 positions:","metadata":{}},{"cell_type":"code","source":"df_pos_t3.groupby('track_condition')['position_at_finish'].sum().sort_values(ascending=False)","metadata":{"execution":{"iopub.status.busy":"2022-11-10T20:07:34.758085Z","iopub.execute_input":"2022-11-10T20:07:34.759026Z","iopub.status.idle":"2022-11-10T20:07:34.933463Z","shell.execute_reply.started":"2022-11-10T20:07:34.758978Z","shell.execute_reply":"2022-11-10T20:07:34.932261Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Fast and firm is the best condition","metadata":{}},{"cell_type":"markdown","source":"**Course Type**","metadata":{}},{"cell_type":"code","source":"df_result['course_type'].unique()","metadata":{"execution":{"iopub.status.busy":"2022-11-10T20:07:34.934888Z","iopub.execute_input":"2022-11-10T20:07:34.935235Z","iopub.status.idle":"2022-11-10T20:07:35.292964Z","shell.execute_reply.started":"2022-11-10T20:07:34.935203Z","shell.execute_reply":"2022-11-10T20:07:35.291985Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_course_type = df_pos1.groupby('course_type')['position_at_finish'].sum().sort_values(ascending=False)","metadata":{"execution":{"iopub.status.busy":"2022-11-10T20:07:35.294151Z","iopub.execute_input":"2022-11-10T20:07:35.294961Z","iopub.status.idle":"2022-11-10T20:07:35.356542Z","shell.execute_reply.started":"2022-11-10T20:07:35.294927Z","shell.execute_reply":"2022-11-10T20:07:35.355546Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_course_type","metadata":{"execution":{"iopub.status.busy":"2022-11-10T20:07:35.361831Z","iopub.execute_input":"2022-11-10T20:07:35.362213Z","iopub.status.idle":"2022-11-10T20:07:35.370918Z","shell.execute_reply.started":"2022-11-10T20:07:35.362179Z","shell.execute_reply":"2022-11-10T20:07:35.369386Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_course_type.plot(kind='bar')","metadata":{"execution":{"iopub.status.busy":"2022-11-10T20:07:35.372113Z","iopub.execute_input":"2022-11-10T20:07:35.372492Z","iopub.status.idle":"2022-11-10T20:07:35.576181Z","shell.execute_reply.started":"2022-11-10T20:07:35.372459Z","shell.execute_reply":"2022-11-10T20:07:35.575324Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Top 3 positions:","metadata":{}},{"cell_type":"code","source":"df_pos_t3.groupby('course_type')['position_at_finish'].sum().sort_values(ascending=False)","metadata":{"execution":{"iopub.status.busy":"2022-11-10T20:07:35.577442Z","iopub.execute_input":"2022-11-10T20:07:35.577967Z","iopub.status.idle":"2022-11-10T20:07:35.745786Z","shell.execute_reply.started":"2022-11-10T20:07:35.577937Z","shell.execute_reply":"2022-11-10T20:07:35.745034Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Dirt and turf are the best terrains to win (D and T)","metadata":{}},{"cell_type":"markdown","source":"#### Numeric variables","metadata":{}},{"cell_type":"markdown","source":"The weight in the race that carries the most advantages is 120","metadata":{}},{"cell_type":"code","source":"df_pos1.groupby('weight_carried')['position_at_finish'].sum().sort_values(ascending=False)","metadata":{"execution":{"iopub.status.busy":"2022-11-10T21:00:57.911053Z","iopub.execute_input":"2022-11-10T21:00:57.912690Z","iopub.status.idle":"2022-11-10T21:00:57.941441Z","shell.execute_reply.started":"2022-11-10T21:00:57.912636Z","shell.execute_reply":"2022-11-10T21:00:57.940175Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(df_result['weight_carried'].min())\nprint(df_result['weight_carried'].max())","metadata":{"execution":{"iopub.status.busy":"2022-11-10T20:59:46.926440Z","iopub.execute_input":"2022-11-10T20:59:46.926919Z","iopub.status.idle":"2022-11-10T20:59:46.950136Z","shell.execute_reply.started":"2022-11-10T20:59:46.926883Z","shell.execute_reply":"2022-11-10T20:59:46.948539Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(df_result['odds'].min())\nprint(df_result['odds'].max())","metadata":{"execution":{"iopub.status.busy":"2022-11-10T20:07:35.766655Z","iopub.execute_input":"2022-11-10T20:07:35.767004Z","iopub.status.idle":"2022-11-10T20:07:35.784679Z","shell.execute_reply.started":"2022-11-10T20:07:35.766974Z","shell.execute_reply":"2022-11-10T20:07:35.783547Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(df_result['distance_id'].min())\nprint(df_result['distance_id'].max())","metadata":{"execution":{"iopub.status.busy":"2022-11-10T20:07:35.786380Z","iopub.execute_input":"2022-11-10T20:07:35.787418Z","iopub.status.idle":"2022-11-10T20:07:35.807092Z","shell.execute_reply.started":"2022-11-10T20:07:35.787370Z","shell.execute_reply":"2022-11-10T20:07:35.805917Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(df_result['purse'].min())\nprint(df_result['purse'].max())","metadata":{"execution":{"iopub.status.busy":"2022-11-10T20:07:35.808655Z","iopub.execute_input":"2022-11-10T20:07:35.809151Z","iopub.status.idle":"2022-11-10T20:07:35.827601Z","shell.execute_reply.started":"2022-11-10T20:07:35.809105Z","shell.execute_reply":"2022-11-10T20:07:35.826422Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"my_array = np.array(X)\ndf_code = pd.DataFrame(my_array, columns=['track_condition_code','weight_carried_code','odds_code','distance_id_code','course_type_code','race_type_code','purse_code'])\ndf_ncode = df_result[['track_condition','weight_carried','odds','distance_id','course_type','race_type','purse']]\ndf_ncode[['track_condition_code','weight_carried_code','odds_code','distance_id_code','course_type_code','race_type_code','purse_code']] = df_code[['track_condition_code','weight_carried_code','odds_code','distance_id_code','course_type_code','race_type_code','purse_code']]","metadata":{"execution":{"iopub.status.busy":"2022-11-10T20:07:35.828741Z","iopub.execute_input":"2022-11-10T20:07:35.829050Z","iopub.status.idle":"2022-11-10T20:07:39.536263Z","shell.execute_reply.started":"2022-11-10T20:07:35.829022Z","shell.execute_reply":"2022-11-10T20:07:39.535421Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_ncode[['track_condition','track_condition_code']].drop_duplicates(ignore_index = True).sort_values('track_condition_code')","metadata":{"execution":{"iopub.status.busy":"2022-11-10T20:07:39.537842Z","iopub.execute_input":"2022-11-10T20:07:39.538530Z","iopub.status.idle":"2022-11-10T20:07:42.474537Z","shell.execute_reply.started":"2022-11-10T20:07:39.538469Z","shell.execute_reply":"2022-11-10T20:07:42.473333Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_ncode[['course_type','course_type_code']].drop_duplicates(ignore_index = True).sort_values('course_type_code')","metadata":{"execution":{"iopub.status.busy":"2022-11-10T20:07:42.477474Z","iopub.execute_input":"2022-11-10T20:07:42.477816Z","iopub.status.idle":"2022-11-10T20:07:43.418533Z","shell.execute_reply.started":"2022-11-10T20:07:42.477787Z","shell.execute_reply":"2022-11-10T20:07:43.417768Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_ncode[['race_type','race_type_code']].drop_duplicates(ignore_index = True).sort_values('race_type_code')","metadata":{"execution":{"iopub.status.busy":"2022-11-10T20:07:43.420102Z","iopub.execute_input":"2022-11-10T20:07:43.421057Z","iopub.status.idle":"2022-11-10T20:07:44.366207Z","shell.execute_reply.started":"2022-11-10T20:07:43.421021Z","shell.execute_reply":"2022-11-10T20:07:44.365075Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### App using model","metadata":{}},{"cell_type":"code","source":"def app_using_model(c_t,r_t,t_c,w_c,o_s,d_i,p_e):\n    print(\"\"\" \n\n    Course Type  |  Race Type  | Track Condition | Weight Carried  | Odds       | Distance Id |   Purse       |\n        0 - D    |   ALW - 0   |  FM - 0         | Min: 110        | Min: 0     |  Min: 450   | Min: $100     |\n        1 - I    |   AOC - 1   |  FT - 1         | Max: 160        | Max: 19100 |  Max: 2000  | Max: $1500000 |\n        3 - M    |   CLM - 2   |  GD - 2         |                 |            |             |               |\n        2 - O    |   MCL - 3   |  MY - 3         |                 |\n        4 - T    |   MSW - 4   |  SF - 4         |                 | \n                 |   SHP - 5   |  SY - 5         |                 |\n                 |   SOC - 6   |  YL - 6         |                 | \n                 |   SST - 7   | \n                 |   STK - 8   |\n                 |   STR - 9   |\n                 |   WCL - 10  |\n                 |   WMC - 11  |\n             \n    \"\"\")\n    c_t =input(\"Choose a number for Course Type:\")\n    r_t =input(\"Choose a number for Race Type:\")\n    t_c =input(\"Choose a number for Track Condition:\")\n    w_c =input(\"Choose a number for Weight Carried:\")\n    o_s =input(\"Choose a number for Odds:\")\n    d_i =input(\"Choose a number for Distance Id:\")\n    p_e =input(\"Choose a number for Purse:\")\n    vector = np.array([[c_t,r_t,t_c,w_c,o_s,d_i,p_e]])\n    return print( \"\\n\\nThe most likely position your decisions will have is:\", RaceTree.predict(vector))   ","metadata":{"execution":{"iopub.status.busy":"2022-11-10T21:08:47.606573Z","iopub.execute_input":"2022-11-10T21:08:47.607080Z","iopub.status.idle":"2022-11-10T21:08:47.616178Z","shell.execute_reply.started":"2022-11-10T21:08:47.607031Z","shell.execute_reply":"2022-11-10T21:08:47.614970Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"You can try with the suggestions that the statistics provide when the variables are analyzed and you will see that the best expected results will be obtained or it will be the most probable, or test your ideas.","metadata":{}},{"cell_type":"markdown","source":"#### Save the model","metadata":{}},{"cell_type":"code","source":"joblib.dump(RaceTree, 'RaceTree.pkl')","metadata":{"execution":{"iopub.status.busy":"2022-11-10T20:10:40.890520Z","iopub.status.idle":"2022-11-10T20:10:40.891038Z","shell.execute_reply.started":"2022-11-10T20:10:40.890833Z","shell.execute_reply":"2022-11-10T20:10:40.890855Z"},"trusted":true},"execution_count":null,"outputs":[]}]}