{"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":"code","source":"import 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\nfrom datetime import datetime\nimport seaborn as sns\n\nimport math","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-11-10T17:06:29.092446Z","iopub.execute_input":"2022-11-10T17:06:29.092898Z","iopub.status.idle":"2022-11-10T17:06:29.099885Z","shell.execute_reply.started":"2022-11-10T17:06:29.092864Z","shell.execute_reply":"2022-11-10T17:06:29.098415Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<H1>Goal of the Competition 🐴</H1>\n<p>The goal of this competition is to analyze horse racing tactics, drafting strategies, and path efficiency. You will develop a model using never-before-released coordinate data along with basic race information.<br>\n\nYour work will help racing horse owners, trainers, and veterinarians better understand how equine performance and welfare fit together. With better data analysis, equine welfare could significantly improve.","metadata":{}},{"cell_type":"markdown","source":"<H2>Usefull Funtions</H2>","metadata":{}},{"cell_type":"code","source":"def MemoryOptimisation(DataFrame):\n    \n    InitialMemory = DataFrame.memory_usage().sum() / 1024 ** 2\n    print(f'Initial Memory Usage: \\033[1m{round(InitialMemory, 2)} MB\\033[0m')\n\n    SubTypeInt = ['uint8','uint16','uint32','uint64','int8','int16','int32','int64']\n    SubTypeFloat = ['float16','float32','float64']\n\n    for column in DataFrame.columns:\n\n        ColumnType = str(DataFrame[column].dtypes)\n        MaximumColumn = DataFrame[column].max()\n        MinimumColumn = DataFrame[column].min()\n\n        if 'object' in ColumnType:\n            NumberOfUnique = len(DataFrame[column].unique())\n            NumberOfTotal = len(DataFrame[column])\n            if NumberOfUnique / NumberOfTotal < 0.5:\n                DataFrame[column] = DataFrame[column].astype('category')\n        elif 'int' in ColumnType:\n            for element in SubTypeInt:\n                if MinimumColumn > np.iinfo(element).min and MaximumColumn < np.iinfo(element).max:\n                    DataFrame[column] = DataFrame[column].astype(element)\n                    break\n        elif 'float' in ColumnType:\n            for element in SubTypeFloat:\n                if MinimumColumn > np.finfo(element).min and MaximumColumn < np.finfo(element).max:\n                    DataFrame[column] = DataFrame[column].astype(element)\n\n    FinalMemory = DataFrame.memory_usage().sum() / 1024**2\n    print(f'Final Memory Usage after Optimisation: \\033[1m{round(FinalMemory, 2)} MB\\033[0m')\n    print(f'Compressed by: \\033[1m{round(100*(InitialMemory - FinalMemory) / InitialMemory, 2)} %\\033[0m')\n    return DataFrame\n\n\n# initial Check\n\ndef font_size(s, props=[]):\n    color = [\" \" for i in range(len(s))]\n    color[0] = props[-1]\n    color[-1] = props[-1]\n    color[3] = props[-1]     \n    return color\n\ndef initial_check(df):\n    Features = []\n    for i in df.columns:\n        Features.append([i,df[i].dtype, df[i].nunique(),\n                         \", \".join([str(i) for i in df[i].drop_duplicates().sort_values().values])])\n    display(pd.DataFrame(Features, columns = ['Features','Dtype', 'Unique Count', 'Unique Values']).style\n            .set_caption(\"Let's Check All Colums\")\n            .format(precision=0)\n            .hide_index()\n            .apply(font_size, props=[\"font-size: 18px; color:red; border: 2px solid red\",\n                                      \"\"\"display: block;width: 300px;\n                                          text-overflow: ellipsis;\n                                          word-wrap: break-word;\n                                          overflow: hidden;\n                                          max-height: 1.8em;\n                                          line-height: 1.8em;\"\"\" ], axis=1))\n","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-11-10T17:00:55.294170Z","iopub.execute_input":"2022-11-10T17:00:55.294639Z","iopub.status.idle":"2022-11-10T17:00:55.312825Z","shell.execute_reply.started":"2022-11-10T17:00:55.294604Z","shell.execute_reply":"2022-11-10T17:00:55.311210Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<H1> Table 1 - Horse/Jockey Race Data </H1>","metadata":{}},{"cell_type":"code","source":"start_table = ['track_id', 'race_date', 'race_number', 'program_number', 'weight_carried', 'jockey', 'odds', 'position_at_finish']\ndateparse = lambda x: datetime.strptime(x, '%Y-%m-%d')\ndf_start = pd.read_csv('../input/big-data-derby-2022/nyra_start_table.csv', names=start_table, parse_dates=['race_date'], date_parser=dateparse)\n# df_start.set_index('race_date', inplace=True)\nprint(f\"\\n\\033[1;30;34m Table have \\033[0;0m \\033[1m{df_start.shape[0]} rows \\033[1;30;34mand\\033[0;0m  \\033[1m{df_start.shape[1]} columns\\033[0m \")\ndisplay(df_start.head(2))","metadata":{"execution":{"iopub.status.busy":"2022-11-09T17:45:03.199832Z","iopub.execute_input":"2022-11-09T17:45:03.201336Z","iopub.status.idle":"2022-11-09T17:45:03.426420Z","shell.execute_reply.started":"2022-11-09T17:45:03.201261Z","shell.execute_reply":"2022-11-09T17:45:03.424748Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"initial_check(df_start)","metadata":{"execution":{"iopub.status.busy":"2022-11-09T17:45:07.963014Z","iopub.execute_input":"2022-11-09T17:45:07.963565Z","iopub.status.idle":"2022-11-09T17:45:07.997897Z","shell.execute_reply.started":"2022-11-09T17:45:07.963524Z","shell.execute_reply":"2022-11-09T17:45:07.996163Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"race = df_start.iloc[:, [0,1,2]].drop_duplicates().reset_index(drop=True)\nprint(f\"\\n\\033[1;30;34m Total # Races : \\033[0;0m \\033[1m{race.shape[0]} \\n\")\n\nfig = plt.figure(figsize = (13,3), dpi=100)\ngs = fig.add_gridspec(1,4)\ngs.update(wspace=0.35, hspace=0.47)\nax0 = fig.add_subplot(gs[0,0])\nax1 = fig.add_subplot(gs[0,1])\nax2 = fig.add_subplot(gs[0,2])\nax3 = fig.add_subplot(gs[0,3])\n\na = race.track_id.value_counts().plot(kind='pie', title='# Races by Tracks', autopct='%.0f%%', ax=ax0)\na.set_ylabel(\" \");\n\nc = race.race_date.dt.month.value_counts().sort_index().plot(kind='bar', title='# Races by Month', ax=ax1);\nc.set_xticklabels(['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'])\n\nd = race.race_date.dt.weekday.value_counts().sort_index().plot(kind='bar', title='# Races by Day', ax=ax2);\nd.set_xticklabels(['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', \"Sun\"])\n\ne = race.groupby('race_date').race_number.max().value_counts().sort_index().plot(kind='bar', title=\"Races per Day\", ax=ax3);","metadata":{"execution":{"iopub.status.busy":"2022-11-09T18:31:29.414708Z","iopub.execute_input":"2022-11-09T18:31:29.415173Z","iopub.status.idle":"2022-11-09T18:31:30.149065Z","shell.execute_reply.started":"2022-11-09T18:31:29.415139Z","shell.execute_reply":"2022-11-09T18:31:30.147646Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"★   **Total Races :-** `2000`\n\n★   **Racing Track Inference:-** It can be seen that the `825` races were ran at **`AQU - Aqueduct `**, and `772` on **` BEL - Belmont `** and `403` on **` SAR - Saratoga `** at last.\n\n★   **Race Date:-** The majority of the races are scheduled for the **` Fall `**, followed by the **` Summer`**, and **` Winter`**.\n\n★   **Race Day:-** Most of race scheduled on Weekends.\n\n★   **Race per Day:-** Most of time only `9 races` scheduled per day.","metadata":{"execution":{"iopub.status.busy":"2022-11-09T18:09:22.242626Z","iopub.execute_input":"2022-11-09T18:09:22.243096Z","iopub.status.idle":"2022-11-09T18:09:22.253089Z","shell.execute_reply.started":"2022-11-09T18:09:22.243033Z","shell.execute_reply":"2022-11-09T18:09:22.251937Z"}}},{"cell_type":"code","source":"df_start.groupby('jockey').weight_carried.mean().astype(int).value_counts().sort_index()\\\n.plot(kind='bar', title='Avg Weight of Jockey(since large part of weight carried is of jockey)' );","metadata":{"execution":{"iopub.status.busy":"2022-11-09T19:01:28.297146Z","iopub.execute_input":"2022-11-09T19:01:28.297566Z","iopub.status.idle":"2022-11-09T19:01:28.555187Z","shell.execute_reply.started":"2022-11-09T19:01:28.297532Z","shell.execute_reply":"2022-11-09T19:01:28.553985Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\na = sns.relplot(kind='scatter',\n            data=df_start.query('odds < 10000'),col_wrap = 5,\n            x='weight_carried', y='odds', hue='track_id',\n            col='position_at_finish');","metadata":{"execution":{"iopub.status.busy":"2022-11-09T19:07:31.385980Z","iopub.execute_input":"2022-11-09T19:07:31.387007Z","iopub.status.idle":"2022-11-09T19:07:36.685302Z","shell.execute_reply.started":"2022-11-09T19:07:31.386966Z","shell.execute_reply":"2022-11-09T19:07:36.684152Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**ODDS Never Help!!**","metadata":{}},{"cell_type":"markdown","source":"<H1> Table 2 - Racetrack Race Data</H1>","metadata":{}},{"cell_type":"code","source":"df_race = pd.read_csv('../input/big-data-derby-2022/nyra_race_table.csv', parse_dates=['race_date'])\nprint(f\"\\n\\033[1;30;34m Table have \\033[0;0m \\033[1m{df_race.shape[0]} rows \\033[1;30;34mand\\033[0;0m  \\033[1m{df_race.shape[1]} columns\\033[0m \")\ndisplay(df_race.head(2))","metadata":{"execution":{"iopub.status.busy":"2022-11-10T17:23:23.673478Z","iopub.execute_input":"2022-11-10T17:23:23.673871Z","iopub.status.idle":"2022-11-10T17:23:23.708494Z","shell.execute_reply.started":"2022-11-10T17:23:23.673839Z","shell.execute_reply":"2022-11-10T17:23:23.706631Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"initial_check(df_race)","metadata":{"execution":{"iopub.status.busy":"2022-11-10T17:01:01.059454Z","iopub.execute_input":"2022-11-10T17:01:01.059943Z","iopub.status.idle":"2022-11-10T17:01:01.149158Z","shell.execute_reply.started":"2022-11-10T17:01:01.059894Z","shell.execute_reply":"2022-11-10T17:01:01.147689Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<H5> SAR - Saratoga</H5>\n<p> Saratoga Race Course, situated in Saratoga Spring a city in New York, United States. Famous for thoroughbred horse racing.</p>\n\n<h5> AQU - Aqueduct</h5>\n<p>Aqueduct Racetrack is a Thoroughbred horse racing facility in Queens, New York City, United States.</p>\n\n<h5> BEL - Belmont</h5>\n<p>The Belmont Stakes is an American Grade I stakes race for three-year-old Thoroughbreds run at Belmont Park in Elmont, New York, United States.</p>\n\nAll three track are in New York, United States","metadata":{"execution":{"iopub.status.busy":"2022-11-10T18:17:27.432617Z","iopub.execute_input":"2022-11-10T18:17:27.433023Z","iopub.status.idle":"2022-11-10T18:17:27.440551Z","shell.execute_reply.started":"2022-11-10T18:17:27.432991Z","shell.execute_reply":"2022-11-10T18:17:27.439203Z"}}},{"cell_type":"code","source":"df_race['race_month'] = df_race.race_date.dt.month\ndf_race.groupby(['race_month', 'track_id']).size().unstack().plot(kind='bar', stacked=True, title='# Race by Month and Track');\n","metadata":{"execution":{"iopub.status.busy":"2022-11-10T19:04:13.307537Z","iopub.execute_input":"2022-11-10T19:04:13.308323Z","iopub.status.idle":"2022-11-10T19:04:13.663023Z","shell.execute_reply.started":"2022-11-10T19:04:13.308278Z","shell.execute_reply":"2022-11-10T19:04:13.661680Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Race track-** All winter matches held in Aqueduct. August, which register highest number of races, all matches held in Saratoga ","metadata":{}},{"cell_type":"code","source":"# distance_id\nprint('Distance is in furlongs')\nprint('A furlong is a measure of distance in imperial units and United States customary units equal to one\\\n eighth of a mile, equivalent to 660 feet, 220 yards, 40 rods, 10 chains or approximately 201 metres.')\n\ndf_race['distance']=((df_race.distance_id/100)*201//100*100).astype(int)\ndf_race.distance.value_counts().sort_index().plot(kind='bar', title='Distance(Meter) of the Race');","metadata":{"execution":{"iopub.status.busy":"2022-11-10T17:33:04.694655Z","iopub.execute_input":"2022-11-10T17:33:04.696253Z","iopub.status.idle":"2022-11-10T17:33:05.013386Z","shell.execute_reply.started":"2022-11-10T17:33:04.696192Z","shell.execute_reply":"2022-11-10T17:33:05.011900Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"★   **Distance of the race:-** More than `500+ races` are of distance `1200 meters`.","metadata":{}},{"cell_type":"code","source":"df_race['course_type']=df_race.course_type.replace({'M': 'Hurdle', 'D' :'Dirt', 'O' : 'Outer Turf', 'I' : 'Inner Turf', 'T' : 'Turf'})\n\n\nfig = plt.figure(figsize = (13,8), dpi=100)\ngs = fig.add_gridspec(2,2)\ngs.update(wspace=0.35, hspace=0.6)\nax0 = fig.add_subplot(gs[0,0])\nax1 = fig.add_subplot(gs[0,1])\nax2 = fig.add_subplot(gs[1,0])\n\n\na = df_race.groupby(['course_type', 'track_id']).size().unstack().plot(kind='bar', stacked=True, title='# Race by Course Type', ax=ax0);\na.set_xlabel('')\n\ndf_race.groupby(['race_month', 'course_type']).size().unstack().plot(kind='bar', stacked=True, title='# Race by Month and Course type', ax=ax1);\n\ndf_race.groupby(['distance','course_type']).size().unstack().plot(kind='bar', stacked=True, ax=ax2)","metadata":{"execution":{"iopub.status.busy":"2022-11-10T18:59:28.110005Z","iopub.execute_input":"2022-11-10T18:59:28.110530Z","iopub.status.idle":"2022-11-10T18:59:29.408806Z","shell.execute_reply.started":"2022-11-10T18:59:28.110490Z","shell.execute_reply":"2022-11-10T18:59:29.407276Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"★   **Course Type :-** More than `70%` of Race are in `Dirt`.\n\n★   Turf is simply Grass. Since grass is a living and growing plant, turf racing is seasonal. In New York, for example, turf racing isn’t conducted during the winter. ","metadata":{}},{"cell_type":"code","source":"a = sns.relplot(kind='scatter', height=2.5,\n            data=df_race,col_wrap = 3,\n            x='distance_id', y='purse',\n            col='course_type'\n               );","metadata":{"execution":{"iopub.status.busy":"2022-11-10T17:00:58.752126Z","iopub.execute_input":"2022-11-10T17:00:58.752572Z","iopub.status.idle":"2022-11-10T17:00:59.823085Z","shell.execute_reply.started":"2022-11-10T17:00:58.752534Z","shell.execute_reply":"2022-11-10T17:00:59.822127Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<H1>To Be Continued....","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}