First Post
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()
Out[10]:
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))
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))
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]))
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()
Out[16]:
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]:
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()