{"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 pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport seaborn as sns\n\nfrom scipy import stats","metadata":{"execution":{"iopub.status.busy":"2022-09-20T16:54:46.767121Z","iopub.execute_input":"2022-09-20T16:54:46.768596Z","iopub.status.idle":"2022-09-20T16:54:46.775950Z","shell.execute_reply.started":"2022-09-20T16:54:46.768532Z","shell.execute_reply":"2022-09-20T16:54:46.774458Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"col_name = ['track_id', 'race_date', 'race_number', 'program_number',\n            'trakus_index', 'latitude', 'longitude', 'distance_id',\n            'course_type', 'track_condition', 'run_up_distance',\n            'race_type', 'purse', 'post_time', 'weight_carried',\n            'jockey', 'odds']","metadata":{"execution":{"iopub.status.busy":"2022-09-20T17:02:17.288060Z","iopub.execute_input":"2022-09-20T17:02:17.288521Z","iopub.status.idle":"2022-09-20T17:02:17.295965Z","shell.execute_reply.started":"2022-09-20T17:02:17.288482Z","shell.execute_reply":"2022-09-20T17:02:17.294379Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nyra_2019 = pd.read_csv('../input/big-data-derby-2022/nyra_2019_complete.csv',\n                       low_memory=False, names = col_name, index_col=False)","metadata":{"execution":{"iopub.status.busy":"2022-09-20T17:02:20.212058Z","iopub.execute_input":"2022-09-20T17:02:20.212500Z","iopub.status.idle":"2022-09-20T17:02:33.343785Z","shell.execute_reply.started":"2022-09-20T17:02:20.212467Z","shell.execute_reply":"2022-09-20T17:02:33.342287Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nyra_2019.info()","metadata":{"execution":{"iopub.status.busy":"2022-09-20T17:02:36.072130Z","iopub.execute_input":"2022-09-20T17:02:36.073265Z","iopub.status.idle":"2022-09-20T17:02:36.084849Z","shell.execute_reply.started":"2022-09-20T17:02:36.073221Z","shell.execute_reply":"2022-09-20T17:02:36.083870Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nyra_2019.head()","metadata":{"execution":{"iopub.status.busy":"2022-09-20T17:02:39.172806Z","iopub.execute_input":"2022-09-20T17:02:39.173252Z","iopub.status.idle":"2022-09-20T17:02:39.196002Z","shell.execute_reply.started":"2022-09-20T17:02:39.173213Z","shell.execute_reply":"2022-09-20T17:02:39.194612Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"# description of categorical features\nnyra_2019.describe(include=['object'])","metadata":{"execution":{"iopub.status.busy":"2022-09-20T17:02:42.485546Z","iopub.execute_input":"2022-09-20T17:02:42.485982Z","iopub.status.idle":"2022-09-20T17:02:46.534966Z","shell.execute_reply.started":"2022-09-20T17:02:42.485946Z","shell.execute_reply":"2022-09-20T17:02:46.533699Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# description of numerical features\nnyra_2019.describe(include=['int', 'float'])","metadata":{"execution":{"iopub.status.busy":"2022-09-20T17:02:49.850219Z","iopub.execute_input":"2022-09-20T17:02:49.851013Z","iopub.status.idle":"2022-09-20T17:02:51.307263Z","shell.execute_reply.started":"2022-09-20T17:02:49.850972Z","shell.execute_reply":"2022-09-20T17:02:51.305610Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# weight_carried kg\nnyra_2019['weight_carried'] = nyra_2019['weight_carried']*0.45\n\n# odds/100\nnyra_2019['odds_new'] = nyra_2019['odds']/100\n\n# distance_id km\nnyra_2019['distance_id'] = nyra_2019['distance_id'] * 0.2/100\n\n# meter\nnyra_2019['run_up_distance'] = round(nyra_2019['run_up_distance'] * 0.3)\n\n#  datetime64[ns]\nnyra_2019['race_date'] = nyra_2019['race_date'].astype('datetime64[ns]')\n# year \nnyra_2019['year'] = nyra_2019['race_date'].dt.year\n# month\nnyra_2019['month'] = nyra_2019['race_date'].dt.month\n# day\nnyra_2019['day'] = nyra_2019['race_date'].dt.day\n# day_of_week\nnyra_2019['day_of_week'] = pd.to_datetime(nyra_2019['race_date']).dt.dayofweek","metadata":{"execution":{"iopub.status.busy":"2022-09-20T17:03:02.293037Z","iopub.execute_input":"2022-09-20T17:03:02.293524Z","iopub.status.idle":"2022-09-20T17:03:05.809484Z","shell.execute_reply.started":"2022-09-20T17:03:02.293486Z","shell.execute_reply":"2022-09-20T17:03:05.808183Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nyra_2019.info()","metadata":{"execution":{"iopub.status.busy":"2022-09-20T17:03:08.057459Z","iopub.execute_input":"2022-09-20T17:03:08.057889Z","iopub.status.idle":"2022-09-20T17:03:08.072455Z","shell.execute_reply.started":"2022-09-20T17:03:08.057855Z","shell.execute_reply":"2022-09-20T17:03:08.071150Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# delete 'race_date'\nnyra_2019.drop(columns='race_date', axis=1, inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-09-20T17:03:11.249886Z","iopub.execute_input":"2022-09-20T17:03:11.250290Z","iopub.status.idle":"2022-09-20T17:03:12.468192Z","shell.execute_reply.started":"2022-09-20T17:03:11.250259Z","shell.execute_reply":"2022-09-20T17:03:12.467010Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# check for empty values\nnyra_2019.isna().sum()","metadata":{"execution":{"iopub.status.busy":"2022-09-20T17:03:16.954766Z","iopub.execute_input":"2022-09-20T17:03:16.955162Z","iopub.status.idle":"2022-09-20T17:03:18.420990Z","shell.execute_reply.started":"2022-09-20T17:03:16.955129Z","shell.execute_reply":"2022-09-20T17:03:18.419630Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# EDA analysis","metadata":{}},{"cell_type":"markdown","source":"1. Feature distribution ''ODDS''","metadata":{}},{"cell_type":"code","source":"sns.histplot(nyra_2019['odds_new'], kde=True);","metadata":{"execution":{"iopub.status.busy":"2022-09-20T16:14:20.893305Z","iopub.execute_input":"2022-09-20T16:14:20.893795Z","iopub.status.idle":"2022-09-20T16:14:46.500446Z","shell.execute_reply.started":"2022-09-20T16:14:20.893734Z","shell.execute_reply":"2022-09-20T16:14:46.498986Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_shapiro = stats.shapiro(nyra_2019['odds_new'])\n\nprint(test_shapiro)\nif test_shapiro.pvalue < 0.05:\n    print('distribution is normal')\nelse:\n    print('distribution is no normal')","metadata":{"execution":{"iopub.status.busy":"2022-09-20T17:06:20.164228Z","iopub.execute_input":"2022-09-20T17:06:20.164723Z","iopub.status.idle":"2022-09-20T17:06:20.398975Z","shell.execute_reply.started":"2022-09-20T17:06:20.164680Z","shell.execute_reply":"2022-09-20T17:06:20.397490Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"\nsince the distribution is not normal, it is necessary to calculate logarithm","metadata":{}},{"cell_type":"code","source":"nyra_2019['odds_log'] = np.log(nyra_2019['odds_new'] + 1)","metadata":{"execution":{"iopub.status.busy":"2022-09-20T17:07:43.847163Z","iopub.execute_input":"2022-09-20T17:07:43.847673Z","iopub.status.idle":"2022-09-20T17:07:43.911536Z","shell.execute_reply.started":"2022-09-20T17:07:43.847638Z","shell.execute_reply":"2022-09-20T17:07:43.910239Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.histplot(nyra_2019['odds_log'], kde=True);","metadata":{"execution":{"iopub.status.busy":"2022-09-20T17:07:50.322534Z","iopub.execute_input":"2022-09-20T17:07:50.323016Z","iopub.status.idle":"2022-09-20T17:08:15.848885Z","shell.execute_reply.started":"2022-09-20T17:07:50.322977Z","shell.execute_reply":"2022-09-20T17:08:15.847382Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"\ndistribution has improved","metadata":{}},{"cell_type":"markdown","source":"2. Which coefficients are generally better and which account for more money.","metadata":{}},{"cell_type":"code","source":"fig = plt.figure(figsize=(23,12));\n\nax1 = fig.add_subplot(121)  \nsns.barplot (x=\"race_type\", y = \"purse\", hue = \"track_id\", data=nyra_2019, palette='rainbow_r')\nax1.set_xlabel('race_type', fontsize=18)\nax1.set_ylabel('purse', fontsize=18)\n\nax2 = fig.add_subplot(122)  \nsns.barplot (x=\"race_type\", y = \"odds_log\", hue = \"track_id\", data=nyra_2019, palette='rainbow_r');\nax2.set_xlabel('race_type', fontsize=18)\nax2.set_ylabel('odds_log', fontsize=18)\n\nplt.show();","metadata":{"execution":{"iopub.status.busy":"2022-09-20T17:10:34.192343Z","iopub.execute_input":"2022-09-20T17:10:34.192925Z","iopub.status.idle":"2022-09-20T17:13:46.908262Z","shell.execute_reply.started":"2022-09-20T17:10:34.192884Z","shell.execute_reply":"2022-09-20T17:13:46.906841Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(12, 10))\nsns.scatterplot(x ='purse' ,y='odds_log',data = nyra_2019,hue = 'track_id')\nplt.title(\"The dependence of purse on odds_log\", fontsize=24);\nplt.show();","metadata":{"execution":{"iopub.status.busy":"2022-09-20T17:15:15.560141Z","iopub.execute_input":"2022-09-20T17:15:15.560620Z","iopub.status.idle":"2022-09-20T17:17:17.695247Z","shell.execute_reply.started":"2022-09-20T17:15:15.560582Z","shell.execute_reply":"2022-09-20T17:17:17.693817Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"The higher the ratio, the less money. With a small coefficient in the horse, there is more confidence in winning. More money for a small ratio. Most money on STK (highest prize pool horse racing with the best horses). Least of all on CLM, WCL, MCL, WMC (races of horses after a long break, challengers or beginners).","metadata":{}},{"cell_type":"markdown","source":"3. The race_type attribute shows what kind of races there are in general. With the help of boxplot, you can see what range of coefficients exists for each type of race.","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(15, 10))\n\nsns.boxplot(x = \"odds_log\", y = \"track_id\", hue = \"race_type\",\n            data = nyra_2019, palette='Paired')\nplt.title(\"Odds range on track_id\",\n          fontsize=20);","metadata":{"execution":{"iopub.status.busy":"2022-09-20T17:20:46.931102Z","iopub.execute_input":"2022-09-20T17:20:46.931598Z","iopub.status.idle":"2022-09-20T17:20:56.094004Z","shell.execute_reply.started":"2022-09-20T17:20:46.931564Z","shell.execute_reply":"2022-09-20T17:20:56.092555Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"on the AQU track: lower coefficients for species: SHP, SOC, STR, AOC. higher odds: CLM, ALW, SST, STK, WCL, MCL, MSW.\n\non the BEL path: lower coefficients for species: CLM, AOC, STR. higher odds: ALW, STK, WCL, SOC, WMS, MSW.\n\non the SAR track: lower coefficients for species: STR, SOC. higher odds: CLM, AOC, ALW, STK, WCL, MCL, MSW.","metadata":{}},{"cell_type":"markdown","source":"4. Which horse has the best odds on which track","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(15, 10));\nplt.title('Which horse has the best odds on which track');\nsns.barplot (x=\"program_number\", y = \"odds_log\", \n             hue = \"track_id\", data=nyra_2019, palette='rainbow_r');","metadata":{"execution":{"iopub.status.busy":"2022-09-20T17:24:06.621498Z","iopub.execute_input":"2022-09-20T17:24:06.621963Z","iopub.status.idle":"2022-09-20T17:25:28.631610Z","shell.execute_reply.started":"2022-09-20T17:24:06.621929Z","shell.execute_reply":"2022-09-20T17:25:28.630146Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Horse 2B has good odds. 3X also has a good ratio, but this horse only races on the BEL track.\nAll horses have better odds on the SAR track, except number 14. Worse odds on the AQU track.\nThe 4th horse with the most participation does not have the best odds.","metadata":{}},{"cell_type":"markdown","source":"5. Purse","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(15, 10))\n\nsns.boxplot(x=\"track_id\", y = \"purse\", data=nyra_2019, palette='rainbow_r')\nplt.title(\"Purse on track_id\", fontsize=24);","metadata":{"execution":{"iopub.status.busy":"2022-09-20T17:31:41.488230Z","iopub.execute_input":"2022-09-20T17:31:41.488766Z","iopub.status.idle":"2022-09-20T17:31:45.825061Z","shell.execute_reply.started":"2022-09-20T17:31:41.488707Z","shell.execute_reply":"2022-09-20T17:31:45.823432Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"The largest range of purse values ​​on the BEL trace. But the SAR track has the highest income.","metadata":{}},{"cell_type":"markdown","source":"There is also such a sign as the weight that is attached to the horse during the races. Is there a connection between money and the weight that is on a horse? The more weight, the more stakes? And it's interesting to see on which track what weight is preferable","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(15, 10))\nsns.scatterplot(x ='purse', y='weight_carried' ,data=nyra_2019, hue='track_id')\nplt.title(\"Purse and weight_carried\", fontsize=24);","metadata":{"execution":{"iopub.status.busy":"2022-09-20T17:34:33.668845Z","iopub.execute_input":"2022-09-20T17:34:33.669352Z","iopub.status.idle":"2022-09-20T17:38:08.508959Z","shell.execute_reply.started":"2022-09-20T17:34:33.669313Z","shell.execute_reply":"2022-09-20T17:38:08.507701Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"The graphs show that there is more weight on the SAR track and a little BEL.\n\nMore cash on the BEL and SAR tracks, a little on the AQU.\n\nOn the AQU track, mostly horses with a load of up to 60 kg. And this route does not have such a large fund of money as the others.","metadata":{}},{"cell_type":"markdown","source":"6. In each of the main tracks (AQU, BEL, SAR) there are the following types of tracks on which competitions take place:\n\nM - barrier,\nD - dirt,\n\nO - outer lawn,\n\nI - inner lawn,\n\nT - lawn.\n\nYou can see what percentage of these traces are contained in the main traces. That is, what type prevails on the track, in order to roughly understand what type of races can be on them.","metadata":{}},{"cell_type":"code","source":"# a function that groups by a feature and counts the percentage of another feature in it\ndef barplot_group(col_main: str, col_group: str, title: str, rotation: int) -> None:\n\n    plt.figure(figsize=(15, 10))\n\n    data = (\n        nyra_2019.groupby([col_group])[col_main]  \n        .value_counts(normalize=True)  \n        .rename('percentage').mul(100).reset_index().sort_values(col_group)\n    )  # сортировка значений\n\n    ax = sns.barplot(x=col_main,\n                     y=\"percentage\",\n                     hue=col_group,\n                     data=data,\n                     palette='Paired')\n\n    for p in ax.patches:\n        percentage = '{:.1f}%'.format(p.get_height())\n        ax.annotate(\n            percentage,  \n            (p.get_x() + p.get_width() / 2., p.get_height()), \n            ha='center',  \n            va='center',\n            xytext=(0, 10),\n            textcoords='offset points',   \n            rotation=rotation,\n            fontsize=10)\n  \n    plt.title(title, fontsize=20)\n    plt.ylabel('Percentage', fontsize=14)\n    plt.xlabel(col_main, fontsize=14)\n\n    plt.xticks(fontsize=14)\n    plt.yticks(fontsize=14)","metadata":{"execution":{"iopub.status.busy":"2022-09-20T17:41:42.573340Z","iopub.execute_input":"2022-09-20T17:41:42.573864Z","iopub.status.idle":"2022-09-20T17:41:42.585342Z","shell.execute_reply.started":"2022-09-20T17:41:42.573823Z","shell.execute_reply":"2022-09-20T17:41:42.583861Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"barplot_group('track_id', 'course_type',\n              \"What percentage of course_type is contained in track_id\", 0)","metadata":{"execution":{"iopub.status.busy":"2022-09-20T17:43:09.253437Z","iopub.execute_input":"2022-09-20T17:43:09.253920Z","iopub.status.idle":"2022-09-20T17:43:10.877264Z","shell.execute_reply.started":"2022-09-20T17:43:09.253880Z","shell.execute_reply":"2022-09-20T17:43:10.876059Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"AQU (Averduk): this is a dirt track (more than the rest), outside turf everywhere and turf in places.\n\nBEL (Belmont): This is a dirt track, indoor turf (larger than the rest), with small obstacles and turf.\n\nSAR (Saratoga): This is a dirt track with indoor turf, obstacles (larger than the others) and turf.\n\nIt can be concluded that the AQU track is intended simply for running at speed on a non-uniform (slippery) surface. The BEL track is cleaner, more turf, but already with a few obstacles. The SAR course is already a route with a predominance of obstacles.","metadata":{}},{"cell_type":"markdown","source":"Having such a feature as 'track_condition' (track state), you can look at the state of the track:\n\nYL - Compliant,\n\nFM - Rigid,\n\nSY - Sloppy,\n\nGD - good,\n\nFT - Fast,\n\nMY - Dirty,\n\nSF - Quiet","metadata":{}},{"cell_type":"code","source":"barplot_group('track_id', 'track_condition',\n              \"What percentage of track_condition is contained in track_id\", 0)","metadata":{"execution":{"iopub.status.busy":"2022-09-20T17:43:38.846217Z","iopub.execute_input":"2022-09-20T17:43:38.846678Z","iopub.status.idle":"2022-09-20T17:43:40.558276Z","shell.execute_reply.started":"2022-09-20T17:43:38.846644Z","shell.execute_reply":"2022-09-20T17:43:40.557039Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"AQU (Averduk): the track is dirty, sloppy, solid, good. (more dirt)\n\nBEL (Belmont): the track is quiet, firm, good. (more lawn)\n\nSAR (Saratoga): The track is good, firm, quiet.\n\nIn terms of performance, the BEL (Belmont) track is better than the rest.","metadata":{}},{"cell_type":"markdown","source":"You can also analyze the state of the trace type","metadata":{}},{"cell_type":"code","source":"barplot_group('course_type', 'track_condition',\n              \"Track_condition on course_type\", 0)","metadata":{"execution":{"iopub.status.busy":"2022-09-20T17:52:16.590501Z","iopub.execute_input":"2022-09-20T17:52:16.591025Z","iopub.status.idle":"2022-09-20T17:52:18.426583Z","shell.execute_reply.started":"2022-09-20T17:52:16.590989Z","shell.execute_reply":"2022-09-20T17:52:18.425353Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"T (lawn): firm, good, quiet, compliant.\n\nI (inner turf): firm, good, quietest, compliant.\n\nO (outer turf): not very hard, good, yielding.\n\nM (obstruction): not very hard, not very good, yielding.\n\nD (dirt): most durable, good, dirtiest, most sloppy.","metadata":{}},{"cell_type":"markdown","source":"You can see what types of competitions prevail on each of the tracks","metadata":{}},{"cell_type":"code","source":"barplot_group('track_id', 'race_type',\n              \"Types of competitions on each of the tracks\", 60)","metadata":{"execution":{"iopub.status.busy":"2022-09-20T17:54:14.678251Z","iopub.execute_input":"2022-09-20T17:54:14.678785Z","iopub.status.idle":"2022-09-20T17:54:16.661633Z","shell.execute_reply.started":"2022-09-20T17:54:14.678723Z","shell.execute_reply":"2022-09-20T17:54:16.660186Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"\nBEL Course: Always has a Winners' Event (WMC), fewest SOCs (Hybrid start and claim allowance). 30-40% are races of types ALW, AOC, CLM, MCL, MSW, STK, STR, WCL.\n\nAQU Course: This course hosts the most competitions of any type. There are always SHP (race with horses that have improved their performance), SST (horses that perform at the declared price) competitions. A large share of SOC type competitions (hybrid start and declared allowance race). 30-40% are races of types ALW, AOC, CLM, MCL, MSW, STK, STR, WCL.\n\nSAR Course: This course hosts the fewest types of competition. Most STK type competitions (largest prize pool). Least of all SOC competitions. 20-30% are ALW, STR, AOC, MSW, WCL competitions.\n\nCompetitions of types ALW, AOC, MSW, STR, WCL are held on all tracks.\n\nWe can say that competitions with more experienced horses are held on the SAR track. It also has the most obstacles.","metadata":{}},{"cell_type":"markdown","source":"What distances prevail on each of the tracks","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(15, 14))\nsns.boxplot(data=nyra_2019, x='distance_id', y='track_id')\nplt.title('Distance_id on track_id', fontsize=20);","metadata":{"execution":{"iopub.status.busy":"2022-09-20T17:57:04.961403Z","iopub.execute_input":"2022-09-20T17:57:04.961907Z","iopub.status.idle":"2022-09-20T17:57:08.106061Z","shell.execute_reply.started":"2022-09-20T17:57:04.961868Z","shell.execute_reply":"2022-09-20T17:57:08.105053Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"The longest distances on the BEL and SAR tracks.\n\nThe largest range of distances on the BEL circuit.\n\nThe AQU track has the shortest distances.","metadata":{}},{"cell_type":"markdown","source":"7. Horses","metadata":{}},{"cell_type":"markdown","source":"At what distances and on what tracks the horses took part","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(12, 10))\nsns.scatterplot(x ='program_number' ,y='distance_id',data = nyra_2019, hue = 'track_id')\nplt.title(\"Program_number and distance_id on track_id\", fontsize=24);\nplt.show();","metadata":{"execution":{"iopub.status.busy":"2022-09-20T17:59:41.502038Z","iopub.execute_input":"2022-09-20T17:59:41.502505Z","iopub.status.idle":"2022-09-20T18:01:49.380228Z","shell.execute_reply.started":"2022-09-20T17:59:41.502471Z","shell.execute_reply":"2022-09-20T18:01:49.378945Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"According to the resulting graph, it can be revealed that 1X, 3X 16, 2B are completely newbies. 1X took part, apparently for the first time at a short distance. It can be concluded that 1X is the youngest member.\n\nHorses 6, 2, 9, 3, 1, 5, 4, 7, 8 take part in long distances on the BEL and SAR tracks. We can conclude that they are the oldest.\n\nAlso, at long distances, horses 11 and 12 took part only in SAR. The rest took part in distances up to 2.5 km.","metadata":{}},{"cell_type":"markdown","source":"# Feature engineering","metadata":{}},{"cell_type":"code","source":"# in str\nnyra_2019['post_time'] = nyra_2019['post_time'].astype(str)","metadata":{"execution":{"iopub.status.busy":"2022-09-20T18:02:55.602410Z","iopub.execute_input":"2022-09-20T18:02:55.602873Z","iopub.status.idle":"2022-09-20T18:02:58.963471Z","shell.execute_reply.started":"2022-09-20T18:02:55.602834Z","shell.execute_reply":"2022-09-20T18:02:58.962399Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# post_time in datetime64[ns]\n\ndef post_time_preobr(data: str, minutes: int) -> None:\n    if isinstance (data, (str)):\n        return data[0:minutes] + ':' + data[minutes:]","metadata":{"execution":{"iopub.status.busy":"2022-09-20T18:03:27.400129Z","iopub.execute_input":"2022-09-20T18:03:27.400556Z","iopub.status.idle":"2022-09-20T18:03:27.407774Z","shell.execute_reply.started":"2022-09-20T18:03:27.400520Z","shell.execute_reply":"2022-09-20T18:03:27.406275Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nyra_2019['post_time_new'] = nyra_2019['post_time'].apply(\n    lambda x: post_time_preobr(x, -2))\n\n# in datetime\nnyra_2019['post_time_new'] = nyra_2019['post_time_new'].astype(\n    'datetime64[ns]')\n# hours\nnyra_2019['hours'] = pd.to_datetime(nyra_2019['post_time_new']).dt.hour\n# minutes\nnyra_2019['minutes'] = pd.to_datetime(nyra_2019['post_time_new']).dt.minute","metadata":{"execution":{"iopub.status.busy":"2022-09-20T18:03:59.746218Z","iopub.execute_input":"2022-09-20T18:03:59.746663Z","iopub.status.idle":"2022-09-20T18:04:05.502505Z","shell.execute_reply.started":"2022-09-20T18:03:59.746629Z","shell.execute_reply":"2022-09-20T18:04:05.500812Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def seasons(data: int) -> object:\n    \"\"\"\n    Seasons feature formation\n    \"\"\"\n    if isinstance(data, (int)):\n        if data == 1 & data == 2 & data == 12:\n             return 'Winter'\n        elif data == 3 & data == 4 & data == 5:\n            return 'Spring'\n        elif data == 6 & data == 7 & data == 8:\n            return 'Summer'\n        else: return 'Autumn'","metadata":{"execution":{"iopub.status.busy":"2022-09-20T18:05:10.643022Z","iopub.execute_input":"2022-09-20T18:05:10.643475Z","iopub.status.idle":"2022-09-20T18:05:10.652966Z","shell.execute_reply.started":"2022-09-20T18:05:10.643443Z","shell.execute_reply":"2022-09-20T18:05:10.651376Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nyra_2019['season'] = nyra_2019.month.apply(lambda x: seasons(x))","metadata":{"execution":{"iopub.status.busy":"2022-09-20T16:45:36.102325Z","iopub.execute_input":"2022-09-20T16:45:36.102775Z","iopub.status.idle":"2022-09-20T16:45:39.427127Z","shell.execute_reply.started":"2022-09-20T16:45:36.102724Z","shell.execute_reply":"2022-09-20T16:45:39.425558Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def time_day(data: int) -> object:\n    \"\"\"\n    Time_of_day feature formation\n    \"\"\"\n    if isinstance(data, (int)):\n        if data == 11:\n             return 'Morning'\n        elif (1 <= data <= 4) or (data == 12):\n            return 'Day'\n        elif 5 <= data <= 8:\n            return 'Evening'","metadata":{"execution":{"iopub.status.busy":"2022-09-20T18:06:44.860779Z","iopub.execute_input":"2022-09-20T18:06:44.861258Z","iopub.status.idle":"2022-09-20T18:06:44.869378Z","shell.execute_reply.started":"2022-09-20T18:06:44.861222Z","shell.execute_reply":"2022-09-20T18:06:44.867845Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nyra_2019['time_of_day'] = nyra_2019.hours.apply(lambda x: time_day(x))","metadata":{"execution":{"iopub.status.busy":"2022-09-20T18:06:48.397531Z","iopub.execute_input":"2022-09-20T18:06:48.397963Z","iopub.status.idle":"2022-09-20T18:06:50.959447Z","shell.execute_reply.started":"2022-09-20T18:06:48.397929Z","shell.execute_reply":"2022-09-20T18:06:50.958029Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# list in day_of_week\nlst_week = (0, 1, 2, 3, 4, 5, 6)\n\n# list for replace\nlst_week_new = ('Sunday', 'Monday', 'Tuesday',\n                'Wednesday', 'Thursday', 'Friday', 'Saturday')","metadata":{"execution":{"iopub.status.busy":"2022-09-20T18:09:34.532690Z","iopub.execute_input":"2022-09-20T18:09:34.533176Z","iopub.status.idle":"2022-09-20T18:09:34.539088Z","shell.execute_reply.started":"2022-09-20T18:09:34.533140Z","shell.execute_reply":"2022-09-20T18:09:34.538096Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# replace\nnyra_2019['name_of_day'] = nyra_2019['day_of_week'].replace(lst_week, lst_week_new) # заменяем","metadata":{"execution":{"iopub.status.busy":"2022-09-20T18:09:39.703083Z","iopub.execute_input":"2022-09-20T18:09:39.703474Z","iopub.status.idle":"2022-09-20T18:09:40.119236Z","shell.execute_reply.started":"2022-09-20T18:09:39.703445Z","shell.execute_reply":"2022-09-20T18:09:40.117998Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nyra_2019.info()","metadata":{"execution":{"iopub.status.busy":"2022-09-20T18:09:44.873884Z","iopub.execute_input":"2022-09-20T18:09:44.874302Z","iopub.status.idle":"2022-09-20T18:09:44.888262Z","shell.execute_reply.started":"2022-09-20T18:09:44.874270Z","shell.execute_reply":"2022-09-20T18:09:44.886783Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# delete features\nnyra_2019.drop(columns=['odds', 'post_time', \n                        'post_time_new', 'year',\n                       'minutes', 'hours'], axis=1, inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-09-20T18:10:03.052939Z","iopub.execute_input":"2022-09-20T18:10:03.053354Z","iopub.status.idle":"2022-09-20T18:10:07.074314Z","shell.execute_reply.started":"2022-09-20T18:10:03.053323Z","shell.execute_reply":"2022-09-20T18:10:07.072809Z"},"trusted":true},"execution_count":null,"outputs":[]}]}