{"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 required libraries\nimport pandas as pd\nfrom sklearn.preprocessing import LabelEncoder\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.preprocessing import StandardScaler\nimport tensorflow as tf\nfrom tensorflow.keras import Sequential\nfrom tensorflow.keras.metrics import Precision\nfrom tensorflow.keras.losses import CategoricalCrossentropy\nfrom tensorflow.keras.optimizers import Adam\nfrom tensorflow.keras.layers import Dense, Dropout\nimport numpy as np\nimport keras\nfrom matplotlib import pyplot as plt\nfrom sklearn.metrics import mean_squared_error\n","metadata":{"execution":{"iopub.status.busy":"2022-08-25T12:06:33.163361Z","iopub.execute_input":"2022-08-25T12:06:33.164593Z","iopub.status.idle":"2022-08-25T12:06:33.178839Z","shell.execute_reply.started":"2022-08-25T12:06:33.164529Z","shell.execute_reply":"2022-08-25T12:06:33.177490Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Hey Kagglers, I am participating in the big data derby competition to overcome my depression and loss of a family member. \n\nI felt, I would be able to forget few things and recover faster, if I would be writing some python code. \n\nThis is a deep learning sequential model notebook to predict the odds. I have prepared this notebook as a starter. \n\nPlease upvote my notebook if you found it interesting or learned something new. It would motivate me ! Thanks. ☺️ ","metadata":{}},{"cell_type":"markdown","source":"# 🏇🏿🏇🏿 Load the data set ","metadata":{}},{"cell_type":"code","source":"# Load complete dataset\ncdf = pd.read_csv('../input/big-data-derby-2022/nyra_2019_complete.csv')","metadata":{"execution":{"iopub.status.busy":"2022-08-25T12:06:33.180393Z","iopub.execute_input":"2022-08-25T12:06:33.181454Z","iopub.status.idle":"2022-08-25T12:06:43.478094Z","shell.execute_reply.started":"2022-08-25T12:06:33.181413Z","shell.execute_reply":"2022-08-25T12:06:43.476931Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Look at dataset\ncdf.head(5)","metadata":{"execution":{"iopub.status.busy":"2022-08-25T12:06:43.480800Z","iopub.execute_input":"2022-08-25T12:06:43.481523Z","iopub.status.idle":"2022-08-25T12:06:43.502562Z","shell.execute_reply.started":"2022-08-25T12:06:43.481477Z","shell.execute_reply":"2022-08-25T12:06:43.501375Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Check for null values\ncdf.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-08-25T12:06:43.504810Z","iopub.execute_input":"2022-08-25T12:06:43.505189Z","iopub.status.idle":"2022-08-25T12:06:45.075972Z","shell.execute_reply.started":"2022-08-25T12:06:43.505156Z","shell.execute_reply":"2022-08-25T12:06:45.074730Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Drop na values\ncdf = cdf.dropna()","metadata":{"execution":{"iopub.status.busy":"2022-08-25T12:06:45.077613Z","iopub.execute_input":"2022-08-25T12:06:45.077991Z","iopub.status.idle":"2022-08-25T12:06:47.530551Z","shell.execute_reply.started":"2022-08-25T12:06:45.077948Z","shell.execute_reply":"2022-08-25T12:06:47.529289Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# check data types \ncdf.info()","metadata":{"execution":{"iopub.status.busy":"2022-08-25T12:06:47.531931Z","iopub.execute_input":"2022-08-25T12:06:47.532720Z","iopub.status.idle":"2022-08-25T12:06:47.545103Z","shell.execute_reply.started":"2022-08-25T12:06:47.532683Z","shell.execute_reply":"2022-08-25T12:06:47.543419Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Encode ordinal columns ( categorical values with dtype-object)\n\nle = LabelEncoder()\nobj = cdf.select_dtypes(include=\"object\").columns\nfor f in obj:\n    cdf[f] = le.fit_transform(cdf[f].astype(str))\n\nprint(cdf.info())\n","metadata":{"execution":{"iopub.status.busy":"2022-08-25T12:06:47.546578Z","iopub.execute_input":"2022-08-25T12:06:47.546977Z","iopub.status.idle":"2022-08-25T12:06:59.587214Z","shell.execute_reply.started":"2022-08-25T12:06:47.546941Z","shell.execute_reply":"2022-08-25T12:06:59.585882Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 🏇🏿🏇🏿 Data Transformation","metadata":{}},{"cell_type":"code","source":"# Take subset of dataset and build a deep learning model\nfdf = cdf.head(1000)\ndata = fdf.drop(['track_id','program_number','race_date','latitude','longitude','distance_id'],axis=1)\ntarget = fdf['odds']","metadata":{"execution":{"iopub.status.busy":"2022-08-25T12:06:59.591831Z","iopub.execute_input":"2022-08-25T12:06:59.592326Z","iopub.status.idle":"2022-08-25T12:06:59.599901Z","shell.execute_reply.started":"2022-08-25T12:06:59.592289Z","shell.execute_reply":"2022-08-25T12:06:59.598423Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Scale the data (transforming  data so that it fits within a specific scale, like 0-100 or 0-1)\n\nss = StandardScaler()\ndata = pd.DataFrame(ss.fit_transform(data))\nprint(data.shape, target.shape)\n\n# Split the dataset\n\nxtrain, xtest, ytrain, ytest = train_test_split(data, target, train_size = 0.8, test_size = 0.2,\n                                               random_state = 1)\nprint(xtrain.shape, xtest.shape, ytrain.shape, ytest.shape)\n\n","metadata":{"execution":{"iopub.status.busy":"2022-08-25T12:06:59.613705Z","iopub.execute_input":"2022-08-25T12:06:59.614359Z","iopub.status.idle":"2022-08-25T12:06:59.632769Z","shell.execute_reply.started":"2022-08-25T12:06:59.614321Z","shell.execute_reply":"2022-08-25T12:06:59.631596Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 🏇🏿🏇🏿Build the model ","metadata":{}},{"cell_type":"code","source":"\n\nmodel = Sequential([\n    Dense(12, input_dim=11,activation='relu'),\n    Dropout(0.5),\n    Dense(8, activation='relu'),\n    Dropout(0.25),\n    Dense(1, activation='softmax')\n])\nprint(model.summary,'\\n')\nmodel.compile(optimizer = 'adam',\n             loss = 'mean_squared_error',\n             metrics = ['mean_squared_error'])\nprint(\"******* Start training *********\")\nhistory = model.fit(xtrain,ytrain, \n          batch_size=500,verbose=1,\n          epochs = 200, validation_data = (xtest,ytest))\nprint('Done')\n","metadata":{"execution":{"iopub.status.busy":"2022-08-25T12:29:46.397904Z","iopub.execute_input":"2022-08-25T12:29:46.398369Z","iopub.status.idle":"2022-08-25T12:29:55.780227Z","shell.execute_reply.started":"2022-08-25T12:29:46.398332Z","shell.execute_reply":"2022-08-25T12:29:55.779093Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.DataFrame(history.history).head()\n","metadata":{"execution":{"iopub.status.busy":"2022-08-25T12:25:46.433853Z","iopub.execute_input":"2022-08-25T12:25:46.434680Z","iopub.status.idle":"2022-08-25T12:25:46.448726Z","shell.execute_reply.started":"2022-08-25T12:25:46.434637Z","shell.execute_reply":"2022-08-25T12:25:46.447740Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 🏇🏿🏇🏿 Model performance visualization","metadata":{}},{"cell_type":"code","source":"\nmae = 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()\n","metadata":{"execution":{"iopub.status.busy":"2022-08-25T12:27:07.738070Z","iopub.execute_input":"2022-08-25T12:27:07.738495Z","iopub.status.idle":"2022-08-25T12:27:08.170756Z","shell.execute_reply.started":"2022-08-25T12:27:07.738461Z","shell.execute_reply":"2022-08-25T12:27:08.169214Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 🏇🏿🏇🏿 Model prediction","metadata":{}},{"cell_type":"code","source":"\npred_train = model.predict(xtrain)\nprint(np.sqrt(mean_squared_error(ytrain,pred_train)))\n\npred_test = model.predict(xtest)\nprint(np.sqrt(mean_squared_error(ytest,pred_test)))","metadata":{"execution":{"iopub.status.busy":"2022-08-25T12:32:49.335011Z","iopub.execute_input":"2022-08-25T12:32:49.335534Z","iopub.status.idle":"2022-08-25T12:32:49.534884Z","shell.execute_reply.started":"2022-08-25T12:32:49.335492Z","shell.execute_reply":"2022-08-25T12:32:49.533642Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 🏇🏿🏇🏿 Model performance evaluation\n\nThe output above shows that the RMSE, which is our evaluation metric, was 1810 thousand for train data and 1876 thousand for test data. Ideally, the lower the RMSE value, the better the model performance. However, in contrast to accuracy, it is not straightforward to interpret RMSE as we would have to look at the unit which in our case is in thousands.\n\n[Regression keras](https://www.pluralsight.com/guides/regression-keras)","metadata":{}}]}