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(".", ","))
    def run(self, league, complete=True, **params):
        if self.type == 0:
            import src.application.MachineLearning.experiment.experiment_0 as exp_0
            type_evaluation = util.get_default(params, "type_evaluation", 1)
            exp_0.run_experiment_0(self, league, type_evaluation)
        elif self.type == 1:
            import src.application.MachineLearning.experiment.experiment_1 as exp_1
            if complete:
                for ml_train_input_id, ml_train_input_descr in mli.get_input_ids(
                ).items():
                    for ml_train_input_representation in mli.get_representations(
                            ml_train_input_id):
                        exp_1.run_experiment_1(self, league, ml_train_input_id,
                                               ml_train_input_representation)
            else:
                ml_train_input_id = util.get_default(params,
                                                     "ml_train_input_id", 1)
                ml_train_input_representation = util.get_default(
                    params, "ml_train_input_representation", 1)
                del (params["ml_train_input_id"])
                del (params["ml_train_input_representation"])
                exp_1.run_experiment_1(self, league, ml_train_input_id,
                                       ml_train_input_representation, **params)

        elif self.type == 2:
            import src.application.MachineLearning.experiment.experiment_2 as exp_2
            ml_train_input_id = util.get_default(params, "ml_train_input_id",
                                                 1)
            ml_train_input_representation = util.get_default(
                params, "ml_train_input_representation", 1)
            exp_2.run_experiment_2(self, league, ml_train_input_id,
                                   ml_train_input_representation)
            pass

        elif self.type == 3:
            import src.application.MachineLearning.experiment.experiment_3 as exp_3
            if complete:
                pass
            else:
                ml_train_input_id = util.get_default(params,
                                                     "ml_train_input_id", 1)
                ml_train_input_representation = util.get_default(
                    params, "ml_train_input_representation", 1)
                ml_train_stages_to_train = util.get_default(
                    params, "ml_train_stages_to_train", 10)
                exp_3.run_experiment_3(self, league, ml_train_input_id,
                                       ml_train_input_representation,
                                       ml_train_stages_to_train)

        elif self.type == 4:
            import src.application.MachineLearning.experiment.experiment_4 as exp_4
            exp_4.run_experiment_4(self, league)
    def __init__(self, **params):
        MachineLearningAlgorithm.__init__(self,
                                          [],
                                          [],
                                          [],
                                          [],
                                          [],
                                          [])
        self.params = params

        self.number_sample = util.get_default(params, "poisson_n_sample", 10000)
    def get_title(self):
        if self.experiment_type == 0:
            title = "Stage accuracy of the season " + util.get_default(
                self.params, "season", "")

        elif self.experiment_type == 1:
            title = "Best number of training matches by match representation [" + str(self.params["ml_train_input_id"])\
                    + ", " + str(self.params["ml_train_input_representation"]) + "]"
        else:
            title = "[TITLE]"

        return title
    def train(self,):
        if self.method == "SVM":
            kernel = util.get_default(self.params, "kernel", "rbf")
            self.estimator = get_SVM_estimator(self.train_data, self.train_label, kernel)

        elif self.method == "KNN":
            self.estimator = get_KNeighborsClassifier(self.train_data, self.train_label)

        elif self.method == "RandomForest":
            self.estimator = get_RandomForestClassifier(self.train_data, self.train_label)
            # self.estimator = RandomForestClassifier()
            self.estimator.fit(self.train_data, self.train_label)

        elif self.method == "AdaBoostClassifier":
            self.estimator = AdaBoostClassifier(DecisionTreeClassifier(max_depth=5),
                                                n_estimators=1000, learning_rate=0.1, random_state=42)
            self.estimator.fit(self.train_data, self.train_label)

        else:
            raise MLException(4)
    def parse_json(self, season):
        """
        The match is a json string, comment information are not useful for our purposes.
        :param season:
        :return:
        """
        '''
        status_descfk = self.json_match["status_descfk"]
        status_type = self.json_match["status_type"]
        status_desc_short = self.json_match["status_desc_short"]
        status_desc_name = self.json_match["status_desc_name"]

        match_n = self.json_match["n"]
        sportfk = self.json_match["sportfk"]

        countryfk = self.json_match["countryfk"]
        tournamentfk = self.json_match["tournamentfk"]
        tournament_templatefk = self.json_match["tournament_templatefk"]
        tournament_stagefk = self.json_match["tournament_stagefk"]

        live = self.json_match["live"]

        winner = self.json_match["winner"]
        winners = self.json_match["winners"]
        scopes_hash = self.json_match["scopes_hash"]
        incidents_hash = self.json_match["incidents_hash"]


        n_home_yellow_card = 0
        n_home_double_yellow_card = 0
        n_home_red_card = 0
        n_away_yellow_card = 0
        n_away_double_yellow_card = 0
        n_away_red_card = 0

        if type(self.json_match["cards"]) == dict:
            try:
                self.json_match["cards"]["1"]
                n_home_yellow_card = util.get_default(self.json_match["cards"]["1"], "14", 0)
                n_home_double_yellow_card = util.get_default(self.json_match["cards"]["1"], "15", 0)
                n_home_red_card = util.get_default(self.json_match["cards"]["1"], "16", 0)
            except KeyError:
                pass

            try:
                self.json_match["cards"]["2"]
                n_away_yellow_card = util.get_default(self.json_match["cards"]["2"], "14", 0)
                n_away_double_yellow_card = util.get_default(self.json_match["cards"]["2"], "15", 0)
                n_away_red_card = util.get_default(self.json_match["cards"]["2"], "16", 0)
            except KeyError:
                pass
        '''

        # Goals of the match
        # n_home_goal_first_time = util.get_default(self.json_match["results"]["1"]["r"], "5", 0)
        n_home_goal = util.get_default(self.json_match["results"]["1"]["r"],
                                       "1", 0)
        # n_away_goal_first_time = util.get_default(self.json_match["results"]["2"]["r"], "5", 0)
        n_away_goal = util.get_default(self.json_match["results"]["2"]["r"],
                                       "1", 0)

        # Represent a match to persist with a dictionary
        match_attributes = dict()
        match_attributes["country_id"] = self.league.country_id
        match_attributes["league_id"] = self.league.id
        match_attributes["season"] = season
        match_attributes["stage"] = self.json_match[
            "round"]  # stage of a match
        match_attributes["date"] = self.json_match["startdate"]
        match_attributes["match_api_id"] = self.json_match["eventfk"]
        match_attributes["home_team_api_id"] = self.json_match["homefk"]
        match_attributes["away_team_api_id"] = self.json_match["awayfk"]
        match_attributes["home_team_goal"] = n_home_goal
        match_attributes["away_team_goal"] = n_away_goal

        # check team
        home_team_name = check_team(self.json_match["homefk"])
        away_team_name = check_team(self.json_match["awayfk"])

        # formations to be crawled if:
        #   - the match is not stored in the DB
        #   - teams formation are not stored in the DB
        #   - match is not finished
        if not self.match or not self.match.are_teams_linedup(
        ) or not self.match.is_finished():
            lc = CrawlerLineup(self.match, match_attributes, self.event)
            lc.get_lineups()

        # event incidents to be crawled if:
        #   - the match is not stored in the DB
        #   - some information are missing
        if not self.match or (self.match
                              and not self.match.are_incidents_managed()):
            li = CrawlerIncidents(self.match, match_attributes, self.event)
            li.get_incidents()

        if not self.match:
            # persist match
            Match.write_new_match(match_attributes)
        else:
            # update match
            Match.update_match(self.match, match_attributes)

        print("\t", home_team_name, "vs", away_team_name)