Exemple #1
0
def run_experiment_2(exp, league, ml_train_input_id, ml_train_input_representation, **params):
    print("Running experiment 2")
    print("\tml_train_input_id:", ml_train_input_id)
    print("\tml_train_input_representation:", ml_train_input_representation)

    params["ml_train_input_id"] = ml_train_input_id
    params["ml_train_input_representation"] = ml_train_input_representation

    # ml_train_stages_to_train = [9,11,19,35,71,105,141,175,211,245,281]
    # shrink windows
    ml_train_stages_to_train = [9, 11, 19, 35, 71, 105, 141]

    plot_entries = {w:entry(w) for w in ml_train_stages_to_train}

    for n_matches in ml_train_stages_to_train:
        params["ml_train_stages_to_train"] = n_matches

        for season in league.get_seasons()[-3:]:
            # only consider last two season
            if season == util.get_current_season():
                break

            params["season"] = season
            for f in mla.get_frameworks():

                if ml_train_input_id not in mla.get_inputs_by_framework(f):
                    continue

                if len(mla.get_methods_by_framework(f))==0:
                    # framework has no methods (ex. poisson)
                    #   --> just predict without specifying it

                    params["ml_alg_framework"] = f
                    pa = PredictionAccuracy(league, only_team_history=False, **params)
                    pa.compute_prediction_accuracy()
                    plot_entries[n_matches].add_accuracy(f, None, pa.get_average_accuracy(), pa.get_match_predicted(), pa.get_execution_time())

                for m in mla.get_methods_by_framework(f):
                    # framework can be used in different manner (ex. SKlearn)
                    #   --> test every algorithm available (SVM, Knn,..)

                    params["ml_alg_method"]=m
                    params["ml_alg_framework"]=f
                    pa = PredictionAccuracy(league, only_team_history=False, **params)
                    pa.compute_prediction_accuracy()
                    plot_entries[n_matches].add_accuracy(f, m, pa.get_average_accuracy(), pa.get_match_predicted(),
                                                         pa.get_execution_time())

                    print(plot_entries[n_matches])

            report = open(exp.experiment_dir+"/report_"+str(ml_train_input_id)+"_"
                          +str(ml_train_input_representation)+".txt", "w")
            report.write("Input id:\t"+str(ml_train_input_id)+"\n")
            report.write("Representation:\t" + str(ml_train_input_representation) + "\n")
            report.write("Window\tMethod\tSuccessful Pred.\tTotal Pred.\tExecution Time\tAccuracy\tTime x prediction\n")
            sorted(plot_entries)
            for w in sorted(plot_entries.keys()):
                report.write(plot_entries[w].__str__()+"\n")

            report.close()
    def __init__(self, league, only_team_history=False, **params):
        self.league = league
        self.only_team_history = only_team_history

        self.season = util.get_default(params, "season", util.get_current_season())
        self.stages = util.get_default(params, "stages", [s for s in range(1, 39)])

        self.ml_alg_method    = util.get_default(params, "ml_alg_method", "SVM")
        self.ml_alg_framework = util.get_default(params, "ml_alg_framework", "Sklearn")

        self.ml_train_input_id        = util.get_default(params, "ml_train_input_id", 1)
        self.representation           = util.get_default(params, "ml_train_input_representation", 1)
        self.ml_train_stages_to_train = util.get_default(params, "ml_train_stages_to_train", 10)

        self.ml_alg_params = dict()
        self.ml_alg_params["k"]           = util.get_default(params, "ml_alg_k", 9)
        self.ml_alg_params["kernel"]      = util.get_default(params, "ml_alg_kernel", "rbf")
        self.ml_alg_params["number_step"] = util.get_default(params, "ml_alg_number_step", 1000)

        self.n_predicted_match     = 0
        self.accuracy_by_team_dic  = dict()
        self.accuracy_by_stage_dic = {x: 0 for x in self.stages}

        self.finish_time = -1

        log.debug("[" + self.season + "], size train [" + str(self.ml_train_stages_to_train) + "], train input id[" +
                  str(self.ml_train_input_id) + "], consider only team history [" + str(self.only_team_history) + "]")
        print("[" + self.season + "], size train [" + str(self.ml_train_stages_to_train) + "], train input id[" +
                  str(self.ml_train_input_id) + "], consider only team history [" + str(self.only_team_history) + "],"
                  " ml_alg_framework ["+self.ml_alg_framework+"], ml_alg_method ["+self.ml_alg_method+"]")
