{"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":"## Headers for import","metadata":{}},{"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport os\nimport matplotlib.pyplot as plt\n\n# Preprocessing Required imports\nfrom scipy.stats import zscore\nfrom sklearn import preprocessing\nfrom sklearn.model_selection import train_test_split\nfrom category_encoders import LeaveOneOutEncoder\n\n# Model Imports\nfrom sklearn.linear_model import LogisticRegression\n\n\nimport warnings \nwarnings.filterwarnings( \"ignore\")","metadata":{"ExecuteTime":{"end_time":"2022-11-10T04:42:19.770294Z","start_time":"2022-11-10T04:42:12.777032Z"},"executionInfo":{"elapsed":614,"status":"ok","timestamp":1667154527916,"user":{"displayName":"Megan Mirnalini Sundaram R","userId":"12974737852416514030"},"user_tz":-330},"id":"-Cx_Pks5HepI","execution":{"iopub.status.busy":"2022-11-10T05:13:48.367768Z","iopub.execute_input":"2022-11-10T05:13:48.368277Z","iopub.status.idle":"2022-11-10T05:13:50.039584Z","shell.execute_reply.started":"2022-11-10T05:13:48.368199Z","shell.execute_reply":"2022-11-10T05:13:50.038522Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Ingestion","metadata":{"ExecuteTime":{"end_time":"2022-11-02T07:51:54.609486Z","start_time":"2022-11-02T07:51:54.593682Z"},"id":"K4y0LcM4HepU"}},{"cell_type":"code","source":"##Header columns provided by Ken Miller @ https://www.kaggle.com/code/mlconsult/fix-missing-data-column-names/notebook\n\nstart_columns = ['track_id','race_date','race_number','program_number','weight_carried','jockey','odds','position_at_finish']\ncomplete_columns = ['track_id','race_date','race_number','program_number','trakus_index','latitude','longitude','distance_id','course_type','track_condition','run_up_distance','race_type','purse','post_time','weight_carried','jockey','odds','position_at_finish']","metadata":{"ExecuteTime":{"end_time":"2022-11-10T04:42:19.785953Z","start_time":"2022-11-10T04:42:19.770294Z"},"id":"gfyABL0zHepS","execution":{"iopub.status.busy":"2022-11-10T05:13:50.041490Z","iopub.execute_input":"2022-11-10T05:13:50.041835Z","iopub.status.idle":"2022-11-10T05:13:50.048579Z","shell.execute_reply.started":"2022-11-10T05:13:50.041803Z","shell.execute_reply":"2022-11-10T05:13:50.047270Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nyra_start = pd.read_csv('/kaggle/input/big-data-derby-2022/nyra_start_table.csv',names=start_columns,low_memory=False)\nnyra_data = pd.read_csv(\"../input/big-data-derby-2022/nyra_2019_complete.csv\",names=complete_columns,low_memory=False)\nnyra_tracking_table = pd.read_csv(\"../input/big-data-derby-2022/nyra_tracking_table.csv\",low_memory=False)\nnyra_race = pd.read_csv(\"../input/big-data-derby-2022/nyra_race_table.csv\",low_memory=False)","metadata":{"ExecuteTime":{"end_time":"2022-11-10T04:42:19.801541Z","start_time":"2022-11-10T04:42:19.785953Z"},"id":"M20lssnvHepV","execution":{"iopub.status.busy":"2022-11-10T05:13:50.049728Z","iopub.execute_input":"2022-11-10T05:13:50.050131Z","iopub.status.idle":"2022-11-10T05:13:51.263358Z","shell.execute_reply.started":"2022-11-10T05:13:50.050096Z","shell.execute_reply":"2022-11-10T05:13:51.258238Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del start_columns, complete_columns","metadata":{"ExecuteTime":{"end_time":"2022-11-10T04:42:43.135590Z","start_time":"2022-11-10T04:42:43.119957Z"},"execution":{"iopub.status.busy":"2022-11-10T05:13:51.264544Z","iopub.status.idle":"2022-11-10T05:13:51.264988Z","shell.execute_reply.started":"2022-11-10T05:13:51.264706Z","shell.execute_reply":"2022-11-10T05:13:51.264729Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Preprocessing Checks","metadata":{}},{"cell_type":"code","source":"list_of_dataframes = [nyra_race, nyra_start, nyra_data, nyra_tracking_table]","metadata":{"ExecuteTime":{"end_time":"2022-11-10T04:42:43.151344Z","start_time":"2022-11-10T04:42:43.135590Z"},"id":"YGLTs0fEHepY","execution":{"iopub.status.busy":"2022-11-10T05:13:51.267414Z","iopub.status.idle":"2022-11-10T05:13:51.268348Z","shell.execute_reply.started":"2022-11-10T05:13:51.268104Z","shell.execute_reply":"2022-11-10T05:13:51.268129Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Printing the info. for all dataframes\n\nfor i in list_of_dataframes:\n    item = i.info()\n    print(item)\n    print(\"--------------------------------------------------\")\n    \ndel item","metadata":{"ExecuteTime":{"end_time":"2022-11-10T04:42:43.214152Z","start_time":"2022-11-10T04:42:43.151344Z"},"id":"p4YoTjkxHepa","outputId":"3e08530c-f189-4fbb-ba2d-75328b29068f","execution":{"iopub.status.busy":"2022-11-10T05:13:51.270021Z","iopub.status.idle":"2022-11-10T05:13:51.270497Z","shell.execute_reply.started":"2022-11-10T05:13:51.270276Z","shell.execute_reply":"2022-11-10T05:13:51.270297Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Printing the null values in each dataframe\n\nfor i in list_of_dataframes:\n    item = i.isnull().sum()\n    # print(item)\n    \ndel item","metadata":{"ExecuteTime":{"end_time":"2022-11-10T04:42:51.803642Z","start_time":"2022-11-10T04:42:43.214152Z"},"id":"2cF76Os0Hepd","execution":{"iopub.status.busy":"2022-11-10T05:13:51.272344Z","iopub.status.idle":"2022-11-10T05:13:51.272799Z","shell.execute_reply.started":"2022-11-10T05:13:51.272581Z","shell.execute_reply":"2022-11-10T05:13:51.272602Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Printing the count of duplicates in each dataframe\n\nfor i in list_of_dataframes:\n    item = i.duplicated().sum()\n    # print(item)\n    \ndel item,i","metadata":{"ExecuteTime":{"end_time":"2022-11-10T04:43:04.686894Z","start_time":"2022-11-10T04:42:51.803642Z"},"id":"VwI4n6EoHepf","execution":{"iopub.status.busy":"2022-11-10T05:13:51.274535Z","iopub.status.idle":"2022-11-10T05:13:51.274984Z","shell.execute_reply.started":"2022-11-10T05:13:51.274755Z","shell.execute_reply":"2022-11-10T05:13:51.274776Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del list_of_dataframes","metadata":{"ExecuteTime":{"end_time":"2022-11-10T04:43:04.702469Z","start_time":"2022-11-10T04:43:04.686894Z"},"execution":{"iopub.status.busy":"2022-11-10T05:13:51.275977Z","iopub.status.idle":"2022-11-10T05:13:51.276471Z","shell.execute_reply.started":"2022-11-10T05:13:51.276264Z","shell.execute_reply":"2022-11-10T05:13:51.276285Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## DateTime","metadata":{}},{"cell_type":"code","source":"nyra_race['race_date'] = pd.to_datetime(nyra_race['race_date'])\nnyra_start['race_date'] = pd.to_datetime(nyra_start['race_date'])\nnyra_data['race_date'] = pd.to_datetime(nyra_data['race_date'])\nnyra_tracking_table['race_date'] = pd.to_datetime(nyra_tracking_table['race_date'])","metadata":{"ExecuteTime":{"end_time":"2022-11-10T04:43:05.910800Z","start_time":"2022-11-10T04:43:04.702469Z"},"id":"yL3FsmdlHepg","execution":{"iopub.status.busy":"2022-11-10T05:13:51.277632Z","iopub.status.idle":"2022-11-10T05:13:51.277912Z","shell.execute_reply.started":"2022-11-10T05:13:51.277770Z","shell.execute_reply":"2022-11-10T05:13:51.277782Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Preliminary Insights","metadata":{}},{"cell_type":"markdown","source":"Going forward, all insights will be gathered from NYRA_data which contains the complete data for 2019. ","metadata":{}},{"cell_type":"markdown","source":"#### Adding another column called track_type\n\nThe tracks are primarily split into 2 - Dirt and Turf. \n\n    The tracks which are Sloppy, Fast and Muddy are taken as Dirt. \n    The tracks which are Yielding, Firm, Good and Soft are taken as Turf. ","metadata":{}},{"cell_type":"code","source":"nyra_data['track_type'] = nyra_data['track_condition']","metadata":{"ExecuteTime":{"end_time":"2022-11-10T04:43:05.942043Z","start_time":"2022-11-10T04:43:05.910800Z"},"execution":{"iopub.status.busy":"2022-11-10T05:13:51.279225Z","iopub.status.idle":"2022-11-10T05:13:51.279520Z","shell.execute_reply.started":"2022-11-10T05:13:51.279375Z","shell.execute_reply":"2022-11-10T05:13:51.279388Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nyra_data['track_type'] = nyra_data['track_type'].str.strip()\nnyra_data['track_condition'] = nyra_data['track_condition'].str.strip()","metadata":{"ExecuteTime":{"end_time":"2022-11-10T04:43:11.071972Z","start_time":"2022-11-10T04:43:05.942043Z"},"execution":{"iopub.status.busy":"2022-11-10T05:13:51.280368Z","iopub.status.idle":"2022-11-10T05:13:51.280652Z","shell.execute_reply.started":"2022-11-10T05:13:51.280517Z","shell.execute_reply":"2022-11-10T05:13:51.280529Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nyra_data['track_type'] = np.where(nyra_data['track_type'] == 'FT', \"Dirt\", nyra_data['track_type'])\nnyra_data['track_type'] = np.where(nyra_data['track_type'] == 'MY', \"Dirt\", nyra_data['track_type'])\nnyra_data['track_type'] = np.where(nyra_data['track_type'] == 'SY', \"Dirt\", nyra_data['track_type'])\nnyra_data['track_type'] = np.where(nyra_data['track_type'] == 'FM', \"Turf\", nyra_data['track_type'])\nnyra_data['track_type'] = np.where(nyra_data['track_type'] == 'GD', \"Turf\", nyra_data['track_type'])\nnyra_data['track_type'] = np.where(nyra_data['track_type'] == 'YL', \"Turf\", nyra_data['track_type'])\nnyra_data['track_type'] = np.where(nyra_data['track_type'] == 'SF', \"Turf\", nyra_data['track_type'])","metadata":{"ExecuteTime":{"end_time":"2022-11-10T04:43:14.997013Z","start_time":"2022-11-10T04:43:11.071972Z"},"execution":{"iopub.status.busy":"2022-11-10T05:13:51.281523Z","iopub.status.idle":"2022-11-10T05:13:51.281787Z","shell.execute_reply.started":"2022-11-10T05:13:51.281656Z","shell.execute_reply":"2022-11-10T05:13:51.281668Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"jockey_names = nyra_data['jockey']","metadata":{"ExecuteTime":{"end_time":"2022-11-10T04:43:15.027933Z","start_time":"2022-11-10T04:43:15.003996Z"},"execution":{"iopub.status.busy":"2022-11-10T05:13:51.282566Z","iopub.status.idle":"2022-11-10T05:13:51.282825Z","shell.execute_reply.started":"2022-11-10T05:13:51.282697Z","shell.execute_reply":"2022-11-10T05:13:51.282709Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Encoding for Object values","metadata":{}},{"cell_type":"markdown","source":"## List of object values","metadata":{}},{"cell_type":"code","source":"for column in nyra_data.columns:\n    if nyra_data[column].dtype == 'object':\n        print(column.upper(),': ',nyra_data[column].nunique())\n        print(nyra_data[column].value_counts().sort_values())\n        print('\\n')","metadata":{"ExecuteTime":{"end_time":"2022-11-10T04:43:22.348301Z","start_time":"2022-11-10T04:43:15.033913Z"},"id":"EthHukANHepk","outputId":"2a6f97ad-5f91-495f-b2ad-a8851959e613","scrolled":true,"execution":{"iopub.status.busy":"2022-11-10T05:13:51.284087Z","iopub.status.idle":"2022-11-10T05:13:51.284417Z","shell.execute_reply.started":"2022-11-10T05:13:51.284252Z","shell.execute_reply":"2022-11-10T05:13:51.284267Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## encoding track_id","metadata":{}},{"cell_type":"code","source":"label_encoder = preprocessing.LabelEncoder()\nprint(label_encoder.fit_transform(['SAR','BEL','AQU']))","metadata":{"ExecuteTime":{"end_time":"2022-11-10T04:43:22.364222Z","start_time":"2022-11-10T04:43:22.352254Z"},"id":"utJl9in9Hepm","outputId":"771a049b-1ee1-4978-8da2-5ef736da6401","execution":{"iopub.status.busy":"2022-11-10T05:13:51.285379Z","iopub.status.idle":"2022-11-10T05:13:51.285708Z","shell.execute_reply.started":"2022-11-10T05:13:51.285543Z","shell.execute_reply":"2022-11-10T05:13:51.285558Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nyra_data['track_id'] = np.where(nyra_data['track_id'] =='AQU', 0, nyra_data['track_id'])\nnyra_data['track_id'] = np.where(nyra_data['track_id'] =='BEL', 1, nyra_data['track_id'])\nnyra_data['track_id'] = np.where(nyra_data['track_id'] =='SAR', 2, nyra_data['track_id'])\n\nnyra_data['track_id'] = nyra_data['track_id'].astype('int64')","metadata":{"ExecuteTime":{"end_time":"2022-11-10T04:43:25.551662Z","start_time":"2022-11-10T04:43:22.373206Z"},"id":"PEDWryWxHepo","execution":{"iopub.status.busy":"2022-11-10T05:13:51.286898Z","iopub.status.idle":"2022-11-10T05:13:51.287252Z","shell.execute_reply.started":"2022-11-10T05:13:51.287081Z","shell.execute_reply":"2022-11-10T05:13:51.287097Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## encoding track_type","metadata":{}},{"cell_type":"code","source":"print(label_encoder.fit_transform(['FT','MY','SY','FM','GD','YL','SF']))\nprint(label_encoder.fit_transform(['Dirt','Track']))","metadata":{"ExecuteTime":{"end_time":"2022-11-10T04:43:25.582606Z","start_time":"2022-11-10T04:43:25.557649Z"},"execution":{"iopub.status.busy":"2022-11-10T05:13:51.288710Z","iopub.status.idle":"2022-11-10T05:13:51.289075Z","shell.execute_reply.started":"2022-11-10T05:13:51.288886Z","shell.execute_reply":"2022-11-10T05:13:51.288901Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nyra_data['track_condition'] = np.where(nyra_data['track_condition'] =='FT', 1, nyra_data['track_condition'])\nnyra_data['track_condition'] = np.where(nyra_data['track_condition'] =='MY', 3, nyra_data['track_condition'])\nnyra_data['track_condition'] = np.where(nyra_data['track_condition'] =='SY', 5, nyra_data['track_condition'])\nnyra_data['track_condition'] = np.where(nyra_data['track_condition'] =='FM', 0, nyra_data['track_condition'])\nnyra_data['track_condition'] = np.where(nyra_data['track_condition'] =='GD', 2, nyra_data['track_condition'])\nnyra_data['track_condition'] = np.where(nyra_data['track_condition'] =='YL', 6, nyra_data['track_condition'])\nnyra_data['track_condition'] = np.where(nyra_data['track_condition'] =='SF', 4, nyra_data['track_condition'])","metadata":{"ExecuteTime":{"end_time":"2022-11-10T04:43:30.993361Z","start_time":"2022-11-10T04:43:25.591566Z"},"execution":{"iopub.status.busy":"2022-11-10T05:13:51.290555Z","iopub.status.idle":"2022-11-10T05:13:51.290915Z","shell.execute_reply.started":"2022-11-10T05:13:51.290733Z","shell.execute_reply":"2022-11-10T05:13:51.290749Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nyra_data['track_type'] = np.where(nyra_data['track_type'] == 'Dirt', 0, nyra_data['track_type'])\nnyra_data['track_type'] = np.where(nyra_data['track_type'] == 'Turf', 1, nyra_data['track_type'])","metadata":{"ExecuteTime":{"end_time":"2022-11-10T04:43:31.736030Z","start_time":"2022-11-10T04:43:30.995366Z"},"execution":{"iopub.status.busy":"2022-11-10T05:13:51.293251Z","iopub.status.idle":"2022-11-10T05:13:51.293603Z","shell.execute_reply.started":"2022-11-10T05:13:51.293431Z","shell.execute_reply":"2022-11-10T05:13:51.293447Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nyra_data['track_condition'] = nyra_data['track_condition'].astype('int64')\nnyra_data['track_type'] = nyra_data['track_type'].astype('int64')","metadata":{"ExecuteTime":{"end_time":"2022-11-10T04:43:32.162707Z","start_time":"2022-11-10T04:43:31.738851Z"},"execution":{"iopub.status.busy":"2022-11-10T05:13:51.295745Z","iopub.status.idle":"2022-11-10T05:13:51.296119Z","shell.execute_reply.started":"2022-11-10T05:13:51.295929Z","shell.execute_reply":"2022-11-10T05:13:51.295945Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## encoding course_type","metadata":{}},{"cell_type":"code","source":"print(label_encoder.fit_transform(['M','O','D','I','T']))","metadata":{"ExecuteTime":{"end_time":"2022-11-10T04:43:32.178320Z","start_time":"2022-11-10T04:43:32.164707Z"},"execution":{"iopub.status.busy":"2022-11-10T05:13:51.298467Z","iopub.status.idle":"2022-11-10T05:13:51.298815Z","shell.execute_reply.started":"2022-11-10T05:13:51.298645Z","shell.execute_reply":"2022-11-10T05:13:51.298661Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nyra_data['course_type'] = np.where(nyra_data['course_type'] == 'M', \"2\", nyra_data['course_type'])\nnyra_data['course_type'] = np.where(nyra_data['course_type'] == 'O', \"3\", nyra_data['course_type'])\nnyra_data['course_type'] = np.where(nyra_data['course_type'] == 'D', \"0\", nyra_data['course_type'])\nnyra_data['course_type'] = np.where(nyra_data['course_type'] == 'I', \"1\", nyra_data['course_type'])\nnyra_data['course_type'] = np.where(nyra_data['course_type'] == 'T', \"4\", nyra_data['course_type'])\n\nnyra_data['course_type'] = nyra_data['course_type'].astype('int64')","metadata":{"ExecuteTime":{"end_time":"2022-11-10T04:43:36.463337Z","start_time":"2022-11-10T04:43:32.181321Z"},"execution":{"iopub.status.busy":"2022-11-10T05:13:51.301378Z","iopub.status.idle":"2022-11-10T05:13:51.301741Z","shell.execute_reply.started":"2022-11-10T05:13:51.301572Z","shell.execute_reply":"2022-11-10T05:13:51.301588Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## encoding race_type","metadata":{}},{"cell_type":"code","source":"nyra_data[\"race_type\"] = nyra_data[\"race_type\"].str.strip()","metadata":{"ExecuteTime":{"end_time":"2022-11-10T04:43:38.408264Z","start_time":"2022-11-10T04:43:36.468320Z"},"execution":{"iopub.status.busy":"2022-11-10T05:13:51.303938Z","iopub.status.idle":"2022-11-10T05:13:51.304285Z","shell.execute_reply.started":"2022-11-10T05:13:51.304120Z","shell.execute_reply":"2022-11-10T05:13:51.304136Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(label_encoder.fit_transform(['WMC','SHP','WCL','SST','SOC','STR','ALW','AOC','STK','MCL','MSW','CLM']))","metadata":{"ExecuteTime":{"end_time":"2022-11-10T04:43:38.439262Z","start_time":"2022-11-10T04:43:38.415274Z"},"scrolled":true,"execution":{"iopub.status.busy":"2022-11-10T05:13:51.306118Z","iopub.status.idle":"2022-11-10T05:13:51.306441Z","shell.execute_reply.started":"2022-11-10T05:13:51.306278Z","shell.execute_reply":"2022-11-10T05:13:51.306293Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mappings_racetype = {'ALW':0,'AOC':1,'CLM':2,'MCL':3,'MSW':4,'SHP':5,'SOC':6,'SST':7,'STK':8,'STR':9,'WCL':10,'WMC':11}\n\nnyra_data[\"race_type\"] = nyra_data[\"race_type\"].map(mappings_racetype)\ndel mappings_racetype\n\nnyra_data['race_type'] = nyra_data['race_type'].astype('int64')","metadata":{"ExecuteTime":{"end_time":"2022-11-10T04:43:39.353005Z","start_time":"2022-11-10T04:43:38.453217Z"},"execution":{"iopub.status.busy":"2022-11-10T05:13:51.308716Z","iopub.status.idle":"2022-11-10T05:13:51.309072Z","shell.execute_reply.started":"2022-11-10T05:13:51.308886Z","shell.execute_reply":"2022-11-10T05:13:51.308901Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## encoding program number","metadata":{}},{"cell_type":"code","source":"nyra_data[\"program_number\"] = nyra_data[\"program_number\"].str.strip()","metadata":{"ExecuteTime":{"end_time":"2022-11-10T04:43:42.307416Z","start_time":"2022-11-10T04:43:39.493258Z"},"scrolled":true,"execution":{"iopub.status.busy":"2022-11-10T05:13:51.310984Z","iopub.status.idle":"2022-11-10T05:13:51.311347Z","shell.execute_reply.started":"2022-11-10T05:13:51.311181Z","shell.execute_reply":"2022-11-10T05:13:51.311197Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nyra_data['program_number'] = np.where(nyra_data['program_number'] == '1A', 110 , nyra_data['program_number'])\nnyra_data['program_number'] = np.where(nyra_data['program_number'] == '2B', 211 , nyra_data['program_number'])\nnyra_data['program_number'] = np.where(nyra_data['program_number'] == '1X', 124 , nyra_data['program_number'])\nnyra_data['program_number'] = np.where(nyra_data['program_number'] == '3X', 324 , nyra_data['program_number'])\n\nnyra_data['program_number'] = nyra_data['program_number'].astype('int64')","metadata":{"ExecuteTime":{"end_time":"2022-11-10T04:43:46.179514Z","start_time":"2022-11-10T04:43:42.310412Z"},"execution":{"iopub.status.busy":"2022-11-10T05:13:51.313847Z","iopub.status.idle":"2022-11-10T05:13:51.314237Z","shell.execute_reply.started":"2022-11-10T05:13:51.314065Z","shell.execute_reply":"2022-11-10T05:13:51.314081Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## encoding jockey","metadata":{}},{"cell_type":"code","source":"list_to_process = [nyra_data]\nfor item in list_to_process:\n    temp_df = LeaveOneOutEncoder(cols=['jockey']).fit(item, item['position_at_finish']).transform(item)\n    item['jockey'] = temp_df['jockey']\n    \ndel temp_df,item,list_to_process","metadata":{"ExecuteTime":{"end_time":"2022-11-10T04:43:54.458750Z","start_time":"2022-11-10T04:43:46.181513Z"},"execution":{"iopub.status.busy":"2022-11-10T05:13:51.319931Z","iopub.status.idle":"2022-11-10T05:13:51.320601Z","shell.execute_reply.started":"2022-11-10T05:13:51.320379Z","shell.execute_reply":"2022-11-10T05:13:51.320401Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nyra_data['position_at_finish'] = nyra_data['position_at_finish'].astype('float64')","metadata":{"ExecuteTime":{"end_time":"2022-11-10T04:43:54.616222Z","start_time":"2022-11-10T04:43:54.463706Z"},"execution":{"iopub.status.busy":"2022-11-10T05:13:51.321699Z","iopub.status.idle":"2022-11-10T05:13:51.322065Z","shell.execute_reply.started":"2022-11-10T05:13:51.321877Z","shell.execute_reply":"2022-11-10T05:13:51.321893Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Transforming Date","metadata":{}},{"cell_type":"code","source":"nyra_data['race_month'] = nyra_data['race_date'].dt.month\nnyra_data['race_day'] = nyra_data['race_date'].dt.day","metadata":{"ExecuteTime":{"end_time":"2022-11-10T04:43:55.737952Z","start_time":"2022-11-10T04:43:54.618214Z"},"execution":{"iopub.status.busy":"2022-11-10T05:13:51.323875Z","iopub.status.idle":"2022-11-10T05:13:51.324247Z","shell.execute_reply.started":"2022-11-10T05:13:51.324073Z","shell.execute_reply":"2022-11-10T05:13:51.324090Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nyra_data.drop(columns=['race_date'],inplace=True)","metadata":{"ExecuteTime":{"end_time":"2022-11-10T04:43:57.102527Z","start_time":"2022-11-10T04:43:55.737952Z"},"execution":{"iopub.status.busy":"2022-11-10T05:13:51.325937Z","iopub.status.idle":"2022-11-10T05:13:51.326317Z","shell.execute_reply.started":"2022-11-10T05:13:51.326140Z","shell.execute_reply":"2022-11-10T05:13:51.326156Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Normalizing and prepping the dataset","metadata":{}},{"cell_type":"code","source":"train_data = nyra_data","metadata":{"ExecuteTime":{"end_time":"2022-11-10T04:43:57.118147Z","start_time":"2022-11-10T04:43:57.102527Z"},"execution":{"iopub.status.busy":"2022-11-10T05:13:51.328387Z","iopub.status.idle":"2022-11-10T05:13:51.328756Z","shell.execute_reply.started":"2022-11-10T05:13:51.328586Z","shell.execute_reply":"2022-11-10T05:13:51.328602Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Scaling the dataset","metadata":{}},{"cell_type":"code","source":"from sklearn.preprocessing import StandardScaler\nscaler = StandardScaler()","metadata":{"ExecuteTime":{"end_time":"2022-11-10T04:43:57.133771Z","start_time":"2022-11-10T04:43:57.118147Z"},"execution":{"iopub.status.busy":"2022-11-10T05:13:51.331337Z","iopub.status.idle":"2022-11-10T05:13:51.331716Z","shell.execute_reply.started":"2022-11-10T05:13:51.331541Z","shell.execute_reply":"2022-11-10T05:13:51.331557Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"col_list = train_data.columns","metadata":{"ExecuteTime":{"end_time":"2022-11-10T04:43:57.149392Z","start_time":"2022-11-10T04:43:57.133771Z"},"execution":{"iopub.status.busy":"2022-11-10T05:13:51.333604Z","iopub.status.idle":"2022-11-10T05:13:51.333969Z","shell.execute_reply.started":"2022-11-10T05:13:51.333789Z","shell.execute_reply":"2022-11-10T05:13:51.333805Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data_scaled = scaler.fit_transform(train_data)\ntrain_data_scaled = pd.DataFrame(train_data_scaled)\ntrain_data_scaled.columns= col_list","metadata":{"ExecuteTime":{"end_time":"2022-11-10T04:44:00.617012Z","start_time":"2022-11-10T04:43:57.149392Z"},"execution":{"iopub.status.busy":"2022-11-10T05:13:51.336109Z","iopub.status.idle":"2022-11-10T05:13:51.336483Z","shell.execute_reply.started":"2022-11-10T05:13:51.336309Z","shell.execute_reply":"2022-11-10T05:13:51.336324Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y = train_data['position_at_finish']\nX = train_data_scaled.drop('position_at_finish',axis=1)","metadata":{"ExecuteTime":{"end_time":"2022-11-10T04:44:01.071735Z","start_time":"2022-11-10T04:44:00.617012Z"},"execution":{"iopub.status.busy":"2022-11-10T05:13:51.339145Z","iopub.status.idle":"2022-11-10T05:13:51.339553Z","shell.execute_reply.started":"2022-11-10T05:13:51.339372Z","shell.execute_reply":"2022-11-10T05:13:51.339389Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Splitting the dataset","metadata":{}},{"cell_type":"code","source":"X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3 , random_state=1)","metadata":{"ExecuteTime":{"end_time":"2022-11-10T04:44:03.472522Z","start_time":"2022-11-10T04:44:01.071735Z"},"execution":{"iopub.status.busy":"2022-11-10T05:13:51.341705Z","iopub.status.idle":"2022-11-10T05:13:51.343444Z","shell.execute_reply.started":"2022-11-10T05:13:51.342863Z","shell.execute_reply":"2022-11-10T05:13:51.342885Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Model Building","metadata":{}},{"cell_type":"markdown","source":"## LogReg","metadata":{}},{"cell_type":"code","source":"log_reg_model = LogisticRegression()","metadata":{"ExecuteTime":{"end_time":"2022-11-10T04:44:03.488030Z","start_time":"2022-11-10T04:44:03.472522Z"},"execution":{"iopub.status.busy":"2022-11-10T05:13:51.344720Z","iopub.status.idle":"2022-11-10T05:13:51.345116Z","shell.execute_reply.started":"2022-11-10T05:13:51.344920Z","shell.execute_reply":"2022-11-10T05:13:51.344936Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"log_reg_model.fit(X_train, y_train)","metadata":{"ExecuteTime":{"end_time":"2022-11-10T04:49:27.679199Z","start_time":"2022-11-10T04:44:03.488030Z"},"scrolled":true,"execution":{"iopub.status.busy":"2022-11-10T05:13:51.347273Z","iopub.status.idle":"2022-11-10T05:13:51.347640Z","shell.execute_reply.started":"2022-11-10T05:13:51.347466Z","shell.execute_reply":"2022-11-10T05:13:51.347483Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cols = [train_data.columns]\ncoeff = [log_reg_model.coef_]\nfor name, i in zip(iter(cols), iter(coeff)):\n    print('The coefficient of', name ,'is',i) ","metadata":{"ExecuteTime":{"end_time":"2022-11-10T04:49:27.726870Z","start_time":"2022-11-10T04:49:27.681710Z"},"execution":{"iopub.status.busy":"2022-11-10T05:13:51.350802Z","iopub.status.idle":"2022-11-10T05:13:51.351311Z","shell.execute_reply.started":"2022-11-10T05:13:51.351113Z","shell.execute_reply":"2022-11-10T05:13:51.351133Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Based on the co-efficients generated, the effective iteration is chosen for further prediction of the position at finish. This is mapped against the jockey for the position. ","metadata":{}},{"cell_type":"code","source":"test_data_scaled = train_data_scaled.drop(columns=['position_at_finish'],axis=1)","metadata":{"ExecuteTime":{"end_time":"2022-11-10T04:49:30.529829Z","start_time":"2022-11-10T04:49:27.726870Z"},"execution":{"iopub.status.busy":"2022-11-10T05:13:51.354295Z","iopub.status.idle":"2022-11-10T05:13:51.354686Z","shell.execute_reply.started":"2022-11-10T05:13:51.354505Z","shell.execute_reply":"2022-11-10T05:13:51.354522Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"logreg_predictions = log_reg_model.predict(test_data_scaled)","metadata":{"ExecuteTime":{"end_time":"2022-11-10T04:49:35.579240Z","start_time":"2022-11-10T04:49:30.529829Z"},"execution":{"iopub.status.busy":"2022-11-10T05:13:51.356680Z","iopub.status.idle":"2022-11-10T05:13:51.357062Z","shell.execute_reply.started":"2022-11-10T05:13:51.356856Z","shell.execute_reply":"2022-11-10T05:13:51.356872Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final_results = pd.DataFrame({'Jockey':jockey_names,\"Ranking\":logreg_predictions})","metadata":{"ExecuteTime":{"end_time":"2022-11-10T04:49:36.034364Z","start_time":"2022-11-10T04:49:35.579240Z"},"execution":{"iopub.status.busy":"2022-11-10T05:13:51.359701Z","iopub.status.idle":"2022-11-10T05:13:51.360095Z","shell.execute_reply.started":"2022-11-10T05:13:51.359879Z","shell.execute_reply":"2022-11-10T05:13:51.359896Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final_results.drop_duplicates(inplace=True)\nfinal_results.reset_index()","metadata":{"ExecuteTime":{"end_time":"2022-11-10T04:58:22.849443Z","start_time":"2022-11-10T04:58:22.825485Z"},"scrolled":true,"execution":{"iopub.status.busy":"2022-11-10T05:13:51.362247Z","iopub.status.idle":"2022-11-10T05:13:51.362613Z","shell.execute_reply.started":"2022-11-10T05:13:51.362433Z","shell.execute_reply":"2022-11-10T05:13:51.362450Z"},"trusted":true},"execution_count":null,"outputs":[]}]}