{"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":"<a href=\"https://colab.research.google.com/github/GunjanGrunge/SVHNNeuralNetwork/blob/main/nfl_big_data_bowl_2022_Neural_Network_Experiment.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>","metadata":{"id":"view-in-github"}},{"cell_type":"code","source":" ! pip install -q kaggle","metadata":{"id":"Uxox9Frt-dIg"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":" from google.colab import files\n\nfiles.upload()","metadata":{"id":"wbjkhbnb-fIB","outputId":"850c0166-b5d5-4ba8-ffd4-7c9eb6c39232"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!mkdir -p ~/.kaggle","metadata":{"id":"ERW_pd1YCwGQ"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!cp kaggle.json ~/.kaggle/","metadata":{"id":"d7hro5rzC5_P"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!ls ~/.kaggle","metadata":{"id":"UsbCF1tl-tog","outputId":"fa030eb4-1095-492e-df85-ac53c8b9d340"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"! chmod 600 ~/.kaggle/kaggle.json","metadata":{"id":"uOKU79v7_Hoo"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pip show kaggle","metadata":{"id":"3dqM1SIPArvn","outputId":"07b91f71-71e7-4c15-cdcb-5d9a3c59e345"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!kaggle competitions download nfl-big-data-bowl-2022","metadata":{"id":"Cl47B1j2ArtO","outputId":"ff4ae32c-fc48-42d6-8720-ac55cbcc69b9"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"missing_values = [\"n/a\", \"na\", \"--\",\"-\"] \nimport warnings\nimport tensorflow as tf\nfrom tensorflow import keras \ntf.test.is_gpu_available()\nimport numpy             as np\nimport pandas            as pd\npd.set_option('display.max_columns', 100)\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nfrom sklearn.preprocessing import LabelEncoder\nfrom sklearn.model_selection import train_test_split\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Activation, Dense\nfrom tensorflow.keras.layers import BatchNormalization, Dropout\nfrom tensorflow.keras import optimizers,regularizers","metadata":{"id":"AItbf-D2DMDM","outputId":"bc1b06d0-0e32-4892-d8d8-9697b3158cfe"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"\n<FONT COLOR = TEAL>\n<B> File descriptions </b>\n\n    Game data: The games.csv contains the teams playing in each game. The key variable is gameId.\n\n    Play data: The plays.csv file contains play-level information from each game. The key variables are gameId and playId.\n\n    Player data: The players.csv file contains player-level information from players that participated in any of the tracking data files. The key variable is nflId.\n\n    Tracking data: Files tracking[season].csv contain player tracking data from season [season]. The key variables are gameId, playId, and nflId.\n\n    PFF Scouting data: The PFFScoutingData.csv file contains play-level scouting information for each game. The key variables are gameId and playId*\n\n","metadata":{"id":"h_863A2IDjlD"}},{"cell_type":"code","source":"!unzip \"/content/tracking2018.csv.zip\"","metadata":{"id":"7njuhSfZEwyr","outputId":"c026143b-088a-4458-b0c8-dc9549c1e1bc"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tracking2018= pd.read_csv('/content/tracking2018.csv', index_col=False)","metadata":{"id":"fqn2X0ivDhyj"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tracking2018.head()","metadata":{"id":"Mv9ymatCEKxq","outputId":"05844f5c-90c4-41e9-8d74-7651c86033f1"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.set_style(\"darkgrid\")\nsns.displot(data=tracking2018, x=\"playDirection\")","metadata":{"id":"nw7tLaAgEM3a","outputId":"97e4e487-19b6-46c2-f29c-eaacaf8b6033"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tracking2018.fillna(tracking2018.median())","metadata":{"id":"DSnqCp6oEX0I","outputId":"524b00c8-0760-42f2-bee2-3bd7cce68416"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tracking2018.isnull().sum()","metadata":{"id":"jO5Rz1YLEqAS","outputId":"21052d3b-bb49-40ee-8205-f7365fa9dc2b"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ax = sns.heatmap(tracking2018.corr(), linewidth=0.8)\nplt.show()","metadata":{"id":"yU-cPNf-F3-5","outputId":"161ab110-57db-43e9-f7bb-59b862de08f2"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Corr between S,A, dis, dir Frame id seems to be \nconvincing \n\n---\n\nConsidering playDirection as our target variable we will keep the coorelated columns for further processing. \n\n","metadata":{"id":"oj9zwyCaIPvf"}},{"cell_type":"code","source":"tracking2018.drop(['x','y','jerseyNumber','gameId','playId','time','event','displayName','team'], axis='columns', inplace=True)","metadata":{"id":"xrMB4vNPIGbx"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tracking2018['o'].fillna(tracking2018.o.median(), inplace = True)  \ntracking2018['nflId'].fillna(tracking2018.nflId.median(), inplace = True)  \ntracking2018['dir'].fillna(tracking2018.dir.mean(), inplace = True)  ","metadata":{"id":"zr0x0eoRMy7V"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tracking2018.describe()","metadata":{"id":"lwamnUzmTcJD","outputId":"01c85d1c-5150-49c7-dab7-fc66c1dfde82"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"one_hot = pd.get_dummies(tracking2018['position'])","metadata":{"id":"M5Mj1CNKN5IO"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tracking2018 = tracking2018.drop('position',axis = 1)","metadata":{"id":"NZwxmB35PdSk"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tracking2018 = tracking2018.join(one_hot)\ntracking2018['playDirection'] = tracking2018['playDirection'].astype('bool') ","metadata":{"id":"3EqAyAurPpF1"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tracking2018.head()","metadata":{"id":"bloCwi5S0fqZ","outputId":"d3ab01dd-4956-406c-ae43-a51d5702ab28"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tracking2018.isnull().sum()","metadata":{"id":"2dxLdRmePtzU","outputId":"432e978c-cdc3-42da-b189-2c49a36c455d"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = tracking2018.drop(\"playDirection\", axis=1)\ny = tracking2018['playDirection']","metadata":{"id":"QwuLm1EpP7Cc"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# splitting into training and testing\nX_vtrain, X_test, y_vtrain, y_test = train_test_split(X, y, test_size=.30)","metadata":{"id":"DHT_TQO-TwPU"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# splitting to create training and validation data\nX_train, X_val, y_train, y_val = train_test_split(X_vtrain, y_vtrain, test_size=.20)","metadata":{"id":"_HZ2r4rZT9jq"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"datashape = X_train.shape\ndatashape","metadata":{"id":"JTmuwh7eUAFr","outputId":"3177fee4-0b9c-4a29-c659-ebb24f33bdfb"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Input Layer\n#Adding Input layer and activation functions ReLU\nnn_reg = Sequential([\n                     Dense(34, activation ='relu',input_shape= (34, ))])","metadata":{"id":"R-AnHcVDlY56"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nn_reg.add(Dense(34, activation ='relu'))\nnn_reg.add(Dense(17, activation ='relu'))\nnn_reg.add(Dense(8, activation ='relu'))\nnn_reg.add(Dense(4, activation ='relu'))\nnn_reg.add(Dense(2, activation ='relu'))\nnn_reg.add(Dense(1, activation ='softmax'))","metadata":{"id":"71bpqztXr-SR"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nn_reg.summary()","metadata":{"id":"naXWk734thnv","outputId":"f0ce9dbc-0f61-4532-ea14-baa02cb7f372"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nn_reg.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=[\"accuracy\"])","metadata":{"id":"B6RKUI2hute_"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(y_train)","metadata":{"id":"kDPXP1zPwYeh","outputId":"ac346374-efb7-4576-beb3-e0e2394133d5"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nn_reg.fit(X_train, y_train, validation_data=(X_val,y_val),epochs=5, batch_size=1000)","metadata":{"id":"Hx-WjnhdtxaQ","outputId":"33525d31-d0e1-4acf-bfb6-fd50c92cc04d"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tf.keras.utils.plot_model(nn_reg, show_shapes=True)\n","metadata":{"id":"KqIdD87o1BKO","outputId":"c552898c-da0c-4e5a-90d1-65bc7f27c372"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nlosses = pd.DataFrame(nn_reg.history.history)\nlosses.plot()\n","metadata":{"id":"ykb-0Tsv1BwB","outputId":"dac7ecd9-d484-467c-c274-6334be8a768e"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<font color = red> <b> This surely is an overfit data, however we can further use the logic to combine multiple NN to drive at an outcome after the target variable is decied. \n\n\nwe can futher feature engineer other sets and find out the best possible combination of NN to give out the desired output. ","metadata":{"id":"4qEWOhsJ1vvk"}},{"cell_type":"code","source":"","metadata":{"id":"feZJ_M822L9c"},"execution_count":null,"outputs":[]}]}