{"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":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-10-21T08:45:19.534398Z","iopub.execute_input":"2022-10-21T08:45:19.534845Z","iopub.status.idle":"2022-10-21T08:45:19.543194Z","shell.execute_reply.started":"2022-10-21T08:45:19.534805Z","shell.execute_reply":"2022-10-21T08:45:19.541100Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nimport tensorflow as tf\nimport numpy as np\nimport keras\nfrom sklearn.model_selection import train_test_split\nfrom tensorflow.keras.losses import CategoricalCrossentropy\nfrom tensorflow.keras.optimizers import Adam\nfrom matplotlib import pyplot as plt\nfrom sklearn.metrics import mean_squared_error\nfrom tensorflow.keras import Sequential\nfrom tensorflow.keras.layers import Dense, Dropout\nfrom sklearn.preprocessing import LabelEncoder","metadata":{"execution":{"iopub.status.busy":"2022-10-21T08:44:24.746062Z","iopub.execute_input":"2022-10-21T08:44:24.746503Z","iopub.status.idle":"2022-10-21T08:44:33.822430Z","shell.execute_reply.started":"2022-10-21T08:44:24.746471Z","shell.execute_reply":"2022-10-21T08:44:33.820855Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Load complete dataset\nbdd = pd.read_csv('../input/big-data-derby-2022/nyra_2019_complete.csv')","metadata":{"execution":{"iopub.status.busy":"2022-10-21T08:46:04.254806Z","iopub.execute_input":"2022-10-21T08:46:04.255203Z","iopub.status.idle":"2022-10-21T08:46:24.767842Z","shell.execute_reply.started":"2022-10-21T08:46:04.255172Z","shell.execute_reply":"2022-10-21T08:46:24.766414Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"bdd.head()","metadata":{"execution":{"iopub.status.busy":"2022-10-21T08:46:32.740222Z","iopub.execute_input":"2022-10-21T08:46:32.741141Z","iopub.status.idle":"2022-10-21T08:46:32.774660Z","shell.execute_reply.started":"2022-10-21T08:46:32.741094Z","shell.execute_reply":"2022-10-21T08:46:32.773421Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"bdd.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-10-21T08:46:48.095255Z","iopub.execute_input":"2022-10-21T08:46:48.095804Z","iopub.status.idle":"2022-10-21T08:46:49.741195Z","shell.execute_reply.started":"2022-10-21T08:46:48.095755Z","shell.execute_reply":"2022-10-21T08:46:49.739697Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"bdd.columns","metadata":{"execution":{"iopub.status.busy":"2022-10-21T08:46:58.366991Z","iopub.execute_input":"2022-10-21T08:46:58.367571Z","iopub.status.idle":"2022-10-21T08:46:58.376332Z","shell.execute_reply.started":"2022-10-21T08:46:58.367521Z","shell.execute_reply":"2022-10-21T08:46:58.375410Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"bdd = bdd.dropna()","metadata":{"execution":{"iopub.status.busy":"2022-10-21T08:47:11.396033Z","iopub.execute_input":"2022-10-21T08:47:11.396444Z","iopub.status.idle":"2022-10-21T08:47:14.068099Z","shell.execute_reply.started":"2022-10-21T08:47:11.396410Z","shell.execute_reply":"2022-10-21T08:47:14.066832Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"bdd.columns","metadata":{"execution":{"iopub.status.busy":"2022-10-21T08:47:19.715972Z","iopub.execute_input":"2022-10-21T08:47:19.716366Z","iopub.status.idle":"2022-10-21T08:47:19.726253Z","shell.execute_reply.started":"2022-10-21T08:47:19.716334Z","shell.execute_reply":"2022-10-21T08:47:19.724889Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"bdd.info()","metadata":{"execution":{"iopub.status.busy":"2022-10-21T08:47:26.616293Z","iopub.execute_input":"2022-10-21T08:47:26.616814Z","iopub.status.idle":"2022-10-21T08:47:26.646969Z","shell.execute_reply.started":"2022-10-21T08:47:26.616775Z","shell.execute_reply":"2022-10-21T08:47:26.645588Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del bdd['8']","metadata":{"execution":{"iopub.status.busy":"2022-10-21T08:47:36.744911Z","iopub.execute_input":"2022-10-21T08:47:36.745403Z","iopub.status.idle":"2022-10-21T08:47:36.752120Z","shell.execute_reply.started":"2022-10-21T08:47:36.745353Z","shell.execute_reply":"2022-10-21T08:47:36.751130Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"bdd.columns","metadata":{"execution":{"iopub.status.busy":"2022-10-21T08:47:45.424334Z","iopub.execute_input":"2022-10-21T08:47:45.424841Z","iopub.status.idle":"2022-10-21T08:47:45.434685Z","shell.execute_reply.started":"2022-10-21T08:47:45.424798Z","shell.execute_reply":"2022-10-21T08:47:45.433091Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"bdd.columns = ['track_id', \n               'race_date',\n               'race_number', \n               'program_number', \n               'trakus_index',\n               'latitude', \n               'longtiude',\n               'distance_id',\n               'course_type',\n               'track_condition',\n               'run_up_distance',\n               'race_type',\n               'purse',\n               'post_time',\n               'weight_carried',\n               'jockey',\n               'odds']","metadata":{"execution":{"iopub.status.busy":"2022-10-21T08:47:54.416388Z","iopub.execute_input":"2022-10-21T08:47:54.416972Z","iopub.status.idle":"2022-10-21T08:47:54.426026Z","shell.execute_reply.started":"2022-10-21T08:47:54.416913Z","shell.execute_reply":"2022-10-21T08:47:54.423909Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"bdd.info()","metadata":{"execution":{"iopub.status.busy":"2022-10-21T08:48:01.905446Z","iopub.execute_input":"2022-10-21T08:48:01.905956Z","iopub.status.idle":"2022-10-21T08:48:01.923255Z","shell.execute_reply.started":"2022-10-21T08:48:01.905915Z","shell.execute_reply":"2022-10-21T08:48:01.921368Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Encode ordinal columns ( categorical values with dtype-object)\n\nlabel_encoder = LabelEncoder()\nobj = bdd.select_dtypes(include=\"object\").columns\nfor f in obj:\n    bdd[f] = label_encoder.fit_transform(bdd[f].astype(str))\n\nprint(bdd.info())","metadata":{"execution":{"iopub.status.busy":"2022-10-21T08:48:50.505233Z","iopub.execute_input":"2022-10-21T08:48:50.505717Z","iopub.status.idle":"2022-10-21T08:49:04.828263Z","shell.execute_reply.started":"2022-10-21T08:48:50.505679Z","shell.execute_reply":"2022-10-21T08:49:04.826842Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"conv_dict = {\n    'purse' : int\n}","metadata":{"execution":{"iopub.status.busy":"2022-10-21T08:49:08.684271Z","iopub.execute_input":"2022-10-21T08:49:08.684796Z","iopub.status.idle":"2022-10-21T08:49:08.690557Z","shell.execute_reply.started":"2022-10-21T08:49:08.684754Z","shell.execute_reply":"2022-10-21T08:49:08.689526Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"bdd = bdd.astype(conv_dict)","metadata":{"execution":{"iopub.status.busy":"2022-10-21T08:49:16.081852Z","iopub.execute_input":"2022-10-21T08:49:16.083755Z","iopub.status.idle":"2022-10-21T08:49:16.406362Z","shell.execute_reply.started":"2022-10-21T08:49:16.083678Z","shell.execute_reply":"2022-10-21T08:49:16.404581Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"bdd.info()","metadata":{"execution":{"iopub.status.busy":"2022-10-21T08:49:19.884083Z","iopub.execute_input":"2022-10-21T08:49:19.884497Z","iopub.status.idle":"2022-10-21T08:49:19.900486Z","shell.execute_reply.started":"2022-10-21T08:49:19.884466Z","shell.execute_reply":"2022-10-21T08:49:19.899169Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fdf = bdd.head(1000)\ndata = fdf.drop(['track_id','program_number','race_date','latitude','longtiude','distance_id'],axis=1)\ntarget = fdf['odds']","metadata":{"execution":{"iopub.status.busy":"2022-10-21T08:55:57.205234Z","iopub.execute_input":"2022-10-21T08:55:57.205837Z","iopub.status.idle":"2022-10-21T08:55:57.214674Z","shell.execute_reply.started":"2022-10-21T08:55:57.205788Z","shell.execute_reply":"2022-10-21T08:55:57.213417Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.columns","metadata":{"execution":{"iopub.status.busy":"2022-10-21T08:55:58.564467Z","iopub.execute_input":"2022-10-21T08:55:58.564908Z","iopub.status.idle":"2022-10-21T08:55:58.573811Z","shell.execute_reply.started":"2022-10-21T08:55:58.564873Z","shell.execute_reply":"2022-10-21T08:55:58.572343Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(data.shape, target.shape)","metadata":{"execution":{"iopub.status.busy":"2022-10-21T08:56:02.758337Z","iopub.execute_input":"2022-10-21T08:56:02.758765Z","iopub.status.idle":"2022-10-21T08:56:02.765000Z","shell.execute_reply.started":"2022-10-21T08:56:02.758730Z","shell.execute_reply":"2022-10-21T08:56:02.763756Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train , x_test , y_train , y_test = train_test_split(data , target , train_size = 0.8 ,test_size =0.2 , random_state = 1)\nprint(x_train.shape, x_test.shape, y_train.shape, y_test.shape)","metadata":{"execution":{"iopub.status.busy":"2022-10-21T08:56:04.304178Z","iopub.execute_input":"2022-10-21T08:56:04.304582Z","iopub.status.idle":"2022-10-21T08:56:04.316562Z","shell.execute_reply.started":"2022-10-21T08:56:04.304549Z","shell.execute_reply":"2022-10-21T08:56:04.314589Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential([\n    Dense(512 , input_dim = 11 , activation  = 'relu'),\n    Dropout(0.5),\n    Dense(128, activation='relu'),\n     Dropout(0.25),\n    Dense(1, activation='sigmoid')\n])","metadata":{"execution":{"iopub.status.busy":"2022-10-21T08:56:14.023931Z","iopub.execute_input":"2022-10-21T08:56:14.024543Z","iopub.status.idle":"2022-10-21T08:56:14.089958Z","shell.execute_reply.started":"2022-10-21T08:56:14.024484Z","shell.execute_reply":"2022-10-21T08:56:14.088334Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer = 'adam',\n             loss = 'mean_squared_error',\n             metrics = ['mean_squared_error'])","metadata":{"execution":{"iopub.status.busy":"2022-10-21T08:56:16.404858Z","iopub.execute_input":"2022-10-21T08:56:16.405288Z","iopub.status.idle":"2022-10-21T08:56:16.417673Z","shell.execute_reply.started":"2022-10-21T08:56:16.405255Z","shell.execute_reply":"2022-10-21T08:56:16.416125Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(x_train,y_train, \n          batch_size=500,verbose=1,\n          epochs = 200, validation_data = (x_test,y_test))","metadata":{"execution":{"iopub.status.busy":"2022-10-21T08:56:18.805282Z","iopub.execute_input":"2022-10-21T08:56:18.805781Z","iopub.status.idle":"2022-10-21T08:56:32.488070Z","shell.execute_reply.started":"2022-10-21T08:56:18.805742Z","shell.execute_reply":"2022-10-21T08:56:32.486717Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.DataFrame(history.history).head()","metadata":{"execution":{"iopub.status.busy":"2022-10-21T09:00:37.215136Z","iopub.execute_input":"2022-10-21T09:00:37.215546Z","iopub.status.idle":"2022-10-21T09:00:37.233802Z","shell.execute_reply.started":"2022-10-21T09:00:37.215514Z","shell.execute_reply":"2022-10-21T09:00:37.232328Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mae = history.history['mean_squared_error']\nval_mae = history.history['val_mean_squared_error']\nloss = history.history['loss']\nval_loss = history.history['val_loss']\n\nepochs = range(1,len(loss)+1)\nplt.plot(epochs,mae,'b',label='Training Accuracy')\nplt.plot(epochs,val_mae,'r',label='Validation Accuracy')\nplt.title('Training and validation mae')\nplt.ylabel('mae')\nplt.xlabel('Epoch')\nplt.legend()\nplt.figure()\n\nplt.plot(epochs,loss,'g',label='Training Loss')\nplt.plot(epochs,val_loss,'o',label='Validation Loss')\nplt.title('Training and validation loss')\nplt.ylabel('Loss')\nplt.xlabel('Epoch')\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-10-21T09:00:59.676342Z","iopub.execute_input":"2022-10-21T09:00:59.676896Z","iopub.status.idle":"2022-10-21T09:01:00.190703Z","shell.execute_reply.started":"2022-10-21T09:00:59.676856Z","shell.execute_reply":"2022-10-21T09:01:00.189250Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred_train = model.predict(x_train)\nprint(np.sqrt(mean_squared_error(y_train,pred_train)))\n\npred_test = model.predict(x_test)\nprint(np.sqrt(mean_squared_error(y_test,pred_test)))","metadata":{"execution":{"iopub.status.busy":"2022-10-21T09:01:12.575279Z","iopub.execute_input":"2022-10-21T09:01:12.575775Z","iopub.status.idle":"2022-10-21T09:01:12.902484Z","shell.execute_reply.started":"2022-10-21T09:01:12.575738Z","shell.execute_reply":"2022-10-21T09:01:12.900639Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}