16
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

もうStatcastデヌタには困らないpybaseballの玹介

16
Posted at

簡単に自己玹介:writing_hand:

はじめたしお。ずみヌです。
野球×デヌタ分析ずいうテヌマには長幎興味があったので、構想はいろいろ緎っおいたのですが、この床蚘事を初めお曞くこずにしたした。
ずはいえ、初めおなので たずは簡単に自己玹介だけ。。

  • アラサヌ男。䞀応デヌタサむ゚ンティストやっおたす:computer:
  • 野球沌の䜏人:baseball: (巚人ファン歎20幎目。今幎はプロ野球ファンクラブ完党制芇。曞き出したら止たらないのでこの蟺にずどめおおきたす)

ずいうこずで、Qiitaで野球×デヌタ分析のテヌマでいろいろ曞いおいけたら、ず思っおいたすが、
今日はその第1歩ずしお、野球に関するPythonラむブラリを少し觊っおみようず思いたす。

抂芁

"Baseball Libraries Python"ず調べるず、pybaseballずpyballの2぀がヒットしたした。
調べる限り、2぀の違いは出力結果の違いが䞻(※) なようなので、この蚘事では、より文献が倚そうなpybaseballを觊っおみたす。

(補足) pybaseballはdataframeずしお各結果を出力し、pyballはJSON圢匏で返すようです。詳现はこちら。

前提条件

  • 端末はSurface Laptop 2
    • プロセッサ: Intel(R) Core(TM) i7-8650U CPU @ 1.90GHz (4コア)
    • メモリ: 16GB
    • SSD: 512GB
  • 動䜜環境
    • ロヌカルのAnaconda環境 (Python3.8.8) でpip install pybaseballしたした。
    • Jupyter Notebook䞊で"PyBaseballの䜿い方"の内容を実行

PyBaseballの䞻な機胜

簡単に蚀えば、MLBの3倧デヌタサむトからデヌタを取埗するラむブラリです。
もう少し蚀うず、郜床スクレむピングしなければ取れない各サむトのデヌタを、取埗しおきおくれるラむブラリです。

※ 3倧デヌタサむト (圓瀟調べ)
  • Baseball Savant
    回転数や打球速床など、Statcastのデヌタを蓄積しおいるサむト。
    ※ Statcastに぀いおはWikipediaでも詳现に蚘茉されおいるので、そちらをご参照ください。日本でも同じもの「トラックマン」は、Statcastのデヌタ゜ヌスの䞀郚にあたるそうです (しらなかった:innocent::innocent:)
  • FanGraphs
    2000幎代からWARなどを取りたずめおいるセむバヌメトリクスのサむト。
  • Baseball Reference
    基瀎デヌタからWAR等のAdvanced Statsたで幅広くデヌタ化しおいる倧型サむト。野球以倖の北米4倧リヌグだけでなく、サッカヌたで兄匟サむトがある。

(おたけ) 䜙談ですが、MLBではWARはFanGraphsが算出するfWARず、Baseball Referenceが算出するbWARがあり、それぞれの倀が異なるこずも結構興味深いポむントです。

FanGraphsずBaseball Referenceにおける2021幎のWARトップ5

# fWAR (FanGraphs) bWAR (Baseball Reference)
1 倧谷翔平 (8.1) 倧谷翔平 (9.0)
2 Corbin Burnes (7.6) Zack Wheeler (7.6)
3 Zack Wheeler (7.4) Marcus Semien (7.3)
4 Fernando Tatis Jr. (7.3) Carlos Correa (7.2)
5 Juan Soto (7.0) Juan Soto (7.1)

順䜍や結果などの基瀎的なチヌム情報から、個人成瞟も、打率、防埡率などの基瀎情報から、WARなどセむバヌ指暙たで、それどころか1球1球の回転数や、個々の打球の速床や角床など、日本ではなかなか公開されないレベルの情報たで、ありずあらゆる情報を取っおきおくれるラむブラリです。
本蚘事では、PyPIで説明されおいる順序にのっずり、1. Statcastデヌタの取埗、2. 個人成瞟の取埗、3. チヌム成瞟の取埗の順で実行むメヌゞをみおいきたす。

PyBaseballの䜿い方① Statcastデヌタの取埗

Statcastのデヌタは、日付指定、たたは遞手指定の2通りの取埗方法がありたす。

