{"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":"## ノートブックの目的\n* NFL Big Data Bowl 2022 のコンペ概要理解、EDAなど。\n* そもそもアメフトのことをよく知らないので、アメフト情報収集。","metadata":{}},{"cell_type":"markdown","source":"## 参考にしたノートブック\n\n[NFL Big Data Bowl 2022 - Starters EDA](https://www.kaggle.com/hijest/nfl-big-data-bowl-2022-starters-eda)\n\nありがとうございます！！","metadata":{}},{"cell_type":"markdown","source":"## コンペ概要\n\nBefore National Football League (NFL) coaches celebrate a big W, they strategize ways to improve field position and score points. Both of these objectives receive significant contributions from special teams plays, which consist of punts, kickoffs, field goals and extra points. These play types take on important roles in a game’s final score—so much so that coaches say they're a third of the game. Yet special teams remain an understudied part of American football, with an opportunity for data science to offer better ways to understand its impact.\n\n* ナショナルフットボールリーグ（NFL）のコーチがビッグWを祝う前に、彼らはフィールドポジションとスコアポイントを改善する方法を戦略化します。これらの目標は両方とも、パント、キックオフ、フィールドゴール、追加ポイントで構成される特別なチームプレーから多大な貢献を受けています。これらのプレイタイプは、ゲームの最終スコアで重要な役割を果たします。\n\n→ 'big W'は何等かのスラング？\n\n→ punts, kickoffs, field goals and extra points.このへんはルール（競技内容）みたい。\n\n* 2018〜2020年のすべての特別チームのプレーからのNFLの次世代統計（NGS）追跡データを提供しました。このデータは、フィールド上のどこにいても、各特別チームプレーヤーの位置情報を提供し、速度、加速度、方向が含まれます。\n* (コンペ)参加者はPFFからのスカウティングデータを利用できます。","metadata":{}},{"cell_type":"markdown","source":"## コンペ評価\n\nhttps://www.kaggle.com/c/nfl-big-data-bowl-2022/overview/evaluation\n\n現在整理中、、、　以下google翻訳\n\n```\n概要\nあなたの課題は、特別なチームのプレーに対応するプレーヤーの追跡データから、実用的で実用的で斬新な洞察を生み出すことです。参加者が分析する可能性のあるトピックがいくつかあります。\n\nこれらには以下が含まれますが、これらに限定されません。\n\n新しい特別チームの指標を作成します。 2020年のビッグデータボウルで優勝したアルゴリズムは、NFL / NFLネットワークでオンエア配信に採用されており、今年の大会で得られる可能性のある特別チームのプレーの新しい統計が得られることを期待しています。\n特別チームの戦略を定量化します。特別チームのコーチは、リーグで最も創造的で革新的な選手の1人です。各チームのゲームの計画を比較/対比します。どの戦略が最良の結果をもたらしますか？採用できる他の戦略は何ですか？\n特別チームの選手をランク付けします。各チームは、さまざまなプレーヤー（ロングスナッパー、キッカー、パンター、およびその他のユーティリティ特別チームプレーヤーを含む）を採用しています。それらはどのように相互に積み重なっていますか？\n上記のリストは包括的ではなく、参加者がカバーするためのガイドとなることを意図したものでもありません。今年のコンテストでは、すべての特別チーム関連のアイデアを受け付けています。\n\n1つのアイデアをより徹底的に検討する提出は、複数のアイデアをある程度徹底的に検討する提出よりも優先されます。 \n```\n\n```\nスコアリング\nコンテストへのエントリーは、次の5つのコンポーネントで評価されるノートブックの提出で構成されます。0は低スコア、10は高スコアです。提出物は、それらがどれだけうまく対処しているかに基づいて判断されます。\n\n革新：\n提案された調査結果は実行可能ですか？\nこれは斬新な追跡データの見方ですか？\nこのプロジェクトはクリエイティブですか？\n\n正確さ：\n仕事は正しいですか？\nクレームはデータによってバックアップされていますか？\nデータを考えると、統計モデルは適切ですか？\n\n関連性：\nNFLチーム（またはリーグオフィス）は、これらの結果を週ごとに使用できますか？\n分析はサッカーのデータを複雑にする変数を考慮していますか？\n\n明快さ：\n執筆者がどの程度明確に調査結果を出しているかに関して、執筆を評価します。\n\nデータの視覚化/表：\n提供されているチャートと表は、アクセス可能で、興味深く、視覚的に魅力的で、正確ですか？\n\n審査員は、（i）NFL本社、（ii）32のNFLチーム、または（iii）NFLチームと協力するプレーヤー追跡ベンダーのいずれかで働く分析スタッフで構成されます。上記の各コンポーネントが均等に計量されるように、スコアが平均化されます。\n\n次の2つのカテゴリのそれぞれで最高得点のエントリが勝者として授与されます。\n\nオープンコンペティション：特別チームのプレーに関連する提出物はすべて、オープンコンペティションに提出できます。\n\n大学のコンテスト：上記と同じですが、大学生のみが参加できます。学生は、オープンコンペティションまたはカレッジコンペティションのいずれかにのみ応募できますが、両方に応募することはできません。\n```\n\n```\nノートブックの要件\nノートブックは、2,000語以内、10個以下の表/図で構成する必要があります。 この制限を下回る単語や数字の数については、提出物にペナルティは課されません。 読者が分析をよりよく理解するのに役立つ場合は、参加者は統計コードを表示することをお勧めします。 ただし、すべてではないにしても、ほとんどのコードは付録で非表示にする必要があります。\n\n提出されたすべてのノートブックは、資格を得るために提出期限までに公開されなければなりません。 チームとして提出する場合は、提出するすべてのノートブックにすべてのチームメンバーが共同編集者としてリストされている必要があります。\n\nプレーヤーの追跡データを使用しないノートブックはスコアリングされません。\n```","metadata":{}},{"cell_type":"markdown","source":"## NFL,アメフトとは？\n\n### NFL\n* NFL(National Football League)でアメリカのプロリーグ。\n* 発足は1920年。\n* 全米各地の32チームで競い合う。２つのカンファレンス（日本野球のセ・パリーグみたいなもん）に分かれ、さらにそれぞれ東西南北の４地区に４チームずつ所属；４チーム×４地区×２カンファレンス＝32チーム\n* レギュラーシーズン　→　ポストシーズン　→　スーパーボウル　の流れ\n* レギュラーシーズンは9月第1週〜からの18週であり、その間に各チームが17試合を戦う（全272試合）。\n* ポストシーズンはレギュラーシーズンの成績によって選出された AFC・NFC両カンファレンスの地区優勝4チームとそれ以外の成績上位3チームによってプレーオフが行われる。\n* スーパーボウルでは、AFC・NFCの両カンファレンスの勝者で戦う、全米No1決定戦。テレビの視聴率が高い。\n\n### アメフト\n* [アメフトルール解説（NFL日本公式より）](https://nfljapan.com/guide/rule](https://nfljapan.com/guide/rule)\n* [ネコでもわかるアメフトのルール](http://nekoame.com/%e3%83%8d%e3%82%b3%e3%81%a7%e3%82%82%e3%82%8f%e3%81%8b%e3%82%8b%e3%82%a2%e3%83%a1%e3%83%95%e3%83%88%e3%81%ae%e3%83%ab%e3%83%bc%e3%83%ab%e7%9b%ae%e6%ac%a1)\n* [アメフトとラグビーは違う。](https://www.sportingnews.com/jp/%E3%83%A9%E3%82%B0%E3%83%93%E3%83%BC/news/rugby-americanfootball-difference/qvfciq1usyxy1w36p5nspahlc)\n* [アイシールド21](https://ja.wikipedia.org/wiki/%E3%82%A2%E3%82%A4%E3%82%B7%E3%83%BC%E3%83%AB%E3%83%8921)はアメフト\n\n### リンク\n\nNFL日本公式\n\n[https://nfljapan.com/](https://nfljapan.com/)\n\nNFLとは（NFL日本公式より）\n\n[https://nfljapan.com/guide/about](https://nfljapan.com/guide/about)\n\n\nNFL(Wikipediaより)\n\n[https://ja.wikipedia.org/wiki/NFL](https://ja.wikipedia.org/wiki/NFL)","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport seaborn as sns","metadata":{"execution":{"iopub.status.busy":"2021-10-15T12:37:19.540253Z","iopub.execute_input":"2021-10-15T12:37:19.540776Z","iopub.status.idle":"2021-10-15T12:37:20.630544Z","shell.execute_reply.started":"2021-10-15T12:37:19.540688Z","shell.execute_reply":"2021-10-15T12:37:20.629656Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## インプットデータの確認","metadata":{}},{"cell_type":"code","source":"! ls /kaggle/input/nfl-big-data-bowl-2022","metadata":{"execution":{"iopub.status.busy":"2021-10-15T12:37:20.632552Z","iopub.execute_input":"2021-10-15T12:37:20.632856Z","iopub.status.idle":"2021-10-15T12:37:21.458051Z","shell.execute_reply.started":"2021-10-15T12:37:20.632818Z","shell.execute_reply":"2021-10-15T12:37:21.457245Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"データとして存在しているのは\n\n* PFFScoutingData.csv\n* games.csv\n* players.csv\n* plays.csv\n* tracking2018.csv\n* tracking2019.csv\n* tracking2020.csv\n","metadata":{}},{"cell_type":"markdown","source":"## Games\n\n* homeTeamAbbr : ホーム側のチーム\n* visitorTeamAbbr : ビジター側のチーム","metadata":{}},{"cell_type":"code","source":"df_games = pd.read_csv(\"/kaggle/input/nfl-big-data-bowl-2022/games.csv\")\ndf_games.head()","metadata":{"execution":{"iopub.status.busy":"2021-10-15T12:37:21.459516Z","iopub.execute_input":"2021-10-15T12:37:21.459880Z","iopub.status.idle":"2021-10-15T12:37:21.500475Z","shell.execute_reply.started":"2021-10-15T12:37:21.459845Z","shell.execute_reply":"2021-10-15T12:37:21.499540Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"チーム数が33で多くない、、、？","metadata":{}},{"cell_type":"code","source":"print( 'チーム数:' + str( len( np.sort( df_games['homeTeamAbbr'].unique() ) )) )\nsns.displot( data=df_games, y=\"homeTeamAbbr\" )\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-10-15T12:37:21.501818Z","iopub.execute_input":"2021-10-15T12:37:21.502058Z","iopub.status.idle":"2021-10-15T12:37:21.986443Z","shell.execute_reply.started":"2021-10-15T12:37:21.502024Z","shell.execute_reply":"2021-10-15T12:37:21.985476Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Players","metadata":{}},{"cell_type":"code","source":"df_players = pd.read_csv(\"/kaggle/input/nfl-big-data-bowl-2022/players.csv\")\ndf_players.head()","metadata":{"execution":{"iopub.status.busy":"2021-10-15T12:37:21.988338Z","iopub.execute_input":"2021-10-15T12:37:21.988630Z","iopub.status.idle":"2021-10-15T12:37:22.018526Z","shell.execute_reply.started":"2021-10-15T12:37:21.988595Z","shell.execute_reply":"2021-10-15T12:37:22.017505Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print( 'プレーヤー数:'+str( len( df_players ) ) )","metadata":{"execution":{"iopub.status.busy":"2021-10-15T12:37:22.020316Z","iopub.execute_input":"2021-10-15T12:37:22.020706Z","iopub.status.idle":"2021-10-15T12:37:22.025458Z","shell.execute_reply.started":"2021-10-15T12:37:22.020676Z","shell.execute_reply":"2021-10-15T12:37:22.024536Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### ポジション\n\n[アメフトのポジション（wikipediaに飛びます）](https://ja.wikipedia.org/wiki/%E3%82%A2%E3%83%A1%E3%83%AA%E3%82%AB%E3%83%B3%E3%83%95%E3%83%83%E3%83%88%E3%83%9C%E3%83%BC%E3%83%AB%E3%81%AE%E3%83%9D%E3%82%B8%E3%82%B7%E3%83%A7%E3%83%B3#/media/%E3%83%95%E3%82%A1%E3%82%A4%E3%83%AB:Positions_American_Football_-_ja.svg)\n\n","metadata":{}},{"cell_type":"code","source":"print( 'ポジション数:' + str( len( np.sort( df_players['Position'].unique() ) )) )\nsns.displot( data=df_players, y=\"Position\" )\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-10-15T12:37:22.026615Z","iopub.execute_input":"2021-10-15T12:37:22.026894Z","iopub.status.idle":"2021-10-15T12:37:22.421478Z","shell.execute_reply.started":"2021-10-15T12:37:22.026861Z","shell.execute_reply":"2021-10-15T12:37:22.420624Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### 出身大学","metadata":{}},{"cell_type":"code","source":"df_players['collegeName'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2021-10-15T12:37:22.422710Z","iopub.execute_input":"2021-10-15T12:37:22.423024Z","iopub.status.idle":"2021-10-15T12:37:22.431560Z","shell.execute_reply.started":"2021-10-15T12:37:22.422990Z","shell.execute_reply":"2021-10-15T12:37:22.430920Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### 身長と体重\n\n* フィート・インチ/ポンド単位なので、換算が必要。\n* 身長に至っては、数値データになり切れておらず大変。→あとで数値に換算する。","metadata":{}},{"cell_type":"code","source":"sns.displot( data=df_players, y=\"height\" )\nplt.show()\n\nsns.displot( data=df_players, y=\"weight\" )\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-10-15T12:37:22.432687Z","iopub.execute_input":"2021-10-15T12:37:22.432907Z","iopub.status.idle":"2021-10-15T12:37:23.129245Z","shell.execute_reply.started":"2021-10-15T12:37:22.432880Z","shell.execute_reply":"2021-10-15T12:37:23.128402Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Plays\n\n以下は公式そのまま\n\n* gameId: Game identifier, unique (numeric)\n* playId: Play identifier, not unique across games (numeric)\n* playDescription: Description of play (text)\n* quarter: Game quarter (numeric)\n* down: Down (numeric)\n* yardsToGo: Distance needed for a first down (numeric)\n* possessionTeam: Team punting, placekicking or kicking off the ball (text)\n* specialTeamsPlayType: Formation of play: Extra Point, Field Goal, Kickoff or Punt (text)\n* specialTeamsPlayResult: Special Teams outcome of play dependent on play type: Blocked Kick Attempt, Blocked Punt, Downed, Fair Catch, Kick Attempt Good, Kick Attempt No Good, Kickoff Team Recovery, Muffed, Non-Special Teams Result, Out of Bounds, Return or Touchback (text)\n* kickerId: nflId of placekicker, punter or kickoff specialist on play (numeric)\n* returnerId: nflId(s) of returner(s) on play if there was a special teams return. Multiple returners on a play are separated by a ; (text)\n* kickBlockerId: nflId of blocker of kick on play if there was a blocked field goal or blocked punt (numeric)\n* yardlineSide: 3-letter team code corresponding to line-of-scrimmage (text)\n* yardlineNumber: Yard line at line-of-scrimmage (numeric)\n* gameClock: Time on clock of play (MM:SS)\n* penaltyCodes: NFL categorization of the penalties that occurred on the play. Multiple penalties on a play are separated by a ; (text)\n* penaltyJerseyNumber: Jersey number and team code of the player committing each penalty. Multiple penalties on a play are separated by a ; (text)\n* penaltyYards: yards gained by possessionTeam by penalty (numeric)\n* preSnapHomeScore: Home score prior to the play (numeric)\n* preSnapVisitorScore: Visiting team score prior to the play (numeric)\n* passResult: Scrimmage outcome of the play if specialTeamsPlayResult is \"Non-Special Teams Result\" (C: Complete pass, I: Incomplete pass, S: Quarterback sack, IN: Intercepted pass, R: Scramble, ' ': Designed Rush, text)\n* kickLength: Kick length in air of kickoff, field goal or punt (numeric)\n* kickReturnYardage: Yards gained by return team if there was a return on a kickoff or punt (numeric)\n* playResult: Net yards gained by the kicking team, including penalty yardage (numeric)\n* absoluteYardlineNumber: Location of ball downfield in tracking data coordinates (numeric)\n\nゲームの内容に関する情報のようです","metadata":{}},{"cell_type":"code","source":"df_plays = pd.read_csv(\"/kaggle/input/nfl-big-data-bowl-2022/plays.csv\")\ndf_plays.head()","metadata":{"execution":{"iopub.status.busy":"2021-10-15T12:37:23.130379Z","iopub.execute_input":"2021-10-15T12:37:23.130625Z","iopub.status.idle":"2021-10-15T12:37:23.287652Z","shell.execute_reply.started":"2021-10-15T12:37:23.130596Z","shell.execute_reply":"2021-10-15T12:37:23.286721Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_plays.iloc[0,:]","metadata":{"execution":{"iopub.status.busy":"2021-10-15T12:45:34.721285Z","iopub.execute_input":"2021-10-15T12:45:34.722056Z","iopub.status.idle":"2021-10-15T12:45:34.729149Z","shell.execute_reply.started":"2021-10-15T12:45:34.722011Z","shell.execute_reply":"2021-10-15T12:45:34.728547Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"specialTeamsPlayType はKickoff, Punt Field Goal, ExtraPointの種類のみ","metadata":{}},{"cell_type":"code","source":"sns.displot( data=df_plays, y=\"specialTeamsPlayType\" )\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-10-15T12:39:23.136317Z","iopub.execute_input":"2021-10-15T12:39:23.137331Z","iopub.status.idle":"2021-10-15T12:39:23.348199Z","shell.execute_reply.started":"2021-10-15T12:39:23.137265Z","shell.execute_reply":"2021-10-15T12:39:23.347570Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"specialTeamsPlayTypeごとのspecialTeamsResult","metadata":{}},{"cell_type":"code","source":"for playtype in df_plays['specialTeamsPlayType'].unique():\n    print( playtype )\n    sns.displot( data=df_plays[ df_plays['specialTeamsPlayType']==playtype ], y=\"specialTeamsResult\" )\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2021-10-15T12:43:16.614448Z","iopub.execute_input":"2021-10-15T12:43:16.614841Z","iopub.status.idle":"2021-10-15T12:43:17.644785Z","shell.execute_reply.started":"2021-10-15T12:43:16.614806Z","shell.execute_reply":"2021-10-15T12:43:17.643806Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"playResultはなんかの結果でしょうね。。。","metadata":{}},{"cell_type":"code","source":"sns.displot( data=df_plays, y=\"playResult\" )\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-10-15T12:47:40.954086Z","iopub.execute_input":"2021-10-15T12:47:40.954795Z","iopub.status.idle":"2021-10-15T12:47:41.274736Z","shell.execute_reply.started":"2021-10-15T12:47:40.954754Z","shell.execute_reply":"2021-10-15T12:47:41.273731Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## PFFScoutingData","metadata":{}},{"cell_type":"code","source":"df_pffs = pd.read_csv(\"/kaggle/input/nfl-big-data-bowl-2022/PFFScoutingData.csv\")\ndf_pffs.head()","metadata":{"execution":{"iopub.status.busy":"2021-10-15T12:37:23.289317Z","iopub.execute_input":"2021-10-15T12:37:23.289641Z","iopub.status.idle":"2021-10-15T12:37:23.380312Z","shell.execute_reply.started":"2021-10-15T12:37:23.289601Z","shell.execute_reply":"2021-10-15T12:37:23.379469Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Tracking","metadata":{}},{"cell_type":"markdown","source":"選手の座標トラッキングデータ。すごい。","metadata":{}},{"cell_type":"code","source":"df_tracking_2018 = pd.read_csv(\"/kaggle/input/nfl-big-data-bowl-2022/tracking2018.csv\")\ndf_tracking_2018.head()","metadata":{"execution":{"iopub.status.busy":"2021-10-15T12:37:23.381694Z","iopub.execute_input":"2021-10-15T12:37:23.381947Z","iopub.status.idle":"2021-10-15T12:38:00.412414Z","shell.execute_reply.started":"2021-10-15T12:37:23.381919Z","shell.execute_reply":"2021-10-15T12:38:00.411542Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"playIdとgameIdを固定してデータを確認します","metadata":{}},{"cell_type":"code","source":"mask_playId = df_tracking_2018['playId'] == 36\nmask_gameId = df_tracking_2018['gameId'] == 2018123000\ndf_track = df_tracking_2018[ np.array( mask_playId )*np.array( mask_gameId ) ]\ndf_track","metadata":{"execution":{"iopub.status.busy":"2021-10-15T12:54:51.166136Z","iopub.execute_input":"2021-10-15T12:54:51.166472Z","iopub.status.idle":"2021-10-15T12:54:51.221688Z","shell.execute_reply.started":"2021-10-15T12:54:51.166439Z","shell.execute_reply":"2021-10-15T12:54:51.220560Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"displayNameに各選手の情報が入っているのが分かります。","metadata":{}},{"cell_type":"code","source":"plt.plot( df_track['displayName'] )","metadata":{"execution":{"iopub.status.busy":"2021-10-15T12:55:48.841953Z","iopub.execute_input":"2021-10-15T12:55:48.842311Z","iopub.status.idle":"2021-10-15T12:55:49.159384Z","shell.execute_reply.started":"2021-10-15T12:55:48.842279Z","shell.execute_reply":"2021-10-15T12:55:49.158497Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"各選手とボールの軌跡","metadata":{}},{"cell_type":"code","source":"for display in df_track['displayName'].unique():\n    mask = df_track['displayName'] == display\n    plt.plot( df_track[mask]['x'],df_track[mask]['y'],label=display )\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-10-15T13:00:30.070505Z","iopub.execute_input":"2021-10-15T13:00:30.070808Z","iopub.status.idle":"2021-10-15T13:00:30.529566Z","shell.execute_reply.started":"2021-10-15T13:00:30.070776Z","shell.execute_reply":"2021-10-15T13:00:30.528609Z"},"trusted":true},"execution_count":null,"outputs":[]}]}