Esempio n. 1
0
 def test_getUpcomingGames(self):
     db.DBConnection.UpcomingGames.remove({})
     _match = upcoming_match(away_att=77,
                             away_def=67,
                             away_mid=69,
                             away_odds_n=0.27669516494195245,
                             away_odds_nn=3.2583333333333333,
                             away_team_name="Sampdoria",
                             away_team_rank=11,
                             away_team_received=2,
                             away_team_scored=1,
                             date=today,
                             draw_odds_n=0.31504895018143103,
                             draw_odds_nn=2.8616666666666664,
                             home_att=80,
                             home_def=71,
                             home_mid=74,
                             home_odds_n=0.4082558848766165,
                             home_odds_nn=2.208333333333333,
                             home_team_name="Fiorentina",
                             home_team_rank=18,
                             home_team_received=1,
                             home_team_scored=2,
                             league="Serie",
                             round=1)
     db.insertMatchToUpcomingGames(_match)
     dtos = db.getUpcomingGames(league='all')
     self.assertEqual(_match, dtos[0])
Esempio n. 2
0
 def getUpcomingGames(self, request, context):
     print("request: getUpcomingGames")
     matchlist = match_pb2.MatchList()
     upcoming = DBController.getUpcomingGames(league=request.league)
     for game in upcoming:
         _match = matchlist.list.add()
         _match.league = game.league
         _match.date = game.date.isoformat()
         _match.round = game.round
         _match.home_team_name = game.home_team_name
         _match.away_team_name = game.away_team_name
         _match.home_team_rank = game.home_team_rank
         _match.away_team_rank = game.away_team_rank
         _match.home_team_scored = game.home_team_scored
         _match.away_team_scored = game.away_team_scored
         _match.home_team_received = game.home_team_received
         _match.away_team_received = game.away_team_received
         _match.home_att = game.home_att
         _match.away_att = game.away_att
         _match.home_def = game.home_def
         _match.away_def = game.away_def
         _match.home_mid = game.home_mid
         _match.away_mid = game.away_mid
         _match.home_odds_n = game.home_odds_n
         _match.draw_odds_n = game.draw_odds_n
         _match.away_odds_n = game.away_odds_n
         _match.home_odds_nn = game.home_odds_nn
         _match.draw_odds_nn = game.draw_odds_nn
         _match.away_odds_nn = game.away_odds_nn
     return matchlist
def predict(league):
    """
    @param league: string that represent the league (can be 'all')
    @return: list of predictions DTOs
    """
    predictions = []
    #region Data
    all_data = DBController.getAllData(as_dataframe=True)
    upcoming_games = DBController.getUpcomingGames(league, as_dataframe=True)
    x, y = data_preprocessor.train_preprocess(all_data)
    to_predict = data_preprocessor.prediction_preprocess(upcoming_games)
    #endregion
    #region ANN
    for i in range(0, AVG):
        ann = NeuralNet(x.shape[1])
        ann.train(x, y, EPOC)
        predictions.append(ann.predict(to_predict))
    #endregion
    #region Calculate avg of predictions
    lines = predictions[0].shape[0]
    columns = predictions[0].shape[1]
    avgPrediction = np.zeros((lines, columns))
    for line in range(lines):
        for cell in range(columns):
            sum = 0
            for prediction in predictions:
                sum = sum + prediction[line, cell]
            avgPrediction[line, cell] = sum / AVG
    #endregion
    #region Converting avgPrediction to pandas DataFrame
    y_pred, indexes = apply_indexes(avgPrediction, upcoming_games)
    details = upcoming_games.iloc[indexes]
    details = details[[
        'league', 'date', 'home_team_name', 'away_team_name', 'home_odds_nn',
        'draw_odds_nn', 'away_odds_nn'
    ]]
    final = pd.concat([details, y_pred], axis=1, sort=False)

    prediction_dtos = []
    for row in final.shape[0]:
        prediction_dtos.append(
            Prediction(
                final.loc[row, 'leauge'], final.loc[row, 'date'],
                final.loc[row, 'home_team_name'], final.loc[row,
                                                            'away_team_name'],
                final.loc[row, 'home_odds_nn'], final.loc[row, 'draw_odds_nn'],
                final.loc[row, 'away_odds_nn'], final.loc[row, 'pred_1'],
                final.loc[row, 'pred_2'], final.loc[row, 'pred_x'],
                calc_exp(predictions=[
                    final.loc[row, 'pred_1'], final.loc[row, 'pred_x'],
                    final.loc[row, 'pred_2']
                ],
                         odds=[
                             final.loc[row, 'home_odds_nn'],
                             final.loc[row, 'draw_odds_nn'],
                             final.loc[row, 'away_odds_nn']
                         ]), final.loc[row, 'result']))
    return prediction_dtos