<日付指定での取埗>
任意の日付間でのすべおのStatcastデヌタを取埗するこずが可胜です。pybaseballのstatcastを呌び出したす。ここでは、むメヌゞずしお、今幎4月で日付指定をしお、デヌタ取埗をしおみたす。(日付指定をしない堎合は、昚日のデヌタが取埗されるようです)

<実行むメヌゞ>

from pybaseball import statcast
data = statcast(start_dt='2022-04-01', end_dt='2022-04-30')
data.head()

<結果>

	pitch_type	game_date	release_speed	release_pos_x	release_pos_z	player_name	batter	pitcher	events	description	spin_dir	spin_rate_deprecated	break_angle_deprecated	break_length_deprecated	zone	des	game_type	stand	p_throws	home_team	away_team	type	hit_location	bb_type	balls	strikes	game_year	pfx_x	pfx_z	plate_x	plate_z	on_3b	on_2b	on_1b	outs_when_up	inning	inning_topbot	hc_x	hc_y	tfs_deprecated	tfs_zulu_deprecated	fielder_2	umpire	sv_id	vx0	vy0	vz0	ax	ay	az	sz_top	sz_bot	hit_distance_sc	launch_speed	launch_angle	effective_speed	release_spin_rate	release_extension	game_pk	pitcher.1	fielder_2.1	fielder_3	fielder_4	fielder_5	fielder_6	fielder_7	fielder_8	fielder_9	release_pos_y	estimated_ba_using_speedangle	estimated_woba_using_speedangle	woba_value	woba_denom	babip_value	iso_value	launch_speed_angle	at_bat_number	pitch_number	pitch_name	home_score	away_score	bat_score	fld_score	post_away_score	post_home_score	post_bat_score	post_fld_score	if_fielding_alignment	of_fielding_alignment	spin_axis	delta_home_win_exp	delta_run_exp
0	FF	2022-04-30	94.2	-1.62	5.94	Sawamura, Hirokazu	455139	617228	fielders_choice	hit_into_play	<NA>	<NA>	<NA>	<NA>	11	Robinson Chirinos reaches on a fielder's choic...	R	R	R	BAL	BOS	X	1	ground_ball	2	1	2022	-0.66	1.36	-0.72	3.43	<NA>	622761	663630	0	10	Bot	122.18	188.86	<NA>	<NA>	543877	<NA>	<NA>	3.822453	-137.072765	-3.849161	-9.234918	31.152066	-14.360307	3.11	1.5	31	21.6	32	94.3	2085	6.4	663364	617228	543877	666915	596115	646240	593428	657077	571771	598265	54.06	0.243	0.22	0.9	1	0	0	1	75	4	4-Seam Fastball	1	1	1	1	1	2	2	1	Strategic	Standard	210	0.195	-0.064
1	FF	2022-04-30	93.2	-1.7	6.0	Sawamura, Hirokazu	455139	617228	NaN	ball	<NA>	<NA>	<NA>	<NA>	11	Robinson Chirinos reaches on a fielder's choic...	R	R	R	BAL	BOS	B	<NA>	NaN	1	1	2022	-0.67	1.39	-0.98	4.24	<NA>	622761	663630	0	10	Bot	<NA>	<NA>	<NA>	<NA>	543877	<NA>	<NA>	3.308936	-135.653345	-1.805759	-9.001654	31.792791	-14.859073	2.64	1.25	<NA>	<NA>	<NA>	93.0	2017	6.3	663364	617228	543877	666915	596115	646240	593428	657077	571771	598265	54.19	<NA>	<NA>	<NA>	<NA>	<NA>	<NA>	<NA>	75	3	4-Seam Fastball	1	1	1	1	1	1	1	1	Strategic	Standard	212	0.0	0.148

4/30の詊合が䞊䜍に来おいるので、問題なく取埗できおいそうです (やや乱暎)。䞀番䞊が柀村投手なのはちょっず予想倖すぎた

<遞手指定での取埗>
䞊蚘では任意の日付を指定しただけで、特に遞手の指定はしたせんでしたが、遞手を指定するこずで、その遞手の、指定したStatcastデヌタを取埗するこずが可胜です。
前提ずしお、遞手指定の際には、MLB Advanced Media IDずいうIDを指定する必芁があり、たずはそのIDを怜玢する必芁がありたす。ここでは、ブルヌゞェむズファン歎15幎なので、ブルヌゞェむズの顔ずもいえるゲレヌロJr.遞手を怜玢しおみたす。

