{"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":"<div align=\"center\"><p style=\"font-family: 'Mochiy Pop P One';font-size:32px;color:teal; font-weight:bold\">Horse racing - Where (tracks and course) and When (date and time) </p></div>\n\n<font size=4><u><mark>In this notebook, I rewrapped the visualizations of notebook <a href=https://www.kaggle.com/code/xxxxyyyy80008/derby-2022-eda>[Derby 2022 - EDA]</a>. <mark></u></font>\n\n\n<div align=\"center\"><p style=\"font-family: 'Mochiy Pop P One';font-size:26px;color:teal; font-weight:bold\">Outline of this analysis </p></div>\n    \n<a id=\"toc\"></a>\n\n\n- [1. Tracks and courses](#1)\n    - 1.1 How many races took place on each track\n    - 1.2 How many races in a day\n    - 1.3 How many horses in a race\n    \n    \n- [2. Racing course types - How do they differ from each other](#2)\n    - 2.1 The shapes of different courses and distances\n\n    \n\n\n- [3. Dates and time: When did the races take place](#3)\n    - 3.1 Which months in the year and which days in the week\n    - 3.2 When during the day races took place\n    - 3.2 When the 1st race took place\n\n\n    \n","metadata":{}},{"cell_type":"code","source":"import numpy as np \nimport pandas as pd \nimport matplotlib.pyplot as plt\nimport matplotlib.lines as mlines\nimport pandas as pd\nimport statsmodels.api as sm\nimport seaborn as sns\n\nimport pyarrow.parquet as pq\nimport pyarrow as pa","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-10-15T08:25:45.606153Z","iopub.execute_input":"2022-10-15T08:25:45.607324Z","iopub.status.idle":"2022-10-15T08:25:47.054097Z","shell.execute_reply.started":"2022-10-15T08:25:45.606893Z","shell.execute_reply":"2022-10-15T08:25:47.052726Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#/kaggle/input/derby-2022-merge-and-convert-data-to-parquet-file/nyra_2019_complete.parquet\n\nrace_table = pd.read_csv('/kaggle/input/big-data-derby-2022/nyra_race_table.csv')\nstart_table = pd.read_csv('/kaggle/input/big-data-derby-2022/nyra_start_table.csv', header=None)\n# tracking_table = pd.read_csv('/kaggle/input/big-data-derby-2022/nyra_tracking_table.csv')\nstart_table.columns=['track_id', 'race_date', 'race_number', 'program_number', 'weight_carried', 'jockey', 'odds', 'position_at_finish']","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-09-28T05:31:53.811556Z","iopub.execute_input":"2022-09-28T05:31:53.811994Z","iopub.status.idle":"2022-09-28T05:31:53.841610Z","shell.execute_reply.started":"2022-09-28T05:31:53.811957Z","shell.execute_reply":"2022-09-28T05:31:53.840753Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"race_type_dict =   {  'STK': 'Stakes', \n                      'WCL': 'Waiver Claiming', \n                      'WMC': 'Waiver Maiden Claiming', \n                      'SST': 'Starter Stakes', \n                      'SHP': 'Starter Handicap',\n                      'CLM': 'Claiming', \n                      'STR': 'Starter Allowance', \n                      'AOC': 'Allowance Optionl Claimer', \n                      'SOC': 'Starter Optional Claimer', \n                      'MCL': 'Maiden Claiming', \n                      'ALW': 'Allowance', \n                      'MSW': 'Maiden Special Weight'\n                      }\n\ntrack_id_dict = dict(AQU = 'Aqueduct', BEL = 'Belmont', SAR = 'Saratoga')\ncourse_type_dict = dict(M = 'Hurdle', D = 'Dirt', O = 'Outer turf', I = 'Inner turf', T = 'turf')\ntrack_condition_dict =  dict(YL = 'Yielding', FM ='Firm', SY = 'Sloppy', GD = 'Good', FT = 'Fast', MY = 'Muddy', SF = 'Soft')\n\nrace_table['race_type'] = race_table['race_type'].map(race_type_dict)\nrace_table['track_id'] = race_table['track_id'].map(track_id_dict)\nrace_table['course_type'] = race_table['course_type'].map(course_type_dict)\n\nrace_table['track_condition']=race_table['track_condition'].apply(lambda x:x.replace(\" \", ''))\nrace_table['track_condition'] = race_table['track_condition'].map(track_condition_dict)\n\n\nstart_table['track_id'] = start_table['track_id'].map(track_id_dict)\n\nprint(start_table.shape, race_table.shape)\nstart_table = start_table.merge(race_table, on=['track_id','race_date', 'race_number'], how='inner')\nprint(start_table.shape, race_table.shape)","metadata":{"_kg_hide-input":true,"_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-09-28T05:31:54.851659Z","iopub.execute_input":"2022-09-28T05:31:54.852253Z","iopub.status.idle":"2022-09-28T05:31:54.889725Z","shell.execute_reply.started":"2022-09-28T05:31:54.852219Z","shell.execute_reply":"2022-09-28T05:31:54.888445Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"\n\n<a id=\"1\"></a>\n\n<div align=\"center\"><p style=\"font-family: 'Mochiy Pop P One';font-size:26px;color:teal; font-weight: bold\">1. Tracks and courses </p></div>\n\n\n\n[back to top](#toc)\n\n","metadata":{}},{"cell_type":"code","source":"#https://www.kaggle.com/code/gaetanlopez/how-to-make-clean-visualizations\nfig=plt.figure(figsize=(5,2),facecolor='white')\n\nax0=fig.add_subplot(1,1,1)\nax0.text(1.1,1,\"Derby 2022 - NYRA/NYTHA Horse Racing 2019\",color='black',fontsize=22, fontweight='bold', fontfamily='monospace',ha='center')\n\nax0.text(0,0.4,race_table['track_id'].nunique(),color='teal',fontsize=25, fontweight='bold', fontfamily='monospace',ha='center')\nax0.text(0,0.001,\"Tracks\",color='dimgrey',fontsize=17, fontweight='light', fontfamily='monospace',ha='center')\n\nax0.text(0.75,0.4,race_table['race_date'].nunique(),color='teal',fontsize=25, fontweight='bold', fontfamily='monospace',ha='center')\nax0.text(0.75,0.001,\"Race dates\",color='dimgrey',fontsize=17, fontweight='light', fontfamily='monospace',ha='center')\n\nax0.text(1.5,0.4,race_table.shape[0],color='teal',fontsize=25, fontweight='bold', fontfamily='monospace',ha='center')\nax0.text(1.5,0.001,\"Races\",color='dimgrey',fontsize=17, fontweight='light', fontfamily='monospace',ha='center')\n\nax0.text(2.25,0.4,start_table['jockey'].nunique(),color='teal',fontsize=25, fontweight='bold', fontfamily='monospace',ha='center')\nax0.text(2.25,0.001,\"Jockeys\",color='dimgrey',fontsize=17, fontweight='light', fontfamily='monospace',ha='center')\n\nax0.set_yticklabels('')\nax0.tick_params(axis='y',length=0)\nax0.tick_params(axis='x',length=0)\nax0.set_xticklabels('')\n\nfor direction in ['top','right','left','bottom']:\n    ax0.spines[direction].set_visible(False)","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-09-28T05:31:57.184574Z","iopub.execute_input":"2022-09-28T05:31:57.184977Z","iopub.status.idle":"2022-09-28T05:31:57.339544Z","shell.execute_reply.started":"2022-09-28T05:31:57.184929Z","shell.execute_reply":"2022-09-28T05:31:57.338407Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#https://www.kaggle.com/code/gaetanlopez/how-to-make-clean-visualizations\ncs = race_table['track_id'].value_counts().sort_values(ascending=False)\ncs1 = race_table['course_type'].value_counts().sort_values(ascending=False)[:-1]\n\nfig=plt.figure(figsize=(15,7),facecolor='white')\ngs=fig.add_gridspec(1,2)\n\nax0=fig.add_subplot(gs[0,0])\nax1=fig.add_subplot(gs[0,1])\n\n#colormap1=['firebrick','LightCoral','Salmon','DarkSalmon','mistyrose']\n#colormap2=['mistyrose','LightCoral','firebrick','Salmon','DarkSalmon','LightSalmon']\n\ncolormap1 = ['lightseagreen', 'salmon', 'lightskyblue', 'mediumseagreen', 'sandybrown', 'lightslategray', 'wheat', 'crimson']\ncolormap2 = ['turquoise', 'tomato', 'skyblue', 'wheat', 'peru', 'slategray', 'tan',  'firebrick']\n\nax0.text(-0,1.45,\"% of races on each track\",fontsize=18,fontweight='bold', fontfamily='monospace',ha='center')\nax0.text(-0,1.3,\"41% races were raced on Aqueduct\",fontsize=15,fontweight='light', fontfamily='monospace',ha='center')\nax1.text(-0,1.45,\"% of races by course type\",fontsize=18,fontweight='bold', fontfamily='monospace',ha='center')\nax1.text(-0,1.3,\"more than two-thirds of races were on dirt track\",fontsize=15,fontweight='light', fontfamily='monospace',ha='center')\n\nax0.pie(cs.values, explode=[0.1,0.1,0.1],labels=cs.index.values, autopct='%1.1f%%',\n        shadow=True, startangle=90,colors=colormap1, )\n\nax1.pie(cs1.values, explode=[0.1,0.1,0, 0],labels=cs1.index.values, autopct='%1.1f%%',\n        shadow=True, startangle=90,colors=colormap2)\n\nax0.axis('equal')\nax1.axis('equal')\nplt.tight_layout()\nplt.show()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-09-28T05:31:58.659166Z","iopub.execute_input":"2022-09-28T05:31:58.659944Z","iopub.status.idle":"2022-09-28T05:31:58.988350Z","shell.execute_reply.started":"2022-09-28T05:31:58.659901Z","shell.execute_reply":"2022-09-28T05:31:58.987304Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#https://www.kaggle.com/code/joshuaswords/netflix-data-visualization\ndata = race_table['distance_id'].value_counts().sort_values(ascending=False)/20\ndata.index = data.index.astype(str)\ndata = data[data>0.1]\n# Plot\n\ncolor_map = ['lightseagreen' ]*5 + ['wheat']*(data.size - 5)\n\n\nfig, ax = plt.subplots(1,1, figsize=(12, 6))\nax.bar(data.index, data, width=0.5, \n       #edgecolor='darkgray',\n       linewidth=0.6,color=color_map)\n\n#annotations\nfor i in data.index:\n    ax.annotate(f\"{data[i]}\", \n                   xy=(i, data[i] + 1.250), \n                   va = 'center', ha='center',fontweight='light', )\n\n\n\n# Remove border from plot\n\nfor s in ['top', 'left', 'right']:\n    ax.spines[s].set_visible(False)\n    \n\nfig.text(0.09, 1, '% of races by distance (600 means 6 furlongs)', fontsize=15, fontweight='bold', fontfamily='monospace')\nfig.text(0.09, 0.95, 'The top 5 distances are highlighted, only display distances with more than 1% of races.', fontsize=13, fontweight='light', fontfamily='monospace')\n\nfig.text(1., 1.0, 'Insight', fontsize=15, fontweight='bold', fontfamily='monospace')\n\nfig.text(1., 0.45, '''\nThe distances of races ranged from 4.5 furlongs \nto 14 furlongs. \n\n6 to 8.5 furlongs were top 5 distances in terms\nof % of races in 2019. \n\nMost number of races - more than \none-fourth races - were in 6 furlongs (i.e 600).\n\n8 furlongs accounted for about one-fifth races. \n\nLess than 10% races in total were \nabove 10 furlongs.\n'''\n         , fontsize=13, fontweight='light', )\n\nax.grid(axis='y', linestyle='-', alpha=0.4)   \n\ngrid_y_ticks = np.arange(0, 35, 5) # y ticks, min, max, then step\nax.set_yticks(grid_y_ticks)\nax.set_axisbelow(True)\n\nplt.axhline(y = 0, color = 'black', linewidth = 1.3, alpha = .7)\n\nax.tick_params(axis='both', which='major', labelsize=12)\nax.grid(visible=True, which='major', axis='both', color='lightgray', linestyle='-', linewidth=0.2)\nax.spines[['top', 'right']].set_visible(False)\nax.spines[['bottom', 'left']].set_edgecolor('whitesmoke')\n\nimport matplotlib.lines as lines\nl1 = lines.Line2D([0.95, 0.95], [0, 0.95], transform=fig.transFigure, figure=fig,color='black',lw=0.2)\nfig.lines.extend([l1])\n\nax.tick_params(axis=u'both', which=u'both',length=0)\n\nplt.show()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-09-28T05:32:00.339748Z","iopub.execute_input":"2022-09-28T05:32:00.340130Z","iopub.status.idle":"2022-09-28T05:32:00.843488Z","shell.execute_reply.started":"2022-09-28T05:32:00.340100Z","shell.execute_reply":"2022-09-28T05:32:00.842197Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#https://www.kaggle.com/code/joshuaswords/netflix-data-visualization\ndata = race_table['race_type'].value_counts().sort_values()/20\ndata.index = data.index.astype(str)\ndata = data[data>0.1]\n# Plot\n\ncolor_map =  ['wheat']*(data.size - 3) + ['lightseagreen' ]*3\n\n\nfig, ax = plt.subplots(1,1, figsize=(12, 6))\nax.barh(y = data.index.values, width = data.values, height=0.8, color=color_map, alpha=0.8)\nax.bar_label(ax.containers[0], labels=data.values, label_type='edge', color='gray', fontsize=10, fontweight=\"bold\")\n\n    \n\nfig.text(0.0, 0.97, '% of races by race type', fontsize=18, fontweight='bold', fontfamily='monospace')\nfig.text(0.0, 0.915, 'The top 3 race types are highlighted', fontsize=15, fontweight='light', fontfamily='monospace')\n\nfig.text(0.95, 0.97, 'Insight', fontsize=18, fontweight='bold', fontfamily='monospace')\n\nfig.text(0.95, 0.4, '''\nClaiming races accounted for more than 23% races.  \nMaiden Special Weight races accounted for \naround 20% races.\n\nMaiden Claiming races are for horses that \nhave never won a race and are eligible to be claimed.\nThis type accounted for 16% races.\n\nStakes race is a horse race in which the prize \noffered is made up at least in part of money \n(such as entry fees) put up by the owners of the \nhorses entered. This type of races accounted for\nabout 13% races.\n'''\n         , fontsize=13, fontweight='light', )\n\nax.grid(axis='y', linestyle='-', alpha=0.4)   \nax.set_xticks([])\n\n\nax.tick_params(axis='both', which='major', labelsize=12)\nax.grid(visible=True, which='major', axis='both', color='lightgray', linestyle='-', linewidth=0.2)\nax.spines[['top', 'right']].set_visible(False)\nax.spines[['bottom', 'left']].set_edgecolor('whitesmoke')\n\nfor s in ['top', 'left', 'right']:\n    ax.spines[s].set_visible(False)\n    \n    \nimport matplotlib.lines as lines\nl1 = lines.Line2D([0.93, 0.93], [0, 0.93], transform=fig.transFigure, figure=fig,color='black',lw=0.2)\nfig.lines.extend([l1])\n\nax.tick_params(axis=u'both', which=u'both',length=0)\n\nplt.show()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-09-28T05:32:04.089480Z","iopub.execute_input":"2022-09-28T05:32:04.090617Z","iopub.status.idle":"2022-09-28T05:32:04.413608Z","shell.execute_reply.started":"2022-09-28T05:32:04.090577Z","shell.execute_reply":"2022-09-28T05:32:04.412810Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"datas = [\n    race_table[['race_date', 'race_number']].groupby('race_date').count()['race_number'].value_counts().sort_index(),  \n    start_table[['race_date', 'race_number', 'program_number']].groupby(['race_date', 'race_number',]).count()['program_number'].value_counts().sort_index(), \n]\ncaptions = [\n    'Num of race dates by number of races on the same day',\n    'Num of races by number of horse in a race'\n]\n","metadata":{"execution":{"iopub.status.busy":"2022-09-28T05:32:33.578978Z","iopub.execute_input":"2022-09-28T05:32:33.579365Z","iopub.status.idle":"2022-09-28T05:32:33.599482Z","shell.execute_reply.started":"2022-09-28T05:32:33.579321Z","shell.execute_reply":"2022-09-28T05:32:33.598396Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, axs = plt.subplots(1, 2, figsize=(18, 4.5))\nplt.style.use('seaborn-whitegrid')\n\nfig.text(0.115, 1.1, '''\nTypically there were 8 - 10 races in a race day and 5-10 racehorses in a race. \n''', \n         fontsize=15, fontweight='bold', fontfamily='monospace')\n\nfig.text(0.115, .8, '''\n\nThe days that had 5 or less races on the day are outliers.\nThe races that had less than 5 horses or more than 12 horses are outliers.\n\n\n\n''', \n         fontsize=14, )\n\n\n    \nfor i, data in enumerate(datas):\n    if data.index.dtype in ['int64', 'int32', 'float64', 'float32']:\n        data.index = data.index.astype(str)\n\n    axs[i].bar(data.index.values, data.values, color=\"lightseagreen\", width=.5, alpha=0.7)\n    axs[i].bar_label(axs[i].containers[0], labels=data.values, label_type='edge', color='teal', fontsize=10, fontweight=\"bold\")\n    axs[i].grid(visible=True, which='major', axis='both', color='lightgray', linestyle='-', linewidth=0.2)\n    axs[i].spines[['top', 'right']].set_visible(False)\n    axs[i].spines[['bottom', 'left']].set_edgecolor('whitesmoke')\n\n    #axs[i].set_ylabel(data.name)\n    axs[i].set_title(captions[i], fontsize=12, loc='left')\n\n\n#     plt.suptitle('')\n\nplt.subplots_adjust(left=None, bottom=None, right=None, top=None, wspace=0.3, hspace=None) # useful for adjusting space between subplots\nplt.show()    ","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-09-28T05:32:37.693668Z","iopub.execute_input":"2022-09-28T05:32:37.694073Z","iopub.status.idle":"2022-09-28T05:32:38.229144Z","shell.execute_reply.started":"2022-09-28T05:32:37.694042Z","shell.execute_reply":"2022-09-28T05:32:38.227957Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"2\"></a>\n\n\n<div align=\"center\"><p style=\"font-family: 'Mochiy Pop P One';font-size:26px;color:teal; font-weight: bold\">2. Racing course types - How do they differ from each other</p></div>\n\n\n\n[back to top](#toc)\n\n**The following graphs show the shapes for each type of course at each track.**\n","metadata":{}},{"cell_type":"code","source":"tracking_table = pd.read_csv('/kaggle/input/big-data-derby-2022/nyra_tracking_table.csv')\nrace_table = pd.read_csv('/kaggle/input/big-data-derby-2022/nyra_race_table.csv')\nrace_table['course_type'] = race_table['course_type'].map(course_type_dict)","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-09-28T05:32:53.403951Z","iopub.execute_input":"2022-09-28T05:32:53.404413Z","iopub.status.idle":"2022-09-28T05:32:57.712083Z","shell.execute_reply.started":"2022-09-28T05:32:53.404354Z","shell.execute_reply":"2022-09-28T05:32:57.711082Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\nprint(tracking_table.shape)\ntracking_table = tracking_table.merge(race_table, on = ['track_id','race_date','race_number'], how='inner')\nprint(tracking_table.shape)","metadata":{"_kg_hide-input":true,"_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-09-28T05:32:59.542208Z","iopub.execute_input":"2022-09-28T05:32:59.543424Z","iopub.status.idle":"2022-09-28T05:33:02.943574Z","shell.execute_reply.started":"2022-09-28T05:32:59.543379Z","shell.execute_reply":"2022-09-28T05:33:02.942267Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"colors = ['lightseagreen', 'salmon', 'lightskyblue', 'mediumseagreen', 'sandybrown', 'lightslategray', 'wheat', 'crimson']\nmcolors = ['teal', 'tomato', 'skyblue', 'mediumseagreen', 'peru', 'slategray', 'tan',  'firebrick']","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-09-28T05:33:02.945524Z","iopub.execute_input":"2022-09-28T05:33:02.945854Z","iopub.status.idle":"2022-09-28T05:33:02.950939Z","shell.execute_reply.started":"2022-09-28T05:33:02.945817Z","shell.execute_reply":"2022-09-28T05:33:02.949654Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#https://www.kaggle.com/code/iamleonie/big-data-derby-step-by-step-analysis\ncolor_dict = {'Dirt' : 'lightseagreen',\n             'Inner turf' : 'lightskyblue',\n             'Outer turf' : 'salmon',\n             'turf' : 'mediumseagreen',\n             'Hurdle' : 'wheat'}\n\nfig, ax = plt.subplots(1, 3, figsize=(18, 6.5))\ni = 0\nfor track_id in ['AQU', 'BEL', 'SAR']:\n    \n    sns.scatterplot(data = tracking_table[(tracking_table[\"track_id\"] == track_id) & (tracking_table[\"program_number\"] == '1  ')], \n                    x = \"longitude\", y = \"latitude\", \n                    hue = \"course_type\", \n                    marker='o',ax=ax[i],\n                   palette=color_dict, )\n\n\n    \n    ax[i].grid(visible=True, which='major', axis='both', color='lightgray', linestyle='-', linewidth=0.2)\n    ax[i].spines[['top', 'right']].set_visible(False)\n    ax[i].spines[['bottom', 'left']].set_edgecolor('whitesmoke')\n\n    ax[i].set_title(track_id_dict[track_id],  fontsize=14, loc='left')\n    \n    \n    \n    i = i + 1\n\nplt.tight_layout()\nplt.legend()\nplt.show()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-09-28T05:33:07.685595Z","iopub.execute_input":"2022-09-28T05:33:07.685988Z","iopub.status.idle":"2022-09-28T05:33:29.466783Z","shell.execute_reply.started":"2022-09-28T05:33:07.685956Z","shell.execute_reply":"2022-09-28T05:33:29.465397Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"3\"></a>\n\n\n<div align=\"center\"><p style=\"font-family: 'Mochiy Pop P One';font-size:26px;color:teal; font-weight: bold\">3. Dates and time: When did the races take place</p></div>\n\n\n\n[back to top](#toc)\n\n\n","metadata":{}},{"cell_type":"code","source":"def post_time_(x):\n    v = '12:00'\n    if (x is None)|(x is np.nan):\n        return v\n    else:\n        x = str(x)\n        x1_ = int(x[:-2])\n        x2_ = int(x[-2:])\n        if x1_<10:\n            x1_ = x1_ + 12\n        if x2_<22:\n            v = f'{x1_}:00'\n        elif x2_< 48: \n            v = f'{x1_}:30'\n        else: \n            v = f'{x1_+1}:00'\n    return v","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-09-28T05:33:29.469191Z","iopub.execute_input":"2022-09-28T05:33:29.470084Z","iopub.status.idle":"2022-09-28T05:33:29.477104Z","shell.execute_reply.started":"2022-09-28T05:33:29.470051Z","shell.execute_reply":"2022-09-28T05:33:29.475787Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"race_table['race_type'] = race_table['race_type'].map(race_type_dict)\nrace_table['track_id'] = race_table['track_id'].map(track_id_dict)\n\nrace_table['track_condition']=race_table['track_condition'].apply(lambda x:x.replace(\" \", ''))\nrace_table['track_condition'] = race_table['track_condition'].map(track_condition_dict)\n\n","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-09-28T05:33:29.478273Z","iopub.execute_input":"2022-09-28T05:33:29.478590Z","iopub.status.idle":"2022-09-28T05:33:29.494248Z","shell.execute_reply.started":"2022-09-28T05:33:29.478562Z","shell.execute_reply":"2022-09-28T05:33:29.493548Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"race_date = pd.to_datetime(race_table['race_date'])\n\n\n\nrd = pd.DataFrame(data={'weekday': race_date.dt.weekday, \n                        'month_name':race_date.dt.strftime(\"%b\"),\n                        'month':race_date.dt.month,\n                        'week': race_date.dt.isocalendar().week,\n                        'day_name': race_date.dt.day_name(),\n                        'race_number': race_table['race_number'], \n                        'post_time': race_table['post_time'].apply(post_time_),\n                        'track': race_table['track_id'] ,\n                       }\n                 )","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-09-28T05:33:29.497406Z","iopub.execute_input":"2022-09-28T05:33:29.498065Z","iopub.status.idle":"2022-09-28T05:33:29.529198Z","shell.execute_reply.started":"2022-09-28T05:33:29.498023Z","shell.execute_reply":"2022-09-28T05:33:29.528182Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cs = pd.pivot_table(rd, index=['track'], columns=['month'], values='day_name', aggfunc=np.size)\n\nmnth_map = rd[['month', 'month_name']].drop_duplicates().sort_values(by='month').set_index(keys='month').to_dict()['month_name']\n\ncs.columns = [mnth_map[a] for a in cs.columns]\n#cs.index = [day_map[a] for a in cs.index]\ncs.index.name=''\n\nfig, ax = plt.subplots(1, 1, figsize=(12, 5))\nsns.heatmap(cs, xticklabels=cs.columns, yticklabels=cs.index, cmap='RdYlGn', square=True, linewidth=2.5,cbar=False,\n            center=0, annot=True,  fmt='.0f', \n            ax=ax,annot_kws={\"fontsize\":12})#YlGnBu#RdYlGn\n\nax.spines['top'].set_visible(True)\n\n\nfig.text(.012, .915, 'Which months did races take place on each track?', fontweight='bold', fontfamily='monospace', fontsize=15,ha='left')   \nfig.text(0.012, 0.82, '''\nAqueduct: races took place from Nov to Apr; Belmont: races in May/Jun/Sep/Oct, and a few in Apr and and Jul. \nSaratoga: races only from Jul to Sep.''',ha='left', fontsize=13,) \n\nax.set_yticklabels(ax.get_yticklabels(), fontfamily='serif', rotation = 0, fontsize=11)\nax.set_xticklabels(ax.get_xticklabels(), fontfamily='serif', rotation=0, fontsize=11)\n\nax.set_ylabel('')    \nax.set_xlabel('')\nax.tick_params(axis=u'both', which=u'both',length=0)\nplt.tight_layout()\nplt.show()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-09-28T05:33:29.530516Z","iopub.execute_input":"2022-09-28T05:33:29.531200Z","iopub.status.idle":"2022-09-28T05:33:29.864716Z","shell.execute_reply.started":"2022-09-28T05:33:29.531168Z","shell.execute_reply":"2022-09-28T05:33:29.863623Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ncs = pd.pivot_table(rd, index=['weekday'], columns=['month'], values='day_name', aggfunc=np.size)\n\nmnth_map = rd[['month', 'month_name']].drop_duplicates().sort_values(by='month').set_index(keys='month').to_dict()['month_name']\nday_map = rd[['weekday', 'day_name']].drop_duplicates().sort_values(by='weekday').set_index(keys='weekday').to_dict()['day_name']\n\ncs.columns = [mnth_map[a] for a in cs.columns]\ncs.index = [day_map[a] for a in cs.index]\n\nfig, ax = plt.subplots(1, 1, figsize=(12, 10))\nsns.heatmap(cs, xticklabels=cs.columns, yticklabels=cs.index, cmap='RdYlGn', square=True, linewidth=2.5,cbar=False,\n            center=0, annot=True,  fmt='.0f', \n            ax=ax,annot_kws={\"fontsize\":12})#YlGnBu#RdYlGn\n\nax.spines['top'].set_visible(True)\n\n\nfig.text(.012, .965, 'When day in the week did races take place?', fontweight='bold', fontfamily='monospace', fontsize=15,ha='left')   \nfig.text(0.012, 0.8, '''\nThroughout the year, most horse races took place on Friday and the whole weekend. \nThere were a few races on Mondays and Tuesdays, and those days were holidays. \n\nFor example, the races on Tuesdays were at track `Aqueduct` on Jan 1st (New Year) and Dec 31st (Last day in the Year). \nFrom Jun to Nov, there were also races on Wednesdays.\n\n''',ha='left', fontsize=13,) \n\nax.set_yticklabels(ax.get_yticklabels(), fontfamily='serif', rotation = 0, fontsize=11)\nax.set_xticklabels(ax.get_xticklabels(), fontfamily='serif', rotation=0, fontsize=11)\n\nax.set_ylabel('')    \nax.set_xlabel('')\nax.tick_params(axis=u'both', which=u'both',length=0)\nplt.tight_layout()\nplt.show()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-09-28T05:33:29.866296Z","iopub.execute_input":"2022-09-28T05:33:29.866690Z","iopub.status.idle":"2022-09-28T05:33:30.478841Z","shell.execute_reply.started":"2022-09-28T05:33:29.866658Z","shell.execute_reply":"2022-09-28T05:33:30.477713Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cs = pd.pivot_table(rd, index=['weekday'], columns=['post_time'], values='day_name', aggfunc=np.size)\nday_map = rd[['weekday', 'day_name']].drop_duplicates().sort_values(by='weekday').set_index(keys='weekday').to_dict()['day_name']\ncs.columns.name=''\ncs.index = [day_map[a] for a in cs.index]\n\n\nfig, ax = plt.subplots(1, 1, figsize=(15, 7))\nsns.heatmap(cs, xticklabels=cs.columns, yticklabels=cs.index, cmap='RdYlGn', square=True, linewidth=2.5,cbar=False,\n            center=0, annot=True,  fmt='.0f', \n            ax=ax,annot_kws={\"fontsize\":12})#YlGnBu#RdYlGn\n\nax.spines['top'].set_visible(True)\n\n\nfig.text(.012, .965, 'When day in the week did races take place?', fontweight='bold', fontfamily='monospace', fontsize=15,ha='left')   \nfig.text(0.012, 0.86, '''\nThe majority of races took place between 1pm and 6pm in the afternoon; only few races started after 6PM. \n\n''',ha='left', fontsize=13,) \n\nax.set_yticklabels(ax.get_yticklabels(), fontfamily='serif', rotation = 0, fontsize=11)\nax.set_xticklabels(ax.get_xticklabels(), fontfamily='serif', rotation=0, fontsize=11)\n\nax.set_ylabel('')    \nax.set_xlabel('')\nax.tick_params(axis=u'both', which=u'both',length=0)\nplt.tight_layout()\nplt.show()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-09-28T05:33:30.480275Z","iopub.execute_input":"2022-09-28T05:33:30.480732Z","iopub.status.idle":"2022-09-28T05:33:31.220249Z","shell.execute_reply.started":"2022-09-28T05:33:30.480688Z","shell.execute_reply":"2022-09-28T05:33:31.219077Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cs = pd.pivot_table(rd[rd['race_number']==1], index=['weekday'], columns=['post_time'], values='day_name', aggfunc=np.size)\nday_map = rd[['weekday', 'day_name']].drop_duplicates().sort_values(by='weekday').set_index(keys='weekday').to_dict()['day_name']\ncs.columns.name=''\ncs.index = [day_map[a] for a in cs.index]\n\n\n\nfig, ax = plt.subplots(1, 1, figsize=(8, 4))\nsns.heatmap(cs, xticklabels=cs.columns, yticklabels=cs.index, cmap='RdYlGn', square=False, linewidth=2.5,cbar=False,\n            center=0, annot=True,  fmt='.0f', \n            ax=ax,annot_kws={\"fontsize\":12})#YlGnBu#RdYlGn\n\nax.spines['top'].set_visible(True)\n\n\nfig.text(.0125, .985, 'The 1st race typically started before 1:30PM', fontweight='bold', fontfamily='monospace', fontsize=15,ha='left')   \n\nax.set_yticklabels(ax.get_yticklabels(), fontfamily='serif', rotation = 0, fontsize=11)\nax.set_xticklabels(ax.get_xticklabels(), fontfamily='serif', rotation=0, fontsize=11)\n\nax.set_ylabel('')    \nax.set_xlabel('')\nax.tick_params(axis=u'both', which=u'both',length=0)\nplt.tight_layout()\nplt.show()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-09-28T05:33:31.221834Z","iopub.execute_input":"2022-09-28T05:33:31.222158Z","iopub.status.idle":"2022-09-28T05:33:31.552011Z","shell.execute_reply.started":"2022-09-28T05:33:31.222130Z","shell.execute_reply":"2022-09-28T05:33:31.550683Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"\n\n","metadata":{}},{"cell_type":"markdown","source":"\n### References\n\n- https://pandas.pydata.org/docs/user_guide/style.html\n- [Take Your Histograms to the Next Level Using Matplotlib](https://towardsdatascience.com/take-your-histograms-to-the-next-level-using-matplotlib-5f093ad7b9d3)\n- https://github.com/arseniyturin/Capstone-Project/blob/master/plotting.py\n- https://github.com/arseniyturin/Capstone-Project/blob/master/Part%203.%20Machine%20Learning.ipynb","metadata":{}}]}