In [1]:
from lxml import html
import requests
from bs4 import BeautifulSoup
import urllib2
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
import pickle
%matplotlib inline

In [121]:
r = urllib2.urlopen('http://www.buda.org/leagues/past-leagues')
soup = BeautifulSoup(r, 'html.parser')
iframe = soup.find_all('iframe')[0]
response = urllib2.urlopen(iframe.attrs['src'])
iframe_soup = BeautifulSoup(response)
leaguelinks = [i.a['href'] for i in iframe_soup.find_all("td", class_="infobody")]

In [2]:
picklefile = '../all_players.p'

In [3]:
all_players = pickle.load( open( picklefile, "rb" ) )

In [4]:
pmean = []
players_means = {}
for player in all_players.keys():
    pratings = np.array(all_players[player])
    toolow = np.where(pratings < 0)
    if toolow[0].size > 0:
        pratings[toolow[0]] = 0
    pmean.append(pratings.mean())
    if pratings.mean() < 0:
        print(pratings)
    players_means[player] = pratings.mean()

In [127]:
# teamratings = {}
# playerlist = {}
teamavgratings = {}
# teamself = {}
# teamcaptain = {}
# teamcombined = {}
truedifferential = {}

# loop over all leagues in the BUDA database
for link in leaguelinks:

    # extract the league id for this league
    leagueid = link[link.index('league=') + 7:]


    # extract the league id for this league
    # springhat2016id = '30924'
#     leagueid = springhat2016id
    print("Working on league {}".format(leagueid))

    # scrape the list of teams for this league
    teamsurl = 'http://www.buda.org/hatleagues/rosters.php?section=showTeams&league=' + leagueid
    response = urllib2.urlopen(teamsurl)
    teams_soup = BeautifulSoup(response)

    # generate list of team ids and names for this league
    tdlist = teams_soup.find_all('td', class_='infobody')
    teamids = []
    teamnames = []
    for td in tdlist:
        try:
            url = td.a['href']
            idindex = url.index('team=')
            whichindex = url.index('which=')
            teamids.append(url[idindex+5:whichindex-1])
            teamnames.append(td.a.get_text())
        except:
            continue

    # if this league is complete, get the true score differential for each team

    # scrape the scores for this league
    leaguescoreurl = 'http://www.buda.org/hatleagues/scores.php?section=showLeagueSchedule&league=' + leagueid + '&byDivision=1&showGames=0'
    response = urllib2.urlopen(leaguescoreurl)
    leaguescore_soup = BeautifulSoup(response)

    # assemble the data of team ratings for this league
    print("Building database of team ratings")
    data = []
    data_opponent = []
    try:
        table = leaguescore_soup.find_all('table', attrs={'class':'info'})[1]
    except IndexError:
        print("Unable to find a database of scores for league {}".format(leagueid))
        continue
    rows = table.find_all('tr')
    for row in rows:
        cols = row.find_all('th')
        cols = [ele.text.strip() for ele in cols]
        data.append([ele for ele in cols if ele]) # Get rid of empty values

    # convert to dataframe and drop irrelevant columns
    dfdata = pd.DataFrame(data)
    #     print(leagueid, dfdata.columns)
    dfdata.columns = dfdata.ix[0, :]#['Team', 'Record', 'Plus/Minus', 'Tourney Qualifying games']
    #     print(leagueid, dfdata.columns)
    dfdata = dfdata.drop(0).reset_index()

    # fill na's with -99 to facilitate division dividers
    dfdata = dfdata.fillna(-99)

    # get the list of divisions in this league
    divnames = dfdata.ix[dfdata['Record'] == -99, 'Team'].values
    if len(divnames) == 0:
        print("No divisions found, skipping league {}".format(leagueid))
    #     continue

    dfdata['div'] = np.zeros(len(dfdata))
    for i in range(len(divnames)-1):
        try:
            divstart = np.where(dfdata['Team'] == divnames[i])[0][0]
        except IndexError:
            print("{} not found, skipping league {}".format(divnames[i], leagueid))
    #         continue
        try:
            divend = np.where(dfdata['Team'] == divnames[i + 1])[0][0]
        except IndexError:
            print("{} not found, skipping league {}".format(divnames[i + 1], leagueid))
    #         continue
        try:
            dfdata.ix[divstart + 1: divend, 'div'] = divnames[i]
        except KeyError:
            print("No base rating for {}, skipping league {}".format(divnames[i], leagueid))
    #         continue
    try:
        dfdata.ix[divend + 1:, 'div'] = divnames[-1]
    except KeyError:
        print("No base rating for {}, skipping league {}".format(divnames[-1], leagueid))
    #     continue

    # remove the division dividers from the dataframe
    for i in range(len(divnames)):
        dfdata = dfdata.drop(dfdata.index[dfdata['Team'] == divnames[i]])

    # generate the average goal differential column
    dfdata['wins'] = dfdata['Record'].apply(lambda x: int(x.split('-')[0]))
    dfdata['losses'] = dfdata['Record'].apply(lambda x: int(x.split('-')[1]))
    dfdata['games'] = dfdata['wins'] + dfdata['losses']
    dfdata['avgplusminus'] = dfdata['Plus/Minus'].astype('float') / dfdata['games']

    # find all players associated with each team
    print("Finding all players associated with each team")
    for teamid, teamname in zip(teamids, teamnames):

        teamurl = 'http://www.buda.org/hatleagues/rosters.php?section=showTeamRoster&team=' + teamid
        response = urllib2.urlopen(teamurl)
        roster_soup = BeautifulSoup(response)

        playerratings = []
        selfrating = []
        captainrating = []
        combinedrating = []
        players = [td.get_text() for td in roster_soup.find_all("td", class_="infobody")]
        for player in players:
            if player == '':
                continue
            if player in all_players:
                playerratings.append(players_means[player])
            else:
                # if someone hasn't played club league, they probably aren't very good
                playerratings.append(700)
            PLAYER = player.upper()
    #         if player == 'Bussmann, Shane':
    #             import pdb; pdb.set_trace()
            if PLAYER in sph2011['full name'].values:
    #             import pdb; pdb.set_trace()
                sph2011row = sph2011['full name'] == PLAYER
                selfrating.append(sph2011.ix[sph2011row, 'Self'].values[0])
                captainrating.append(sph2011.ix[sph2011row, 'CR'].values[0])
                combinedrating.append(sph2011.ix[sph2011row, 'Rating'].values[0])
        # the team rating is the average of the player ratings for that team
#         teamratings[teamname] = playerratings
#         playerlist[teamname] = players
        teamavgratings[teamname] = np.mean(playerratings)
#         teamself[teamname] = np.mean(selfrating)
#         teamcaptain[teamname] = np.mean(captainrating)
#         teamcombined[teamname] = np.mean(combinedrating)
    #     print("Finished with team {}".format(teamname))
        dfdatarow = dfdata['Team'] == teamname
        truedifferential[teamname] = dfdata.ix[dfdatarow, 'avgplusminus'].values[0]
        
        dfdata['divmean'] = np.zeros(len(dfdata))
        for div in divnames:
            divmean = np.mean(dfdata.ix[dfdata['div'] == div, 'Historical Rating'])
            allteamdf.ix[allteamdf['div'] == div, 'divmean'] = divmean

    print("Finished successfully with league {}".format(leagueid))
    print("")
#     import pdb; pdb.set_trace()


Working on league 40491
Building database of team ratings
Unable to find a database of scores for league 40491
Working on league 40278
Building database of team ratings
Finding all players associated with each team
Finished successfully with league 40278

Working on league 40273
Building database of team ratings
Finding all players associated with each team
Finished successfully with league 40273

Working on league 40268
Building database of team ratings
Unable to find a database of scores for league 40268
Working on league 40264
Building database of team ratings
Unable to find a database of scores for league 40264
Working on league 40258
Building database of team ratings
Finding all players associated with each team
Finished successfully with league 40258

Working on league 40253
Building database of team ratings
Finding all players associated with each team
Finished successfully with league 40253

Working on league 40249
Building database of team ratings
Unable to find a database of scores for league 40249
Working on league 40245
Building database of team ratings
Unable to find a database of scores for league 40245
Working on league 39633
Building database of team ratings
Finding all players associated with each team
Finished successfully with league 39633

Working on league 39960
Building database of team ratings
Unable to find a database of scores for league 39960
Working on league 39939
Building database of team ratings
Unable to find a database of scores for league 39939
Working on league 39904
Building database of team ratings
Unable to find a database of scores for league 39904
Working on league 39678
Building database of team ratings
Unable to find a database of scores for league 39678
Working on league 39673
Building database of team ratings
Unable to find a database of scores for league 39673
Working on league 39641
Building database of team ratings
Finding all players associated with each team
Finished successfully with league 39641