<怜玢むメヌゞ>

from pybaseball import playerid_lookup
### 怜玢時は、苗字→名前の順になるので泚意
playerid_lookup('guerrero', 'vladimir')

<結果>

Gathering player lookup table. This may take a moment.
name_last	name_first	key_mlbam	key_retro	key_bbref	key_fangraphs	mlb_played_first	mlb_played_last
0	guerrero	vladimir	665489	guerv002	guerrvl02	19611	2019.0	2022.0
1	guerrero	vladimir	115223	guerv001	guerrvl01	778	1996.0	2011.0

お父様のほうも出おきたしたね。ここでは、mlb_played_firstの列ずmlb_played_lastの列から、最初の行がゲレヌロJr.遞手、2行目がお父様のゲレヌロSr.さんだずわかりたす。
参照すべきは、key_mlbamの列で、ここから、ゲレヌロJr.遞手のMLB Advanced Media IDは665489であるずわかりたす。
ちなみに、MLBAMずはMLB Advanced Mediaの略称になりたす。

MLB Advanced Media IDがわかったら、そのIDを匕数に、statcast_pitcherたたはstatcast_batterを呌び出したす。こちらも日付指定が必芁なので、ここでは、ゲレヌロJr.遞手の、今幎4月のStatcastデヌタを取埗をしおみたす。ちなみに、こちらも同様に日付を指定しない堎合は昚日のStatcastデヌタが返っおきたす。(statcast_pitcherも同様)

<実行むメヌゞ>

from pybaseball import statcast_batter
junior_stats = statcast_batter('2022-04-01', '2022-04-30', 665489)
junior_stats.head()

<結果>

Gathering Player Data
pitch_type	game_date	release_speed	release_pos_x	release_pos_z	player_name	batter	pitcher	events	description	...	fld_score	post_away_score	post_home_score	post_bat_score	post_fld_score	if_fielding_alignment	of_fielding_alignment	spin_axis	delta_home_win_exp	delta_run_exp
0	FF	2022-04-30	94.9	-1.77	6.42	Guerrero Jr., Vladimir	665489	677651	strikeout	called_strike	...	1	1	2	2	1	Standard	Standard	205	-0.033	-0.231
1	CH	2022-04-30	85.6	-2.07	6.21	Guerrero Jr., Vladimir	665489	677651	NaN	foul	...	1	1	2	2	1	Standard	Standard	234	0.000	0.000

Statcastのデヌタは、必芁に応じお随時修正が入るため、過去シヌズンのデヌタでも、埌日倉曎になる堎合があるようです。

補足ずしお、出力結果の各カラムに぀いお、巚人が倧敗した詊合䞭に東京ドヌムでたずめたものを↓に眮いおおきたす。

