immersinn-ds

Tue 13 September 2016

First Post

Posted by TRII in posts   

A basic look at quering a specific subset of players, calculating per-week point values for players, and comparing score values.

Imports

In [1]:
from IPython.core.display import display
In [2]:
import itertools
In [3]:
from nfl_analytics.core import players as nfl_players
from nfl_analytics.core import fantasy
from nfl_analytics.core import queries
In [4]:
import datetime
import numpy
import pandas
from matplotlib import style
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import matplotlib.cbook as cbook
%matplotlib inline

Some QBs

In [5]:
pids = ["8439", "13994", "2330"]
season = 'NFL - 2015'

players = [nfl_players.NFLPlayerSeason(pid, season) for pid in pids]

for p in players:
    p.prep()

Plots

In [6]:
months = mdates.MonthLocator()
monthsFmt = mdates.DateFormatter('%B %d')
weeks = mdates.WeekdayLocator()

# Seaborn's visual styling was inspired by ggplot,
#   so this style should be very similar:
plt.style.use('ggplot')
#plt.style.use('default')
plt.rcParams['xtick.minor.visible'] = True
In [7]:
def plotPlayersPoints(players_list):
    
    colors = [c for (p,c) in zip(players_list,
                                 itertools.cycle(['b', 'g', 'r', 'c', 'm', 'y', 'k']))]
    shape_formats = ['go--', 'bo--', 'ko--', 'ro--',]
    
    
    plt.rcParams['xtick.minor.visible'] = False
    
    fig, ax = plt.subplots(figsize = (12, 9))
    ax.set_title('QB Fantasy Football Points, 2015 Season')
    
    #
    
    
    # Axis Labels
    ax.set_xlabel("Week")
    ax.set_ylabel("Points per Game")

    # x-axis Ticks
    ax.xaxis.set_ticks([r for r in range(1,18)])

    # Define x-Axis Limits
    weekmin = 0.5
    weekmax = 17.5
    
    y_max = -999
    lines = []
    for i, player in enumerate(players_list):
        color = colors[i]
        p,  = ax.plot(player.ppg.week,
                      player.ppg.points,
                      c = color,
                      marker = 'o',
                      linestyle='--',
                      label=player.name,
                      markersize=8)
        lines.append(p)
        y_max = max(y_max, 
                    player.ppg.points.max())

    
    # Set Axis limits
    ax.set_xlim(weekmin, weekmax)
    ax.set_ylim(0, y_max + 10)

    # Set Legent from Lines
    ax.legend(lines, [l.get_label() for l in lines])

    plt.grid(True)
    plt.show()
In [8]:
plotPlayersPoints(players)

All QBs

In [9]:
season = 'NFL - 2015'
db_path = 'data/NFLGames'
In [10]:
qbs = queries.fetchPlayersPositionSeason('QB', season=season, db_path=db_path)
print(qbs.shape)
qbs.head()
(115, 2)
Out[10]:
name pid
0 Drew Stanton 10487
1 Matt Moore 11128
2 Matt Ryan 11237
3 Joe Flacco 11252
4 Chad Henne 11291
In [11]:
players = [nfl_players.NFLPlayerSeason(pid=pid, season=season) for pid in qbs.pid]
In [12]:
for p in players:
    p.prep()
In [13]:
# Filter out players with no games..
print(len(players))
players = [p for p in players if p.ppg.shape[0] > 0]
print(len(players))
115
64
In [14]:
# Remove players with less than 5 games:
target_players = [p for p in players if p.ppg.shape[0] >= 7]
print(len(target_players))
38
In [15]:
ppgs = pandas.DataFrame([{'pid' : p.pid,
                          'name' : p.name,
                          'total_points' : sum(p.ppg.points)} \
                          for p in target_players])