Working on league 39628
Building database of team ratings
Finding all players associated with each team
Finished successfully with league 39628

Working on league 39622
Building database of team ratings
Finding all players associated with each team
Finished successfully with league 39622

Working on league 39616
Building database of team ratings
Finding all players associated with each team
Finished successfully with league 39616

Working on league 39611
Building database of team ratings
Unable to find a database of scores for league 39611
Working on league 39602
Building database of team ratings
Finding all players associated with each team
Finished successfully with league 39602

Working on league 39344
Building database of team ratings
Finding all players associated with each team
Finished successfully with league 39344

Working on league 38871
Building database of team ratings
Unable to find a database of scores for league 38871
Working on league 38552
Building database of team ratings
Unable to find a database of scores for league 38552
Working on league 38548
Building database of team ratings
Unable to find a database of scores for league 38548
Working on league 38516
Building database of team ratings
Unable to find a database of scores for league 38516
Working on league 38506
Building database of team ratings
Finding all players associated with each team
Finished successfully with league 38506

Working on league 38502
Building database of team ratings
Unable to find a database of scores for league 38502
Working on league 38498
Building database of team ratings
Unable to find a database of scores for league 38498
Working on league 38493
Building database of team ratings
Finding all players associated with each team
Finished successfully with league 38493

Working on league 38490
Building database of team ratings
Finding all players associated with each team
Finished successfully with league 38490

Working on league 38484
Building database of team ratings
Finding all players associated with each team
Finished successfully with league 38484

Working on league 38480
Building database of team ratings
Finding all players associated with each team
Finished successfully with league 38480

Working on league 38475
Building database of team ratings
Unable to find a database of scores for league 38475
Working on league 37668
Building database of team ratings
Finding all players associated with each team
Finished successfully with league 37668

Working on league 37959
Building database of team ratings
Unable to find a database of scores for league 37959
Working on league 37690
Building database of team ratings
Unable to find a database of scores for league 37690
Working on league 37686
Building database of team ratings
Unable to find a database of scores for league 37686
Working on league 37674
Building database of team ratings
Finding all players associated with each team
Finished successfully with league 37674

Working on league 37663
Building database of team ratings
Finding all players associated with each team
Finished successfully with league 37663

Working on league 37659
Building database of team ratings
Finding all players associated with each team
Finished successfully with league 37659

Working on league 37653
Building database of team ratings
Finding all players associated with each team
Finished successfully with league 37653

Working on league 37648
Building database of team ratings
Finding all players associated with each team
Finished successfully with league 37648

Working on league 37634
Building database of team ratings
Unable to find a database of scores for league 37634
Working on league 37376
Building database of team ratings
Unable to find a database of scores for league 37376
Working on league 37370
Building database of team ratings
Unable to find a database of scores for league 37370
Working on league 36881
Building database of team ratings
Finding all players associated with each team
Finished successfully with league 36881

Working on league 36852
Building database of team ratings
Unable to find a database of scores for league 36852
Working on league 36841
Building database of team ratings
Unable to find a database of scores for league 36841
Working on league 36824
Building database of team ratings
Unable to find a database of scores for league 36824
Working on league 36530
Building database of team ratings
Unable to find a database of scores for league 36530
Working on league 36486
Building database of team ratings
Unable to find a database of scores for league 36486
Working on league 36481
Building database of team ratings
Finding all players associated with each team
Finished successfully with league 36481

Working on league 36469
Building database of team ratings
Unable to find a database of scores for league 36469
Working on league 36465
Building database of team ratings
Finding all players associated with each team
Finished successfully with league 36465

Working on league 36459
Building database of team ratings
Finding all players associated with each team
Finished successfully with league 36459

Working on league 35836
Building database of team ratings
Finding all players associated with each team
Finished successfully with league 35836

Working on league 35831
Building database of team ratings
Finding all players associated with each team
Finished successfully with league 35831

Working on league 35821
Building database of team ratings
Unable to find a database of scores for league 35821
Working on league 35817
Building database of team ratings
Unable to find a database of scores for league 35817
Working on league 35487
Building database of team ratings
Finding all players associated with each team
Finished successfully with league 35487

Working on league 35481
Building database of team ratings
Finding all players associated with each team
Finished successfully with league 35481

Working on league 35476
Building database of team ratings
Finding all players associated with each team
Finished successfully with league 35476

Working on league 35470
Building database of team ratings
Finding all players associated with each team
Finished successfully with league 35470

Working on league 35462
Building database of team ratings
Unable to find a database of scores for league 35462
Working on league 35358
Building database of team ratings
Unable to find a database of scores for league 35358
Working on league 34896
Building database of team ratings
Finding all players associated with each team
Finished successfully with league 34896

Working on league 34841
Building database of team ratings
Unable to find a database of scores for league 34841
Working on league 34579
Building database of team ratings
Unable to find a database of scores for league 34579
Working on league 34550
Building database of team ratings
Finding all players associated with each team
Finished successfully with league 34550

Working on league 34524
Building database of team ratings
Finding all players associated with each team
Finished successfully with league 34524

Working on league 34405
Building database of team ratings
Finding all players associated with each team
Finished successfully with league 34405

Working on league 34400
Building database of team ratings
Unable to find a database of scores for league 34400
Working on league 34395
Building database of team ratings
Finding all players associated with each team
Finished successfully with league 34395

Working on league 34330
Building database of team ratings
Finding all players associated with each team
Finished successfully with league 34330

Working on league 34170
Building database of team ratings
Unable to find a database of scores for league 34170
Working on league 34023
Building database of team ratings
Finding all players associated with each team
Finished successfully with league 34023

Working on league 33993
Building database of team ratings
Finding all players associated with each team
Finished successfully with league 33993

Working on league 33985
Building database of team ratings
Finding all players associated with each team
Finished successfully with league 33985

Working on league 33980
Building database of team ratings
Finding all players associated with each team
Finished successfully with league 33980

Working on league 33233
Building database of team ratings
Finding all players associated with each team
Finished successfully with league 33233

Working on league 32945
Building database of team ratings
Finding all players associated with each team
Finished successfully with league 32945

Working on league 32941
Building database of team ratings
Unable to find a database of scores for league 32941
Working on league 32936
Building database of team ratings
Finding all players associated with each team
Finished successfully with league 32936

Working on league 32746
Building database of team ratings
Finding all players associated with each team
Finished successfully with league 32746

Working on league 32741
Building database of team ratings
Finding all players associated with each team
Finished successfully with league 32741

Working on league 32566
Building database of team ratings
Unable to find a database of scores for league 32566
Working on league 32563
Building database of team ratings
Unable to find a database of scores for league 32563
Working on league 32420
Building database of team ratings
Finding all players associated with each team
Finished successfully with league 32420

Working on league 32268
Building database of team ratings
Unable to find a database of scores for league 32268
Working on league 32262
Building database of team ratings
Finding all players associated with each team
Finished successfully with league 32262

Working on league 31034
Building database of team ratings
Finding all players associated with each team
Finished successfully with league 31034

Working on league 31029
Building database of team ratings
Finding all players associated with each team
Finished successfully with league 31029

Working on league 31023
Building database of team ratings
Unable to find a database of scores for league 31023
Working on league 31018
Building database of team ratings
Finding all players associated with each team
Finished successfully with league 31018

Working on league 31014
Building database of team ratings
Unable to find a database of scores for league 31014
Working on league 31007
Building database of team ratings
Unable to find a database of scores for league 31007
Working on league 31002
Building database of team ratings
Finding all players associated with each team
Finished successfully with league 31002

Working on league 30924
Building database of team ratings
Finding all players associated with each team
Finished successfully with league 30924

Working on league 30919
Building database of team ratings
Finding all players associated with each team
Finished successfully with league 30919

Working on league 30844
Building database of team ratings
Finding all players associated with each team
Finished successfully with league 30844

Working on league 29449
Building database of team ratings
Finding all players associated with each team
Finished successfully with league 29449

Working on league 29432
Building database of team ratings
Finding all players associated with each team
Finished successfully with league 29432

Working on league 29390
Building database of team ratings
Finding all players associated with each team
Finished successfully with league 29390

Working on league 29389
Building database of team ratings
Finding all players associated with each team
Finished successfully with league 29389

Working on league 29388
Building database of team ratings
Finding all players associated with each team
Finished successfully with league 29388

Working on league 29387
Building database of team ratings
Finding all players associated with each team
Finished successfully with league 29387

Working on league 29386
Building database of team ratings
Unable to find a database of scores for league 29386
Working on league 29385
Building database of team ratings
Finding all players associated with each team
Finished successfully with league 29385

Working on league 29237
Building database of team ratings
Finding all players associated with each team
Finished successfully with league 29237

Working on league 29235
Building database of team ratings
Finding all players associated with each team
Finished successfully with league 29235