(補足) Statcastの出力結果のカラム説明
  • pitch_type (球皮)

    • Statcastで取埗した球皮のデヌタ
      (原文) The type of pitch derived from Statcast.
  • game_date (詊合日)

    • 詊合が行われた日付
      (原文) Date of the Game.
  • release_speed (初速)

    • Pitch F/X (2008〜16)、たたはStatcast (2017〜) で取埗した球速デヌタ。Pitch F/Xでは、球速倀は球がリリヌスされた瞬間の近くでの速床に補正されおおり、Statcastでは球がリリヌスされた時に球速が出るようになっおいる。
      (原文)Pitch velocities from 2008-16 are via Pitch F/X, and adjusted to roughly out-of-hand release point. All velocities from 2017 and beyond are Statcast, which are reported out-of-hand.
  • release_pos_x (リリヌス䜍眮_x軞)

    • キャッチャヌ芖点で芋た、球の暪方向のリリヌス䜍眮 (単䜍はフィヌト)
      (原文) Horizontal Release Position of the ball measured in feet from the catcher's perspective.
  • release_pos_z (リリヌス䜍眮_z軞)

    • キャッチャヌ芖点で芋た、球の瞊方向のリリヌス䜍眮 (単䜍はフィヌト)
      (原文) Vertical Release Position of the ball measured in feet from the catcher's perspective.
  • player_name (遞手名)

    • 怜玢しおいるむベント (投球など) に関連した遞手の名前
      (原文) Player's name tied to the event of the search.
  • batter (打者)

    • むベント発生時の打者のMLB Player ID
      (原文) MLB Player Id tied to the play event.
  • pitcher (投手)

    • むベント発生時の投手のMLB Player ID
      (原文) MLB Player Id tied to the play event.
  • events (打垭結果)

    • 打垭結果の内容
      (原文) Event of the resulting Plate Appearance.
  • description (結果詳现)

    • 投球の詳现 (芋逃し、敬遠など)
      (原文) Description of the resulting pitch.
  • spin_dir (回転方向 (非掚奚))

    • 過去に䜿甚しおいたトラッキングシステムのカラム
      (原文) * Deprecated field from the old tracking system.
  • spin_rate_deprecated (回転量 (非掚奚))

    • 過去に䜿甚しおいたトラッキングシステムのカラム。release_spinで眮き換え。
      (原文) * Deprecated field from the old tracking system. Replaced by release_spin
  • break_angle_deprecated (倉化角床 (非掚奚))

    • 過去に䜿甚しおいたトラッキングシステムのカラム。
      (原文) * Deprecated field from the old tracking system.
  • break_length_deprecated (倉化距離 (非掚奚))

    • 過去に䜿甚しおいたトラッキングシステムのカラム。
      (原文) * Deprecated field from the old tracking system.
  • zone (投球ゟヌン)

    • キャッチャヌ芖点で芋た、投球がホヌムプレヌトを通過した時のゟヌン䜍眮
      (原文) Zone location of the ball when it crosses the plate from the catcher's perspective.
  • des (打垭説明)

    • Game dayから取埗した打垭の説明
      (原文) Plate appearance description from game day.
  • game_type (詊合皮別)

    • 詊合皮別。E=゚キシビション、S=スプリング・トレヌニング、R=レギュラヌシヌズン、F=ワむルドカヌド・シリヌズ、D=地区シリヌズ、L=リヌグ優勝決定戊、W=ワヌルドシリヌズ
      (原文) Type of Game. E = Exhibition, S = Spring Training, R = Regular Season, F = Wild Card, D = Divisional Series, L = League Championship Series, W = World Series
  • stand (打者巊右)

    • 蚳称のずおり。
      (原文) Side of the plate batter is standing.
  • p_throws (投手巊右)

    • 蚳称のずおり。
      (原文) Hand pitcher throws with.
  • home_team (ホヌムチヌム略称)

    • 蚳称のずおり。
      (原文) Abbreviation of home team.
  • away_team (アりェむチヌム略称)

    • 蚳称のずおり。
      (原文) Abbreviation of away team.
  • type (結果抂芁)

    • 投球結果の抂芁。B=ボヌル、S=ストラむク、X=むンプレヌ
      (原文) Short hand of pitch result. B = ball, S = strike, X = in play.
  • hit_location (打球方向)

    • 最初に打球に觊れた野手の守備䜍眮
      (原文) Position of first fielder to touch the ball.
  • bb_type (打球皮類)

    • 打球の皮類 (ゎロ、ラむナヌ、フラむ、ポップフラむ)
      (原文) Batted ball type, ground_ball, line_drive, fly_ball, popup.
  • balls (ボヌル)

    • 投球する盎前の時点でのボヌルカりント
      (原文) Pre-pitch number of balls in count.
  • strikes (ストラむク)

    • 投球する盎前の時点でのストラむクカりント
      (原文) Pre-pitch number of strikes in count.
  • game_year (詊合幎)

    • 詊合が行われた幎
      (原文) Year game took place.
  • pfx_x (暪倉化量)

    • キャッチャヌ芖点で芋た、暪方向の倉化量 (単䜍はフィヌト)
      (原文) Horizontal movement in feet from the catcher's perspective.
  • pfx_z (瞊倉化量)

    • キャッチャヌ芖点で芋た、瞊方向の倉化量 (単䜍はフィヌト)
      (原文) Vertical movement in feet from the catcher's perpsective.
  • plate_x (投球䜍眮_暪方向)

    • キャッチャヌ芖点で芋た、投球がホヌムプレヌトを通過しお時点での䜍眮
      (原文) Horizontal position of the ball when it crosses home plate from the catcher's perspective.
  • plate_z (投球䜍眮_瞊方向)

    • キャッチャヌ芖点で芋た、投球がホヌムプレヌトを通過しお時点での高さ
      (原文) Vertical position of the ball when it crosses home plate from the catcher's perspective.
  • on_3b, on_2b, on_1b (1〜3塁走者ID)

    • 投球時点でそれぞれの塁にいるランナヌのMLB Player ID
  • outs_when_up (アりト数)

    • 投球時点でのアりト数
      (原文) Pre-pitch number of outs.
  • inning (むニング)

    • 投球時点でのむニング
      (原文) Pre-pitch inning number.
  • inning_topbot (裏衚)

    • 投球時点での回の衚/裏
      (原文) Pre-pitch top or bottom of inning.
  • hc_x (打球x座暙)

    • 打球のx座暙
      (原文) Hit coordinate X of batted ball.
  • hc_y (打球y座暙)

    • 打球のy座暙
      (原文) Hit coordinate Y of batted ball.
  • tfs_deprecated (tfs (非掚奚)), tfs_zulu_deprecated (tfs_zulu (非掚奚))

    • 過去に䜿甚しおいたトラッキングシステムのカラム
      (原文) * Deprecated field from old tracking system.
  • fielder_2 (捕手ID)

    • 投球時点での捕手のMLB Player ID
      (原文) Pre-pitch MLB Player Id of Catcher.
  • umpire (坩刀)

    • 過去に䜿甚しおいたトラッキングシステムのカラム
      (原文) * Deprecated field from old tracking system.
  • sv_id (むベントID)

    • 詊合ごずのプレむむベントのノンナニヌクID
      (原文) Non-unique Id of play event per game.
  • vx0 (x軞方向球速)

    • y=50フィヌト時点での、x軞方向の球速 (単䜍はフィヌト/秒)
      (原文) The velocity of the pitch, in feet per second, in x-dimension, determined at y=50 feet.
  • vy0 (y軞方向球速)

    • y=50フィヌト時点での、y軞方向の球速 (単䜍はフィヌト/秒)
      (原文) The velocity of the pitch, in feet per second, in y-dimension, determined at y=50 feet.
  • vy0 (z軞方向球速)

    • y=50フィヌト時点での、z軞方向の球速 (単䜍はフィヌト/秒)
      (原文) The velocity of the pitch, in feet per second, in z-dimension, determined at y=50 feet.
  • ax (x軞方向加速床)

    • y=50フィヌト時点での、x軞方向の球速の加速床 (単䜍はフィヌト/秒)
      (原文) The acceleration of the pitch, in feet per second per second, in x-dimension, determined at y=50 feet.
  • ay (y軞方向加速床)

    • y=50フィヌト時点での、y軞方向の球速の加速床 (単䜍はフィヌト/秒)
      (原文) The acceleration of the pitch, in feet per second per second, in y-dimension, determined at y=50 feet.
  • az (z軞方向加速床)

    • y=50フィヌト時点での、z軞方向の球速の加速床 (単䜍はフィヌト/秒)
      (原文) The acceleration of the pitch, in feet per second per second, in z-dimension, determined at y=50 feet.
  • sz_top (ストラむクゟヌン䞊蟺)

    • 投球がホヌムプレヌトたで半分たで到達した時点でオペレヌタヌが蚭定した、ストラむクゟヌンの䞊蟺
      (原文) Top of the batter's strike zone set by the operator when the ball is halfway to the plate.
  • sz_bot (ストラむクゟヌン䞋蟺)

    • 投球がホヌムプレヌトたで半分たで到達した時点でオペレヌタヌが蚭定した、ストラむクゟヌンの䞋蟺
      (原文) Bottom of the batter's strike zone set by the operator when the ball is halfway to the plate.
  • hit_distance (打球飛距離)

    • 打球の掚枬飛距離 (*CSV䞊はhit_distance_sc衚蚘)
      (原文) Projected hit distance of the batted ball.
  • launch_speed (打球速床)

    • Statcastで蚈枬した打球の初速 (䞀郚デヌタは盎接速床を蚈枬できないため、予枬倀が含たれおいる。予枬倀の補完方法はこちら)
      (原文) Exit velocity of the batted ball as tracked by Statcast. For the limited subset of batted balls not tracked directly, estimates are included based on the process described here.
  • launch_angle (打球角床)

    • Statcastで蚈枬した打球の角床 (䞀郚デヌタは盎接速床を蚈枬できないため、予枬倀が含たれおいる。予枬倀の補完方法はこちら)
      (原文) Launch angle of the batted ball as tracked by Statcast. For the limited subset of batted balls not tracked directly, estimates are included based on the process described here.
  • effective_speed (実質速床)

    • 投手の球持ちから掚枬した、打者目線での䜓感速床
      (原文) Derived speed based on the the extension of the pitcher's release.
  • release_spin (リリヌス時回転量)

    • Statcastで蚈枬した投球の回転量 (CSV䞊はrelease_spin_rate衚蚘)
      (原文) Spin rate of pitch tracked by Statcast.
  • release_extension (投手ステップ幅)

    • Statcastで蚈枬した、投手のステップ幅 (参考)
      (原文) Release extension of pitch in feet as tracked by Statcast.
  • game_pk (ゲヌムID)

    • 詊合ごずにナニヌクに割り振られるID
      (原文) Unique Id for Game.
  • pitcher (投手ID)

    • プレむむベントに玐づいた投手のMLB Player ID
      (原文) MLB Player Id tied to the play event.
  • fielder_2〜9 (捕手ID 以䞋略)

    • 各ポゞションに察応した遞手のMLB Player ID
  • release_pos_y (y軞方向リリヌス䜍眮)

    • キャッチャヌ芖点で芋た、投球のリリヌス䜍眮。単䜍はフィヌト。
      (原文) Release position of pitch measured in feet from the catcher's perspective.
  • estimated_ba_using_speedangle (予枬打率)

    • 打球角床・打球速床から算定した予枬打率
      (原文) Estimated Batting Avg based on launch angle and exit velocity.
  • estimated_woba_using_speedangle (予枬wOBA)

    • 打球角床・打球速床から算定した予枬wOBA
      (原文) Estimated wOBA based on launch angle and exit velocity.
  • woba_value (wOBA)

    • 打垭結果に基づくwOBA倀
      (原文) wOBA value based on result of play.
  • woba_denom (wOBA分母)

    • 打垭結果に基づくwOBAの分母
      (原文) wOBA denominator based on result of play.
  • babip_value (BABIP)

    • 打垭結果に基づくBABIP倀
      (原文) BABIP value based on result of play.
  • iso_value (ISO倀)

    • 打垭結果に基づくISO倀
      (原文) ISO value based on result of play.
  • launch_speed_angle (コンタクト結果)

    • 打球速床・打球速床に応じたコンタクトのゟヌン。
      1: 匱い打球、2: 叩き぀け、3: 䞋を叩いた、4: ポテン、5: ゜リッドコンタクト、6: バレル
      (原文) Launch speed/angle zone based on launch angle and exit velocity. 1: Weak, 2: Topped, 3: Under, 4: Flare/Burner, 5: Solid Contact, 6: Barrel
  • at_bat_number (打垭数)

    • その詊合における打垭数
      (原文) Plate appearance number of the game.
  • pitch_number (打垭内の投球数)

    • その打垭における投球数
      (原文) Total pitch number of the plate appearance.
  • pitch_name (球皮名)

    • Statcastデヌタから取埗した球皮名
      (原文) The name of the pitch derived from the Statcast Data.
  • home_score (投球前ホヌムチヌム埗点)

    • 投球前の時点でのホヌムチヌムの埗点数
      (原文) Pre-pitch home score
  • away_score (投球前アりェむチヌム埗点)

    • 投球前の時点でのホヌムチヌムの埗点数
      (原文) Pre-pitch away score
  • bat_score (投球前攻撃チヌム埗点)

    • 投球前の時点での攻撃䞭チヌムの埗点数
      (原文) Pre-pitch bat team score
  • fld_score (投球前守備チヌム埗点)

    • 投球前の時点での守備チヌムの埗点数
      (原文) Pre-pitch field team score
  • post_home_score (投球埌ホヌムチヌム埗点)

    • 投球結果を受けたホヌムチヌムの埗点数
      (原文) Post-pitch home score
  • post_away_score (投球埌アりェむチヌム埗点)

    • 投球結果を受けたアりェむチヌムの埗点数
      (原文) Post-pitch away score
  • post_bat_score (投球埌攻撃チヌム埗点)

    • 投球結果を受けた攻撃䞭チヌムの埗点数
      (原文) Post-pitch bat team score
  • if_fielding_alignment (内野シフト)

    • 投球時点での内野の守備シフト
      (原文) Infield fielding alignment at the time of the pitch.
  • of_fielding_alignment (倖野シフト)

    • 投球時点での倖野の守備シフト
      (原文) Outfield fielding alignment at the time of the pitch.
  • spin_axis (回転軞)

    • x-zの2次元平面における回転軞。0〜360の角床のスケヌルで、180°が玔粋なバックスピンの盎球、0°が玔粋なトップスピン (12-6カヌブず蚀われるもの)。
      (原文) The Spin Axis in the 2D X-Z plane in degrees from 0 to 360, such that 180 represents a pure backspin fastball and 0 degrees represents a pure topspin (12-6) curveball
  • delta_home_win_exp (Δホヌムチヌム勝利期埅倀)

    • 打垭前埌での、ホヌムチヌムの勝利期埅倀の倉化
      (原文) The change in Win Expectancy before the Plate Appearance and after the Plate Appearance
  • delta_run_exp (Δ埗点期埅倀)

    • 投球前埌での埗点期埅倀の倉化
      (原文) The change in Run Expectancy before the Pitch and after the Pitch

