{"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\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport plotly.express as px\nimport plotly.graph_objs as go\nfrom pathlib import Path\n\n#Ignore warnings\nimport warnings\nwarnings.filterwarnings('ignore')\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","_kg_hide-input":true,"_kg_hide-output":true,"execution":{"iopub.status.busy":"2021-09-25T22:44:03.047147Z","iopub.execute_input":"2021-09-25T22:44:03.047529Z","iopub.status.idle":"2021-09-25T22:44:03.058720Z","shell.execute_reply.started":"2021-09-25T22:44:03.047497Z","shell.execute_reply":"2021-09-25T22:44:03.058097Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<center style=\"font-family:verdana;\"><h1 style=\"font-size:200%; padding: 20px; background: black;\"><i><b style=\"color:#DEB887;\">NFL Bowl 2022 - Plays and Players</b></i></h1></center>","metadata":{}},{"cell_type":"markdown","source":"![](https://images.daznservices.com/di/library/sporting_news/e7/b2/2022-nfl-draft-big-board_9ege0v5zsuwa1hofx39butfta.jpg?t=476393145&quality=100)sportingnews.com","metadata":{}},{"cell_type":"code","source":"import warnings\nwarnings.filterwarnings('ignore')\n\nimport shap\nimport lightgbm as lgb\nfrom sklearn.preprocessing import LabelEncoder\nfrom sklearn.model_selection import KFold\nimport random","metadata":{"execution":{"iopub.status.busy":"2021-09-25T22:44:08.668940Z","iopub.execute_input":"2021-09-25T22:44:08.669607Z","iopub.status.idle":"2021-09-25T22:44:08.675001Z","shell.execute_reply.started":"2021-09-25T22:44:08.669564Z","shell.execute_reply":"2021-09-25T22:44:08.674239Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"players = pd.read_csv('../input/nfl-big-data-bowl-2022/players.csv')\nplayers.head()","metadata":{"execution":{"iopub.status.busy":"2021-09-25T22:44:15.356188Z","iopub.execute_input":"2021-09-25T22:44:15.356884Z","iopub.status.idle":"2021-09-25T22:44:15.392993Z","shell.execute_reply.started":"2021-09-25T22:44:15.356837Z","shell.execute_reply":"2021-09-25T22:44:15.392236Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h1><span class=\"label label-default\" style=\"background-color:#8B4513;border-radius:100px 100px; font-weight: bold; font-family:Garamond; font-size:20px; color:#DEB887; padding:10px\">NFL Abbreviations</span></h1><br>\n\nAbbreviation\tPosition\n\nQB\tQuarterback\n\nRB\tRunning Back\n\nFB\tFullback\n\nWR\tWide Receiver\n\nTE\tTight End\n\nOL\tOffensive Lineman\n\nC\tCenter\n\nG\tGuard\n\nLG\tLeft Guard\n\nRG\tRight Guard\n\nT\tTackle\n\nLT\tLeft Tackle\n\nRT\tRight Tackle\n\nK\tKicker\n\nKR\tKick Returner\n\nDL\tDefensive Lineman\n\nDE\tDefensive End\n\nDT\tDefensive Tackle\n\nNT\tNose Tackle\n\nLB\tLinebacker\n\nILB\tInside Linebacker\n\nOLB\tOutside Linebacker\n\nMLB\tMiddle Linebacker\n\nDB\tDefensive Back\n\nCB\tCornerback\n\nFS\tFree Safety\n\nSS\tStrong Safety\n\nS\tSafety\n\nP\tPunter\n\nPR\tPunt Returner\n\nhttp://stats.washingtonpost.com/fb/glossary.asp","metadata":{}},{"cell_type":"code","source":"mean=np.ceil(players['weight'].mean())\nmedian=np.ceil(players['weight'].median())","metadata":{"execution":{"iopub.status.busy":"2021-09-25T22:44:58.814972Z","iopub.execute_input":"2021-09-25T22:44:58.815813Z","iopub.status.idle":"2021-09-25T22:44:58.822499Z","shell.execute_reply.started":"2021-09-25T22:44:58.815774Z","shell.execute_reply":"2021-09-25T22:44:58.821901Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"![](https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQBJ4LtgNbo9atvDby0C-pp5tP0ab0_WnIh4Q&usqp=CAU)sportsnaut.com","metadata":{}},{"cell_type":"code","source":"#Code by Chinta https://www.kaggle.com/chinta/mlb-is-player-age-important\n\nplt.figure(figsize=(10, 5))\nsns.set_style('white')\nhist_plot = sns.histplot(players['weight'], )\nhist_plot.axvline(mean, color='r', linestyle='--', linewidth = 4, label = f'mean-{mean}')\nhist_plot.axvline(median, color='g', linestyle='-', linewidth = 4, label = f'median-{median}')\nplt.suptitle(\"Players Weight Distribution\")\nplt.legend();","metadata":{"execution":{"iopub.status.busy":"2021-09-25T22:45:06.634437Z","iopub.execute_input":"2021-09-25T22:45:06.635354Z","iopub.status.idle":"2021-09-25T22:45:07.219699Z","shell.execute_reply.started":"2021-09-25T22:45:06.635311Z","shell.execute_reply":"2021-09-25T22:45:07.218822Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"![](https://nflstatic.s3.amazonaws.com/attachments/ckd0czhw40o680imyjdsqysfy-offensive-vs-defensivev2.max.png)nflflag.com","metadata":{}},{"cell_type":"code","source":"scouting = pd.read_csv('../input/nfl-big-data-bowl-2022/PFFScoutingData.csv')\nscouting.head(2)","metadata":{"execution":{"iopub.status.busy":"2021-09-25T22:45:23.746657Z","iopub.execute_input":"2021-09-25T22:45:23.747633Z","iopub.status.idle":"2021-09-25T22:45:23.862990Z","shell.execute_reply.started":"2021-09-25T22:45:23.747581Z","shell.execute_reply":"2021-09-25T22:45:23.862109Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h1><span class=\"label label-default\" style=\"background-color:#8B4513;border-radius:100px 100px; font-weight: bold; font-family:Garamond; font-size:20px; color:#DEB887; padding:10px\">Receptions, Catches, Laterals</span></h1><br>\n\n#That's the moment I'm really going crazy: We have R and C in Ventilator Pressure Prediction Contest. Above we see R and C (kickDirectionIntended, kickDirectionActual,returnDirectionIntended, returnDirectionActual) columns. \n\nMaybe it's Reception/Catch/Lateral.\n\n\"When a player catches (receives) the ball past the line of scrimmage. If a RECEPTION is made behind the line of scrimmage, it is a LATERAL. Another name for a CATCH.\"\n\nhttps://en.wikipedia.org/wiki/Glossary_of_American_football_terms","metadata":{}},{"cell_type":"markdown","source":"![](https://pbs.twimg.com/media/DMSJFq1WkAAv6BR.jpg)twitter.com","metadata":{}},{"cell_type":"code","source":"tracking = pd.read_csv('../input/nfl-big-data-bowl-2022/tracking2020.csv')\ntracking.head(2)","metadata":{"execution":{"iopub.status.busy":"2021-09-25T22:46:57.201698Z","iopub.execute_input":"2021-09-25T22:46:57.202228Z","iopub.status.idle":"2021-09-25T22:47:35.491176Z","shell.execute_reply.started":"2021-09-25T22:46:57.202190Z","shell.execute_reply":"2021-09-25T22:47:35.490239Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h1><span class=\"label label-default\" style=\"background-color:#8B4513;border-radius:100px 100px; font-weight: bold; font-family:Garamond; font-size:20px; color:#DEB887; padding:10px\">Jersey Numbers: Squad Numbers</span></h1><br>\n\nJersey Numbers:\n\n\"Numbers 1 to 19 are worn by quarterbacks, kickers, and punters. Since 2004, wide receivers are also allowed to wear numbers between 10 and 19 if they so choose, even if there is an 80-89 number available. In the rare event that all numbers from 10-19 and 80-89 are taken, receivers are allowed to wear single digit numbers, such as Biren Ealy, current NFL Free Agent, on the Baltimore Ravens then wore a single digit number in the 2009 Preseason.\"\n\n\n\"Numbers 20 to 49 are worn by running backs, tight ends (who may wear a number between 40-49 when 80-89 are unavailable), cornerbacks and safeties.\"\n\n\"Numbers 50 to 59 are worn by linebackers and centers.\"\n\n\"Numbers 60 to 79 are worn by tackles, guards and defensive linemen (the defensive ends, defensive tackles or nose guard).\"\n\n\"Numbers 80 to 89 are worn by wide receivers and tight ends.\"\n\n\"Numbers 90 to 99 are worn by linebackers and defensive linemen. In use since 1984.\"\n\n\"Numbers 0 and 00 are no longer used, though they were issued in the NFL before the number standardization in 1973. George Plimpton wore 0 during a brief preseason stint at quarterback for the Detroit Lions. Jim Otto (\"aught-oh\") wore number \"00\" during most of his career with the Oakland Raiders. Wide receiver Ken Burrough of the Houston Oilers also wore \"00\" during his NFL career in the 1970s.\"\n\nhttps://americanfootball.fandom.com/wiki/Squad_number","metadata":{}},{"cell_type":"code","source":"tracking19 = pd.read_csv('../input/nfl-big-data-bowl-2022/tracking2019.csv')\ntracking19.head(2)","metadata":{"execution":{"iopub.status.busy":"2021-09-25T22:49:13.948538Z","iopub.execute_input":"2021-09-25T22:49:13.949161Z","iopub.status.idle":"2021-09-25T22:49:52.963611Z","shell.execute_reply.started":"2021-09-25T22:49:13.949106Z","shell.execute_reply":"2021-09-25T22:49:52.962939Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"![](http://static1.squarespace.com/static/54aac868e4b00f7c5fbf0a24/54ad9112e4b00b45194e9d3f/558452a5e4b0875de16d1201/1434736328179/?format=1500w)makegamedayeveryday.com","metadata":{}},{"cell_type":"code","source":"tracking18 = pd.read_csv('../input/nfl-big-data-bowl-2022/tracking2018.csv')\ntracking18.head(2)","metadata":{"execution":{"iopub.status.busy":"2021-09-25T22:49:57.027608Z","iopub.execute_input":"2021-09-25T22:49:57.028409Z","iopub.status.idle":"2021-09-25T22:50:38.217487Z","shell.execute_reply.started":"2021-09-25T22:49:57.028367Z","shell.execute_reply":"2021-09-25T22:50:38.216871Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"games = pd.read_csv('../input/nfl-big-data-bowl-2022/games.csv')\ngames.head()","metadata":{"execution":{"iopub.status.busy":"2021-09-25T23:01:30.858833Z","iopub.execute_input":"2021-09-25T23:01:30.859872Z","iopub.status.idle":"2021-09-25T23:01:30.888897Z","shell.execute_reply.started":"2021-09-25T23:01:30.859830Z","shell.execute_reply":"2021-09-25T23:01:30.887963Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"![](https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRnOP38qQrRtc32f5eG3pzQh0K_iRQOMgq5Wg&usqp=CAU)twitter.com","metadata":{}},{"cell_type":"code","source":"plays = pd.read_csv('../input/nfl-big-data-bowl-2022/plays.csv')\nplays.head(2)","metadata":{"execution":{"iopub.status.busy":"2021-09-25T23:01:44.528489Z","iopub.execute_input":"2021-09-25T23:01:44.529383Z","iopub.status.idle":"2021-09-25T23:01:44.687408Z","shell.execute_reply.started":"2021-09-25T23:01:44.529337Z","shell.execute_reply":"2021-09-25T23:01:44.686666Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h1><span class=\"label label-default\" style=\"background-color:#8B4513;border-radius:100px 100px; font-weight: bold; font-family:Garamond; font-size:20px; color:#DEB887; padding:10px\">American Football Terms</span></h1><br>\n\nWhat is a TOUCHBACK?\n\n\"A situation in football in which the ball is down behind the goal line after a kick or intercepted forward pass after which it is put in play by the team defending the goal on its own 20-yard line — compare safety.\"\n\nDefinition of YARD LINE: \n\nAny of a series of marked or imaginary lines one yard apart on a football field that are parallel to the goal lines and that indicate the distance to the nearest goal line\n\nKICKOFF\n\nA free kick which starts each half, or restarts the game following a touchdown or field goal. The kickoff may be a place kick in American or Canadian football, or a drop kick in American football.\n\nhttps://www.merriam-webster.com/dictionary\n\nhttps://en.wikipedia.org/wiki/Glossary_of_American_football_terms","metadata":{}},{"cell_type":"code","source":"plays.isnull().sum()","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2021-09-25T23:11:16.529625Z","iopub.execute_input":"2021-09-25T23:11:16.529940Z","iopub.status.idle":"2021-09-25T23:11:16.561698Z","shell.execute_reply.started":"2021-09-25T23:11:16.529894Z","shell.execute_reply":"2021-09-25T23:11:16.560786Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#word cloud\nfrom wordcloud import WordCloud, ImageColorGenerator\ntext = \" \".join(str(each) for each in plays.specialTeamsPlayType)\n# Create and generate a word cloud image:\nwordcloud = WordCloud(max_words=200,colormap='Set3', background_color=\"black\").generate(text)\nplt.figure(figsize=(10,6))\nplt.figure(figsize=(15,10))\n# Display the generated image:\nplt.imshow(wordcloud, interpolation='Bilinear')\nplt.axis(\"off\")\nplt.figure(1,figsize=(12, 12))\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-09-25T23:11:30.242487Z","iopub.execute_input":"2021-09-25T23:11:30.243156Z","iopub.status.idle":"2021-09-25T23:11:30.898921Z","shell.execute_reply.started":"2021-09-25T23:11:30.243102Z","shell.execute_reply":"2021-09-25T23:11:30.885374Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"playscorr=plays.corr()\nplayscorr","metadata":{"execution":{"iopub.status.busy":"2021-09-25T23:13:12.992171Z","iopub.execute_input":"2021-09-25T23:13:12.992488Z","iopub.status.idle":"2021-09-25T23:13:13.037516Z","shell.execute_reply.started":"2021-09-25T23:13:12.992458Z","shell.execute_reply":"2021-09-25T23:13:13.036709Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(10,4))\nsns.heatmap(plays.corr(),annot=False,cmap='summer')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-09-25T23:13:28.287467Z","iopub.execute_input":"2021-09-25T23:13:28.288308Z","iopub.status.idle":"2021-09-25T23:13:28.773533Z","shell.execute_reply.started":"2021-09-25T23:13:28.288261Z","shell.execute_reply":"2021-09-25T23:13:28.772733Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Code by Thor the God of Thunder\n\ncat = []\nnum = []\nfor col in tracking.columns:\n    if tracking[col].dtype=='O':\n        cat.append(col)\n    else:\n        num.append(col)  \n        \n        \nnum ","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2021-09-25T23:13:42.263143Z","iopub.execute_input":"2021-09-25T23:13:42.264025Z","iopub.status.idle":"2021-09-25T23:13:42.274081Z","shell.execute_reply.started":"2021-09-25T23:13:42.263980Z","shell.execute_reply":"2021-09-25T23:13:42.273497Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#word cloud\nfrom wordcloud import WordCloud, ImageColorGenerator\ntext = \" \".join(str(each) for each in plays.specialTeamsResult)\n# Create and generate a word cloud image:\nwordcloud = WordCloud(max_words=200,colormap='GnBu', background_color=\"white\").generate(text)\nplt.figure(figsize=(10,6))\nplt.figure(figsize=(15,10))\n# Display the generated image:\nplt.imshow(wordcloud, interpolation='Bilinear')\nplt.axis(\"off\")\nplt.figure(1,figsize=(12, 12))\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-09-25T23:17:25.366664Z","iopub.execute_input":"2021-09-25T23:17:25.367485Z","iopub.status.idle":"2021-09-25T23:17:25.959095Z","shell.execute_reply.started":"2021-09-25T23:17:25.367441Z","shell.execute_reply":"2021-09-25T23:17:25.958274Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Code by Thor the God of Thunder\n\n#plt.style.use('dark_background') #dark ruined my Shap values \nfor col in tracking[num].drop(['playId'],axis=1):\n    plt.figure(figsize=(8,5))\n    plt.plot(tracking[col].value_counts(),color='Red')\n    plt.xlabel(col)\n    plt.ylabel('playId')\n    plt.tight_layout()\n    plt.show()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2021-09-25T23:18:17.995786Z","iopub.execute_input":"2021-09-25T23:18:17.996277Z","iopub.status.idle":"2021-09-25T23:18:25.017762Z","shell.execute_reply.started":"2021-09-25T23:18:17.996237Z","shell.execute_reply":"2021-09-25T23:18:25.017015Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#In spite of the charts above, I still don't know the meaning of x, y, s, a, dis, o features.","metadata":{}},{"cell_type":"code","source":"#codes from Rodrigo Lima  @rodrigolima82\nfrom IPython.display import Image\nImage(url = 'https://www.rookieroad.com/img/football/football-touchback.png',width=400,height=400)","metadata":{"execution":{"iopub.status.busy":"2021-09-25T23:27:44.269649Z","iopub.execute_input":"2021-09-25T23:27:44.269950Z","iopub.status.idle":"2021-09-25T23:27:44.276475Z","shell.execute_reply.started":"2021-09-25T23:27:44.269920Z","shell.execute_reply":"2021-09-25T23:27:44.275918Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Code by rossinEndrew https://www.kaggle.com/endrewrossin/fast-initial-lightgbm-model-to-detect-exam-result/comments\n\nSEED = 99\nrandom.seed(SEED)\nnp.random.seed(SEED)","metadata":{"execution":{"iopub.status.busy":"2021-09-25T23:19:05.278241Z","iopub.execute_input":"2021-09-25T23:19:05.278538Z","iopub.status.idle":"2021-09-25T23:19:05.285255Z","shell.execute_reply.started":"2021-09-25T23:19:05.278509Z","shell.execute_reply":"2021-09-25T23:19:05.284264Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Code by rossinEndrew https://www.kaggle.com/endrewrossin/fast-initial-lightgbm-model-to-detect-exam-result/comments\n\nplaysmodel = plays.copy()\n\n# read the \"object\" columns and use labelEncoder to transform to numeric\nfor col in playsmodel.columns[playsmodel.dtypes == 'object']:\n    le = LabelEncoder()\n    playsmodel[col] = playsmodel[col].astype(str)\n    le.fit(playsmodel[col])\n    playsmodel[col] = le.transform(playsmodel[col])","metadata":{"execution":{"iopub.status.busy":"2021-09-25T23:19:10.800153Z","iopub.execute_input":"2021-09-25T23:19:10.800473Z","iopub.status.idle":"2021-09-25T23:19:10.955192Z","shell.execute_reply.started":"2021-09-25T23:19:10.800443Z","shell.execute_reply":"2021-09-25T23:19:10.954308Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#change columns names to alphanumeric\nplaysmodel.columns = [\"\".join (c if c.isalnum() else \"_\" for c in str(x)) for x in playsmodel.columns]","metadata":{"execution":{"iopub.status.busy":"2021-09-25T23:19:17.221210Z","iopub.execute_input":"2021-09-25T23:19:17.221857Z","iopub.status.idle":"2021-09-25T23:19:17.227680Z","shell.execute_reply.started":"2021-09-25T23:19:17.221816Z","shell.execute_reply":"2021-09-25T23:19:17.226987Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = playsmodel.drop(['playResult','playId'], axis = 1)\ny = playsmodel['playResult']","metadata":{"execution":{"iopub.status.busy":"2021-09-25T23:19:21.820403Z","iopub.execute_input":"2021-09-25T23:19:21.820694Z","iopub.status.idle":"2021-09-25T23:19:21.841275Z","shell.execute_reply.started":"2021-09-25T23:19:21.820668Z","shell.execute_reply":"2021-09-25T23:19:21.840517Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lgb_params = {\n                    'objective':'binary',\n                    'metric':'auc',\n                    'n_jobs':-1,\n                    'learning_rate':0.005,\n                    'num_leaves': 20,\n                    'max_depth':-1,\n                    'subsample':0.9,\n                    'n_estimators':2500,\n                    'seed': SEED,\n                    'early_stopping_rounds':100, \n                }","metadata":{"execution":{"iopub.status.busy":"2021-09-25T23:19:27.718919Z","iopub.execute_input":"2021-09-25T23:19:27.719953Z","iopub.status.idle":"2021-09-25T23:19:27.725458Z","shell.execute_reply.started":"2021-09-25T23:19:27.719891Z","shell.execute_reply":"2021-09-25T23:19:27.724578Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Code by rossinEndrew https://www.kaggle.com/endrewrossin/fast-initial-lightgbm-model-to-detect-exam-result/comments\n\n# choose the number of folds, and create a variable to store the auc values and the iteration values.\nK = 5\nfolds = KFold(K, shuffle = True, random_state = SEED)\nbest_scorecv= 0\nbest_iteration=0\n\n# Separate data in folds, create train and validation dataframes, train the model and cauculate the mean AUC.\nfor fold , (train_index,test_index) in enumerate(folds.split(X, y)):\n    print('Fold:',fold+1)\n          \n    X_traincv, X_testcv = X.iloc[train_index], X.iloc[test_index]\n    y_traincv, y_testcv = y.iloc[train_index], y.iloc[test_index]\n    \n    train_data = lgb.Dataset(X_traincv, y_traincv)\n    val_data   = lgb.Dataset(X_testcv, y_testcv)\n    \n    LGBM = lgb.train(lgb_params, train_data, valid_sets=[train_data,val_data], verbose_eval=250)\n    best_scorecv += LGBM.best_score['valid_1']['auc']\n    best_iteration += LGBM.best_iteration\n\nbest_scorecv /= K\nbest_iteration /= K\nprint('\\n Mean AUC score:', best_scorecv)\nprint('\\n Mean best iteration:', best_iteration)","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2021-09-25T23:19:32.530513Z","iopub.execute_input":"2021-09-25T23:19:32.530811Z","iopub.status.idle":"2021-09-25T23:19:34.713994Z","shell.execute_reply.started":"2021-09-25T23:19:32.530782Z","shell.execute_reply":"2021-09-25T23:19:34.713064Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Code by rossinEndrew https://www.kaggle.com/endrewrossin/fast-initial-lightgbm-model-to-detect-exam-result/comments\n\nlgb_params = {\n                    'objective':'binary',\n                    'metric':'auc',\n                    'n_jobs':-1,\n                    'learning_rate':0.05,\n                    'num_leaves': 20,\n                    'max_depth':-1,\n                    'subsample':0.9,\n                    'n_estimators':round(best_iteration),\n                    'seed': SEED,\n                    'early_stopping_rounds':None, \n                }\n\ntrain_data_final = lgb.Dataset(X, y)\nLGBM = lgb.train(lgb_params, train_data)","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2021-09-25T23:19:45.056108Z","iopub.execute_input":"2021-09-25T23:19:45.056406Z","iopub.status.idle":"2021-09-25T23:19:45.138568Z","shell.execute_reply.started":"2021-09-25T23:19:45.056377Z","shell.execute_reply":"2021-09-25T23:19:45.137697Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(LGBM)","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2021-09-25T23:19:53.533047Z","iopub.execute_input":"2021-09-25T23:19:53.533904Z","iopub.status.idle":"2021-09-25T23:19:53.538330Z","shell.execute_reply.started":"2021-09-25T23:19:53.533870Z","shell.execute_reply":"2021-09-25T23:19:53.537793Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# telling wich model to use\nexplainer = shap.TreeExplainer(LGBM)\n# Calculating the Shap values of X features\nshap_values = explainer.shap_values(X)","metadata":{"execution":{"iopub.status.busy":"2021-09-25T23:20:02.284828Z","iopub.execute_input":"2021-09-25T23:20:02.285647Z","iopub.status.idle":"2021-09-25T23:20:04.189655Z","shell.execute_reply.started":"2021-09-25T23:20:02.285588Z","shell.execute_reply":"2021-09-25T23:20:04.188762Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"shap.summary_plot(shap_values[1], X, plot_type=\"bar\")","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2021-09-25T23:20:08.370284Z","iopub.execute_input":"2021-09-25T23:20:08.370566Z","iopub.status.idle":"2021-09-25T23:20:08.759464Z","shell.execute_reply.started":"2021-09-25T23:20:08.370536Z","shell.execute_reply":"2021-09-25T23:20:08.758608Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"shap.summary_plot(shap_values[1], X)","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2021-09-25T23:20:17.847618Z","iopub.execute_input":"2021-09-25T23:20:17.848134Z","iopub.status.idle":"2021-09-25T23:20:22.826752Z","shell.execute_reply.started":"2021-09-25T23:20:17.848082Z","shell.execute_reply":"2021-09-25T23:20:22.825864Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Are you going to stick up for your teammates in this competition?\n\n#This is what you call sticking up for your teammates\n\nhttps://www.youtube.com/watch?v=i8wkptMptOE ","metadata":{}},{"cell_type":"markdown","source":"<iframe width=\"956\" height=\"538\" src=\"https://www.youtube.com/embed/i8wkptMptOE\" title=\"YouTube video player\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen></iframe>","metadata":{}},{"cell_type":"markdown","source":"#Since the Label Encoder above didn't work, I encoded again so that I could see the density of some features.","metadata":{}},{"cell_type":"code","source":"from sklearn.preprocessing import LabelEncoder\n\n#fill in mean for floats\nfor c in plays.columns:\n    if plays[c].dtype=='float16' or  plays[c].dtype=='float32' or  plays[c].dtype=='float64':\n        plays[c].fillna(plays[c].mean())\n\n#fill in -999 for categoricals\nplays = plays.fillna(-999)\n# Label Encoding\nfor f in plays.columns:\n    if plays[f].dtype=='object': \n        lbl = LabelEncoder()\n        lbl.fit(list(plays[f].values))\n        plays[f] = lbl.transform(list(plays[f].values))\n        \nprint('Labelling done.')","metadata":{"execution":{"iopub.status.busy":"2021-09-25T23:20:37.911553Z","iopub.execute_input":"2021-09-25T23:20:37.911845Z","iopub.status.idle":"2021-09-25T23:20:38.645615Z","shell.execute_reply.started":"2021-09-25T23:20:37.911816Z","shell.execute_reply":"2021-09-25T23:20:38.644688Z"},"_kg_hide-output":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Code by Tudor Ioan  https://www.kaggle.com/tudoale1/heart-failure-prediction-using-adasys\n\ndef plotting(col): #function to plot data\n    plt.figure(figsize=(5, 3))\n    sns.distplot(plays[col])\n\nfor col in plays:\n    plotting(col)","metadata":{"_kg_hide-output":false,"execution":{"iopub.status.busy":"2021-09-25T23:20:49.377696Z","iopub.execute_input":"2021-09-25T23:20:49.378542Z","iopub.status.idle":"2021-09-25T23:21:03.255360Z","shell.execute_reply.started":"2021-09-25T23:20:49.378493Z","shell.execute_reply":"2021-09-25T23:21:03.254665Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"brady = players[(players['displayName']=='Tom Brady')].reset_index(drop=True)\nbrady.head()","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2021-09-26T00:45:19.978687Z","iopub.execute_input":"2021-09-26T00:45:19.979015Z","iopub.status.idle":"2021-09-26T00:45:19.992914Z","shell.execute_reply.started":"2021-09-26T00:45:19.978983Z","shell.execute_reply":"2021-09-26T00:45:19.992044Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#I couldn't find Tom Brady. Anyhow....","metadata":{}},{"cell_type":"markdown","source":"![](https://media0.giphy.com/media/Nj4QsiCRlqIUuVji0B/200w.webp?cid=ecf05e47keq40ler7q04o5hamk99zdqniwcm3hhr49lzai89&rid=200w.webp&ct=g)","metadata":{"execution":{"iopub.status.busy":"2021-09-25T01:42:55.284125Z","iopub.execute_input":"2021-09-25T01:42:55.284505Z","iopub.status.idle":"2021-09-25T01:42:55.291853Z","shell.execute_reply.started":"2021-09-25T01:42:55.284468Z","shell.execute_reply":"2021-09-25T01:42:55.290295Z"}}}]}