{"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":"The theme of this notebook is Returner.\nBecause the audience gets excited when the returner runs a long distance.\nMe too. I've been a fan of the NFL since 1992. for example, Tom Rathman, Christian Okoye, John Taylor, Roger Craig, Bo Jackson,etc.. \n    \nanyway, I'm a beginner in PYTHON. The training started on December 29th, and the following are the results of 5 days.\nI will continue to study and challenge KAGGLE. In particular, I will do my best to automatically extract the features of human relationships using DGL and GCNN.\n\n\nBy the way, the feature of the returner that I paid attention to is the blocker player who runs in front of the returner.\nI hypothesized that if the returner runs in hiding behind three blockers, he can run a longer distance invisible to the opposing team.\n\nIt's like Gundam's Black Tri-Stars.　you know?\nTherefore, I want the track data to have an explanatory variable for the affinity between the three and the returner. For example, it is the affinity that I went to when I was taller and faster than the returner. By doing so, I thought that the relationship between the returner and the blocker could be connected and aggregated at the edge of the DGL to prove the hypothesis of the triple star.\n\nIn this analysis, there is not enough DGL edge data,\nSo I want to continue the analysis.\nIn unpredictable sports where diverse human resources continue to change, I think that we can build a stronger team by taking action on our own and embodying the ideal outline of the special team of the future.\n    \n\nSpecial thanks to NFL Big Data Bowl Basic EDA for beginner.\nSpecial thanks to ”Win with Kaggle Data analysis technology” to all of authors and Gijutsu-Hyoron Co., Ltd..I referred to the sample code.\n\n","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport seaborn as sns\nimport matplotlib as mpl\nimport matplotlib.pyplot as plt\n\nplt.style.use('fivethirtyeight')","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_kg_hide-input":true,"_kg_hide-output":false,"execution":{"iopub.status.busy":"2022-01-15T04:58:39.881977Z","iopub.execute_input":"2022-01-15T04:58:39.882425Z","iopub.status.idle":"2022-01-15T04:58:39.983608Z","shell.execute_reply.started":"2022-01-15T04:58:39.882389Z","shell.execute_reply":"2022-01-15T04:58:39.982656Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plays = pd.read_csv('../input/nfl-big-data-bowl-2022/plays.csv')\n\n#plays","metadata":{"execution":{"iopub.status.busy":"2022-01-15T04:58:43.679847Z","iopub.execute_input":"2022-01-15T04:58:43.680779Z","iopub.status.idle":"2022-01-15T04:58:43.860427Z","shell.execute_reply.started":"2022-01-15T04:58:43.680720Z","shell.execute_reply":"2022-01-15T04:58:43.859367Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"scores = plays['returnerId'] \n#plt.hist(scores.dropna(), cumulative=True, density=True, bins='auto')\n\n","metadata":{"execution":{"iopub.status.busy":"2022-01-15T04:58:46.258843Z","iopub.execute_input":"2022-01-15T04:58:46.259587Z","iopub.status.idle":"2022-01-15T04:58:46.269073Z","shell.execute_reply.started":"2022-01-15T04:58:46.259539Z","shell.execute_reply":"2022-01-15T04:58:46.267985Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plays.describe()\n","metadata":{"execution":{"iopub.status.busy":"2022-01-15T04:58:49.426285Z","iopub.execute_input":"2022-01-15T04:58:49.426986Z","iopub.status.idle":"2022-01-15T04:58:49.498531Z","shell.execute_reply.started":"2022-01-15T04:58:49.426932Z","shell.execute_reply":"2022-01-15T04:58:49.497148Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import plotly.express as px\n\n#棒グラフを作成\nfig = px.histogram(plays, x = 'returnerId')\n#軸の設定等\n#fig.update_layout(width=100,height=50,margin=dict(l=10,r=10,b=5,t=5),\n#                 yaxis=dict(range=[0,10]),xaxis=dict(dtick=1, autorange=True),title=dict(font = dict(size=20)))\n\n#データを並び替え\n#fig = px.bar(scores)\nfig.update_layout(xaxis={'categoryorder':'total descending'})\n#グラフの表示\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2022-01-15T04:58:52.860063Z","iopub.execute_input":"2022-01-15T04:58:52.860512Z","iopub.status.idle":"2022-01-15T04:58:55.107293Z","shell.execute_reply.started":"2022-01-15T04:58:52.860471Z","shell.execute_reply":"2022-01-15T04:58:55.106050Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plays.dropna(subset=['kickReturnYardage'])\n\nplays = plays.drop(['playResult'], axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-01-15T04:58:58.520976Z","iopub.execute_input":"2022-01-15T04:58:58.521394Z","iopub.status.idle":"2022-01-15T04:58:58.556704Z","shell.execute_reply.started":"2022-01-15T04:58:58.521351Z","shell.execute_reply":"2022-01-15T04:58:58.555750Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"PFFS = pd.read_csv('../input/nfl-big-data-bowl-2022/PFFScoutingData.csv')\n\nplays = pd.merge(plays, PFFS, on=['gameId', 'playId'])","metadata":{"execution":{"iopub.status.busy":"2022-01-15T04:59:01.765452Z","iopub.execute_input":"2022-01-15T04:59:01.765962Z","iopub.status.idle":"2022-01-15T04:59:01.954546Z","shell.execute_reply.started":"2022-01-15T04:59:01.765916Z","shell.execute_reply":"2022-01-15T04:59:01.953495Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"track2020 = pd.read_csv('../input/nfl-big-data-bowl-2022/tracking2020.csv')\ntrack2020 = track2020.rename(columns={'nflId':'returnerId'})\n","metadata":{"execution":{"iopub.status.busy":"2022-01-15T04:59:04.557110Z","iopub.execute_input":"2022-01-15T04:59:04.557538Z","iopub.status.idle":"2022-01-15T05:00:02.334483Z","shell.execute_reply.started":"2022-01-15T04:59:04.557502Z","shell.execute_reply":"2022-01-15T05:00:02.333220Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plays['returnerId'] = plays['returnerId'].str.replace(';','')\nplays['returnerId'] = plays['returnerId'].astype(float)","metadata":{"execution":{"iopub.status.busy":"2022-01-15T05:00:26.727461Z","iopub.execute_input":"2022-01-15T05:00:26.728474Z","iopub.status.idle":"2022-01-15T05:00:26.755582Z","shell.execute_reply.started":"2022-01-15T05:00:26.728403Z","shell.execute_reply":"2022-01-15T05:00:26.754412Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plays = pd.merge(plays, track2020, on=['gameId', 'playId','returnerId'], how='left')","metadata":{"execution":{"iopub.status.busy":"2022-01-15T05:00:29.068464Z","iopub.execute_input":"2022-01-15T05:00:29.069035Z","iopub.status.idle":"2022-01-15T05:00:38.958925Z","shell.execute_reply.started":"2022-01-15T05:00:29.069001Z","shell.execute_reply":"2022-01-15T05:00:38.957864Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#print(\"plays\", plays.shape)\nplays.dtypes","metadata":{"execution":{"iopub.status.busy":"2022-01-15T05:00:41.443308Z","iopub.execute_input":"2022-01-15T05:00:41.443741Z","iopub.status.idle":"2022-01-15T05:00:41.458942Z","shell.execute_reply.started":"2022-01-15T05:00:41.443708Z","shell.execute_reply":"2022-01-15T05:00:41.457299Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plays = plays.drop(['time','gameId','playId','playDescription','x','y'], axis=1)\n","metadata":{"execution":{"iopub.status.busy":"2022-01-15T05:00:45.223719Z","iopub.execute_input":"2022-01-15T05:00:45.224246Z","iopub.status.idle":"2022-01-15T05:00:46.131225Z","shell.execute_reply.started":"2022-01-15T05:00:45.224203Z","shell.execute_reply":"2022-01-15T05:00:46.130087Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plays.dtypes","metadata":{"execution":{"iopub.status.busy":"2022-01-15T05:00:48.234769Z","iopub.execute_input":"2022-01-15T05:00:48.235588Z","iopub.status.idle":"2022-01-15T05:00:48.246878Z","shell.execute_reply.started":"2022-01-15T05:00:48.235529Z","shell.execute_reply":"2022-01-15T05:00:48.245938Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ntrain_x = plays.dropna(subset=['kickReturnYardage']).drop(['kickReturnYardage'], axis=1)\ntrain_y = plays.dropna(subset=['kickReturnYardage'])['kickReturnYardage']\n\ntrain_x\n\n\n","metadata":{"execution":{"iopub.status.busy":"2022-01-15T05:00:51.482073Z","iopub.execute_input":"2022-01-15T05:00:51.483319Z","iopub.status.idle":"2022-01-15T05:00:51.968409Z","shell.execute_reply.started":"2022-01-15T05:00:51.483246Z","shell.execute_reply":"2022-01-15T05:00:51.967095Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#train_x.dtypes\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.set_option('display.max_columns', 80)\npd.set_option('display.max_rows', 80)\ntrain_x.describe(include='all')\n\n","metadata":{"execution":{"iopub.status.busy":"2022-01-15T05:00:57.913469Z","iopub.execute_input":"2022-01-15T05:00:57.913929Z","iopub.status.idle":"2022-01-15T05:00:59.778465Z","shell.execute_reply.started":"2022-01-15T05:00:57.913887Z","shell.execute_reply":"2022-01-15T05:00:59.777271Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import LabelEncoder\nfrom sklearn.model_selection import train_test_split\n\nfor c in ['possessionTeam', 'specialTeamsPlayType','specialTeamsResult','penaltyCodes','penaltyJerseyNumbers', 'yardlineSide', 'gameClock', 'passResult', 'snapDetail', 'kickType','kickDirectionIntended','kickDirectionActual','returnDirectionIntended','returnDirectionActual','missedTackler', 'assistTackler', 'tackler', 'kickoffReturnFormation', 'gunners', 'puntRushers', 'specialTeamsSafeties', 'vises', 'kickContactType', 'event', 'displayName', 'position', 'team', 'playDirection']:\n    le = LabelEncoder()\n    le.fit(train_x[c].fillna('NA'))\n    \n    train_x[c] = le.transform(train_x[c].fillna('NA'))\n#   test_x[c] = le.transform(test_x[c].fillna('NA'))\n    \ntrain_x\n\nX_train, X_test, y_train, y_test = train_test_split(train_x, train_y, test_size = 0.1)\n\nprint(\"X_train\", X_train.shape)\nprint(\"X_test\", X_test.shape)\nprint(\"y_train\", y_train.shape)\nprint(\"y_test\", y_test.shape)\n","metadata":{"execution":{"iopub.status.busy":"2022-01-15T05:01:08.461073Z","iopub.execute_input":"2022-01-15T05:01:08.461542Z","iopub.status.idle":"2022-01-15T05:01:12.467316Z","shell.execute_reply.started":"2022-01-15T05:01:08.461506Z","shell.execute_reply":"2022-01-15T05:01:12.465988Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test.describe(include='all')\n","metadata":{"execution":{"iopub.status.busy":"2022-01-15T05:01:15.883853Z","iopub.execute_input":"2022-01-15T05:01:15.884311Z","iopub.status.idle":"2022-01-15T05:01:16.094675Z","shell.execute_reply.started":"2022-01-15T05:01:15.884274Z","shell.execute_reply":"2022-01-15T05:01:16.093203Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.set_option('display.max_columns', 80)\npd.set_option('display.max_rows', 80)\n#X_train.dtypes","metadata":{"execution":{"iopub.status.busy":"2022-01-15T05:01:21.508517Z","iopub.execute_input":"2022-01-15T05:01:21.508891Z","iopub.status.idle":"2022-01-15T05:01:21.513859Z","shell.execute_reply.started":"2022-01-15T05:01:21.508860Z","shell.execute_reply":"2022-01-15T05:01:21.512856Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from xgboost import XGBRegressor\nfrom sklearn.metrics import mean_squared_error\nfrom math import sqrt\n\nmodel = XGBRegressor( #目的関数の指定 初期値も二乗誤差です\n                       objective='reg:squarederror',\n                       #学習のラウンド数 early_stoppingを利用するので多めに指定　。。１０００回以上すべきですがテスト時間なく500回\n                       n_estimators=500, \n                       #boosterに何を用いるか 初期値もgbtreeです\n                       booster='gbtree',\n                       #学習率\n                       learning_rate=0.01,\n                       #木の最大深さ\n                       max_depth=6,\n                       #シード値\n                       random_state=2525)\n\n#学習過程を表示するための変数を用意\nevals_result = {}\nmodel.fit(X_train, y_train,\n        eval_set=[(X_train, y_train),(X_test, y_test)],\n        #学習に用いる評価指標\n        eval_metric='rmse',\n        #目的関数の改善が見られない場合に学習を打ち切るラウンド数を指定。１０以上にすべきですがテスト時間なく５回\n        early_stopping_rounds=5)\n        #学習過程の記録をするためにコールバックAPIを利用し先ほどの変数を指定\n#        callbacks=[xgb.callback.record_evaluation(evals_result)])\n\n#model.fit(X_train, y_train,eval_set=[(X_train, y_train),(X_test, y_test)])\n\npred = model.predict(X_test)\n\n#pred_label = np.where(pred > 0.5,1 ,0)\n\n#submission = pd.DataFrame({'returnerID': test['returnerID'], 'Survived': pred_label})\n#submission.to_csv('submission_first.csv', index=False)\n\nrmse = sqrt(mean_squared_error(y_test, pred))\n#print(mean_squared_error(y_test, pred))\nprint(rmse)\n\n\n","metadata":{"execution":{"iopub.status.busy":"2022-01-15T05:01:24.729802Z","iopub.execute_input":"2022-01-15T05:01:24.730347Z","iopub.status.idle":"2022-01-15T05:04:26.426348Z","shell.execute_reply.started":"2022-01-15T05:01:24.730307Z","shell.execute_reply":"2022-01-15T05:04:26.425140Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import plotly.express as px\n\n#棒グラフを作成\nfig = px.histogram(pred)\n#軸の設定等\n#fig.update_layout(width=100,height=50,margin=dict(l=10,r=10,b=5,t=5),\n#                 yaxis=dict(range=[0,10]),xaxis=dict(dtick=1, autorange=True),title=dict(font = dict(size=20)))\n\n#データを並び替え\n#fig = px.bar(scores)\nfig.update_layout(xaxis={'categoryorder':'total descending'})\n#グラフの表示\nfig.show()\n\n#Id = np.array(test[\"Id\"]).astype(int)\nresult = pd.DataFrame(pred, columns = [\"returnyard\"])\n\n# 予測データの確認\n#result.head(10)\n#print(y_test.head(10))","metadata":{"execution":{"iopub.status.busy":"2022-01-07T07:01:43.480977Z","iopub.execute_input":"2022-01-07T07:01:43.48184Z","iopub.status.idle":"2022-01-07T07:01:43.697503Z","shell.execute_reply.started":"2022-01-07T07:01:43.481778Z","shell.execute_reply":"2022-01-07T07:01:43.696622Z"},"jupyter":{"source_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.scatter(y_test, pred, alpha = 0.5)\nplt.plot(np.linspace(0, 100, 100), np.linspace(0, 100, 100), \"red\")\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2022-01-15T05:05:42.780739Z","iopub.execute_input":"2022-01-15T05:05:42.781176Z","iopub.status.idle":"2022-01-15T05:05:43.263379Z","shell.execute_reply.started":"2022-01-15T05:05:42.781137Z","shell.execute_reply":"2022-01-15T05:05:43.262442Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import xgboost as xgb\nxgb.plot_importance(model)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-01-15T05:05:46.745842Z","iopub.execute_input":"2022-01-15T05:05:46.746318Z","iopub.status.idle":"2022-01-15T05:05:47.628153Z","shell.execute_reply.started":"2022-01-15T05:05:46.746276Z","shell.execute_reply":"2022-01-15T05:05:47.626743Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.DataFrame({'returnerId': X_test['returnerId'], 'returnyard': pred})\nsubmission.to_csv('submission_first.csv')\n","metadata":{"execution":{"iopub.status.busy":"2022-01-15T05:05:55.275717Z","iopub.execute_input":"2022-01-15T05:05:55.276232Z","iopub.status.idle":"2022-01-15T05:05:55.694193Z","shell.execute_reply.started":"2022-01-15T05:05:55.276193Z","shell.execute_reply":"2022-01-15T05:05:55.692828Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test.to_csv(\"output_pd.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-01-15T05:06:00.634654Z","iopub.execute_input":"2022-01-15T05:06:00.635114Z","iopub.status.idle":"2022-01-15T05:06:01.258773Z","shell.execute_reply.started":"2022-01-15T05:06:00.635077Z","shell.execute_reply":"2022-01-15T05:06:01.257600Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#!pip install shap\nimport shap","metadata":{"execution":{"iopub.status.busy":"2022-01-15T05:06:04.405548Z","iopub.execute_input":"2022-01-15T05:06:04.406139Z","iopub.status.idle":"2022-01-15T05:06:04.411702Z","shell.execute_reply.started":"2022-01-15T05:06:04.406086Z","shell.execute_reply":"2022-01-15T05:06:04.410322Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"shap.initjs()","metadata":{"execution":{"iopub.status.busy":"2022-01-15T05:06:08.587986Z","iopub.execute_input":"2022-01-15T05:06:08.588405Z","iopub.status.idle":"2022-01-15T05:06:08.613460Z","shell.execute_reply.started":"2022-01-15T05:06:08.588370Z","shell.execute_reply":"2022-01-15T05:06:08.612514Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"explainer = shap.TreeExplainer(model=model, feature_perturbation='tree_path_dependent', model_output='margin')\nshap_values = explainer.shap_values(X=X_train)","metadata":{"execution":{"iopub.status.busy":"2022-01-15T05:13:09.276273Z","iopub.execute_input":"2022-01-15T05:13:09.276837Z","iopub.status.idle":"2022-01-15T05:23:21.731650Z","shell.execute_reply.started":"2022-01-15T05:13:09.276796Z","shell.execute_reply":"2022-01-15T05:23:21.730390Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"shap.summary_plot(shap_values, X_train, plot_type=\"bar\")","metadata":{"execution":{"iopub.status.busy":"2022-01-15T05:23:32.798924Z","iopub.execute_input":"2022-01-15T05:23:32.799338Z","iopub.status.idle":"2022-01-15T05:23:33.129411Z","shell.execute_reply.started":"2022-01-15T05:23:32.799297Z","shell.execute_reply":"2022-01-15T05:23:33.128417Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"shap.summary_plot(shap_values, X_train)","metadata":{"execution":{"iopub.status.busy":"2022-01-15T05:24:34.379083Z","iopub.execute_input":"2022-01-15T05:24:34.379542Z","iopub.status.idle":"2022-01-15T05:25:07.699247Z","shell.execute_reply.started":"2022-01-15T05:24:34.379503Z","shell.execute_reply":"2022-01-15T05:25:07.697378Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"shap_interaction_values = shap.TreeExplainer(model).shap_interaction_values(X_train)\nshap.summary_plot(shap_interaction_values, X_train)","metadata":{"execution":{"iopub.status.busy":"2022-01-15T05:26:05.704122Z","iopub.execute_input":"2022-01-15T05:26:05.704583Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nfrom torch import nn\nfrom torch.utils.data import Dataset, DataLoader\n\n","metadata":{"execution":{"iopub.status.busy":"2022-01-07T09:05:05.054442Z","iopub.execute_input":"2022-01-07T09:05:05.054881Z","iopub.status.idle":"2022-01-07T09:05:06.023219Z","shell.execute_reply.started":"2022-01-07T09:05:05.054841Z","shell.execute_reply":"2022-01-07T09:05:06.021967Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nfrom os.path import join\n\nimport numpy as np\nimport pandas as pd\n\nfrom scipy.spatial import distance_matrix\n\nimport torch\nfrom torch import nn\nfrom torch.utils.data import Dataset, DataLoader\n\ntry:\n    import dgl\nexcept:\n    !pip install dgl\n    import dgl\n    \nimport warnings\nwarnings.filterwarnings('ignore')","metadata":{"execution":{"iopub.status.busy":"2022-01-07T09:24:20.281689Z","iopub.execute_input":"2022-01-07T09:24:20.282127Z","iopub.status.idle":"2022-01-07T09:26:50.702606Z","shell.execute_reply.started":"2022-01-07T09:24:20.282088Z","shell.execute_reply":"2022-01-07T09:26:50.701201Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}