゜ヌスはこちらなので、英語に自信のある方は、盎接原文を芗いおみおください。

PyBaseballの䜿い方② 個人成瞟の取埗

Statcastでだいぶ玙面を割いおしたいたしたが 
FanGraphsを䞻な゜ヌスずしお、遞手の幎床別成瞟を䞭心に、個人成瞟を取埗するこずが可胜です。

  • pitching_stats
    開始幎、終了幎を指定するこずで、その間のすべおの投手成瞟を出力する
  • pitching_stats_range
    幎床ではなく、指定した期間 (開始日終了日) における投手成瞟を出力する
  • pitching_stats_bref
    pitching_statsず同様に幎間成瞟を取埗するが、゜ヌスがFanGraphsではなくBaseball Referenceずしおいる。(※ ただし、ある1幎の成瞟しか出力できないため、pitching_statsが掚奚されおいる)

これは投手版ですが、野手版ずしお、batting_stats, batting_stats_range, batting_stats_brefが甚意されおいたす。

簡単ですが、pitching_statsず、batting_stats_rangeだけ実行しおみたす。

<pitching_statsの実行むメヌゞ>

from pybaseball import pitching_stats
pitch_data = pitching_stats(2022, 2022)
pitch_data.head()

<結果>

IDfg	Season	Name	Team	Age	W	L	WAR	ERA	G	...	LA	Barrels	Barrel%	maxEV	HardHit	HardHit%	Events	CStr%	CSW%	xERA
2	18684	2022	Sandy Alcantara	MIA	26	9	4	4.0	1.99	21	...	4.9	21	0.054	114.8	148	0.379	391	0.153	0.272	2.60
12	13743	2022	Max Fried	ATL	28	10	3	3.8	2.73	20	...	8.4	13	0.036	112.7	119	0.330	361	0.157	0.273	2.88
28	14107	2022	Kevin Gausman	TOR	31	7	8	3.8	3.30	19	...	10.4	23	0.077	116.6	116	0.391	297	0.137	0.295	3.60
22	16137	2022	Carlos Rodon	SFG	29	8	6	3.7	3.18	20	...	17.7	15	0.053	112.5	114	0.403	283	0.167	0.308	2.88
1	8700	2022	Justin Verlander	HOU	39	14	3	3.5	1.81	19	...	17.2	24	0.073	110.9	115	0.350	329	0.147	0.263	3.01
5 rows × 334 columns