Working on league 29192
Building database of team ratings
Finding all players associated with each team
Finished successfully with league 29192

Working on league 29187
Building database of team ratings
Finding all players associated with each team
Finished successfully with league 29187

Working on league 28912
Building database of team ratings
Finding all players associated with each team
Finished successfully with league 28912

Working on league 26998
Building database of team ratings
Finding all players associated with each team
Finished successfully with league 26998

Working on league 26909
Building database of team ratings
Finding all players associated with each team
Finished successfully with league 26909

Working on league 26908
Building database of team ratings
Finding all players associated with each team
Finished successfully with league 26908

Working on league 26907
Building database of team ratings
Finding all players associated with each team
Finished successfully with league 26907

Working on league 26906
Building database of team ratings
Finding all players associated with each team
Finished successfully with league 26906

Working on league 26905
Building database of team ratings
Finding all players associated with each team
Finished successfully with league 26905

Working on league 26904
Building database of team ratings
Unable to find a database of scores for league 26904
Working on league 26903
Building database of team ratings
Finding all players associated with each team
Finished successfully with league 26903

Working on league 23714
Building database of team ratings
Finding all players associated with each team
Finished successfully with league 23714

Working on league 23696
Building database of team ratings
Finding all players associated with each team
Finished successfully with league 23696

Working on league 23695
Building database of team ratings
Finding all players associated with each team
Finished successfully with league 23695

Working on league 23694
Building database of team ratings
Finding all players associated with each team
Finished successfully with league 23694

Working on league 23693
Building database of team ratings
Finding all players associated with each team
Finished successfully with league 23693

Working on league 23692
Building database of team ratings
Unable to find a database of scores for league 23692
Working on league 23691
Building database of team ratings
Finding all players associated with each team
Finished successfully with league 23691

Working on league 23626
Building database of team ratings
Finding all players associated with each team
Finished successfully with league 23626

Working on league 20439
Building database of team ratings
Finding all players associated with each team
Finished successfully with league 20439

Working on league 20438
Building database of team ratings
Finding all players associated with each team
Finished successfully with league 20438

Working on league 20436
Building database of team ratings
Finding all players associated with each team
Finished successfully with league 20436

Working on league 20435
Building database of team ratings
Finding all players associated with each team
Finished successfully with league 20435

Working on league 20434
Building database of team ratings
Unable to find a database of scores for league 20434
Working on league 20433
Building database of team ratings
Finding all players associated with each team
Finished successfully with league 20433

Working on league 20175
Building database of team ratings
Finding all players associated with each team
Finished successfully with league 20175

Working on league 18047
Building database of team ratings
Finding all players associated with each team
Finished successfully with league 18047

Working on league 17692
Building database of team ratings
Finding all players associated with each team
Finished successfully with league 17692

Working on league 17691
Building database of team ratings
Finding all players associated with each team
Finished successfully with league 17691

Working on league 17604
Building database of team ratings
Unable to find a database of scores for league 17604
Working on league 17603
Building database of team ratings
Finding all players associated with each team
Finished successfully with league 17603

Working on league 17602
Building database of team ratings
Finding all players associated with each team
Finished successfully with league 17602

Working on league 17499
Building database of team ratings
Finding all players associated with each team
Finished successfully with league 17499

Working on league 16881
Building database of team ratings
Finding all players associated with each team
Finished successfully with league 16881

Working on league 16880
Building database of team ratings
Finding all players associated with each team
Finished successfully with league 16880

Working on league 13647
Building database of team ratings
Finding all players associated with each team
Finished successfully with league 13647

Working on league 13447
Building database of team ratings
Unable to find a database of scores for league 13447
Working on league 13446
Building database of team ratings
Finding all players associated with each team
Finished successfully with league 13446

Working on league 13445
Building database of team ratings
Finding all players associated with each team
Finished successfully with league 13445

Working on league 13215
Building database of team ratings
Finding all players associated with each team
Finished successfully with league 13215

Working on league 12807
Building database of team ratings
Finding all players associated with each team
Finished successfully with league 12807

Working on league 12769
Building database of team ratings
Finding all players associated with each team
Finished successfully with league 12769

Working on league 11632
Building database of team ratings
Unable to find a database of scores for league 11632
Working on league 11631
Building database of team ratings
Unable to find a database of scores for league 11631
Working on league 11604
Building database of team ratings
Finding all players associated with each team
Finished successfully with league 11604

Working on league 11402
Building database of team ratings
Finding all players associated with each team
Finished successfully with league 11402

Working on league 11401
Building database of team ratings
Finding all players associated with each team
Finished successfully with league 11401

Working on league 10918
Building database of team ratings
Finding all players associated with each team
Finished successfully with league 10918

Working on league 10917
Building database of team ratings
Finding all players associated with each team
Finished successfully with league 10917

Working on league 10309
Building database of team ratings
Unable to find a database of scores for league 10309
Working on league 10217
Building database of team ratings
Unable to find a database of scores for league 10217
Working on league 6340
Building database of team ratings
Finding all players associated with each team
Finished successfully with league 6340

Working on league 6326
Building database of team ratings
Finding all players associated with each team
Finished successfully with league 6326

Working on league 6235
Building database of team ratings
Finding all players associated with each team
Finished successfully with league 6235

Working on league 5629
Building database of team ratings
Finding all players associated with each team
Finished successfully with league 5629

Working on league 5130
Building database of team ratings
Finding all players associated with each team
Finished successfully with league 5130

Working on league 5129
Building database of team ratings
Finding all players associated with each team
Finished successfully with league 5129

Working on league 4919
Building database of team ratings
Unable to find a database of scores for league 4919
Working on league 4886
Building database of team ratings
Unable to find a database of scores for league 4886
Working on league 3804
Building database of team ratings
Finding all players associated with each team
Finished successfully with league 3804

Working on league 3669
Building database of team ratings
Finding all players associated with each team
Finished successfully with league 3669

Working on league 3162
Building database of team ratings
Finding all players associated with each team
Finished successfully with league 3162

Working on league 3144
Building database of team ratings
Finding all players associated with each team
Finished successfully with league 3144

Working on league 2408
Building database of team ratings
Unable to find a database of scores for league 2408
Working on league 2407
Building database of team ratings
Finding all players associated with each team
Finished successfully with league 2407

Working on league 1080
Building database of team ratings
Finding all players associated with each team
Finished successfully with league 1080

Working on league 1083
Building database of team ratings
Unable to find a database of scores for league 1083
Working on league 1082
Building database of team ratings
Unable to find a database of scores for league 1082
Working on league 1081
Building database of team ratings
Unable to find a database of scores for league 1081
Working on league 1095
Building database of team ratings
Unable to find a database of scores for league 1095
Working on league 1094
Building database of team ratings
Unable to find a database of scores for league 1094
Working on league 1093
Building database of team ratings
Unable to find a database of scores for league 1093

In [146]:
tdiff[0]


Out[146]:
46    0.705882
Name: avgplusminus, dtype: float64

In [144]:
tag = np.array(teamavgratings.values())
tdiff = np.array(truedifferential.values())
ok = np.where(np.abs(tdiff) < 1e4)
tag = tag[ok]
plt.plot(tdiff[ok], tag, 'o')


---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-144-8b4e05f25715> in <module>()
      1 tag = np.array(teamavgratings.values())
      2 tdiff = np.array(truedifferential.values())
----> 3 ok = np.where(np.abs(tdiff) < 1e4)
      4 tag = tag[ok]
      5 plt.plot(tdiff[ok], tag, 'o')

/Users/rbussman/anaconda/lib/python2.7/site-packages/pandas/core/generic.pyc in __nonzero__(self)
    885         raise ValueError("The truth value of a {0} is ambiguous. "
    886                          "Use a.empty, a.bool(), a.item(), a.any() or a.all()."
--> 887                          .format(self.__class__.__name__))
    888 
    889     __bool__ = __nonzero__

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

In [141]:
tag