def run_experiment_0(exp, league, type_evaluation, **params):
    """

    :param exp:
    :param league:
    :param type_evaluation:
    :param params:
    :return:
    """
    predictor = Predictor.get_predictor()
    filter_season = util.get_default(params, "season", None)
    for season in league.get_seasons():
        if not util.is_None(filter_season) and season != filter_season:
            continue

        invest = 0
        profit = 0

        print(season)
        if season == util.get_current_season():
            break

        for stage in range(1, league.get_stages_by_season(season) + 1):

            # KEY: match id     VALUE: <prediction, probability>
            stage_predictions = predictor.predict(league, season, stage,
                                                  **params)
            current_stage_bet = StageBet(stage, type_evaluation)

            for match_id, pair in stage_predictions.items():
                if len(pair) == 0:
                    continue
                match = Match.read_by_match_id(match_id)
                if util.is_None(match.B365H) or util.is_None(
                        match.B365D) or util.is_None(match.B365A):
                    continue

                predicted_label, prob = pair[0], pair[1]
                bet_odd = get_bet_odd(predicted_label, match)

                m_invest, m_profit = evaluate_bet(predictor, type_evaluation,
                                                  match, predicted_label, prob)

                if type_evaluation == 5:
                    if m_invest == 1:
                        current_stage_bet.add_bet(prob, m_profit, bet_odd)

                elif type_evaluation == 6:
                    current_stage_bet.add_bet(prob, m_profit, bet_odd)

                elif m_invest == 1:
                    current_stage_bet.add_bet(prob, m_profit)

            profit += current_stage_bet.get_profit()
            invest += current_stage_bet.get_invest()

            print(stage, "\t",
                  str(round(profit - invest, 2)).replace(".", ","))
        print("Final investment:\t", str(round(invest, 2)).replace(".", ","))
        print("Final profit:\t", str(round(profit, 2)).replace(".", ","))
Exemple #4
0
def get_team_trend_str(match, team):
    trend = team.get_trend(stage=match.stage, season=util.get_current_season())
    home_trend = team.get_trend(stage=match.stage,
                                season=util.get_current_season(),
                                home=True)
    away_trend = team.get_trend(stage=match.stage,
                                season=util.get_current_season(),
                                home=False)
    trend_str = ""
    if trend.strip() != "":
        trend_str += "\t\tT: " + trend
    if home_trend.strip() != "":
        trend_str += "\t\tHT: " + home_trend
    if away_trend.strip() != "":
        trend_str += "\t\tAT: " + away_trend

    return trend_str
Exemple #5
0
def search_by_league():
    user_input = input("Insert league name: ")
    GuiUtil.print_info("Looking for", user_input)
    leagues = League.read_by_name(user_input, like=True)

    if len(leagues) == 0:
        GuiUtil.print_att("No leagues found with name", user_input)
    elif len(leagues) == 1:
        league = leagues[0]
        matches = league.get_matches(season=util.get_current_season(),
                                     ordered=True)
        for i, match in enumerate(matches):
            match_out = get_printable_match(match)
            GuiUtil.print_indent_answer(i + 1, match_out, True)
Exemple #6
0
def get_league_str(league):
    league_str = league.name

    # ranking
    league_str += "\nRanking:"
    ranking = league.get_ranking(util.get_current_season())
    league_str = get_ramking_str(ranking)

    # home rankig
    # TODO HOME RANKING
    '''
    league_str += "\nHome Ranking:"
    ranking = league.get_ranking(util.get_current_season(), home=True)
    league_str = get_ramking_str(ranking)
    '''

    return league_str
Exemple #7
0
def search_by_team():
    user_input = input("Insert team name: ")
    GuiUtil.print_info("Looking for", user_input)
    teams_found = Team.read_by_name(user_input)

    GuiUtil.print_info("Teams found", len(teams_found))
    if len(teams_found) == 0:
        GuiUtil.print_att("Looking for team with similar name", user_input)
        teams_found = Team.read_by_name(user_input, like=True)

    if len(teams_found) == 0:
        GuiUtil.print_att("Teams found", 0)
    elif len(teams_found) == 1:
        team = teams_found[0]
        matches = team.get_matches(season=util.get_current_season(),
                                   ordered=True)
        for i, match in enumerate(matches):
            match_out = get_printable_match(match)
            GuiUtil.print_indent_answer(i + 1, match_out, True)
    else:
        GuiUtil.print_att("Too many teams found", "Be more precise")