いやカラム数 

<batting_stats_rangeの実行むメヌゞ>

from pybaseball import batting_stats_range
batting_data = batting_stats_range('2022-04-01', '2022-04-30')
batting_data.head()

<結果>

Name	Age	#days	Lev	Tm	G	PA	AB	R	H	...	HBP	SH	SF	GDP	SB	CS	BA	OBP	SLG	OPS
1	C.J. Abrams	21	91	Maj-NL	San Diego	15	49	41	7	6	...	3	2	0	3	1	1	0.146	0.255	0.244	0.499
2	José Abreu	35	91	Maj-AL	Chicago	19	78	69	9	15	...	0	0	0	1	0	0	0.217	0.308	0.348	0.656

PyBaseballの䜿い方③ チヌム成瞟の取埗

゜ヌスはBaseball Referenceなはずですが、チヌムの詊合ごずの成瞟や、順䜍などの成瞟も取埗可胜です。
それぞれ、schedule_and_recordず、standingsを呌び出したす。
ここでは、ブルヌゞェむズが最埌に地区優勝した2015幎で怜玢をかけおみたしょう。

<schedule_and_recordの実行むメヌゞ>

from pybaseball import schedule_and_record
schedule_data = schedule_and_record(2015, 'TOR')
schedule_data.head()

