Skip to content

Instantly share code, notes, and snippets.

@mattdennewitz
Last active December 12, 2015 04:58
Show Gist options
  • Select an option

  • Save mattdennewitz/4718075 to your computer and use it in GitHub Desktop.

Select an option

Save mattdennewitz/4718075 to your computer and use it in GitHub Desktop.
1999-2002 Run Expectancy (from CSV)
import csv
import glob
import os
import sys
from mattreduce import Job
get_state = lambda outs, bases: '%s:%s' % (outs, bases)
def reader():
path = os.path.join(sys.argv[1], '*.csv')
for fn in glob.glob(path):
reader = csv.DictReader(open(fn, 'r'))
for row in reader:
# skip partial inning events
if (int(row['INN_CT']) >= 9
and row['HOME_TEAM_ID'] == row['BAT_TEAM_ID']):
continue
yield row
def mapper(row):
key = get_state(row['OUTS_CT'], row['START_BASES_CD'])
value = int(row['EVENT_RUNS_CT']) + int(row['FATE_RUNS_CT'])
yield key, value
def reducer(key, values):
return key, (sum(values) / float(len(values)))
def main():
for k, v in Job(mapper, reducer).run(reader()):
print k, round(v, 3)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment