{"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":"# **WELCOME TO THE DERBY 2022 DATA ANALYSIS**","metadata":{}},{"cell_type":"markdown","source":"Here we are analyzing which horses performed better overall, in all races combined.\n","metadata":{"execution":{"iopub.status.busy":"2022-11-26T13:46:00.959871Z","iopub.execute_input":"2022-11-26T13:46:00.960852Z","iopub.status.idle":"2022-11-26T13:46:00.968755Z","shell.execute_reply.started":"2022-11-26T13:46:00.960810Z","shell.execute_reply":"2022-11-26T13:46:00.967301Z"}}},{"cell_type":"markdown","source":"The main reasoning behind this was: \n    \n    1.It would be beneficial for placing bets.\n    \n    2.It would be beneficial to the race conducting body           \n      and horses owning body to know which horse needs             \n      more attention, medically and emotionally\n    \n    3.Horses with similar muscle structure as those of    \n      the winning horse should be preferred.","metadata":{}},{"cell_type":"markdown","source":"Initial imports","metadata":{}},{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport matplotlib.pyplot as plt\n\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-12-07T13:53:36.772646Z","iopub.execute_input":"2022-12-07T13:53:36.773078Z","iopub.status.idle":"2022-12-07T13:53:36.783601Z","shell.execute_reply.started":"2022-12-07T13:53:36.773038Z","shell.execute_reply":"2022-12-07T13:53:36.781587Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### **Give proper column names**","metadata":{}},{"cell_type":"markdown","source":"### Give proper column name to start table","metadata":{}},{"cell_type":"code","source":"start_table_df=pd.read_csv(\"/kaggle/input/big-data-derby-2022/nyra_start_table.csv\")\nstart_table_df","metadata":{"execution":{"iopub.status.busy":"2022-12-07T13:53:36.785796Z","iopub.execute_input":"2022-12-07T13:53:36.787203Z","iopub.status.idle":"2022-12-07T13:53:36.876432Z","shell.execute_reply.started":"2022-12-07T13:53:36.787155Z","shell.execute_reply":"2022-12-07T13:53:36.875129Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"start_shifte_df=start_table_df.shift(periods=1,axis=0)\nrem_entry=start_table_df.columns\nwide_numpy=np.array(rem_entry)\nstray_df=dict({})\nfor i in wide_numpy:\n    stray_df[i]=i\nstray_df=pd.DataFrame.from_dict({0:stray_df})\nstray_df=stray_df.transpose()\nstart_shifte_df=start_shifte_df.drop(0,axis=0)\nstart_table_df_shifted=pd.concat([stray_df,start_shifte_df],axis=0)\nstart_table_df_shifted. columns = [\"race_number\",\"program_number\",\"weight_carried\",\"odds\",\"position_at_finish\",\"race_date\",\"track_id\",\"jockey\"]\nstart_table_df_shifted","metadata":{"execution":{"iopub.status.busy":"2022-12-07T13:53:36.878853Z","iopub.execute_input":"2022-12-07T13:53:36.879263Z","iopub.status.idle":"2022-12-07T13:53:36.920324Z","shell.execute_reply.started":"2022-12-07T13:53:36.879226Z","shell.execute_reply":"2022-12-07T13:53:36.919282Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Give proper name to derby all data table","metadata":{}},{"cell_type":"code","source":"derby_all_df=pd.read_csv(\"/kaggle/input/big-data-derby-2022/nyra_2019_complete.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-12-07T13:53:36.921790Z","iopub.execute_input":"2022-12-07T13:53:36.922285Z","iopub.status.idle":"2022-12-07T13:53:51.015523Z","shell.execute_reply.started":"2022-12-07T13:53:36.922248Z","shell.execute_reply":"2022-12-07T13:53:51.014175Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"all_df_1st_row = derby_all_df.columns\n\n\nall_df_1st_array=np.array(all_df_1st_row)\nnew_df=dict({})\nfor i in all_df_1st_array:\n    new_df[i]=i\nnew_df = pd.DataFrame.from_dict({0:new_df})\nnew_df =new_df.transpose()\nderby_all_df=pd.concat([new_df,derby_all_df])\nderby_all_df_columns = [\"longitude\",\"post_time\",\"weight_carried\",\"race_date\",\"odds\",\"purse\",\"latitude\",\"run_up_distance\",\"program_number\",\"distance_id\",\"trakus_index\",\"position_at_finish\",\"race_number\",\"track_id\",\"jockey\",\"race_type\",\"course_type\",\"track_condition\"]\nderby_all_df.columns= derby_all_df_columns\nderby_all_df","metadata":{"execution":{"iopub.status.busy":"2022-12-07T13:53:51.017840Z","iopub.execute_input":"2022-12-07T13:53:51.018162Z","iopub.status.idle":"2022-12-07T13:53:52.960644Z","shell.execute_reply.started":"2022-12-07T13:53:51.018131Z","shell.execute_reply":"2022-12-07T13:53:52.958923Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **Getting those horses which were first** ","metadata":{}},{"cell_type":"code","source":"first_horses_df = start_table_df_shifted[start_table_df_shifted[\"position_at_finish\"]==1.0]\nfirst_horses_df_freq = first_horses_df.program_number.value_counts()\ntotal_horses = len(first_horses_df.odds)\n","metadata":{"execution":{"iopub.status.busy":"2022-12-07T13:53:52.962009Z","iopub.execute_input":"2022-12-07T13:53:52.962307Z","iopub.status.idle":"2022-12-07T13:53:52.977425Z","shell.execute_reply.started":"2022-12-07T13:53:52.962280Z","shell.execute_reply":"2022-12-07T13:53:52.976335Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Table with percentage of horse winning the races","metadata":{}},{"cell_type":"code","source":"win_percentage = pd.DataFrame()\nwin_percentage[\"winning_number_freq\"] =first_horses_df_freq\nwin_percentage[\"win_percenatge\"] = (first_horses_df_freq/total_horses)*100\nwin_percentage","metadata":{"execution":{"iopub.status.busy":"2022-12-07T13:53:52.979243Z","iopub.execute_input":"2022-12-07T13:53:52.979717Z","iopub.status.idle":"2022-12-07T13:53:52.996429Z","shell.execute_reply.started":"2022-12-07T13:53:52.979610Z","shell.execute_reply":"2022-12-07T13:53:52.995734Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(16,7))\n#plt.xticks(\"Horse program number\")\n#plt.yticks(\"winning percentage\")\n\n#plt.plot(win_percentage.index,win_percentage.win_percenatge)\n#plt.hist(win_percentage.index,win_percentage.win_percenatge)\nplt.plot(win_percentage.index,win_percentage.win_percenatge)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-12-07T13:53:52.997540Z","iopub.execute_input":"2022-12-07T13:53:52.997842Z","iopub.status.idle":"2022-12-07T13:53:53.229706Z","shell.execute_reply.started":"2022-12-07T13:53:52.997817Z","shell.execute_reply":"2022-12-07T13:53:53.228764Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## We have done the analysis of derby data","metadata":{}},{"cell_type":"markdown","source":"# **Thank you**","metadata":{}}]}