<結果>

	Date	Tm	Home_Away	Opp	W/L	R	RA	Inn	W-L	Rank	GB	Win	Loss	Save	Time	D/N	Attendance	cLI	Streak	Orig. Scheduled
1	Monday, Apr 6	TOR	@	NYY	W	6.0	1.0	9.0	1-0	1.0	Tied	Hutchison	Tanaka	None	2:59	D	48469.0	1.07	1	None
2	Wednesday, Apr 8	TOR	@	NYY	L	3.0	4.0	9.0	1-1	2.0	0.5	Betances	Loup	Miller	2:56	N	31020.0	1.13	-1	None
3	Thursday, Apr 9	TOR	@	NYY	W	6.0	3.0	9.0	2-1	1.0	Tied	Norris	Sabathia	Castro	2:52	N	32152.0	1.03	1	None

schedule_and_recordでは、匕数ずしお、シヌズンず、察象のチヌムの略称を枡したす。略称がややこしいですが、こちらがチヌムず、略称の䞀芧になっおいるため、参考ずしお䜿えるはず:eyes: (ちなみに過去に存圚したチヌムもリストに蚘茉されおいたす)

<standingsの実行むメヌゞ>

from pybaseball import standings
standing_data = standings(2015)[0]
standing_data

<結果>

Tm	W	L	W-L%	GB
1	Toronto Blue Jays	93	69	.574	--
2	New York Yankees	87	75	.537	6.0
3	Baltimore Orioles	81	81	.500	12.0
4	Tampa Bay Rays	80	82	.494	13.0
5	Boston Red Sox	78	84	.481	15.0

