返回介绍

01. Python 工具

02. Python 基础

03. Numpy

04. Scipy

05. Python 进阶

06. Matplotlib

07. 使用其他语言进行扩展

08. 面向对象编程

09. Theano 基础

10. 有趣的第三方模块

11. 有用的工具

12. Pandas

探索 NBA 数据

发布于 2022-09-03 20:46:15 字数 21429 浏览 0 评论 0 收藏 0

我们首先安装 Goldsberry 包,项目源地址:

https://github.com/bradleyfay/py-Goldsberry

使用 pip 安装:

pip install py-goldsberry

该包的接口与 pandas 兼容,可以与 pandasDataFrame 一起使用。

In [1]:

import goldsberry as gb
import pandas as pd

当前使用的版本号为:

In [2]:

gb.__version__

Out[2]:

'0.8.0.1'

球员信息

获得 2015-2016 赛季运动员的名单:

In [3]:

players = gb.PlayerList().players()
players = pd.DataFrame(players)

players.head()

Out[3]:

DISPLAY_LAST_COMMA_FIRSTFROM_YEARGAMES_PLAYED_FLAGPERSON_IDPLAYERCODEROSTERSTATUSTEAM_ABBREVIATIONTEAM_CITYTEAM_CODETEAM_IDTEAM_NAMETO_YEAR
0Acy, Quincy2012Y203112quincy_acy1SACSacramentokings1610612758Kings2015
1Adams, Jordan2014Y203919jordan_adams1MEMMemphisgrizzlies1610612763Grizzlies2015
2Adams, Steven2013Y203500steven_adams1OKCOklahoma Citythunder1610612760Thunder2015
3Afflalo, Arron2007Y201167arron_afflalo1NYKNew Yorkknicks1610612752Knicks2015
4Ajinca, Alexis2008Y201582alexis_ajinca1NOPNew Orleanspelicans1610612740Pelicans2015

球员总数为:

In [4]:

print len(players)
464

通过查询特定的 TEAM_ABBREVIATION,我们可以查看某个球队本赛季的球员,比如 2014-2015 赛季的总冠军金州勇士 GSW

In [5]:

gsw_players = players.ix[players["TEAM_ABBREVIATION"] == "GSW"]

gsw_players[["DISPLAY_LAST_COMMA_FIRST", "FROM_YEAR", "TEAM_ABBREVIATION", "TEAM_CITY", "TEAM_NAME", "PERSON_ID"]]

Out[5]:

DISPLAY_LAST_COMMA_FIRSTFROM_YEARTEAM_ABBREVIATIONTEAM_CITYTEAM_NAMEPERSON_ID
30Barbosa, Leandro2003GSWGolden StateWarriors2571
33Barnes, Harrison2012GSWGolden StateWarriors203084
52Bogut, Andrew2005GSWGolden StateWarriors101106
86Clark, Ian2013GSWGolden StateWarriors203546
103Curry, Stephen2009GSWGolden StateWarriors201939
135Ezeli, Festus2012GSWGolden StateWarriors203105
164Green, Draymond2012GSWGolden StateWarriors203110
209Iguodala, Andre2004GSWGolden StateWarriors2738
262Livingston, Shaun2004GSWGolden StateWarriors2733
263Looney, Kevon2015GSWGolden StateWarriors1626172
279McAdoo, James Michael2014GSWGolden StateWarriors203949
377Rush, Brandon2008GSWGolden StateWarriors201575
398Speights, Marreese2008GSWGolden StateWarriors201578
414Thompson, Jason2008GSWGolden StateWarriors201574
415Thompson, Klay2011GSWGolden StateWarriors202691

球员比赛数据

通过 DISPLAY_LAST_COMMA_FIRST,我们来查询宣布本赛季之后退役的科比布莱恩特(Kobe, Bryant)的信息:

In [6]:

kobe = players.ix[players["DISPLAY_LAST_COMMA_FIRST"].str.contains("Kobe")]

kobe

Out[6]:

DISPLAY_LAST_COMMA_FIRSTFROM_YEARGAMES_PLAYED_FLAGPERSON_IDPLAYERCODEROSTERSTATUSTEAM_ABBREVIATIONTEAM_CITYTEAM_CODETEAM_IDTEAM_NAMETO_YEAR
64Bryant, Kobe1996Y977kobe_bryant1LALLos Angeleslakers1610612747Lakers2015

为了方便,我们将 KobeID 放到变量中去:

In [7]:

kobe_id = 977

我们来看本赛季 Kobe 的比赛记录:

In [8]:

kobe_logs = gb.player.game_logs(kobe_id)

kobe_logs = pd.DataFrame(kobe_logs.logs())

# 最近五场比赛
kobe_logs.head()

Out[8]:

ASTBLKDREBFG3AFG3MFG3_PCTFGAFGMFG_PCTFTA...PFPLUS_MINUSPTSPlayer_IDREBSEASON_IDSTLTOVVIDEO_AVAILABLEWL
0306730.4291650.3134...2-1917977622015131L
10041440.2862560.2404...0-619977522015001L
24111440.2862890.3213...4-225977222015021L
32091140.36424100.4174...016279771222015211W
45031170.63621100.47612...3638977522015221W

5 rows × 27 columns

截至到全明星赛前,本赛季 Kobe 一共参加了 44 场比赛,其场均数据为:

In [9]:

kobe_logs.Game_ID

Out[9]:

0     0021500795
1     0021500776
2     0021500767
3     0021500747
4     0021500734
5     0021500720
6     0021500697
7     0021500662
8     0021500653
9     0021500638
10    0021500614
11    0021500608
12    0021500592
13    0021500576
14    0021500549
15    0021500539
16    0021500476
17    0021500458
18    0021500455
19    0021500440
20    0021500435
21    0021500422
22    0021500385
23    0021500370
24    0021500349
25    0021500342
26    0021500325
27    0021500308
28    0021500301
29    0021500286
30    0021500269
31    0021500263
32    0021500253
33    0021500244
34    0021500214
35    0021500201
36    0021500188
37    0021500151
38    0021500135
39    0021500095
40    0021500077
41    0021500059
42    0021500045
43    0021500031
44    0021500017
Name: Game_ID, dtype: object

In [10]:

def show_avg_info(avg):
    print "得分:{:.1f}".format(avg.ix["PTS"])
    print "篮板:{:.1f}".format(avg.ix["REB"])
    print "助攻:{:.1f}".format(avg.ix["AST"])
    print "盖帽:{:.1f}".format(avg.ix["BLK"])
    print "时间:{:.1f}".format(avg.ix["MIN"])
    print "抢断:{:.1f}".format(avg.ix["STL"])
    print "失误:{:.1f}".format(avg.ix["TOV"])
    print "犯规:{:.1f}".format(avg.ix["PF"])
    print "投篮:{:.1f}%".format(avg.ix["FGM"] * 100 / avg.ix["FGA"])
    print "三分:{:.1f}%".format(avg.ix["FG3M"] * 100 / avg.ix["FG3A"])
    print "罚篮:{:.1f}%".format(avg.ix["FTM"] * 100 / avg.ix["FTA"])
    print "后篮板:{:.1f}".format(avg.ix["DREB"])
    print "前篮板:{:.1f}".format(avg.ix["OREB"])
    print "正负值:{:.1f}".format(avg.ix["PLUS_MINUS"])

show_avg_info(kobe_logs.mean())
得分:16.9
篮板:4.2
助攻:3.4
盖帽:0.2
时间:29.3
抢断:1.0
失误:2.2
犯规:1.9
投篮:34.9%
三分:28.0%
罚篮:80.3%
后篮板:3.5
前篮板:0.7
正负值:-7.9

再看一下史提芬库里的场均数据(不要问我为什么跪着看球):

In [11]:

curry_id = 201939
curry_logs = gb.player.game_logs(curry_id)
curry_logs = pd.DataFrame(curry_logs.logs())

show_avg_info(curry_logs.mean())
得分:29.8
篮板:5.3
助攻:6.6
盖帽:0.2
时间:33.9
抢断:2.1
失误:3.3
犯规:2.0
投篮:50.8%
三分:45.4%
罚篮:91.2%
后篮板:4.5
前篮板:0.9
正负值:15.5

当然我们也可以对比一下职业生涯的数据:

In [12]:

kobe_career = gb.player.career_stats(kobe_id)
curry_career = gb.player.career_stats(curry_id)

职业生涯最高:

In [13]:

def show_career_high(career):
    career_high = pd.DataFrame(career.career_high()).ix[[0,1,5]]
    print career_high[["GAME_DATE", "STAT", "STAT_VALUE", "VS_TEAM_CITY", "VS_TEAM_NAME"]]

print "Kobe"
show_career_high(kobe_career)

print "Curry"
show_career_high(curry_career)
Kobe
     GAME_DATE STAT  STAT_VALUE VS_TEAM_CITY VS_TEAM_NAME
0  JAN 22 2006  PTS          81      Toronto      Raptors
1  JAN 24 2010  REB          16      Toronto      Raptors
5  JAN 15 2015  AST          17    Cleveland    Cavaliers
Curry
     GAME_DATE STAT  STAT_VALUE VS_TEAM_CITY VS_TEAM_NAME
0  FEB 27 2013  PTS          54     New York       Knicks
1  DEC 28 2015  REB          14   Sacramento        Kings
5  DEC 27 2013  AST          16      Phoenix         Suns