Out[141]:
[1579.8208061183182,
 1012.9647022946754,
 992.44550596165573,
 1131.6961580077641,
 892.82459847844461,
 936.69261052169782,
 1128.2119202811073,
 1061.708407271859,
 946.15549137515598,
 996.36542503299086,
 1597.7704036159919,
 965.84493663111959,
 1215.8000006180791,
 700.0,
 1423.4379699197207,
 958.87395409353314,
 789.94845193868628,
 1450.1281401338424,
 749.90840226487478,
 1028.0266598594014,
 1542.6825648245101,
 828.0934619574324,
 1048.6736173372501,
 1124.2133868396286,
 1327.0228524651982,
 1055.7734539488681,
 1508.2642614381487,
 976.88402205482043,
 1335.5336046031205,
 1502.7746677797543,
 924.72205862483406,
 962.45576768616183,
 1154.0517169864113,
 1080.6603772759104,
 1126.8849055313999,
 895.12473189730326,
 1108.717207743744,
 739.51838055107282,
 1663.73311410624,
 1051.7495954349663,
 884.75747057263823,
 742.7649419616331,
 1041.6669175948748,
 1187.7686202686202,
 1102.3801139621185,
 788.49581076449715,
 875.31468106412001,
 1150.1036675344089,
 1372.5499395901425,
 867.94527716701646,
 1349.6870748299318,
 1309.082536536336,
 808.41463866767504,
 899.76562951426092,
 1079.3025699758143,
 885.28466742665648,
 928.19842894264616,
 1135.0064234756633,
 821.687463534735,
 744.84537849125002,
 834.72052025294704,
 759.31175595238096,
 808.90670024420024,
 923.38952802735241,
 700.0,
 1987.9385828305683,
 846.20159932659919,
 824.15760544335751,
 1013.0707669452339,
 909.51396089143816,
 975.29246031746027,
 875.40280870135712,
 720.90353769037984,
 630.58952796386609,
 1149.9935253119577,
 1115.9109462516321,
 1348.7565314114129,
 889.56372365810387,
 1056.0264024367525,
 1080.7248560400503,
 1154.0825220562992,
 nan,
 1361.7492756361803,
 1702.5030571280572,
 1033.3342379901246,
 1148.7738531972864,
 894.48736686487837,
 1074.2671974777672,
 1292.7734986306782,
 700.0,
 792.02320456854079,
 1040.0156441040442,
 1378.8953399541633,
 1163.4617132220369,
 1376.3633215265013,
 1340.890059015059,
 1077.3670502858877,
 1400.3644464150707,
 1526.7303924544442,
 986.72224367827994,
 912.5009289574075,
 1169.4947965218928,
 1132.8218948637148,
 1309.2410752224655,
 1002.2375268754359,
 1499.1268468487153,
 1315.7376588482787,
 987.6189867680298,
 1388.036640651899,
 1227.5874460417335,
 1130.4406224010686,
 976.50310038027283,
 781.29008281816368,
 1200.5002281393558,
 700.0,
 1783.1188725490197,
 1113.2924625745547,
 1591.6507302246989,
 700.0,
 859.74276685435757,
 1225.3679761352755,
 843.39675577555272,
 843.0279309406128,
 933.2424087254243,
 1149.9907418088028,
 1412.4040707930415,
 1200.3794319871952,
 979.56747710398577,
 1260.0958920368848,
 1606.0704383370166,
 1363.0365118448476,
 941.85340290034333,
 836.3163884639448,
 1112.6277257570214,
 1838.5980055723389,
 969.67698985834238,
 775.22963118348298,
 1478.6360284072962,
 985.48625106645363,
 1564.8479983216953,
 1438.2311635184035,
 1071.3690001916689,
 973.9608389519741,
 1037.0580150947158,
 849.46203153571582,
 1305.1443214160115,
 896.34368000215034,
 886.06051621022084,
 900.37744262518777,
 1077.786390692926,
 578.00411235516935,
 1163.3463411818343,
 944.84373159672305,
 809.13001401793679,
 1333.7787572792924,
 1067.6532739373024,
 1017.749494767365,
 1552.5351992131048,
 1360.2558035694326,
 1212.396851789862,
 817.00706449982761,
 700.0,
 1278.4482763376927,
 945.63027321206368,
 1327.9292974397711,
 885.70247392832118,
 923.70097208052653,
 700.0,
 1339.4714380166386,
 1119.5447300739841,
 902.93415928561444,
 1189.705594242359,
 1581.5436539289376,
 839.69359692594981,
 856.08523345365938,
 1441.7050018632679,
 1418.5155097879674,
 1507.5287271971531,
 1069.6048360039231,
 918.20034335202502,
 962.22403983829065,
 1389.5934019540828,
 1126.5720859489668,
 1027.3972366351343,
 862.91686907889152,
 nan,
 1077.6654847791758,
 706.38421142365053,
 1360.3379120879119,
 1177.1045028049016,
 1032.3705492211434,
 1075.1608233367383,
 992.66303194733189,
 897.32515061209438,
 1419.0955082008268,
 1419.8721260433999,
 700.0,
 1279.2090871377741,
 1065.5600222658747,
 1041.9342055873462,
 925.91351183001723,
 1542.2727047846458,
 997.04541381237811,
 1232.2940769691836,
 1147.04382143251,
 1012.7705572384376,
 956.77107025681016,
 885.01604035164496,
 990.39066017979167,
 992.90075074341621,
 1335.256118327562,
 1077.6760113814735,
 1091.4662875493887,
 931.25327299687808,
 949.23778962439678,
 700.0,
 700.0,
 1025.9794156961298,
 700.0,
 1059.8755189678136,
 1291.8055555555554,
 1121.157587910187,
 1142.7869457190027,
 1031.3977252913496,
 1131.84341669382,
 871.23203372981811,
 1375.0060679758344,
 947.97625764923339,
 810.75692005470046,
 1041.1491119101179,
 1319.2354040503433,
 776.36675016925483,
 950.63430974311507,
 562.72005772005775,
 930.52764738085045,
 1048.7284174226488,
 1064.3430971625326,
 392.46877560902681,
 832.6438251426672,
 1094.3812506521365,
 1097.3046670526344,
 1020.7006944011406,
 1099.931531653725,
 876.62712645802594,
 1077.2992821007324,
 1308.8811487167725,
 808.95186508844733,
 807.92699280159093,
 956.08231884617567,
 855.45418260026088,
 948.72995313063109,
 1110.0,
 996.15901278022238,
 1038.8107236487817,
 1265.8213993418019,
 966.36565566683657,
 984.92147502022033,
 1440.5010690159252,
 1722.0050170068027,
 991.09463807487566,
 641.14005827505821,
 1230.7374365290536,
 963.36540689829758,
 1117.509817003064,
 700.0,
 1034.3551333144057,
 1230.1340519120467,
 831.84635391313884,
 1546.9541304021639,
 1104.2805395205723,
 1478.9186785121287,
 1307.5875891031253,
 1338.6396467722304,
 1084.2557852007744,
 1380.2191533838127,
 1131.8458920368848,
 1161.6013021566932,
 1085.8703054533175,
 917.01929110652554,
 1224.3378660667995,
 1098.5287661615662,
 995.90277777777771,
 952.40390274593199,
 1029.2532577838829,
 974.59827828799644,
 908.94869599429387,
 1096.9041384984966,
 700.0,
 996.41002727814839,
 823.6353074886357,
 767.42569666751194,
 970.70734659015488,
 1110.5882352941176,
 944.09165760852102,
 993.81401829972765,
 989.54010598942887,
 939.11359660475011,
 992.85620456094057,
 1046.25,
 1046.25,
 1053.9567025922795,
 934.50513952739504,
 923.57586752436328,
 941.98717650801836,
 1030.239528905674,
 1126.3055311741139,
 1110.0387305002691,
 1169.299001763603,
 1217.5282556315792,
 1094.5982858927678,
 994.18610863507422,
 811.58344292661241,
 1163.0765894687652,
 913.9192313201504,
 700.0,
 916.12007081917318,
 1474.0641542697667,
 1289.1334093924524,
 646.84601113172539,
 1285.5798097155682,
 1230.4895361454649,
 877.3374786368862,
 1027.7836563438677,
 1101.7080475554533,
 1366.3283208985818,
 1672.6757801875481,
 893.3309159723641,
 793.16682341096407,
 1323.3412416658134,
 1049.5258097319249,
 834.15592771228671,
 1729.8012063940423,
 1165.5803213935433,
 1344.2808296413291,
 1107.6317814849328,
 1446.7616552413665,
 1185.6195406915353,
 1401.6280172677989,
 884.86832207727969,
 964.99684491584412,
 1353.7083966988805,
 996.75113992443073,
 1310.9661386112789,
 1473.7072043442884,
 1260.9070940896779,
 742.43317258901425,
 1092.1389259926495,
 1394.7715084089175,
 1193.1182940928031,
 700.0,
 1460.1546883013223,
 1169.0748452683324,
 1189.705594242359,
 934.18576200753728,
 1060.7952808352225,
 1302.7491064070889,
 904.83615298022119,
 1078.7432371045102,
 992.82343419726442,
 1476.8981294492808,
 1133.7530134076874,
 1112.012987012987,
 1055.3661223365557,
 859.77647257696094,
 717.19516004828506,
 891.32874024517787,
 1308.2848268477583,
 1151.6417029865559,
 835.48757929857948,
 886.05098177788193,
 1672.5824731907385,
 1064.4533882249093,
 1038.1806632054308,
 907.46334937399274,
 952.42318348082597,
 925.58542416580906,
 902.46796529286189,
 700.0,
 1245.4324045407639,
 912.90480214373167,
 808.22568363009543,
 1061.2867888577177,
 1287.9854840926271,
 799.76734938319998,
 1185.3391987934583,
 976.78448488591994,
 824.3975837515311,
 1906.5337995337995,
 972.33539187306667,
 1380.6699255244032,
 1410.1526512156618,
 1072.3172374892888,
 1075.8285472085206,
 1415.16485206987,
 1082.4826963646458,
 1197.1547619047619,
 1493.0765507663768,
 866.31989024961035,
 1125.9850968326457,
 993.73055552487222,
 1124.8517030044841,
 1055.6467087816168,
 1410.224166498997,
 1350.2926839582406,
 1126.2568566578043,
 928.87964365286018,
 982.93958660843646,
 1389.2874458874458,
 1520.7582914996449,
 905.34838855572593,
 1124.3138061237642,
 1170.5717246185168,
 966.08219253981679,
 1390.0938409601108,
 1057.6173630824308,
 1025.2356225704407,
 1090.765792031098,
 1407.4509853945713,
 1061.267146726575,
 1087.6282051282051,
 1080.9743673229123,
 1079.2029034364596,
 1406.0663070562173,
 1091.6150244588648,
 1321.2788592360023,
 1543.1034168362253,
 978.07522500880179,
 1142.1952063215508,
 733.69834578300481,
 991.63014268644508,
 1405.6100422321701,
 993.25607563451683,
 1278.0206130374224,
 1082.996096073748,
 952.95172905444815,
 801.98859485506807,
 862.86378882486383,
 1614.6406028399695,
 920.90095126045753,
 1255.8398252176708,
 871.76661771297245,
 1279.9087126586849,
 1704.8237867301075,
 1340.030861530616,
 1034.5813346317548,
 1046.5558095503666,
 969.31310288133852,
 1002.5351712337612,
 1695.530664881049,
 988.26766103877458,
 1026.6094927642175,
 1047.9388279846742,
 1276.4315728439406,
 1418.7545469000761,
 1168.7036759176258,
 725.58487245898232,
 941.6906438720456,
 1300.7022210993982,
 906.9726853453094,
 896.42096288870562,
 1195.5218733853083,
 764.85624057315238,
 1462.3540711753442,
 1585.7706932480437,
 1223.2441178447198,
 948.43852627030378,
 1008.3877120949458,
 1321.491264950218,
 1133.9925619834712,
 1028.6466218872188,
 953.4151807814269,
 896.23830342375618,
 1057.9451202028486,
 1271.6530848594462,
 893.97811260275967,
 1540.7792207792209,
 1220.048420119283,
 817.48250539593414,
 1577.0482023693958,
 999.56786075602702,
 1109.6841934991767,
 1206.7550642542813,
 973.66845607876041,
 1300.6737321552841,
 1461.3531103208056,
 1033.5254547012828,
 700.0,
 719.53556806245228,
 913.95030532749456,
 840.70628986373436,
 612.35828706845678,
 1002.3128022903691,
 1337.5071645553251,
 1387.0009058442843,
 1421.3493260572261,
 1371.1208974361646,
 1109.3571428571429,
 938.52493956554736,
 1497.657278082487,
 1449.2260879828596,
 689.56382431342524,
 1036.0416358643129,
 1398.3910339801969,
 700.0,
 1300.8080050495435,
 1025.3357429758701,
 1327.0560364271664,
 1189.705594242359,
 1004.1430768163846,
 795.36593809694295,
 1317.8249180369228,
 862.55126436915896,
 1353.4949673243061,
 1307.5311445704558,
 1159.6143488652363,
 937.81154187474544,
 1270.9601725043713,
 833.0906129134919,
 802.1862014995238,
 1047.2052969820411,
 1080.3808634758882,
 1079.9445785446546,
 956.33942956202748,
 517.32074829931969,
 1131.9924904210463,
 1138.2858393446536,
 700.0,
 1726.4863602912105,
 1134.4458760567427,
 1011.5428762607362,
 1104.7579807820564,
 827.4753937628526,
 815.52065940665398,
 1315.008926764649,
 1476.6091800461402,
 1214.2763494615035,
 868.34861177850416,
 933.362219048626,
 832.81035646002124,
 1125.5999381165293,
 1169.2528960672425,
 971.56029683721408,
 700.0,
 924.3241865441596,
 1160.2688803826213,
 892.63176505956721,
 1224.2846485020316,
 1227.6849458364857,
 1492.7511947948269,
 825.62395690875019,
 763.99719887955177,
 1322.9914336417478,
 1100.8381527813406,
 1507.1428571428571,
 1107.9785978388554,
 958.85400881053329,
 900.79194868180059,
 1364.2710415262111,
 1594.6933426620797,
 980.33988732554417,
 1370.1339143312828,
 1129.183516336069,
 1046.6879512679086,
 904.06847781292925,
 888.98381080301965,
 1083.2010910108827,
 1141.5789201011958,
 742.69749536316851,
 932.05918324321465,
 1082.432203907204,
 700.0,
 866.25800801174455,
 1757.0457141943828,
 946.04229557500378,
 1059.5357002415419,
 1078.4656776897177,
 1160.1162240802194,
 926.76475957240382,
 816.73525235147099,
 1030.1753979187015,
 1110.0387305002691,
 1128.2623826243616,
 897.61099820172819,
 848.20740152652502,
 925.87690924443405,
 986.69335765228061,
 1268.9501632223325,
 1103.1769243720676,
 906.4490094154296,
 1005.5369393466378,
 814.10484440431696,
 864.35602439746822,
 1813.2041524743868,
 1213.3596246440479,
 866.53404317050126,
 753.05922828461519,
 1240.2216827178836,
 700.0,
 1356.3718664629539,
 997.7990717701573,
 1479.8026215714108,
 1430.1975729919459,
 1411.8820612872803,
 1071.6923024806001,
 1081.7609383954364,
 1170.3510708746119,
 972.17112356709595,
 1146.2417341430501,
 922.66555592398936,
 1214.5854663382424,
 1203.1848775125954,
 1520.9459155878599,
 971.05036629005667,
 926.77639631847023,
 1187.9749689820833,
 955.38865891472415,
 1084.1348713711864,
 1290.2195880868314,
 884.39918831168836,
 1548.2073767982861,
 1033.634959183265,
 1241.4476084310675,
 1190.9788018302424,
 647.95164495755773,
 1077.7723369588855,
 857.46657863671442,
 1438.9527050648023,
 998.2047078699203,
 1055.8139459628599,
 1334.1446993906018,
 1008.4017452161473,
 1034.7531881698549,
 932.53049539557685,
 1033.2256580294188,
 1074.7126956877487,
 752.00433073870568,
 844.65275385067571,
 1044.0042512240677,
 957.77442446184705,
 777.83811869744534,
 1152.6212305432946,
 1166.0892369146666,
 1225.9246031746031,
 1243.3178643379817,
 1130.8900157051396,
 1537.174469187273,
 1156.7306312271223,
 1222.0783845668848,
 1191.7735410080279,
 813.09195376169589,
 903.75363177546433,
 977.88797434574326,
 1211.6403785641123,
 1067.9240884548806,
 1003.7070001648678,
 1147.3271429458796,
 1340.0308215011971,
 1303.814187581921,
 1095.1908812043739,
 886.3561575623487,
 1344.2813509935786,
 1309.612533797191,
 978.19543650793651,
 700.0,
 1053.4539757411442,
 1850.9680951093032,
 1122.185337127321,
 1003.8518100872566,
 1112.9730740336863,
 913.30675423670573,
 890.07754402305068,
 1261.6670121599057,
 820.29995425706511,
 798.35604587881653,
 1166.6728756839871,
 971.48579077037812,
 823.50096349803096,
 1443.9656962098575,
 947.31439393939399,
 700.0,
 901.74815411635632,
 700.0,
 956.96253510324743,
 1378.8953399541633,
 1288.6510814298892,
 1281.4747886524117,
 1194.169543549403,
 1190.2515741203242,
 891.65504940109497,
 793.23831513865514,
 1198.2883991008994,
 953.60305732504071,
 876.0921362816797,
 1813.2041524743868,
 980.33735625829956,
 1356.3669454870173,
 700.0,
 1471.6913293824784,
 1469.1422336772139,
 687.47365746998105,
 1558.2453052076016,
 990.64087505363148,
 940.96905961765913,
 1644.2059350824088,
 1060.045991629921,
 857.80947308450664,
 986.19925433474668,
 1246.5831484019382,
 705.52521918144703,
 1039.0418233081975,
 1239.379402259887,
 895.66741049640075,
 1048.3479796154138,
 700.0,
 808.41096369065826,
 1073.578715687165,
 989.51759726446994,
 1418.5430670540745,
 1095.5947160534367,
 1010.7739808593431,
 1099.4517228853751,
 1093.2255123033856,
 950.79348502331129,
 813.00328836325355,
 1045.8516330261416,
 1395.1450917503548,
 1457.992814400315,
 1000.0471407929676,
 895.46931391040607,
 813.76666042291038,
 1154.1905362269722,
 1166.43516374607,
 1186.3213046757164,
 807.79450442719667,
 1194.2202838074961,
 857.30611184059239,
 1356.1131637940723,
 1205.9092094433768,
 1101.9019971620719,
 945.46151968006404,
 1339.1155877994381,
 849.75977994862114,
 969.23035630238496,
 1020.7497269694701,
 1322.8780757014638,
 1143.4364552104439,
 924.05719312016299,
 nan,
 913.47218212572557,
 1189.705594242359,
 908.32304044957868,
 1405.4637933394292,
 913.87800133165149,
 943.81294494106589,
 842.71007715298799,
 1257.5898417357625,
 879.39779842323674,
 909.43807472304957,
 859.16583416583421,
 1311.6732112877089,
 923.77814971802832,
 700.0,
 1133.6635301000661,
 956.27133303720598,
 1058.8199410609045,
 657.67885892127924,
 1095.9114865977544,
 1155.8908168406301,
 1219.6246962429773,
 1384.1648052628761,
 897.67902658363755,
 1169.7641419931977,
 777.76003692303357,
 727.30469822810005,
 700.0,
 1150.7581674095702,
 1560.8339949654392,
 1301.61329720507,
 1207.2847039939659,
 1360.931129112794,
 1038.6899914038613,
 868.00558248629682,
 963.75486288125933,
 1109.5720559931881,
 1180.1887327726606,
 1001.8716387979166,
 1330.5002960275019,
 1120.3335306850013,
 1234.6606029652396,
 1258.4177182810929,
 1197.3499294570727,
 1153.9383184095009,
 1052.5447440564681,
 837.66195442085984,
 1346.7107394153834,
 1847.6085100009157,
 846.70611169278345,
 923.79286647500146,
 880.88611196947465,
 1320.4434271008897,
 899.34200909676224,
 1205.4106972651339,
 887.38850990481365,
 978.48412345437498,
 1363.4420729340072,
 959.105967813593,
 1227.4344942168209,
 1258.976062737134,
 1062.1785748120381,
 1124.9277668553505,
 1033.8274146537985,
 1517.1893484666596,
 1139.2033438748681,
 1115.687720040361,
 1078.6323594387572,
 1339.399721559121,
 1173.9225744152291,
 350.48660714285711,
 599.19164097972384,
 1072.8013807904283,
 700.0,
 901.74222143157874,
 1293.641147741148,
 763.17727528287298,
 1026.3464853902547,
 1232.4765341718032,
 1005.378834002222,
 924.61431631119558,
 822.5765462835775,
 1387.7863803740611,
 1065.6027672723026,
 700.0,
 878.56468429446375,
 700.0,
 1671.1636376444064,
 1231.9744581343887,
 700.0,
 1350.1426655846312,
 926.69923152725755,
 1683.6112436826086,
 1309.7321428571429,
 700.0,
 1061.713696502673,
 1207.1437078011818,
 941.45053274442057,
 1167.4387455933147,
 586.60047337483263,
 1426.1296674225248,
 804.58688959180949,
 1082.3482537615082,
 835.38503423195937,
 1185.8887214586466,
 323.46833464154889,
 1175.3492074068067,
 1690.5530970577408,
 881.35828997542501,
 997.67684254760979,
 1333.5946533629801,
 941.8809353660522,
 700.0,
 1236.5205946392498,
 1435.5230973174941,
 951.67446938404316,
 1023.6642226187807,
 762.81461538869632,
 1109.3776959386071,
 1103.4767101589555,
 1409.0652988665365,
 975.56291571666873,
 700.0,
 1342.5419979822416,
 1239.595928727246,
 1059.1937094021466,
 975.91447662981284,
 1081.0357158488839,
 851.10204224865242,
 1332.4153193366151,
 719.27241751304246,
 890.10113220677022,
 1112.9371027257057,
 1333.3862211426429,
 1285.9705747862213,
 1857.0900671377235,
 1400.1582881334732,
 1420.9994802647486,
 1729.184999062589,
 1026.9144318221115,
 1256.7345362568988,
 776.29353532260643,
 1191.2725155223191,
 1156.5713961184736,
 579.94293602853975,
 1082.909937162779,
 700.0,
 923.6448581190557,
 1133.1291025109253,
 805.86156587176993,
 874.16682795187944,
 976.27311772521455,
 1096.0527533329132,
 715.90873015873012,
 1501.9665919246622,
 1160.211147593162,
 1131.976779079833,
 700.0,
 710.15409798534802,
 1321.5188037120629,
 821.54383149930482,
 863.52871506583881,
 749.43751742804898,
 1150.5859393644891,
 904.22963577775613,
 700.0,
 406.29177587118767,
 1599.9659457672549,
 1026.0294256634756,
 1305.375,
 909.34481231525831,
 839.23005488615172,
 1201.5229198268737,
 1147.7264402546925,
 901.91883695222236,
 1424.8953287095403,
 1932.541950858141,
 1042.5491714767479,
 979.38402113187647,
 1086.7739106704348,
 1746.2202272024513,
 1334.4212001935261,
 580.0,
 1011.8252091738686,
 1264.5816542200437,
 nan,
 1413.2948614978404,
 1054.3540935782853,
 1284.7385877537413,
 1127.4751125930611,
 1232.8109866900825,
 700.0,
 700.0,
 1517.1893484666596,
 964.21117807517339,
 1813.2041524743868,
 1033.8687187013866,
 1473.7489386117593,
 1184.3216539343011,
 700.0,
 700.0,
 1275.1154042617318,
 1614.0460041601661,
 1466.6296287188586,
 1048.7405036970258,
 1015.7151822978833,
 869.24234781419523,
 1140.4459753445242,
 1076.3206207574276,
 871.14902750208876,
 1285.4694295255827,
 1104.7272905325242,
 1001.8806659714554,
 700.0,
 988.79988076159952,
 985.69261539479794,
 928.56843758880473,
 943.07940846444887,
 1126.4171951520445,
 1562.7044076324019,
 804.57334236142015,
 1346.7107394153834,
 1346.5823792157453,
 1341.1011962603111,
 1046.3091201031953,
 1182.2271748388594,
 1156.784664805346,
 927.28919445089866,
 796.65681427145682,
 1229.8843301175743,
 1508.2803134172329,
 1241.4856532612355,
 1099.9165215116413,
 1144.0341301837177,
 1495.8948069118183,
 1408.0951754410144,
 1069.0521595262974,
 1006.0336705012002,
 1674.5945705312929,
 1139.2144165508462,
 974.14572623719755,
 881.10115322243655,
 1026.3464853902547,
 942.86392783212068,
 1659.7834751331593,
 968.90618332357315,
 1143.571308511396,
 954.60966322807076,
 948.55135203977306,
 1078.0243379414001,
 1445.1581315393162,
 884.3296449771633,
 1444.7906971491091,
 ...]