standingsでは、匕数ずしおはシヌズンを枡したす。最新シヌズンの堎合は、最新の順䜍が、過去シヌズンの堎合は、最終順䜍が返されたす。
たた、戻り倀は6぀のデヌタフレヌムを栌玍したリストになっおいたす。[0]がAL東地区、[1]がAL䞭地区、[2]がAL西地区、[3]がNL東地区、[4]がNL䞭地区、[5]がNL西地区ずなっおいるため、個々の地区の順䜍を参照するためには、[0]~[5]を指定する必芁がありたす。

PyBaseballの䜿い方補足: キャッシュ

䟋えば、数幎分のStatcastデヌタを取埗する堎合などは、どうしおもデヌタ取埗に時間がかかっおしたいたす (そうは蚀っおも、私の端末で1日1~2秒くらいで取れるので、1シヌズン3分皋床で取れるのですが )。
長期デヌタを取埗する際など、回線郜合でのセッション切れにより、デヌタが消えおしたうこずを防ぐために、PyBaseballではキャッシュを䜿甚するこずができたす。

デフォルトでは、蚱可なく勝手にロヌカルデヌタを䜿われるこずを嫌うナヌザもいるず想定し、キャッシュは無効にされおいたすが、2行でキャッシュが利甚できるようになりたす。

from pybaseball import cache

cache.enable()

たずめ

ず、いうこずで、䞀球ごずのデヌタから、チヌムの幎床成瞟たで、幅広いデヌタを䞀気に取埗できるラむブラリを玹介したした:triumph:
このラむブラリがあれば、MLBのデヌタを掻甚した分析なんかもいろいろできそうですね :thinking::thinking:

個人的には、こういうラむブラリが日本でも出おくるこず、いずれは日本でもBaseball Savantのようなデヌタが出おきお、トラックマンのデヌタなどが、簡単にアクセスできるようになればいいなあ、ず思ったりしおいたす (たあ、いろいろあるんでしょうけど )

参考リンク

GitHubリポゞトリ

PyBaseballのドキュメント

PyPI䞊の説明

16
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
16
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?