ppgs.sort_values(by='total_points', ascending=False, inplace=True)
display(ppgs.head(12))
display(ppgs.describe(percentiles=[.50, .75, .85]))
name pid total_points
7 Cam Newton 13994 373
17 Russell Wilson 14881 370
24 Tom Brady 2330 335
33 Alex Smith 8416 331
27 Jameis Winston 2969939 303
19 Blake Bortles 16724 302
29 Carson Palmer 4459 300
26 Drew Brees 2580 299
34 Aaron Rodgers 8439 284
16 Kirk Cousins 14880 281
3 Matthew Stafford 12483 280
30 Eli Manning 5526 278
total_points
count 38.000000
mean 204.263158
std 102.585675
min 0.000000
50% 221.000000
75% 280.750000
85% 300.900000
max 373.000000
In [16]:
# Build a big dataframe with all the ppgs
ppg_dfs = pandas.concat([p.ppg for p in target_players])
ppg_dfs.index = range(ppg_dfs.shape[0])
ppg_dfs.week = [int(w) for w in ppg_dfs.week]
print(ppg_dfs.shape)
ppg_dfs.head()
(496, 9)
Out[16]:
gid points away date home pid season team week
0 400791596 15 PHI 2015-09-14 ATL 11237 NFL - 2015 Atlanta Falcons 1
1 400791672 18 ATL 2015-09-20 NYG 11237 NFL - 2015 Atlanta Falcons 2
2 400791491 22 ATL 2015-09-27 DAL 11237 NFL - 2015 Atlanta Falcons 3
3 400791709 14 HOU 2015-10-04 ATL 11237 NFL - 2015 Atlanta Falcons 4
4 400791495 6 WSH 2015-10-11 ATL 11237 NFL - 2015 Atlanta Falcons 5
In [17]:
week_stats = []
for name, group in ppg_dfs.groupby('week'):
    week_stats.append({'week' : int(name),
                       'avg' : group['points'].mean(),
                       'std' : group['points'].std()})
week_stats = pandas.DataFrame(week_stats)
week_stats.sort_values(by='week', inplace=True)
week_stats = week_stats.reset_index()
week_stats.week = [int(w) for w in week_stats.week]
week_stats.head()
Out[17]:
index avg std week
0 0 13.218750 7.060725 1
1 1 17.965517 6.763784 2
2 2 15.379310 8.278258 3
3 3 14.750000 5.796710 4
4 4 15.928571 8.335238 5
In [18]:
fig, ax = plt.subplots(figsize = (16, 12))
ax.set_title('QB Fantasy Football Points, 2015 Season')

# Axis Labels
ax.set_xlabel("Week in Season")
ax.set_ylabel("Points per Game")

# x-axis Ticks
ax.xaxis.set_ticks([r for r in range(1,18)])

# Define x-Axis Limits
weekmin = 0.5
weekmax = 17.5

lines = []

e = ax.errorbar(x = 'week',
                y = 'avg',
                yerr = 'std',
                data = week_stats,
                color='k',
                fmt='^',
                markersize=10,
                label='Average')
lines.append(e)

# Plot the top n
y_max = -999
n = 3
colors = [c for (p,c) in zip(range(n),
                             itertools.cycle(['b', 'g', 'r', 'c', 'm', 'y', 'k']))]
for i, np in enumerate(zip(list(ppgs.name)[:n],
                                  list(ppgs.pid)[:n])
                              ):
    name, pid = np
    data = ppg_dfs[ppg_dfs.pid==pid]
    color = colors[i]
    p,  = ax.plot(data.week,
                  data.points,
                  c = color,
                  marker = 'o',
                  linestyle='-.',
                  label=name,
                  markersize=10)
    y_max = max(y_max,
                data.points.max())
    lines.append(p)
    

# Set Axis limits
ax.set_xlim(0.5, 17.5)
ax.set_ylim(0, y_max + 5)

# Set Legent from Lines
ax.legend(lines, [l.get_label() for l in lines])

plt.grid(True)
plt.show()