In [5]:
allratings = pd.DataFrame({'Historical Rating': teamavgratings.values(), 
#                            'Self Rating': teamself.values(),
#                            'Captain Rating': teamcaptain.values(),
#                            'Combined Rating': teamcombined.values(),
                           'Team': teamavgratings.keys()})
alltruediff = pd.DataFrame({'Team': truedifferential.keys(), 'avgplusminus': truedifferential.values()})

alltrueratings = allratings.merge(alltruediff, on='Team', how='inner')
alltrueratings = alltrueratings.set_index('Team')


---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-5-33a50fc0e3dc> in <module>()
----> 1 allratings = pd.DataFrame({'Historical Rating': teamavgratings.values(), 
      2 #                            'Self Rating': teamself.values(),
      3 #                            'Captain Rating': teamcaptain.values(),
      4 #                            'Combined Rating': teamcombined.values(),
      5                            'Team': teamavgratings.keys()})

NameError: name 'teamavgratings' is not defined

In [123]:
alltrueratings


Out[123]:
Historical Rating avgplusminus
Team

In [112]:
allteamdf = teamdf.join(allratings)

In [115]:
allteamdf['divmean'] = np.zeros(len(allteamdf))
for div in divnames:
    divmean = np.mean(allteamdf.ix[allteamdf['div'] == div, 'Historical Rating'])
    allteamdf.ix[allteamdf['div'] == div, 'divmean'] = divmean

