**Table of Contents**
- [Baseball](#baseball)
- [Installing from pypi](#installing-from-pypi)
- [Installing from source](#installing-from-source)
- [Fetch individual MLB game](#fetch-individual-mlb-game)
- [Fetch list of MLB games](#fetch-list-of-mlb-games)
- [Get Game generator given target directory and date range](#get-game-generator-given-target-directory-and-date-range)
- [Get raw XML files for an individual MLB game](#get-raw-xml-files-for-an-individual-mlb-game)
- [Convert XML documents into Game object](#convert-xml-documents-into-game-object)
- [Write scorecard SVGs for all MLB games on a given date](#write-scorecard-svgs-for-all-mlb-games-on-a-given-date)
- [Game Class Structure](#game-class-structure)
- [Game](#game)
- [Team](#team)
- [Inning](#inning)
- [PlateAppearance](#plateappearance)
- [Player](#player)
- [PlayerAppearance](#playerappearance)
- [Pitch](#pitch)
- [Pickoff](#pickoff)
- [RunnerAdvance](#runneradvance)
- [Substitution](#substitution)
- [Switch](#switch)
- [Analyze a game: 2017 World Series - Game 7](#analyze-a-game-2017-world-series---game-7)
- [Analyze a player's season: R.A. Dickey - 2017](#analyze-a-players-season-ra-dickey---2017)
- [Analyze a lineup of pitchers: Atlanta Braves - 2017 Regular Season](#analyze-a-lineup-of-pitchers-atlanta-braves---2017-regular-season)
# Baseball
This package fetches and parses event data for Major League Baseball games. [Game](#game) objects generated via the **\_from\_url** methods pull data from MLB endpoints where events are published within about 30 seconds of occurring. This [XML source data zip file](https://spaces-host.nyc3.digitaloceanspaces.com/livebaseballscorecards-artifacts/baseball_files_2008-2017.zip) contains event data from MLB games 2008-2017.
## Installing from pypi
```
pip3 install baseball
```
## Installing from source
```
git clone git@github.com:benjamincrom/baseball.git
cd baseball/
python3 setup.py install
```
## Fetch individual MLB game
* __get_game_from_url(__*date_str, away_code, home_code, game_number*__)__
Fetch an object which contains metadata and events for a single MLB game.
```python
import baseball
game_id, game = baseball.get_game_from_url('2017-11-1', 'HOU', 'LAD', 1)
game_dict = game._asdict()
game_json_str = game.json()
```
Write scorecard as SVG image:
```python
with open(game_id + '.svg', 'w') as fh:
fh.write(game.get_svg_str())
```
2017-11-01-HOU-LAD-1.svg
![svg](README_images/2017-11-01-HOU-LAD-1.svg)
## Fetch list of MLB games
* __get_game_list_from_file_range(__*start_date_str, end_date_str, input_dir*__)__
Fetch a list of game objects which each contain metadata and events for a single MLB game.
First, download and unzip the [source data zip file](https://spaces-host.nyc3.digitaloceanspaces.com/livebaseballscorecards-artifacts/baseball_files_2008-2017.zip):
```shell
wget https://spaces-host.nyc3.digitaloceanspaces.com/livebaseballscorecards-artifacts/baseball_files_2008-2017.zip
unzip baseball_files_2008-2017.zip -d ./baseball_files_2008-2017
```
Then import the files in Python using this library:
```python
import baseball
game_tuple_list = baseball.get_game_list_from_file_range('1-1-2017', '12-31-2017', 'baseball_files_2008-2017')
```
## Get Game generator given target directory and date range
* __get_game_generator_from_file_range(__*start_date_str, end_date_str, input_dir*__)__
Returns generator which yields (game_id, [Game](#game)) tuples
## Get raw XML files for an individual MLB game
* __get_game_xml_from_url(__*date_str, away_code, home_code, game_number*__)__
Returns game_id and three strings containing XML documents: (game_id, boxscore_raw_xml, players_raw_xml, inning_raw_xml)
## Convert XML documents into Game object
* __get_game_from_xml_strings(__*boxscore_raw_xml, players_raw_xml, inning_raw_xml*__)__
Returns [Game](#game) object if enough information to create one is provided. Otherwise returns None.
## Write scorecard SVGs for all MLB games on a given date
* __write_games_for_date(__*this_datetime, output_dir*__)__
Writes SVG files for all games played on the given date
## Game Class Structure
#### Game
- away_batter_box_score_dict
- away_pitcher_box_score_dict
- away_team ([Team](#team))
- away_team_stats
- start_datetime
- expected_start_datetime
- game_date_str
- home_batter_box_score_dict
- home_pitcher_box_score_dict
- home_team ([Team](#team))
- home_team_stats
- inning_list ([Inning](#inning) list)
- end_datetime
- location
- attendance
- weather
- temp
- timezone_str
- get_svg_str()
- json()
- \_asdict()
#### Team
- abbreviation
- batting_order_list_list (list of nine [PlayerAppearance](#playerappearance) lists)
- name
- pitcher_list ([PlayerAppearance](#playerappearance) list)
- player_id_dict
- player_last_name_dict
- player_name_dict
- \_asdict()
#### Inning
- bottom_half_appearance_list ([PlateAppearance](#plateappearance) list)
- bottom_half_inning_stats
- top_half_appearance_list ([PlateAppearance](#plateappearance) list)
- top_half_inning_stats
- \_asdict()
#### PlateAppearance
- start_datetime
- end_datetime
- batter ([Player](#player))
- batting_team ([Team](#team))
- error_str
- event_list (list of [Pitch](#pitch), [Pickoff](#pickoff), [RunnerAdvance](#runneradvance), [Substitution](#substitution), [Switch](#switch) objects)
- got_on_base
- hit_location
- inning_outs
- out_runners_list ([Player](#player) list)
- pitcher ([Player](#player))
- plate_appearance_description
- plate_appearance_summary
- runners_batted_in_list ([Player](#player) list)
- scorecard_summary
- scoring_runners_list ([Player](#player) list)
- \_asdict()
#### Player
- era
- first_name
- last_name
- mlb_id
- number
- obp
- slg
- \_asdict()
#### PlayerAppearance
- start_inning_batter_num
- start_inning_half
- start_inning_num
- end_inning_batter_num
- end_inning_half
- end_inning_num
- pitcher_credit_code
- player_obj ([Player](#player))
- position
- \_asdict()
#### Pitch
- pitch_datetime
- pitch_description
- pitch_position
- pitch_speed
- pitch_type
- \_asdict()
#### Pickoff
- pickoff_description
- pickoff_base
- pickoff_was_successful
- \_asdict()
#### RunnerAdvance
- runner_advance_datetime
- run_description
- runner ([Player](#player))
- start_base
- end_base
- runner_scored
- run_earned
- is_rbi
- \_asdict()
#### Substitution
- substitution_datetime
- incoming_player ([Player](#player))
- outgoing_player ([Player](#player))
- batting_order
- position
- \_asdict()
#### Switch
- switch_datetime
- player ([Player](#player))
- old_position_num
- new_position_num
- new_batting_order
- \_asdict()
## Analyze a game: 2017 World Series - Game 7
```python
import matplotlib
import matplotlib.pyplot as plt
import pandas as pd
import baseball
%matplotlib inline
game_id, game = baseball.get_game_from_url('11-1-2017', 'HOU', 'LAD', 1)
pitch_tuple_list = []
for inning in game.inning_list:
for appearance in inning.top_half_appearance_list:
for event in appearance.event_list:
if isinstance(event, baseball.Pitch):
pitch_tuple_list.append(
(str(appearance.pitcher),
event.pitch_description,
event.pitch_position,
event.pitch_speed,
event.pitch_type)
)
data = pd.DataFrame(data=pitch_tuple_list, columns=['Pitcher', 'Pitch Description', 'Pitch Coordinate', 'Pitch Speed', 'Pitch Type'])
data.head()
```
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>Pitcher</th>
<th>Pitch Description</th>
<th>Pitch Coordinate</th>
<th>Pitch Speed</th>
<th>Pitch Type</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>21 Yu Darvish</td>
<td>Ball</td>
<td>(155.47, 160.83)</td>
<td>96.0</td>
<td>FF</td>
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
共18个文件
py:9个
txt:4个
pkg-info:2个
资源分类:Python库 所属语言:Python 资源全名:baseball-9.7.tar.gz 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
资源详情
资源评论
资源推荐
收起资源包目录
baseball-9.7.tar.gz (18个子文件)
baseball-9.7
PKG-INFO 309B
setup.cfg 38B
baseball
baseball.py 33KB
fetch_game.py 36KB
process_game_xml.py 35KB
stats.py 31KB
__init__.py 1KB
baseball_events.py 6KB
process_game_json.py 19KB
generate_svg.py 83KB
setup.py 480B
README.md 17KB
baseball.egg-info
PKG-INFO 309B
requires.txt 30B
not-zip-safe 1B
SOURCES.txt 403B
top_level.txt 9B
dependency_links.txt 1B
共 18 条
- 1
挣扎的蓝藻
- 粉丝: 12w+
- 资源: 15万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0