Beispiel #1
0
    def __prediction(self, extractor: AppearanceExtractor, classifier: Classifier, predict_season: int) -> \
            Dict[Player, MultiLayerResult]:
        """ Execute the prediction phase of the Appearance Layer.

        Arguments:
            extractor (AppearanceExtractor): The extractor which delivers the prediction data.
            classifier (Classifier): A classifier which classifies players as either Mol or non-Mol based on how often
                they appear.
            predict_season (int): For which season we make the prediction.

        Returns:
            A dictionary with as key the players that participated in the prediction season and as value a
            MultiLayerResult which contains the predictions.
        """
        all_predictions = dict()
        predict_data = extractor.get_predict_data()
        if not predict_data:
            return EmptyMultiLayer().predict(predict_season, 0, set())

        for player in get_players_in_season(predict_season):
            if player in predict_data:
                predictions = []
                for data in predict_data[player]:
                    predictions.append(classifier.predict_proba([data]))
                all_predictions[player] = MultiLayerResult(np.array(predictions), False)
            else:
                all_predictions[player] = MultiLayerResult(np.array([]), True)

        return all_predictions
Beispiel #2
0
    def __predict(
            self, predict_season: int, latest_episode: int,
            predict_data: Dict[Player, List[np.array]],
            classifier: LogisticRegression) -> Dict[Player, MultiLayerResult]:
        """ Execute the prediction phase of the Money Layer.

        Arguments:
            predict_season (int): The season for which the predictions are made.
            latest_episode (int): The latest episode useable in the predict season.
            predict_data (List[PredictSample]): The prediction data with features used to make predictions.
            classifier (LogisticRegression): The machine learning model used to make predictions.

        Returns:
            A dictionary with as key the players that participated in the prediction season and as value a
            MultiLayerResult which contains the predictions.
        """
        all_predictions = dict()
        season_players = get_players_in_season(predict_season)
        for player in season_players:
            all_predictions[player] = []

        alive_players = MONEY_DATA[predict_season].get_alive(latest_episode)
        for player, all_rows in predict_data.items():
            for row in all_rows:
                likelihood = classifier.predict_proba(np.array([row]))[0][1]
                all_predictions[player] = all_predictions[player] + [
                    likelihood
                ]

        return {player: MultiLayerResult(np.array(predictions), player not in alive_players) for player, predictions in \
                all_predictions.items()}
Beispiel #3
0
    def predict(self, predict_season: int, latest_episode: int,
                train_seasons: Set[int]) -> Dict[Player, MultiLayerResult]:
        predictions = dict()
        for layer in self.__layers:
            prediction = layer.compute_distribution(predict_season,
                                                    latest_episode,
                                                    train_seasons)
            for player, likelihood in prediction.items():
                predictions[player] = predictions.get(player,
                                                      []) + [likelihood]

        likelihood_sums = np.zeros(len(self.__layers))
        if self.__normalize:
            for player, likelihoods in predictions.items():
                if 0.0 not in likelihoods:
                    likelihood_sums += np.array(likelihoods)

        result = dict()
        for player, likelihoods in predictions.items():
            excluded = 0.0 in likelihoods
            if not self.__normalize:
                likelihoods = np.array(likelihoods)
            elif excluded:
                likelihoods = np.zeros(len(self.__layers))
            else:
                likelihoods = np.divide(np.array(likelihoods), likelihood_sums)
            result[player] = MultiLayerResult(likelihoods, excluded)

        return result
Beispiel #4
0
    def __prediction(self, extractor: AppearanceExtractor,
                     non_mol_kde: gaussian_kde, mol_kde: gaussian_kde,
                     predict_season: int) -> Dict[Player, MultiLayerResult]:
        """ Execute the prediction phase of the Appearance Layer.

        Arguments:
            extractor (AppearanceExtractor): The extractor which delivers the prediction data.
            non_mol_kde (gaussian_kde): The Kernel Density Estimator for non-Mol appearance values.
            mol_kde (gaussian_kde): The Kernel Density Estimator for Mol appearance values.
            predict_season (int): For which season we make the prediction.

        Returns:
            A dictionary with as key the players that participated in the prediction season and as value a
            MultiLayerResult which contains the predictions.
        """
        all_predictions = dict()
        predict_data = extractor.get_predict_data()
        if not predict_data:
            return EmptyMultiLayer().predict(predict_season, 0, set())

        min_value = self.get_boundary(non_mol_kde, mol_kde, len(predict_data),
                                      self.__cdf_cutoff / 2, self.MIN_VALUE,
                                      self.MAX_VALUE)
        max_value = self.get_boundary(non_mol_kde, mol_kde, len(predict_data),
                                      1 - self.__cdf_cutoff / 2,
                                      self.MIN_VALUE, self.MAX_VALUE)
        for player in get_players_in_season(predict_season):
            if player in predict_data:
                predictions = []
                for data in predict_data[player]:
                    data = min(max(data, min_value), max_value)
                    non_mol_likelihood = non_mol_kde.pdf(data)[0] * (
                        len(predict_data) - 1) / len(predict_data)
                    mol_likelihood = mol_kde.pdf(data)[0] / len(predict_data)
                    likelihood = mol_likelihood / (non_mol_likelihood +
                                                   mol_likelihood)
                    predictions.append(likelihood)
                all_predictions[player] = MultiLayerResult(
                    np.array(predictions), False)
            else:
                all_predictions[player] = MultiLayerResult(np.array([]), True)

        return all_predictions
Beispiel #5
0
    def __predict(
            self, predict_season: int, latest_episode: int,
            predict_data: List[PredictSample],
            in_classifier: LogisticRegression,
            out_classifier: LogisticRegression
    ) -> Dict[Player, MultiLayerResult]:
        """ Execute the prediction phase of the Exam Drop Layer.

        Arguments:
            predict_season (int): The season for which the predictions are made.
            latest_episode (int): The latest episode useable in the predict season.
            predict_data (List[PredictSample]): The prediction data with features used to make predictions.
            in_classifier (LogisticRegression): The machine learning model used to make predictions for cases where
                a player is in the answer.
            out_classifier (LogisticRegression): The machine learning model used to make predictions for cases where
                a player is out the answer.

        Returns:
            A dictionary with as key the players that participated in the prediction season and as value a
            MultiLayerResult which contains the predictions.
        """
        all_predictions = dict()
        season_players = get_players_in_season(predict_season)
        for player in season_players:
            all_predictions[player] = []

        alive_players = EXAM_DATA[predict_season].get_alive_players(
            latest_episode)
        for data in predict_data:
            in_likelihood = in_classifier.predict_proba(
                np.array([data.features]))[0][1]
            out_likelihood = out_classifier.predict_proba(
                np.array([data.features]))[0][1]
            if out_likelihood < in_likelihood:
                in_likelihood = out_likelihood = 1 / len(alive_players)
            in_likelihood = in_likelihood**data.weight
            out_likelihood = out_likelihood**data.weight
            for player in data.in_answer:
                all_predictions[player] = all_predictions[player] + [
                    in_likelihood
                ]
            for player in data.out_answer:
                all_predictions[player] = all_predictions[player] + [
                    out_likelihood
                ]

        return {
            player: MultiLayerResult(np.array(predictions), player
                                     not in alive_players)
            for player, predictions in all_predictions.items()
        }
Beispiel #6
0
 def predict(self, predict_season: int, latest_episode: int, train_seasons: Set[int]) -> Dict[Player, MultiLayerResult]:
     season_players = get_players_in_season(predict_season)
     return {player: MultiLayerResult(np.array([]), self.__is_excluded_player(player)) for player in season_players}