Exemple #8
0
def get_formation(match, team, home=True):

    formation_str = "\n\n" + team.team_short_name

    # get trends
    formation_str += get_team_trend_str(match, team)

    # get Goals
    formation_str += get_team_goals_str(match, team)
    formation_str += get_team_goals_str(match, team, n=5)

    if home:
        team_lines = match.get_home_team_lines_up()
    else:
        team_lines = match.get_away_team_lines_up()

    team_goal_done, team_goal_received, num_matches = team.get_goals_by_season_and_stage(
        util.get_current_season(), match.stage)

    for i, player in enumerate(team_lines):
        if not util.is_None(player):

            if player.is_gk():
                goal_dr = player.get_goal_received(
                    season=util.get_current_season(), stage=match.stage)
                team_goal_dr = team_goal_received
            else:
                goal_dr = player.get_goal_done(
                    season=util.get_current_season(), stage=match.stage)
                team_goal_dr = team_goal_done

            formation_str += '\n\t' + str(i + 1) + ") " + player.player_name.ljust(25, ".") \
                             + "\t P: "+str(len(player.get_matches(season=util.get_current_season(), stage=match.stage))) + '/' + str(match.stage-1) \
                             + "\t G: "+str(goal_dr)+"/"+str(team_goal_dr)

            if not player.is_gk():
                assist_done = player.get_assist_done(
                    season=util.get_current_season(), stage=match.stage)
                team_assist_done = team.get_assist_by_season_and_stage(
                    season=util.get_current_season(), stage=match.stage)
                formation_str += "\t A: " + str(assist_done) + "/" + str(
                    team_assist_done)

    return formation_str
def run_experiment_3(exp, league, ml_train_input_id,
                     ml_train_input_representation, ml_train_stages_to_train):
    params = dict()

    #frameworks = mla.get_frameworks()
    frameworks = ["Sklearn"]
    methods = mla.get_methods()

    average_time = {f: {m: 0 for m in methods} for f in frameworks}
    average_accuracy = {f: {m: 0 for m in methods} for f in frameworks}
    match_predicted = {f: {m: 0 for m in methods} for f in frameworks}

    params["ml_train_input_id"] = ml_train_input_id
    params["ml_train_input_representation"] = ml_train_input_representation
    params["ml_train_stages_to_train"] = ml_train_stages_to_train

    for only_team_history in [True, False]:
        try:
            for ml_alg_framework in frameworks:
                for ml_alg_method in methods:
                    params["ml_alg_method"] = ml_alg_method
                    params["ml_alg_framework"] = ml_alg_framework
                    print(params)
                    curr_denominator = 0
                    curr_execution_time = 0
                    curr_average_accuracy = 0

                    for season in league.get_seasons():
                        if season == util.get_current_season():
                            break

                        print(season, only_team_history, ml_alg_framework,
                              ml_alg_method)

                        params["season"] = season

                        pa = PredictionAccuracy(
                            league,
                            only_team_history=only_team_history,
                            **params)
                        pa.compute_prediction_accuracy()

                        print("Average accuracy:", pa.get_average_accuracy())
                        print("Match predicted:", pa.get_match_predicted())
                        print("Time:", pa.get_execution_time())

                        curr_denominator += pa.get_match_predicted()
                        curr_execution_time += pa.get_match_predicted(
                        ) * pa.get_execution_time()
                        curr_average_accuracy += pa.get_match_predicted(
                        ) * pa.get_average_accuracy()

                    if curr_denominator != 0:
                        average_time[ml_alg_framework][
                            ml_alg_method] = curr_execution_time / curr_denominator
                        average_accuracy[ml_alg_framework][
                            ml_alg_method] = curr_average_accuracy / curr_denominator
                        match_predicted[ml_alg_framework][
                            ml_alg_method] = curr_denominator

                x, y = experiment.get_x_y(
                    average_accuracy[ml_alg_framework].keys(),
                    average_accuracy[ml_alg_framework])
                file_name = experiment.get_file_name(
                    [ml_alg_framework, only_team_history], post="accurcacy")
                exp.create_plot(x, y, file_name, **params)

                x, y = experiment.get_x_y(
                    average_time[ml_alg_framework].keys(),
                    average_time[ml_alg_framework])
                file_name = experiment.get_file_name(
                    [ml_alg_framework, only_team_history], post="runtime")
                exp.create_plot(x, y, file_name, is_accuracy=False, **params)
        except AttributeError:
            continue