本赛季最高:

In [14]:

def show_season_high(career):
    career_high = pd.DataFrame(career.season_high()).ix[[0,1,5]]
    print career_high[["GAME_DATE", "STAT", "STAT_VALUE", "VS_TEAM_CITY", "VS_TEAM_NAME"]]

print "Kobe"
show_season_high(kobe_career)

print "Curry"
show_season_high(curry_career)
Kobe
     GAME_DATE STAT  STAT_VALUE VS_TEAM_CITY  VS_TEAM_NAME
0  FEB 02 2016  PTS          38    Minnesota  Timberwolves
1  FEB 04 2016  REB          12  New Orleans      Pelicans
5  NOV 15 2015  AST           9      Detroit       Pistons
Curry
     GAME_DATE STAT  STAT_VALUE VS_TEAM_CITY VS_TEAM_NAME
0  OCT 31 2015  PTS          53  New Orleans     Pelicans
1  DEC 28 2015  REB          14   Sacramento        Kings
5  JAN 25 2016  STL           5  San Antonio        Spurs

比赛信息

In [15]:

game_ids = gb.GameIDs()
game_ids = pd.DataFrame(game_ids.game_list())

game_ids.head()

Out[15]:

ASTBLKDREBFG3AFG3MFG3_PCTFGAFGMFG_PCTFTA...PTSREBSEASON_IDSTLTEAM_ABBREVIATIONTEAM_IDTEAM_NAMETOVVIDEO_AVAILABLEWL
0284452980.276124560.45246...14764220157DET1610612765Detroit Pistons111W
1302362390.39187530.60934...14246220159SAC1610612758Sacramento Kings151W
2342302190.42986520.60513...123382201510SAS1610612759San Antonio Spurs131W
32963635160.45795520.54715...131462201510GSW1610612744Golden State Warriors151W
4348383180.258104520.50016...122462201510SAC1610612758Sacramento Kings201L

5 rows × 29 columns

获得运动员的头像

In [16]:

from IPython.display import Image

Image("http://stats.nba.com/media/players/230x185/"+str(kobe_id)+".png")

Out[16]:

https://www.wenjiangs.com/wp-content/uploads/2022/docimg20/vrPGxGQuwi5hQQC2-kl0u88.png alt="">

In [17]:

Image("http://stats.nba.com/media/players/230x185/"+str(curry_id)+".png")

Out[17]:

https://www.wenjiangs.com/wp-content/uploads/2022/docimg20/sws1rXW4MvMjZh3u-fOFLLP.png alt="">

More

修改了 goldsberry\player\_Player.py 代码中的错误,使之能够查询退役球员的信息,修改后的代码在本文件夹下,放到安装目录之后下面的代码均可以运行:

In [18]:

from goldsberry.player import _Player as pl_old

1997 年的球员列表:

In [19]:

players_1997 = pl_old.PlayerList(1997)

players_1997 = pd.DataFrame(players_1997)

乔丹的球员 ID:

In [20]:

jordan_id = players_1997["PERSON_ID"].ix[players_1997["DISPLAY_LAST_COMMA_FIRST"].str.contains("Jordan, Michael")]
jordan_id = jordan_id[jordan_id.index[0]]
jordan_id

Out[20]:

893

乔丹在 1997-1998 赛季常规赛表现:

In [21]:

jordan_logs_1997 = pl_old.game_logs(jordan_id, season="1997")
jordan_logs_1997 = pd.DataFrame(jordan_logs_1997.logs())

show_avg_info(jordan_logs_1997.mean())
得分:28.7
篮板:5.8
助攻:3.5
盖帽:0.5
时间:38.9
抢断:1.7
失误:2.3
犯规:1.8
投篮:46.5%
三分:23.8%
罚篮:78.4%
后篮板:4.2
前篮板:1.6
正负值:7.3

乔丹在 1997-1998 赛季季后赛表现:

In [22]:

jordan_logs_1997 = pl_old.game_logs(jordan_id, season="1997", seasontype=2)
jordan_logs_1997 = pd.DataFrame(jordan_logs_1997.logs())

show_avg_info(jordan_logs_1997.mean())
得分:32.4
篮板:5.1
助攻:3.5
盖帽:0.6
时间:41.0
抢断:1.5
失误:2.1
犯规:2.2
投篮:46.2%
三分:30.2%
罚篮:81.2%
后篮板:3.5
前篮板:1.6
正负值:7.5

头像:

In [23]:

Image("http://stats.nba.com/media/players/230x185/"+str(jordan_id)+".png")

Out[23]:

https://www.wenjiangs.com/wp-content/uploads/2022/docimg20/xkbd9KlC9nxkfsXf-kkXfk8.png alt="">

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文