{"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":"#  <center> Horse racing - Where (tracks and course) and When (date and time) </center>\n\n**Note: I rewrapped the visualizations in this notebook here [Derby 2022 - EDA - Take2](https://www.kaggle.com/code/xxxxyyyy80008/derby-2022-eda-take2)**\n\n## Outline of this analysis\n\n\n<a id=\"toc\"></a>\n\n- [1. Undertand Tracks and course types through bar charts and histgrams](#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-09-29T16:16:32.852687Z","iopub.execute_input":"2022-09-29T16:16:32.853207Z","iopub.status.idle":"2022-09-29T16:16:34.636270Z","shell.execute_reply.started":"2022-09-29T16:16:32.853103Z","shell.execute_reply":"2022-09-29T16:16:34.635172Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#['PiYG', 'PRGn', 'BrBG', 'PuOr', 'RdGy', 'RdBu', 'RdYlBu','RdYlGn', 'Spectral', 'coolwarm', 'bwr', 'seismic']\n#['Pastel1', 'Pastel2', 'Paired', 'Accent', 'Dark2','Set1', 'Set2', 'Set3', 'tab10', 'tab20', 'tab20b', 'tab20c']\n# ['Greys', 'Purples', 'Blues', 'Greens', 'Oranges', 'Reds','YlOrBr', 'YlOrRd', 'OrRd', 'PuRd', 'RdPu', 'BuPu','GnBu', 'PuBu', 'YlGnBu', 'PuBuGn', 'BuGn', 'YlGn']\ncaption = ''\ndef highlight_max(styler):\n    #styler.set_caption(\"Number of races per course type and distance\")\n    styler.format(precision=0, na_rep='', thousands=\", \",)\n    #styler.background_gradient(axis=None, vmin=1, vmax=5, cmap=\"Blues\")#.background_gradient(cmap='Blues')  YlGnBu\n    styler.highlight_max(props='color:white;background-color:dodgerblue', axis=0)\n    styler.highlight_max(props='color:white;background-color:tomato;', axis=1)\n    styler.highlight_max(props='color:white; font-weight:bold; background-color:purple;', axis=None)\n    styler.set_caption(caption)\n    return styler\n\n\ndef highlight_min(styler):\n    #styler.set_caption(\"Number of races per course type and distance\")\n    styler.format(precision=0, na_rep='', thousands=\", \",)\n    #styler.background_gradient(axis=None, vmin=1, vmax=5, cmap=\"Blues\")#.background_gradient(cmap='Blues')  YlGnBu\n    styler.highlight_min(props='color:white;background-color:dodgerblue', axis=0)\n    styler.highlight_min(props='color:white;background-color:tomato;', axis=1)\n    styler.highlight_min(props='color:white; font-weight:bold; background-color:purple;', axis=None)\n    styler.set_caption(caption)\n    return styler\n","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-09-29T16:16:34.638412Z","iopub.execute_input":"2022-09-29T16:16:34.638741Z","iopub.status.idle":"2022-09-29T16:16:34.647641Z","shell.execute_reply.started":"2022-09-29T16:16:34.638711Z","shell.execute_reply":"2022-09-29T16:16:34.646246Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#https://github.com/arseniyturin/Capstone-Project/blob/master/plotting.py\n\n#plt.rcParams['figure.dpi'] = 227 #The default dpi in matplotlib is 100.\n\ndef hist(data, name, bins=50):\n    #plt.rcParams['figure.dpi'] = 227\n    plt.figure(figsize=(6,4))\n    plt.style.use('seaborn-whitegrid')\n    plt.hist(data, bins=bins, facecolor='#4ac2fb')\n    plt.title(name, fontsize=9)\n    plt.axvline(np.array(data.mean()), color='tomato', ls='--', lw=2)\n    plt.grid(visible=True, which='major', axis='both', color='lightgray', linestyle='--', linewidth=0.5)\n    plt.xlabel(data.name, fontsize=10)\n    plt.ylabel('count', fontsize=10)\n    plt.show()   \ndef qqplot(data):\n    #plt.rcParams['figure.dpi'] = 227\n    plt.figure(figsize=(16,6))\n    plt.style.use('seaborn-whitegrid')\n    sm.qqplot(data.dropna(), line='s', scale=1)\n    plt.title('Check for Normality', fontsize=16)\n    plt.show()\n    \n#bar chart: last 2 rows with different color\ndef bar(data, caption, figsize=(12, 6)):\n    if data.index.dtype in ['int64', 'int32', 'float64', 'float32']:\n        data.index = data.index.astype(str)\n\n    plt.figure(figsize=figsize)\n    plt.style.use('seaborn-whitegrid')\n    \n    if data.size>3:\n        colors = ['lightsalmon']*2 + ['lightseagreen']*(data.size-2)\n        colors2 = ['coral']*2 + ['teal']*(data.size-2)\n    else:\n        colors = ['wheat']*1 + ['lightseagreen']*(data.size-1) \n        colors2 = ['tan']*1 + ['teal']*(data.size-1)\n    \n    plt.barh(data.index.values, data.values, color=colors, alpha=0.8)\n\n    for i, v in enumerate(data.values):\n        plt.text(v + 3.9, i + .025, str(v), fontsize=10, color=colors2[i], fontweight='bold')#deepskyblue#dodgerblue   \n    plt.title(caption, fontsize=13, loc='left')\n    #plt.xlabel(data.name, fontsize=13)\n    plt.xticks([])\n    plt.grid(visible=True, which='major', axis='both', color='lightgray', linestyle='-', linewidth=0.2)\n    plt.gca().spines[['top', 'right']].set_visible(False)\n    plt.gca().spines[['bottom', 'left']].set_edgecolor('whitesmoke')\n    plt.rc('ytick', labelsize=10, ) \n    \n    plt.show() \n    \ndef bar(data, caption, figsize=(12, 6)):\n    if data.index.dtype in ['int64', 'int32', 'float64', 'float32']:\n        data.index = data.index.astype(str)\n\n    plt.figure(figsize=figsize)\n    plt.style.use('seaborn-whitegrid')\n    plt.barh(data.index.values, data.values, color=\"lightseagreen\", alpha=0.7)\n    for i, v in enumerate(data.values):\n        plt.text(v + 3.9, i + .025, str(v), fontsize=10, color=\"teal\", fontweight='bold')#deepskyblue#dodgerblue\n    plt.title(caption, fontsize=13, loc='left')\n    #plt.xlabel(data.name, fontsize=13)\n    plt.xticks([])\n    plt.grid(visible=True, which='major', axis='both', color='lightgray', linestyle='-', linewidth=0.2)\n    plt.gca().spines[['top', 'right']].set_visible(False)\n    plt.gca().spines[['bottom', 'left']].set_edgecolor('whitesmoke')\n    plt.rc('ytick', labelsize=10, ) \n    \n    plt.show()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-09-29T16:16:34.649627Z","iopub.execute_input":"2022-09-29T16:16:34.650177Z","iopub.status.idle":"2022-09-29T16:16:34.672483Z","shell.execute_reply.started":"2022-09-29T16:16:34.650120Z","shell.execute_reply":"2022-09-29T16:16:34.671471Z"},"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-29T16:16:34.674755Z","iopub.execute_input":"2022-09-29T16:16:34.675764Z","iopub.status.idle":"2022-09-29T16:16:34.735235Z","shell.execute_reply.started":"2022-09-29T16:16:34.675728Z","shell.execute_reply":"2022-09-29T16:16:34.734014Z"},"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-29T16:16:34.736837Z","iopub.execute_input":"2022-09-29T16:16:34.737283Z","iopub.status.idle":"2022-09-29T16:16:34.788222Z","shell.execute_reply.started":"2022-09-29T16:16:34.737249Z","shell.execute_reply":"2022-09-29T16:16:34.787253Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def stacked_barh(data, figsize=(15, 14), color_set='b', \n                 sum_col='sum', caption='', sub_title='',\n                 labels=[]):\n    fig, ax = plt.subplots(figsize = figsize)\n    plt.style.use('seaborn-whitegrid')\n    \n    if color_set=='b': #blue\n        colors = ['dodgerblue', 'deepskyblue', 'lightskyblue', 'lightcyan',  'aliceblue','royalblue']\n    elif color_set=='g': #green\n        colors = ['teal', 'lightseagreen', 'powderblue', 'lightcyan',  'azure','darkslategray']\n    \n    \n    for i in range(data.shape[1]-1): \n        if i>1:\n            fc = 'dimgray'\n        else:\n            fc = 'white'\n        if i<0:\n            ax.barh(y = data.index.values, width = data.iloc[:, i].values, height=0.8, color=colors[i], label=labels[i])\n        else:\n            ax.barh(y = data.index.values, width = data.iloc[:, i].values, height=0.8, left=data.iloc[:, :(i)].sum(axis=1).values, color=colors[i], label=labels[i])\n        ax.bar_label(ax.containers[i], label_type='center', color=fc, fontsize=10, fontweight=\"bold\")\n        \n        \n    ax.bar_label(ax.containers[i], labels=data[sum_col].map('{:.0f}'.format), label_type='edge', color=colors[-1], fontsize=10, fontweight=\"bold\")\n\n\n    ax.set_ylabel('')\n    ax.set_title(caption, fontsize=10, loc='left')\n    ax.legend()\n\n\n    plt.xlabel('', fontsize=13)\n    plt.xticks([])\n    plt.grid(visible=True, which='major', axis='both', color='lightgray', linestyle='-', linewidth=0.2)\n    plt.gca().spines[['top', 'right']].set_visible(False)\n    plt.gca().spines[['bottom', 'left']].set_edgecolor('whitesmoke')\n    #plt.rc('ytick', labelsize=13, ) \n    plt.suptitle(caption,fontsize=16, y=0.89, x=0.3)\n    plt.title(sub_title,fontsize=12, y=0.96, loc='left')\n    plt.show() ","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-09-29T16:16:34.789377Z","iopub.execute_input":"2022-09-29T16:16:34.789672Z","iopub.status.idle":"2022-09-29T16:16:34.804856Z","shell.execute_reply.started":"2022-09-29T16:16:34.789645Z","shell.execute_reply":"2022-09-29T16:16:34.803445Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def multi_bar(datas, captions, figsize=(13, 4), sharey=False):\n    ncols = len(datas)\n    fig, axs = plt.subplots(1, ncols, figsize=figsize, sharey=sharey)\n    plt.style.use('seaborn-whitegrid')\n    \n    for 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=14, loc='left')\n\n    \n#     plt.suptitle('')\n    plt.show()    \n","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-09-29T16:16:34.806198Z","iopub.execute_input":"2022-09-29T16:16:34.806570Z","iopub.status.idle":"2022-09-29T16:16:34.820391Z","shell.execute_reply.started":"2022-09-29T16:16:34.806540Z","shell.execute_reply":"2022-09-29T16:16:34.819269Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def multi_barh(datas, captions, figsize=(13, 4), sharey=False):\n    ncols = len(datas)\n    fig, axs = plt.subplots(1, ncols, figsize=figsize, sharey=sharey)\n    plt.style.use('seaborn-whitegrid')\n    \n    for i, data in enumerate(datas):\n        if data.index.dtype in ['int64', 'int32', 'float64', 'float32']:\n            data.index = data.index.astype(str)\n            \n       \n        axs[i].barh(y = data.index.values, width = data.values, height=0.8, color='lightseagreen', alpha=0.8)\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=13, loc='left')\n\n    plt.show() ","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-09-29T16:16:34.822167Z","iopub.execute_input":"2022-09-29T16:16:34.822513Z","iopub.status.idle":"2022-09-29T16:16:34.848001Z","shell.execute_reply.started":"2022-09-29T16:16:34.822466Z","shell.execute_reply":"2022-09-29T16:16:34.846972Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def multi_hist(datas, captions, bins=None, figsize=(13, 4), sharey=False):\n    ncols = len(datas)\n    fig, axs = plt.subplots(1, ncols, figsize=figsize, sharey=sharey)\n    plt.style.use('seaborn-whitegrid')\n    \n    if bins is None:\n        bins=[50]*len(datas)\n    \n    for 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.8)\n        axs[i].hist(data, bins=bins[i], facecolor='lightseagreen', alpha=0.7)\n        axs[i].axvline(np.array(data.mean()), color='tomato', ls='--', lw=2)\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=13, loc='left')\n\n    \n#     plt.suptitle('')\n    plt.show()    \n","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-09-29T16:16:34.849619Z","iopub.execute_input":"2022-09-29T16:16:34.850310Z","iopub.status.idle":"2022-09-29T16:16:34.864251Z","shell.execute_reply.started":"2022-09-29T16:16:34.850275Z","shell.execute_reply":"2022-09-29T16:16:34.863152Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"1\"></a>\n\n# <center>Undertand Tracks and course types through bar charts and histgrams</center>\n\n[back to top](#toc)\n\nThere are 3 tracks where races took place, and they are Aqueduct (AQU), Belmont (BEL), and Saratoga (SAR).  Most races took place at Aqueduct (AQU).\n\n\nThere are 3 types of courses: Hurdle, Dirt, and Turf (Outer turf, Inner turf, turf). Among the 2000 races in the dataset, about two thirds of races were on Dirt course, and the remaining one third were on Turf courses. Only a few races were Hurdle races.\n\n\nThe distances of races ranged from 4.5 furlongs to 14 furlongs. Most races were between 5.5 and 9 furlongs, and there were about 20 races longer than 11 furlongs.\n\n\nThe condition of the course the race was run on depended on the course type (Dirt or Turf) and the weather before the race. Heavy rains can make the track soft while sunny days can make the track firm. The level of firmness/softness of the tracks are:  YL - Yielding, FM - Firm, SY - Sloppy, GD - Good, FT - Fast, MY - Muddy, SF - Soft.\n\n\nThe types (classification) of the races are: 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\n\nTypically there were 8 - 10 races in a race day and 5-10 racehorses in a race. The days that had 5 or less races on the day are outliers.\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-29T16:16:34.868367Z","iopub.execute_input":"2022-09-29T16:16:34.869146Z","iopub.status.idle":"2022-09-29T16:16:35.112061Z","shell.execute_reply.started":"2022-09-29T16:16:34.869110Z","shell.execute_reply":"2022-09-29T16:16:35.110850Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cs = race_table['track_id'].value_counts().sort_values()\nbar(cs, 'Number of races for each track', figsize=(6, 2))","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-09-29T16:16:35.113719Z","iopub.execute_input":"2022-09-29T16:16:35.114172Z","iopub.status.idle":"2022-09-29T16:16:35.250403Z","shell.execute_reply.started":"2022-09-29T16:16:35.114123Z","shell.execute_reply":"2022-09-29T16:16:35.248555Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"datas = [\n    race_table['course_type'].value_counts().sort_index(),  \n    race_table['track_condition'].value_counts().sort_index(), \n]\ncaptions = [\n    'Number of races of course type',\n    'Number of races by condictions of the courses'\n]\n\nmulti_bar(datas, captions=captions, figsize=(14, 5))","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-09-29T16:16:35.252589Z","iopub.execute_input":"2022-09-29T16:16:35.253071Z","iopub.status.idle":"2022-09-29T16:16:35.659345Z","shell.execute_reply.started":"2022-09-29T16:16:35.253025Z","shell.execute_reply":"2022-09-29T16:16:35.658197Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"datas = [\n    race_table['race_type'].value_counts().sort_values(), \n    race_table['distance_id'].value_counts().sort_values(),  \n]\ncaptions = [\n    'Number of races by classification of the race',\n    'Number of races by distance (600 means 6 furlongs)',\n]\n\nmulti_barh(datas, captions=captions, figsize=(20, 8))","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-09-29T16:16:35.661195Z","iopub.execute_input":"2022-09-29T16:16:35.661657Z","iopub.status.idle":"2022-09-29T16:16:36.315408Z","shell.execute_reply.started":"2022-09-29T16:16:35.661608Z","shell.execute_reply":"2022-09-29T16:16:36.314250Z"},"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\n\nmulti_bar(datas, captions=captions, figsize=(14, 5))","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-09-29T16:16:36.316476Z","iopub.execute_input":"2022-09-29T16:16:36.316833Z","iopub.status.idle":"2022-09-29T16:16:36.930711Z","shell.execute_reply.started":"2022-09-29T16:16:36.316802Z","shell.execute_reply":"2022-09-29T16:16:36.929342Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"datas = [\n    race_table[['race_date', 'race_number']].groupby('race_date').count()['race_number'],  \n    start_table[['race_date', 'race_number', 'program_number']].groupby(['race_date', 'race_number',]).count()['program_number'], \n]\ncaptions = [\n    'Num of races by race date',\n    'Num of horse in a race'\n]\n\nmulti_hist(datas, captions=captions, figsize=(14, 5))","metadata":{"_kg_hide-input":true,"_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-09-29T16:16:36.932544Z","iopub.execute_input":"2022-09-29T16:16:36.933005Z","iopub.status.idle":"2022-09-29T16:16:37.704631Z","shell.execute_reply.started":"2022-09-29T16:16:36.932970Z","shell.execute_reply":"2022-09-29T16:16:37.703500Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"datas = [\n    race_table[race_table['course_type']=='Dirt']['track_condition'].value_counts().sort_index(),  \n    race_table[~race_table['course_type'].isin(['Dirt', 'Hurdle'])]['track_condition'].value_counts().sort_index(), \n]\ncaptions = [\n    'Number of races by track conditions on Dirt track',\n    'Number of races by track conditions on Turf track'\n]\n\nmulti_bar(datas, captions=captions, figsize=(14, 5), sharey=True)","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-09-29T16:16:37.706096Z","iopub.execute_input":"2022-09-29T16:16:37.707181Z","iopub.status.idle":"2022-09-29T16:16:38.022338Z","shell.execute_reply.started":"2022-09-29T16:16:37.707138Z","shell.execute_reply":"2022-09-29T16:16:38.021188Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h4>Does the distances differ between different type of races?</h4>\n\n- **Starter** and **Waiver** races are normally short distance races and mostly within 8 furlongs \n- **Stakes** races have almost all distances of races","metadata":{}},{"cell_type":"code","source":"datas = [\n    race_table[race_table['race_type']=='Claiming']['distance_id'].value_counts().sort_index(),  \n    race_table[race_table['race_type']=='Maiden Claiming']['distance_id'].value_counts().sort_index(),  \n    race_table[race_table['race_type']=='Maiden Special Weight']['distance_id'].value_counts().sort_index(), \n    race_table[race_table['race_type']=='Stakes']['distance_id'].value_counts().sort_index(),   \n]\ncaptions = [\n    'race type = Claiming',\n    'race type = Maiden Claiming',\n    'race type = Maiden Special Weight',\n    'race type = Stakes',\n]\n\nprint('Number of races by distance for each race type')\nmulti_bar(datas[:2], captions=captions[:2], figsize=(18, 5), sharey=True)\nmulti_bar(datas[2:], captions=captions[2:], figsize=(18, 5), sharey=True)","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-09-29T16:16:38.023884Z","iopub.execute_input":"2022-09-29T16:16:38.024283Z","iopub.status.idle":"2022-09-29T16:16:39.020554Z","shell.execute_reply.started":"2022-09-29T16:16:38.024252Z","shell.execute_reply":"2022-09-29T16:16:39.019689Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"datas = [\n    race_table[race_table['course_type']=='Dirt']['distance_id'].value_counts().sort_index(),  \n    race_table[~race_table['course_type'].isin(['Dirt', 'Hurdle'])]['distance_id'].value_counts().sort_index(),  \n]\ncaptions = [\n    'Number of races by distance on Dirt track',\n    'Number of races by distance on Turf track',\n]\n\nmulti_bar(datas, captions=captions, figsize=(18, 5), sharey=True)","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-09-29T16:16:39.022254Z","iopub.execute_input":"2022-09-29T16:16:39.022859Z","iopub.status.idle":"2022-09-29T16:16:39.594471Z","shell.execute_reply.started":"2022-09-29T16:16:39.022825Z","shell.execute_reply":"2022-09-29T16:16:39.593246Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cs=pd.pivot_table(race_table, values='race_date', \n               index=['distance_id'], columns=['track_id'], aggfunc=np.size)\n\ncs['sum'] = cs.sum(axis=1)\ncs.sort_values(by='sum', ascending=True, inplace=True)\n\ncs.index = [str(v) for v in cs.index.values]\n\nstacked_barh(cs.tail(12), figsize=(14, 8), color_set='g', sum_col='sum', \n             caption='Num of races by distance on each track', \n             sub_title = '',\n             labels=['Aqueduct', 'Belmont', 'Saratoga'], \n             )","metadata":{"execution":{"iopub.status.busy":"2022-09-29T16:17:52.627819Z","iopub.execute_input":"2022-09-29T16:17:52.629077Z","iopub.status.idle":"2022-09-29T16:17:53.141643Z","shell.execute_reply.started":"2022-09-29T16:17:52.629021Z","shell.execute_reply":"2022-09-29T16:17:53.140350Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"2\"></a>\n\n# <center>Racing course types - How do they differ from each other</center>\n\n[back to top](#toc)\n\n#### The following graphs show the shapes for each type of course at each track.\n\n##### star in the graphs indicates the starting point of the corresponding course.","metadata":{}},{"cell_type":"code","source":"def plot_track(data, figsize=(14, 8), caption=''):\n    plt.figure(figsize=(14, 8), dpi=100, )\n\n    i = 0\n    for pn in data['program_number'].unique():\n        plt.scatter('latitude', 'longitude',\n                    data=data[data['program_number']==pn],\n                    s=20, c=colors[i], label='',  marker='o') \n\n        i = i + 1\n        if i >7:\n            break\n\n    plt.scatter('latitude', 'longitude',\n                        data=data[(data['program_number']==pn) & (data['trakus_index']<5)],\n                        s=300, c='black', label='',  marker='*')         \n\n    plt.title(caption,  fontsize=14, loc='left')\n\n    plt.grid(visible=True, which='major', axis='both', color='lightgray', linestyle='-', linewidth=0.1)\n    plt.gca().spines[['top', 'right']].set_visible(False)\n    plt.gca().spines[['bottom', 'left']].set_edgecolor('whitesmoke')\n\n    plt.yticks(fontsize=9, )#rotation=90\n    plt.xticks(fontsize=9, )#rotation=90\n    plt.tight_layout()\n    plt.show()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-09-29T16:17:56.192647Z","iopub.execute_input":"2022-09-29T16:17:56.193115Z","iopub.status.idle":"2022-09-29T16:17:56.203673Z","shell.execute_reply.started":"2022-09-29T16:17:56.193082Z","shell.execute_reply":"2022-09-29T16:17:56.202582Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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-29T16:17:57.260334Z","iopub.execute_input":"2022-09-29T16:17:57.261213Z","iopub.status.idle":"2022-09-29T16:18:06.237988Z","shell.execute_reply.started":"2022-09-29T16:17:57.261160Z","shell.execute_reply":"2022-09-29T16:18:06.236767Z"},"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-29T16:18:06.239850Z","iopub.execute_input":"2022-09-29T16:18:06.240231Z","iopub.status.idle":"2022-09-29T16:18:09.467458Z","shell.execute_reply.started":"2022-09-29T16:18:06.240198Z","shell.execute_reply":"2022-09-29T16:18:09.466279Z"},"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-29T16:18:09.468842Z","iopub.execute_input":"2022-09-29T16:18:09.469383Z","iopub.status.idle":"2022-09-29T16:18:09.475947Z","shell.execute_reply.started":"2022-09-29T16:18:09.469346Z","shell.execute_reply":"2022-09-29T16:18:09.474621Z"},"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\n\n\nfor track_id in ['AQU', 'BEL', 'SAR']:\n    plt.figure(figsize = (16, 8))\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',\n                   palette=color_dict, )\n\n    plt.title(track_id_dict[track_id],  fontsize=14, loc='left')\n\n    plt.grid(visible=True, which='major', axis='both', color='lightgray', linestyle='-', linewidth=0.1)\n    plt.gca().spines[['top', 'right']].set_visible(False)\n    plt.gca().spines[['bottom', 'left']].set_edgecolor('whitesmoke')\n\n    plt.yticks(fontsize=9, )#rotation=90\n    plt.xticks(fontsize=9, )#rotation=90\n    plt.tight_layout()\n    plt.legend()\n    plt.show()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-09-29T16:18:09.478134Z","iopub.execute_input":"2022-09-29T16:18:09.478511Z","iopub.status.idle":"2022-09-29T16:18:30.933829Z","shell.execute_reply.started":"2022-09-29T16:18:09.478478Z","shell.execute_reply":"2022-09-29T16:18:30.932599Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"race_table[['track_id', 'distance_id', 'course_type','race_type' ]].groupby(['track_id', 'distance_id', 'course_type']).count()","metadata":{"_kg_hide-input":true,"_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-09-29T16:18:30.935997Z","iopub.execute_input":"2022-09-29T16:18:30.936461Z","iopub.status.idle":"2022-09-29T16:18:30.964654Z","shell.execute_reply.started":"2022-09-29T16:18:30.936418Z","shell.execute_reply":"2022-09-29T16:18:30.963289Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"race_table[(race_table['track_id']=='AQU') & (race_table['distance_id']>600) & (race_table['course_type']=='T')]","metadata":{"_kg_hide-input":true,"_kg_hide-output":true,"scrolled":true,"execution":{"iopub.status.busy":"2022-09-29T16:16:39.770125Z","iopub.status.idle":"2022-09-29T16:16:39.770568Z","shell.execute_reply.started":"2022-09-29T16:16:39.770343Z","shell.execute_reply":"2022-09-29T16:16:39.770365Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"track_id = 'AQU'\nrace_date = '2019-03-23'\nrace_number = 7\ndistance_id = 600\ncourse_type = 'D'\nrace_type = 'CLM'\n\ncaption = f\"race type: {race_type_dict[race_type]} - track: {track_id_dict[track_id]} - race date: {race_date} ({race_number}) - distance: {distance_id} - course: {course_type_dict[course_type]}\"\n\nsel = (tracking_table['track_id']==track_id) \nsel = sel & (tracking_table['race_date']==race_date) \nsel = sel & (tracking_table['race_number']==race_number)\n#sel = sel & (tracking_table['program_number']=='6  ')\ndata = tracking_table[sel]\n\nplot_track(data, figsize=(14, 8), caption=caption)","metadata":{"_kg_hide-input":true,"_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-09-29T16:16:39.772291Z","iopub.status.idle":"2022-09-29T16:16:39.772690Z","shell.execute_reply.started":"2022-09-29T16:16:39.772494Z","shell.execute_reply":"2022-09-29T16:16:39.772513Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"track_id = 'AQU'\nrace_date = '2019-03-16'\nrace_number = 5\ndistance_id = 800\ncourse_type = 'D'\nrace_type = 'MCL'\n\ncaption = f\"race type: {race_type_dict[race_type]} - track: {track_id_dict[track_id]} - race date: {race_date} ({race_number}) - distance: {distance_id} - course: {course_type_dict[course_type]}\"\n\nsel = (tracking_table['track_id']==track_id) \nsel = sel & (tracking_table['race_date']==race_date) \nsel = sel & (tracking_table['race_number']==race_number)\n#sel = sel & (tracking_table['program_number']=='6  ')\ndata = tracking_table[sel]\n\nplot_track(data, figsize=(14, 8), caption=caption)","metadata":{"_kg_hide-input":true,"_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-09-29T16:16:39.774952Z","iopub.status.idle":"2022-09-29T16:16:39.775373Z","shell.execute_reply.started":"2022-09-29T16:16:39.775165Z","shell.execute_reply":"2022-09-29T16:16:39.775183Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"track_id = 'BEL'\nrace_date = '2019-10-23'\nrace_number = 5\ndistance_id = 600\ncourse_type = 'D'\nrace_type = 'MSW'\n\ncaption = f\"race type: {race_type_dict[race_type]} - track: {track_id_dict[track_id]} - race date: {race_date} ({race_number}) - distance: {distance_id} - course: {course_type_dict[course_type]}\"\n\nsel = (tracking_table['track_id']==track_id) \nsel = sel & (tracking_table['race_date']==race_date) \nsel = sel & (tracking_table['race_number']==race_number)\n#sel = sel & (tracking_table['program_number']=='6  ')\ndata = tracking_table[sel]\n\nplot_track(data, figsize=(14, 8), caption=caption)","metadata":{"_kg_hide-input":true,"_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-09-29T16:16:39.777430Z","iopub.status.idle":"2022-09-29T16:16:39.777867Z","shell.execute_reply.started":"2022-09-29T16:16:39.777659Z","shell.execute_reply":"2022-09-29T16:16:39.777678Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"track_id = 'BEL'\nrace_date = '2019-10-12'\nrace_number = 7\ndistance_id = 600\ncourse_type = 'T'\nrace_type = 'STK'\n\ncaption = f\"race type: {race_type_dict[race_type]} - track: {track_id_dict[track_id]} - race date: {race_date} ({race_number}) - distance: {distance_id} - course: {course_type_dict[course_type]}\"\n\nsel = (tracking_table['track_id']==track_id) \nsel = sel & (tracking_table['race_date']==race_date) \nsel = sel & (tracking_table['race_number']==race_number)\n#sel = sel & (tracking_table['program_number']=='6  ')\ndata = tracking_table[sel]\n\nplot_track(data, figsize=(14, 8), caption=caption)","metadata":{"_kg_hide-input":true,"_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-09-29T16:16:39.779564Z","iopub.status.idle":"2022-09-29T16:16:39.780030Z","shell.execute_reply.started":"2022-09-29T16:16:39.779804Z","shell.execute_reply":"2022-09-29T16:16:39.779823Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"track_id = 'AQU'\nrace_date = '2019-11-03'\nrace_number = 9\ndistance_id = 1200\ncourse_type = 'T'\nrace_type = 'STK'\n\ncaption = f\"race type: {race_type_dict[race_type]} - track: {track_id_dict[track_id]} - race date: {race_date} ({race_number}) - distance: {distance_id} - course: {course_type_dict[course_type]}\"\n\nsel = (tracking_table['track_id']==track_id) \nsel = sel & (tracking_table['race_date']==race_date) \nsel = sel & (tracking_table['race_number']==race_number)\n#sel = sel & (tracking_table['program_number']=='6  ')\ndata = tracking_table[sel]\n\nplot_track(data, figsize=(14, 8), caption=caption)","metadata":{"_kg_hide-input":true,"_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-09-29T16:16:39.781406Z","iopub.status.idle":"2022-09-29T16:16:39.781836Z","shell.execute_reply.started":"2022-09-29T16:16:39.781634Z","shell.execute_reply":"2022-09-29T16:16:39.781653Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"track_id = 'SAR'\nrace_date = '2019-08-25'\nrace_number = 3\ndistance_id = 1100\ncourse_type = 'I'\nrace_type = 'AOC'\n\ncaption = f\"race type: {race_type_dict[race_type]} - track: {track_id_dict[track_id]} - race date: {race_date} ({race_number}) - distance: {distance_id} - course: {course_type_dict[course_type]}\"\n\nsel = (tracking_table['track_id']==track_id) \nsel = sel & (tracking_table['race_date']==race_date) \nsel = sel & (tracking_table['race_number']==race_number)\n#sel = sel & (tracking_table['program_number']=='6  ')\ndata = tracking_table[sel]\n\nplot_track(data, figsize=(14, 8), caption=caption)","metadata":{"_kg_hide-input":true,"_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-09-29T16:16:39.783050Z","iopub.status.idle":"2022-09-29T16:16:39.783467Z","shell.execute_reply.started":"2022-09-29T16:16:39.783246Z","shell.execute_reply":"2022-09-29T16:16:39.783265Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"track_id = 'SAR'\nrace_date = '2019-08-01'\nrace_number = 9\ndistance_id = 1400\ncourse_type = 'D'\nrace_type = 'STK'\n\ncaption = f\"race type: {race_type_dict[race_type]} - track: {track_id_dict[track_id]} - race date: {race_date} ({race_number}) - distance: {distance_id} - course: {course_type_dict[course_type]}\"\n\nsel = (tracking_table['track_id']==track_id) \nsel = sel & (tracking_table['race_date']==race_date) \nsel = sel & (tracking_table['race_number']==race_number)\n#sel = sel & (tracking_table['program_number']=='6  ')\ndata = tracking_table[sel]\n\nplot_track(data, figsize=(14, 8), caption=caption)","metadata":{"_kg_hide-input":true,"_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-09-29T16:16:39.785200Z","iopub.status.idle":"2022-09-29T16:16:39.785628Z","shell.execute_reply.started":"2022-09-29T16:16:39.785432Z","shell.execute_reply":"2022-09-29T16:16:39.785451Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"3\"></a>\n\n# <center>Dates and time: When did the races take place</center>\n\n[back to top](#toc)\n\nThroughout the year, most horse races took place on Friday and the whole weekend. There were a few races on Mondays and Tuesdays, and those days were holidays. For example, the races on Tuesdays were at track `Aqueduct` on Jan 1st (New Year) and Dec 31st (Last day in the Year). From Jun to Nov, there were also races on Wednesdays.\n\nAqueduct: races took place from Nov to Apr; Belmont: races in May/Jun/Sep/Oct, and a few in Apr and and Jul. Saratoga: races only from Jul to Sep.\n\nThe majority of races took place between 1pm and 6pm in the afternoon; only few races started after 6PM. The 1st race typically started before 1:30PM.\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-29T16:16:39.786851Z","iopub.status.idle":"2022-09-29T16:16:39.787303Z","shell.execute_reply.started":"2022-09-29T16:16:39.787083Z","shell.execute_reply":"2022-09-29T16:16:39.787102Z"},"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-29T16:16:39.788604Z","iopub.status.idle":"2022-09-29T16:16:39.788986Z","shell.execute_reply.started":"2022-09-29T16:16:39.788792Z","shell.execute_reply":"2022-09-29T16:16:39.788809Z"},"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-29T16:16:39.792454Z","iopub.status.idle":"2022-09-29T16:16:39.793417Z","shell.execute_reply.started":"2022-09-29T16:16:39.793095Z","shell.execute_reply":"2022-09-29T16:16:39.793123Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ncs = 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# Plot\nplt.figure(figsize=(10,3), dpi= 80)\nsns.heatmap(cs, xticklabels=cs.columns, yticklabels=cs.index, cmap='RdYlGn', center=0, annot=True,  cbar=False, fmt='.0f')#YlGnBu#RdYlGn\n\n# Decorations\nplt.title('Which months did races take place on each track?', fontsize=13, loc='left')\nplt.xticks(fontsize=10)\nplt.yticks(fontsize=10)\nplt.show()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-09-29T16:16:39.794680Z","iopub.status.idle":"2022-09-29T16:16:39.795969Z","shell.execute_reply.started":"2022-09-29T16:16:39.795648Z","shell.execute_reply":"2022-09-29T16:16:39.795675Z"},"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\n# Plot\nplt.figure(figsize=(10,4), dpi= 80)\nsns.heatmap(cs, xticklabels=cs.columns, yticklabels=cs.index, cmap='RdYlGn', center=0, annot=True,  cbar=False, fmt='.0f')#YlGnBu#RdYlGn\n\n# Decorations\nplt.title('When did races take place?', fontsize=13, loc='left')\nplt.xticks(fontsize=10)\nplt.yticks(fontsize=10)\nplt.show()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-09-29T16:16:39.797256Z","iopub.status.idle":"2022-09-29T16:16:39.798115Z","shell.execute_reply.started":"2022-09-29T16:16:39.797786Z","shell.execute_reply":"2022-09-29T16:16:39.797814Z"},"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# Plot\nplt.figure(figsize=(16,4), dpi= 80)\nsns.heatmap(cs, xticklabels=cs.columns, yticklabels=cs.index, cmap='RdYlGn', center=0, annot=True,  cbar=False, fmt='.0f')#YlGnBu#RdYlGn\n\n# Decorations\nplt.title('What time in the day did races take place?', fontsize=13, loc='left')\nplt.xticks(fontsize=10)\nplt.yticks(fontsize=10)\nplt.show()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-09-29T16:16:39.799741Z","iopub.status.idle":"2022-09-29T16:16:39.801040Z","shell.execute_reply.started":"2022-09-29T16:16:39.800711Z","shell.execute_reply":"2022-09-29T16:16:39.800739Z"},"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# Plot\nplt.figure(figsize=(6,3), dpi= 80)\nsns.heatmap(cs, xticklabels=cs.columns, yticklabels=cs.index, cmap='RdYlGn', center=0, annot=True,  cbar=False, fmt='.0f')#YlGnBu#RdYlGn\n\n# Decorations\nplt.title('What time in the day did 1st race take place?', fontsize=13)\nplt.xticks(fontsize=10)\nplt.yticks(fontsize=10)\nplt.show()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-09-29T16:16:39.802389Z","iopub.status.idle":"2022-09-29T16:16:39.803038Z","shell.execute_reply.started":"2022-09-29T16:16:39.802644Z","shell.execute_reply":"2022-09-29T16:16:39.802671Z"},"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":{}}]}