Exemple #10
0
def run_experiment_1(exp, league, ml_train_input_id,
                     ml_train_input_representation, **params):
    print("Running experiment 1")
    print("\tml_train_input_id:", ml_train_input_id)
    print("\tml_train_input_representation:", ml_train_input_representation)

    params["ml_train_input_id"] = ml_train_input_id
    params["ml_train_input_representation"] = ml_train_input_representation

    ml_train_stages_to_train = [10, 25, 50, 95, 130]

    average_time = dict()
    average_accuracy = dict()
    match_predicted = dict()
    for only_team_history in [True, False]:
        for n_matches in ml_train_stages_to_train:
            params["ml_train_stages_to_train"] = n_matches

            curr_denominator = 0
            curr_execution_time = 0
            curr_average_accuracy = 0

            for season in league.get_seasons():
                if season == util.get_current_season():
                    break

                print(season, n_matches)

                params["season"] = season

                pa = PredictionAccuracy(league,
                                        only_team_history=only_team_history,
                                        **params)
                pa.compute_prediction_accuracy()

                print("Average accuracy:", pa.get_average_accuracy())
                print("Match predicted:", pa.get_match_predicted())
                print("Time:", pa.get_execution_time())

                curr_denominator += pa.get_match_predicted()
                curr_execution_time += pa.get_match_predicted(
                ) * pa.get_execution_time()
                curr_average_accuracy += pa.get_match_predicted(
                ) * pa.get_average_accuracy()

            if curr_denominator != 0:
                average_time[
                    n_matches] = curr_execution_time / curr_denominator
                average_accuracy[
                    n_matches] = curr_average_accuracy / curr_denominator
                match_predicted[n_matches] = curr_denominator

        x, y = experiment.get_x_y(ml_train_stages_to_train, average_accuracy)
        file_name = experiment.get_file_name([
            ml_train_input_id, ml_train_input_representation, only_team_history
        ],
                                             post="accurcacy")
        exp.create_plot(x, y, file_name, **params)

        x, y = experiment.get_x_y(ml_train_stages_to_train, average_time)
        file_name = experiment.get_file_name([
            ml_train_input_id, ml_train_input_representation, only_team_history
        ],
                                             post="runtime")
        exp.create_plot(x, y, file_name, is_accuracy=False, **params)
def get_player_str(player, show_details, current_player_team=None):
    player_link = "http://sofifa.com/player/"
    player_str = player.player_name + " " + player_link + str(
        player.player_fifa_api_id)

    if show_details:
        if not current_player_team:
            current_player_team = player.get_current_team()
        if current_player_team:
            # current team
            player_str += '\nCurrent team: ' + current_player_team.team_long_name

            # matches played
            team_matches = current_player_team.get_matches(
                season=util.get_current_season())
            player_str += '\nGames when he starts from the beginning: ' \
                          + str(len(player.get_matches(season=util.get_current_season()))) + '/' \
                          + str(len(team_matches))

            # goals done / received
            team_goal_done, team_goal_received = current_player_team.get_goals_by_season(
                season=util.get_current_season())
            if not player.is_gk():
                goal_done = player.get_goal_done(
                    season=util.get_current_season())
                player_str += '\nGoal done: ' + str(goal_done) + "/" + str(
                    team_goal_done)

                # assist done
                assist_done = player.get_assist_done(
                    season=util.get_current_season())
                team_assist_done = current_player_team.get_assist_by_season_and_stage(
                    season=util.get_current_season())
                player_str += '\nAssist done: ' + str(assist_done) + "/" + str(
                    team_assist_done)
            else:
                goal_recived = player.get_goal_received(
                    season=util.get_current_season())
                player_str += '\nGoal Received: ' + str(
                    goal_recived) + "/" + str(team_goal_received)

        else:
            # current team not found
            player_str += '\nCurrent team: NOT FOUND'

            # matches played
            player_str += '\nGames when he starts from the beginning: ' \
                          + str(len(player.get_matches(season=util.get_current_season())))

            # goals done / received
            if not player.is_gk():
                goal_done = player.get_goal_done(
                    season=util.get_current_season())
                player_str += '\nGoal done: ' + str(goal_done)

                # assist done
                assist_done = player.get_assist_done(
                    season=util.get_current_season())
                player_str += '\nAssist done: ' + str(assist_done)
            else:
                goal_recived = player.get_goal_received(
                    season=util.get_current_season())
                player_str += '\nGoal Received: ' + str(goal_recived)

    return player_str
Exemple #12
0
 def get_teams_current_season(self):
     """
     Return the teams of this league playing the current season
     :return:
     """
     return self.get_teams(util.get_current_season())