In [117]:
allteamdf['Relative Historical Rating'] = allteamdf['Historical Rating'] - allteamdf['divmean']

In [59]:
def teamnumber(x):
    i0 = x.index('(')
    i1 = x.index(')')
    return x[i0+1:i1]

In [82]:
allteamdf.index


Out[82]:
Index([u'Phantasmagoric Phearless Phlckrs (32)', u'Huckwing Duck (36)',
       u'GI Throw (31)', u'Fantastic Force (38)', u'Just Disc League (30)',
       u'D'Ninjas (27)', u'Incredible Huck (33)', u'Baby Blue Avengers (34)',
       u'Superflick (28)', u'Sandman (35)', u'Hellboy The Golden Army (26)',
       u'The Biddler (29)', u'League of Extraordinary Handlers (25)',
       u'Aqua Teen Hucking Force (37)', u'Darkwing Huck (42)',
       u'Pullverine (41)', u'Superman Fortyhands (40)',
       u'Powdered Toast Man (39)', u'The Avengers (23)',
       u'Aqua Team Huck 'N Force (22)', u'Thor's Hammer (9)',
       u'The Incredible Huck (15)', u'Skynet (5)', u'Fantastic Forehand (7)',
       u'Xena Warrior Princess (24)', u'Doctor Horrible Decision (3)',
       u'Cuptonite (18)', u'Blade (11)', u'To The Hatmobile! (10)',
       u'Scooberman (1)', u'Underdog (14)', u'RadioStacktive Man (4)',
       u'Tiger Blood (17)', u'Ambiguously Gray Duos (20)',
       u'Purple Poacher Eaters (8)', u'Holy Handblock Batman! (16)',
       u'Drivin' Miss Daisy (13)', u'Dr Manhattan's Blue Ballerz (2)',
       u'Evil League of Evil (19)', u'Captain Hammer! (21)',
       u'The Mark Knight (6)', u'Huck Norris (12)'],
      dtype='object', name=u'Team')

In [104]:
allteamdf['teamnumber'] = allteamdf.index.str.extract('\((.*)\)').values.astype('int')

In [52]:
sns.jointplot(allteamdf['avgplusminus'], allteamdf['Historical Rating'])


Out[52]:
<seaborn.axisgrid.JointGrid at 0x11bf7fed0>

In [105]:
jpmixed = allteamdf[allteamdf['teamnumber'] < 25]

In [106]:
allteamdf['teamnumber'].values.astype('int')


Out[106]:
array([32, 36, 31, 38, 30, 27, 33, 34, 28, 35, 26, 29, 25, 37, 42, 41, 40,
       39, 23, 22,  9, 15,  5,  7, 24,  3, 18, 11, 10,  1, 14,  4, 17, 20,
        8, 16, 13,  2, 19, 21,  6, 12])

In [119]:
sns.jointplot(allteamdf['avgplusminus'], allteamdf['Relative Historical Rating'])


Out[119]:
<seaborn.axisgrid.JointGrid at 0x11d5faa50>

In [45]:
draftteams = allteamdf.dropna(subset=['Self Rating'])
draftteams = draftteams[draftteams['games'] > 5]

In [47]:
sns.jointplot(draftteams['avgplusminus'], draftteams['Historical Rating'])


Out[47]:
<seaborn.axisgrid.JointGrid at 0x1180efa90>

In [46]:
sns.jointplot(draftteams['avgplusminus'], draftteams['Self Rating'])


Out[46]:
<seaborn.axisgrid.JointGrid at 0x117ebe5d0>

In [50]:
sns.jointplot(draftteams['avgplusminus'], draftteams['Combined Rating'])


Out[50]:
<seaborn.axisgrid.JointGrid at 0x11bab6ed0>

In [1]:
def printteam(teamratings, playerlist, team):
    print(team)
    for i in range(len(teamratings[team])):
        print("{:20} {:5.0f}".format(playerlist[team][i], teamratings[team][i]))
    print("")

In [2]:
def plot2teams(teamratings, team1, team2):
    plt.plot(np.cumsum(np.sort(teamratings[team1])), 'o-', label=team1)
    plt.plot(np.cumsum(np.sort(teamratings[team2])), 'o-', label=team2)
    plt.legend()
#     printteam(teamratings, playerlist, team1)
#     printteam(teamratings, playerlist, team2)

I need a function that takes a league id as input, and then:

  • for every team in the league
    • gets the rating for each player on the team
    • computes the team rating using the average player rating for that team
    • predicts point differential over the full season
    • gets the true point differential for the full season
  • makes a plot comparing true vs. predicted point differential for that league

For spring hat league 2011, I have captain's ratings and self ratings for every player in Monday night hat league. The money plot will be a comparison of predicted and true point differential for three possible metrics: the rating system I've devised here (let's call it "historical experience rating"), self rating, and captain's rating. I'll bet that there is a stronger correlation between true point differential and HER than there is for either self rating or captain's rating.


In [7]:
# load spring hat league self ratings and captain's ratings
sph2011file = '/Users/rbussman/Documents/ultimate/sphl11_mon_draft.csv'
sph2011 = pd.read_csv(sph2011file)

In [8]:
sph2011['full name'] = sph2011['LAST'] + ', ' + sph2011['FIRST']

Validation on the current season: Predictions for every R2Defense game.


In [9]:
teamratings = {}
playerlist = {}
teamavgratings = {}
# teamself = {}
# teamcaptain = {}
# teamcombined = {}
truedifferential = {}

# extract the league id for this league
leagueid = '40258'


# extract the league id for this league
# springhat2016id = '30924'
#     leagueid = springhat2016id
print("Working on league {}".format(leagueid))

# scrape the list of teams for this league
teamsurl = 'http://www.buda.org/hatleagues/rosters.php?section=showTeams&league=' + leagueid
response = urllib2.urlopen(teamsurl)
teams_soup = BeautifulSoup(response)

# generate list of team ids and names for this league
tdlist = teams_soup.find_all('td', class_='infobody')
teamids = []
teamnames = []
for td in tdlist:
    try:
        url = td.a['href']
        idindex = url.index('team=')
        whichindex = url.index('which=')
        teamids.append(url[idindex+5:whichindex-1])
        teamnames.append(td.a.get_text())
    except:
        continue

# if this league is complete, get the true score differential for each team

# scrape the scores for this league
leaguescoreurl = 'http://www.buda.org/hatleagues/scores.php?section=showLeagueSchedule&league=' + leagueid + '&byDivision=1&showGames=0'
response = urllib2.urlopen(leaguescoreurl)
leaguescore_soup = BeautifulSoup(response)

# assemble the data of team ratings for this league
print("Building database of team ratings")
data = []
data_opponent = []
try:
    table = leaguescore_soup.find_all('table', attrs={'class':'info'})[1]
except IndexError:
    print("Unable to find a database of scores for league {}".format(leagueid))
#     continue
rows = table.find_all('tr')
for row in rows:
    cols = row.find_all('th')
    cols = [ele.text.strip() for ele in cols]
    data.append([ele for ele in cols if ele]) # Get rid of empty values

# convert to dataframe and drop irrelevant columns
dfdata = pd.DataFrame(data)
#     print(leagueid, dfdata.columns)
dfdata.columns = dfdata.ix[0, :]#['Team', 'Record', 'Plus/Minus', 'Tourney Qualifying games']
#     print(leagueid, dfdata.columns)
dfdata = dfdata.drop(0).reset_index()

# fill na's with -99 to facilitate division dividers
dfdata = dfdata.fillna(-99)

# get the list of divisions in this league
divnames = dfdata.ix[dfdata['Record'] == -99, 'Team'].values
if len(divnames) == 0:
    print("No divisions found, skipping league {}".format(leagueid))
#     continue

dfdata['div'] = np.zeros(len(dfdata))
for i in range(len(divnames)-1):
    try:
        divstart = np.where(dfdata['Team'] == divnames[i])[0][0]
    except IndexError:
        print("{} not found, skipping league {}".format(divnames[i], leagueid))
#         continue
    try:
        divend = np.where(dfdata['Team'] == divnames[i + 1])[0][0]
    except IndexError:
        print("{} not found, skipping league {}".format(divnames[i + 1], leagueid))
#         continue
    try:
        dfdata.ix[divstart + 1: divend, 'div'] = divnames[i]
    except KeyError:
        print("No base rating for {}, skipping league {}".format(divnames[i], leagueid))
#         continue
try:
    dfdata.ix[divend + 1:, 'div'] = divnames[-1]
except KeyError:
    print("No base rating for {}, skipping league {}".format(divnames[-1], leagueid))
#     continue

# remove the division dividers from the dataframe
for i in range(len(divnames)):
    dfdata = dfdata.drop(dfdata.index[dfdata['Team'] == divnames[i]])

# generate the average goal differential column
dfdata['wins'] = dfdata['Record'].apply(lambda x: int(x.split('-')[0]))
dfdata['losses'] = dfdata['Record'].apply(lambda x: int(x.split('-')[1]))
dfdata['games'] = dfdata['wins'] + dfdata['losses']
dfdata['avgplusminus'] = dfdata['Plus/Minus'].astype('float') / dfdata['games']

# find all players associated with each team
print("Finding all players associated with each team")
for teamid, teamname in zip(teamids, teamnames):

    teamurl = 'http://www.buda.org/hatleagues/rosters.php?section=showTeamRoster&team=' + teamid
    response = urllib2.urlopen(teamurl)
    roster_soup = BeautifulSoup(response)

    playerratings = []
    selfrating = []
    captainrating = []
    combinedrating = []
    players = [td.get_text() for td in roster_soup.find_all("td", class_="infobody")]
    for player in players:
        if player == '':
            continue
        if player in all_players:
            playerratings.append(players_means[player])
        else:
            # if someone hasn't played club league, they probably aren't very good
            playerratings.append(700)
        PLAYER = player.upper()
#         if player == 'Bussmann, Shane':
#             import pdb; pdb.set_trace()
        if PLAYER in sph2011['full name'].values:
#             import pdb; pdb.set_trace()
            sph2011row = sph2011['full name'] == PLAYER
            selfrating.append(sph2011.ix[sph2011row, 'Self'].values[0])
            captainrating.append(sph2011.ix[sph2011row, 'CR'].values[0])
            combinedrating.append(sph2011.ix[sph2011row, 'Rating'].values[0])
    # the team rating is the average of the player ratings for that team
    teamratings[teamname] = playerratings
    playerlist[teamname] = players
    teamavgratings[teamname] = np.mean(playerratings)
#         teamself[teamname] = np.mean(selfrating)
#         teamcaptain[teamname] = np.mean(captainrating)
#         teamcombined[teamname] = np.mean(combinedrating)
#     print("Finished with team {}".format(teamname))
#     dfdatarow = dfdata['Team'] == teamname
#     truedifferential[teamname] = dfdata.ix[dfdatarow, 'avgplusminus'].values[0]

#     dfdata['divmean'] = np.zeros(len(dfdata))
#     for div in divnames:
#         divmean = np.mean(dfdata.ix[dfdata['div'] == div, 'Historical Rating'])
#         allteamdf.ix[allteamdf['div'] == div, 'divmean'] = divmean

print("Finished successfully with league {}".format(leagueid))
print("")
#     import pdb; pdb.set_trace()


Working on league 40258
Building database of team ratings
Finding all players associated with each team
Finished successfully with league 40258


In [10]:
teamavgratings


Out[10]:
{u'107 Dalmatians (24)': 1196.299609679045,
 u'Air Bud VII Break Bark (8)': 1130.4406224010686,
 u'Backhand vs Scooberman (17)': 1159.6143488652363,
 u'Bar Bar Drinks (16)': 1112.2916831323746,
 u'Chewblocka (3)': 1354.8616142817584,
 u'Clifford the Big Red Wookie (30)': 1164.6385826881904,
 u'Cloud City United  (28)': 977.88797434574326,
 u"Ernest goes to Doyle's (5)": 1094.3057033929615,
 u'Fifty Shades of Rey (9)': 1097.2507315727357,
 u'Furious 7 Lassies on the Line (6)': 1143.571308511396,
 u'Golden BUDA (29)': 1041.3869065101123,
 u'Hammers Are Forever (18)': 1125.9850968326457,
 u'Jedi Mind Flicks (27)': 1040.3566902136567,
 u'Kylo Red (22)': 1183.0444835092667,
 u'Mark Hammer (1)': 1059.4535387703113,
 u'Marooned on Jakku (14)': 1268.4454112760307,
 u'Never Teal Me The Odds (4)': 1073.2589911085906,
 u'Orange is the New Darkside (11)': 1314.7487106110952,
 u'Poe Hammeron (15)': 1113.559230080444,
 u'Princess Layout (19)': 1203.7280616274645,
 u'R2Defense (20)': 1077.2992821007324,
 u'R2Team2 (2)': 1275.0748863801027,
 u'Scoober Troopers 7 Huck it Meow (21)': 1193.0011899263045,
 u'Stall Counts Are Forever (12)': 1169.6221231186487,
 u'Team 9 and Three Quarters (10)': 1211.6403785641123,
 u'Team TBD (25)': 1078.4656776897177,
 u"That's Not How The Force Works (26)": 1104.7579807820564,
 u'Too Fast 7 Furious (7)': 1032.3705492211434,
 u'Two Fast Three Furious (23)': 1223.3690743431,
 u'Winning the Pooh (13)': 1201.1230516126079}

In [11]:
names = []
predrating = []
for key in teamavgratings.keys():
    names.append(key)
    predrating.append(teamavgratings[key])
#     print("{:38} {:.0f}".format(key, teamavgratings[key]) )
dfpred = pd.DataFrame({'teamname': names, 'predictedrating': predrating})

In [13]:
dfpred.sort('predictedrating')


Out[13]:
predictedrating teamname
25 977.887974 Cloud City United (28)
16 1032.370549 Too Fast 7 Furious (7)
12 1040.356690 Jedi Mind Flicks (27)
2 1041.386907 Golden BUDA (29)
19 1059.453539 Mark Hammer (1)
22 1073.258991 Never Teal Me The Odds (4)
1 1077.299282 R2Defense (20)
26 1078.465678 Team TBD (25)
3 1094.305703 Ernest goes to Doyle's (5)
4 1097.250732 Fifty Shades of Rey (9)
28 1104.757981 That's Not How The Force Works (26)
9 1112.291683 Bar Bar Drinks (16)
27 1113.559230 Poe Hammeron (15)
29 1125.985097 Hammers Are Forever (18)
18 1130.440622 Air Bud VII Break Bark (8)
7 1143.571309 Furious 7 Lassies on the Line (6)
5 1159.614349 Backhand vs Scooberman (17)
13 1164.638583 Clifford the Big Red Wookie (30)
24 1169.622123 Stall Counts Are Forever (12)
8 1183.044484 Kylo Red (22)
17 1193.001190 Scoober Troopers 7 Huck it Meow (21)
15 1196.299610 107 Dalmatians (24)
21 1201.123052 Winning the Pooh (13)
11 1203.728062 Princess Layout (19)
14 1211.640379 Team 9 and Three Quarters (10)
20 1223.369074 Two Fast Three Furious (23)
10 1268.445411 Marooned on Jakku (14)
6 1275.074886 R2Team2 (2)
23 1314.748711 Orange is the New Darkside (11)
0 1354.861614 Chewblocka (3)

In [35]:
(1077 - 1900/16. - 1400/16.) * 16/14.


Out[35]:
995.1428571428571

In [3]:
plot2teams(teamratings, 'R2Defense (20)', 'Poe Hammeron (15)')


---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-3-5dfc26f3a154> in <module>()
----> 1 plot2teams(teamratings, 'R2Defense (20)', 'Poe Hammeron (15)')

NameError: name 'teamratings' is not defined

In [4]:
plot2teams(teamratings, 'R2Defense (20)', 'Team 14 (14)')


---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-4-38d7b7b9398e> in <module>()
----> 1 plot2teams(teamratings, 'R2Defense (20)', 'Team 14 (14)')

NameError: name 'teamratings' is not defined

In [5]:
plot2teams(teamratings, 'R2Defense (20)', 'Team 24 (24)')


---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-5-369131ac8f97> in <module>()
----> 1 plot2teams(teamratings, 'R2Defense (20)', 'Team 24 (24)')

NameError: name 'teamratings' is not defined

In [6]:
plot2teams(teamratings, 'R2Defense (20)', 'Team 22 (22)')


---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-6-51c9379474e5> in <module>()
----> 1 plot2teams(teamratings, 'R2Defense (20)', 'Team 22 (22)')

NameError: name 'teamratings' is not defined

In [7]:
plot2teams(teamratings, 'R2Defense (20)', 'Winning the Pooh (13)')


---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-7-35a82fc631dc> in <module>()
----> 1 plot2teams(teamratings, 'R2Defense (20)', 'Winning the Pooh (13)')

NameError: name 'teamratings' is not defined

In [8]:
plot2teams(teamratings, 'R2Defense (20)', 'Team 16 (16)')


---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-8-36b7a2bb18a9> in <module>()
----> 1 plot2teams(teamratings, 'R2Defense (20)', 'Team 16 (16)')

NameError: name 'teamratings' is not defined

In [9]:
plot2teams(teamratings, 'R2Defense (20)', 'Team 18 (18)')


---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-9-2225a2a4243b> in <module>()
----> 1 plot2teams(teamratings, 'R2Defense (20)', 'Team 18 (18)')

NameError: name 'teamratings' is not defined

In [10]:
np.sort(teamratings['R2Defense (20)'])


---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-10-c855eb8847db> in <module>()
----> 1 np.sort(teamratings['R2Defense (20)'])

NameError: name 'np' is not defined

In [ ]: