Esempio n. 1
0
    def __training(self, extractor: ExamDropExtractor) -> LogisticRegression:
        """ Execute the training phase of the Exam Drop Layer.

        Arguments:
            extractor (ExamDropExtractor): The extractor which delivers the training data.

        Returns:
            The trained machine learning model used to make predictions.
        """
        train_input, train_output, train_weights = extractor.get_train_data()
        classifier = LogisticRegression(solver="lbfgs")
        classifier.fit(train_input, train_output, train_weights)
        return classifier
Esempio n. 2
0
    def predict(self, predict_season: int, latest_episode: int,
                train_seasons: Set[int]) -> Dict[Player, MultiLayerResult]:
        available_seasons = EXAM_DATA.keys()
        train_seasons = train_seasons.intersection(available_seasons)
        if predict_season not in available_seasons:
            return EmptyMultiLayer().predict(predict_season, latest_episode,
                                             train_seasons)

        extractor = ExamDropExtractor(predict_season, latest_episode,
                                      train_seasons,
                                      self.__anova_f_significance,
                                      self.__pca_explain, self.__max_splits)
        classifier = self.__training(extractor)
        predict_data = extractor.get_predict_data()
        if not predict_data:
            alive_players = EXAM_DATA[predict_season].get_alive_players(
                latest_episode)
            return EmptyMultiLayer(alive_players).predict(
                predict_season, latest_episode, train_seasons)

        return self.__predict(predict_season, latest_episode, predict_data,
                              classifier)
Esempio n. 3
0
    def predict(self, predict_season: int, latest_episode: int,
                train_seasons: Set[int]) -> Dict[Player, MultiLayerResult]:
        available_seasons = EXAM_DATA.keys()
        train_seasons = train_seasons.intersection(available_seasons)
        if predict_season not in available_seasons:
            return EmptyMultiLayer().predict(predict_season, latest_episode,
                                             train_seasons)

        extractor = ExamDropExtractor(predict_season, latest_episode,
                                      train_seasons, self.__min_cluster_size,
                                      self.__spline_curves,
                                      self.__random_generator)
        in_classifier, out_classifier = self.__training(extractor)
        predict_data = extractor.get_predict_data()
        if not predict_data:
            alive_players = EXAM_DATA[predict_season].get_alive_players(
                latest_episode)
            return EmptyMultiLayer(alive_players).predict(
                predict_season, latest_episode, train_seasons)

        return self.__predict(predict_season, latest_episode, predict_data,
                              in_classifier, out_classifier)
Esempio n. 4
0
    def __training(
        self, extractor: ExamDropExtractor
    ) -> Tuple[LogisticRegression, LogisticRegression]:
        """ Execute the training phase of the Exam Drop Layer.

        Arguments:
            extractor (ExamDropExtractor): The extractor which delivers the training data.

        Returns:
            Two trained machine learning model used to make predictions. The first model makes predictions for cases
            where a player is in the answer and the second model makes predictions for cases where a player is out the
            answer.
        """
        train_input, train_output, in_answer, train_weights = extractor.get_train_data(
        )

        in_input = [
            ti for ti, answer in zip(train_input, in_answer) if answer == 1.0
        ]
        in_output = [
            to for to, answer in zip(train_output, in_answer) if answer == 1.0
        ]
        in_weights = [
            tw for tw, answer in zip(train_weights, in_answer) if answer == 1.0
        ]
        in_classifier = LogisticRegression(
            solver="lbfgs",
            max_iter=self.MAX_TRAIN_ITERATIONS,
            random_state=self.__random_generator)
        in_classifier.fit(in_input, in_output, in_weights)

        out_input = [
            ti for ti, answer in zip(train_input, in_answer) if answer == 0.0
        ]
        out_output = [
            to for to, answer in zip(train_output, in_answer) if answer == 0.0
        ]
        out_weights = [
            tw for tw, answer in zip(train_weights, in_answer) if answer == 0.0
        ]
        out_classifier = LogisticRegression(
            solver="lbfgs",
            max_iter=self.MAX_TRAIN_ITERATIONS,
            random_state=self.__random_generator)
        out_classifier.fit(out_input, out_output, out_weights)
        return in_classifier, out_classifier
Esempio n. 5
0
from Data.PlayerData import get_is_mol
from Layers.ExamDrop.ExamDropEncoder import ExamDropEncoder
from Layers.ExamDrop.ExamDropExtractor import ExamDropExtractor
from Tools.Encoders.NaturalSplineEncoding import NaturalSplineEncoding
from Tools.Encoders.StableDiscretizer import StableDiscretizer
from sklearn.linear_model import LinearRegression
import matplotlib.pyplot as plt
import numpy as np
import sys

TRAIN_SEASONS = {5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22}
NUM_CURVES = 4

train_data = []
for season in TRAIN_SEASONS:
    train_data.extend(ExamDropExtractor.get_season_data(season, sys.maxsize, True))
train_input = np.array([ExamDropEncoder.extract_features(sample, sys.maxsize) for sample in train_data])
train_output = np.array([1.0 if get_is_mol(sample.selected_player) else 0.0 for sample in train_data])
m = ExamDropEncoder.NUM_CONTINUOUS_FEATURES

for column, feature_name in zip(train_input.T[-m:], ExamDropEncoder.FEATURE_NAMES[-m:]):
    trans_data = column[:,np.newaxis]
    spline_encoder = NaturalSplineEncoding([NUM_CURVES])
    trans_data = spline_encoder.fit_transform(trans_data)

    in_answer_input = [row for row, data in zip(trans_data, train_data) if data.selected_player in data.answer]
    in_answer_output = [to for to, data in zip(train_output, train_data) if data.selected_player in data.answer]
    regression = LinearRegression()
    regression.fit(in_answer_input, in_answer_output)

    X = np.array([pi for pi in sorted(set(column))])