Skip to content

Instantly share code, notes, and snippets.

@cageyjames
Created October 11, 2012 04:13
Show Gist options
  • Save cageyjames/3870119 to your computer and use it in GitHub Desktop.
Save cageyjames/3870119 to your computer and use it in GitHub Desktop.
Run Differential
#!/usr/bin/python
from scipy import stats
from pylab import *
# Read in the data.
mlb = loadtxt('mlb.txt', dtype=[('team', 'S3'), ('w', 'i'), ('l', 'i'), ('pct', 'f'), ('rdiff', 'i')])
# Plot the data with invisible points.
scatter(mlb['rdiff'], mlb['w'], s=0)
xlabel('Run differential')
ylabel('Wins')
# Put team names at the data points.
for (t, w, rd) in zip(mlb['team'], mlb['w'], mlb['rdiff']):
text(rd, w, t, size=9,
horizontalalignment='center', verticalalignment='center')
# Perform the linear regression
m, b, r, p, stderr = stats.linregress(mlb['rdiff'], mlb['w'])
# Get endpoints of regression line and plot it.
rdMin = min(mlb['rdiff'])
wMin = m*rdMin + b
rdMax = max(mlb['rdiff'])
wMax = m*rdMax + b
plot([rdMin, rdMax], [wMin, wMax])
show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment