Exemplo n.º 1
0
def check_yesterday_fanduel(playerMap):
    yesterdayDate = datetime.date.today()-datetime.timedelta(days=1)
    with open("final_preds.txt","r") as f:
        resultList = json.loads(f.readline())
    with open("final_predList.txt","r") as f:
        predList = json.loads(f.readline())
    with open("yesterday_results.txt","w") as f:
        totalPred = 0
        totalActual = 0
        totalCost = 0
        for i in range(0,len(resultList[0])):
            name = resultList[3][i]
            points = resultList[1][i]
            position = resultList[0][i]
            cost= resultList[2][i]

            totalPred += points
            totalCost += cost

            #print(name)
            playeridStr = Scraper.playername_to_id(str(name))
            #print(playeridStr)
           # print(type(playeridStr))

            gameOrderedDict = playerMap[playeridStr]


            lastGameStats = gameOrderedDict[next(reversed(gameOrderedDict))]

            predictedStatsList = predList[name]
            

            if(lastGameStats[0] != yesterdayDate.month or lastGameStats[1] != yesterdayDate.day or lastGameStats[2] != yesterdayDate.year):
                f.write(name + " might have been injured or did not play\n")
                f.write(name + " (" + position + ") was projected for " + str(points) + " points at " + str(cost) + " cost and actually got " + str(0) + "\n")
            else:
                f.write(json.dumps([float("{0:.2f}".format(x)) for x in predictedStatsList])+"\n")
                statsList = lastGameStats[12:]
                f.write(json.dumps(statsList)+"\n")
                actual_fanduel = Util.calc_fanduel_points(statsList)
                totalActual += actual_fanduel
                f.write(name + " (" + position + ") was projected for " + str(points) + " points at " + str(cost) + " cost and actually got " + str(actual_fanduel) + "\n")
            f.write("\n")
        f.write("Total Predicted points is " + str(totalPred) + " at " + str(totalCost) + " cost, and total actual points is " + "{0:.2f}".format(totalActual)) 
Exemplo n.º 2
0
def generate_features(currentMap,today_stats,injuredIDMap, injuredTodayMap):
     # print(type(currentMap))

     #print(today_stats)
     #featureList = OrderedDict(list)
     trainingFeatureList = deque([])
     testingFeatureList = deque([])

     todayFeatureList = deque([])

     completeFeatureMap = defaultdict(OrderedDict)

     allGameIDs = set()
     
     for playerid,orderedDict in currentMap.items():
         
         #prevGameIds = deque([])
         #19 stats for each game
         seasonGameStatsTotals = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
         prevGameStats = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
         #use deques with max size to keep track of most recent n games
         prev2GamesStats = deque([],2)
         prev3GamesStats = deque([],3)
         prev5GamesStats = deque([],5)
         prev10GamesStats = deque([],10)
         prev20GamesStats = deque([],20)


         prev2GamesStats.append([0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0])
         prev3GamesStats.append([0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0])
         prev5GamesStats.append([0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0])
         prev10GamesStats.append([0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0])
         prev20GamesStats.append([0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0])


         count = 0

         #need how many games stats each player has
         #  split 80%-20% train-test
         #  best to split by time
         #first 4/5 of (games-first) = train, rest for test
         gamesForPlayer = len(orderedDict)


         for gameid,statList in orderedDict.items():

             allGameIDs.add(gameid)

             #count represents how many games the player has previously played
             gameFeature = [int(playerid)] + [int(gameid)] + statList[:8] + [count]


             gameFeature += prevGameStats
             gameFeature += Util.avgStats(prev2GamesStats)
             gameFeature += Util.avgStats(prev3GamesStats)
             gameFeature += Util.avgStats(prev5GamesStats)
             gameFeature += Util.avgStats(prev10GamesStats)
             gameFeature += Util.avgStats(prev20GamesStats)
             gameFeature += (np.array(seasonGameStatsTotals) / max(count,1)).tolist()
                 
             if(count <= 0.8 * (gamesForPlayer-1)):
                 trainingFeatureList.append(gameFeature)
             else:
                 testingFeatureList.append(gameFeature)
             # print("HERE gameid " + str(gameid))
             completeFeatureMap[playerid][gameid] = gameFeature
             # print(len(gameFeature))
             # if(len(gameFeature) != 158):
             #     print(gameFeature)



             count+=1
             #prevGameIds += [gameid]     
             prevGameStats = statList[8:]    
             prev2GamesStats.append(statList[8:])
             prev3GamesStats.append(statList[8:])
             prev5GamesStats.append(statList[8:])
             prev10GamesStats.append(statList[8:])
             prev20GamesStats.append(statList[8:])
             seasonGameStatsTotals = [x + y for x, y in zip(seasonGameStatsTotals,statList[8:])]


        
         if(playerid in today_stats):
             (todayGameid,statsList) = today_stats[playerid].popitem()
             feature = [int(playerid)] + [int(todayGameid)] + statsList[:8] + [count] + prevGameStats + Util.avgStats(prev2GamesStats) + Util.avgStats(prev3GamesStats) + Util.avgStats(prev5GamesStats) + Util.avgStats(prev10GamesStats) + Util.avgStats(prev20GamesStats) + (np.array(seasonGameStatsTotals) / count).tolist()
             todayFeatureList.append(feature)



     for feature in todayFeatureList:
        todayGameid = str(feature[1])
        ownTeamNum = feature[6]
        injuredList = injuredTodayMap[ownTeamNum]

        injuredListFeatures = []


        if(len(injuredList) == 0):
            injuredListFeatures = awayInjuredListFeatures = np.zeros((1,148))

        else:

            for injuredName in injuredList:
                injuredID = Scraper.playername_to_id(injuredName)

                for (gameid) in reversed(list(completeFeatureMap[injuredID].keys())):

                #get the last features that the injured player had
                    if(gameid <= todayGameid):
                        gameStatsList = completeFeatureMap[injuredID][gameid]

                        # weight = gameStatsList[10]
                        injuredListFeatures.append(gameStatsList[10:])
                        # print(len(gameStatsList[10:]))

                        break
            injuredListFeatures = np.array(injuredListFeatures)
        # print(injuredListFeatures.shape)


        meanInjuredStats = np.mean(injuredListFeatures,0)
        stdInjuredStats = np.std(injuredListFeatures,0)

        feature += (meanInjuredStats.tolist() + stdInjuredStats.tolist())


         #print(list(todayFeatureList))


     injuredMap = {}

     for currentGameID in allGameIDs:
        #create injury features
        # print(currentGameID)
        # print(type(currentGameID))

        #for both the away team and home team
        awayInjuredIDList = injuredIDMap[currentGameID][0]
        awayInjuredListFeatures = []
        for awayInjuredID in awayInjuredIDList:
            # print(type(completeFeatureMap[injuredID].keys()))
            # print("new awayInjuredID " + str(awayInjuredID))
            for (gameid) in reversed(list(completeFeatureMap[awayInjuredID].keys())):
                # print(gameid)
                # print(type(gameid))
                #get the last features that the injured player had
                if(gameid <= currentGameID):
                    gameStatsList = completeFeatureMap[awayInjuredID][gameid]

                    # weight = gameStatsList[10]
                    awayInjuredListFeatures.append(gameStatsList[10:])
                    # print(len(gameStatsList[10:]))

                    # print(awayInjuredID + " " + currentGameID)
                    # print(gameStatsList)
                    break
        if(len(awayInjuredListFeatures) == 0):
            awayInjuredListFeatures = np.zeros((1,148))
        else:
            awayInjuredListFeatures = np.array(awayInjuredListFeatures)
        # print(injuredListFeatures.shape)
        awayMeanInjuredStats = np.mean(awayInjuredListFeatures,0)
        awayStdInjuredStats = np.std(awayInjuredListFeatures,0)
        # print(awayMeanInjuredStats.shape)
        # print(awayStdInjuredStats.shape)




        homeInjuredIDList = injuredIDMap[currentGameID][1]
        homeInjuredListFeatures = []
        for homeInjuredID in homeInjuredIDList:
            # print(type(completeFeatureMap[injuredID].keys()))
            # print(reversed(list(completeFeatureMap[homeInjuredID].keys())))
            for (gameid) in reversed(list(completeFeatureMap[homeInjuredID].keys())):
                #get the last features that the injured player had
                if(gameid <= currentGameID):
                    gameStatsList = completeFeatureMap[homeInjuredID][gameid]

                    # weight = gameStatsList[10]
                    homeInjuredListFeatures.append(gameStatsList[10:])
                    # print(len(gameStatsList[10:]))

                    # print(homeInjuredID + " " + currentGameID)
                    # print(gameStatsList)
                    break
        if(len(homeInjuredListFeatures) == 0):
            homeInjuredListFeatures = np.zeros((1,148))
        else:
            homeInjuredListFeatures = np.array(homeInjuredListFeatures)
        # print(injuredListFeatures.shape)
        homeMeanInjuredStats = np.mean(homeInjuredListFeatures,0)
        homeStdInjuredStats = np.std(homeInjuredListFeatures,0)
        # print(homeMeanInjuredStats.shape)
        # print(homeStdInjuredStats.shape)




        injuredMap[currentGameID] = (awayMeanInjuredStats.tolist() + awayStdInjuredStats.tolist(), homeMeanInjuredStats.tolist() + homeStdInjuredStats.tolist())
     # print(injuredMap)



     #add injuryfeatures to previously computed features
     for gameFeature in list(trainingFeatureList):

         gameid = gameFeature[1]
         isAway = gameFeature[8]
         # print("HERE: " + str(gameid))
         gameFeature += injuredMap[str(gameid)][isAway]

     for gameFeature in list(testingFeatureList):
         gameid = gameFeature[1]
         isAway = gameFeature[8]
         # print("HERE: " + str(gameid))
         gameFeature += injuredMap[str(gameid)][isAway]


     return (np.array(list(trainingFeatureList)),np.array(list(testingFeatureList)), np.array(list(todayFeatureList)))