Example #1
0
 def _train_model(self):
   """Trains the internal model using all features seen so far."""
   if not self._can_train():
     return
   self._threshold = np.clip(
       threshold_policies.single_threshold(
           predictions=self._features,
           labels=self._labels,
           weights=None,
           cost_matrix=self._cost_matrix), _SCORE_MIN, _SCORE_MAX)
    def test_weighted_single_threshold(self):
        # With a vanilla cost matrix, this should result in accepting
        # only predictions >= 0.75
        predictions = np.array([0.25, 0.25, 0.33, 0.33, 0.75, 0.75])
        labels = np.array([0, 1, 0, 1, 0, 1])
        weights = np.array([3., 1., 2., 1., 1., 3.])
        weights = weights / sum(weights)

        vanilla_cost_matrix = params.CostMatrix(tn=0., fp=-1., fn=0., tp=1.)

        weighted_threshold = threshold_policies.single_threshold(
            predictions, labels, weights, vanilla_cost_matrix)
        self.assertAlmostEqual(weighted_threshold, 0.75)
    def test_weighted_single_threshold(self):
        # Weighted labels/predictions are perfectly calibrated.
        # With a vanilla cost matrix, this should result in threshold > 0.5
        predictions = np.array([0.25, 0.25, 0.5, 0.5, 0.75, 0.75])
        labels = np.array([0, 1, 0, 1, 0, 1])
        weights = np.array([3., 1., 1., 1., 1., 3.])
        weights = weights / sum(weights)

        vanilla_cost_matrix = params.CostMatrix(tn=1., fp=-1., fn=-1., tp=1.)

        weighted_threshold = threshold_policies.single_threshold(
            predictions, labels, weights, vanilla_cost_matrix)
        self.assertEqual(weighted_threshold, 0.75)
    def _set_thresholds(self, training_corpus):
        self.global_threshold = threshold_policies.single_threshold(
            predictions=self._score_transform(training_corpus.get_features()),
            labels=training_corpus.get_labels(),
            weights=None,
            cost_matrix=self.params.cost_matrix)

        if self.params.threshold_policy == threshold_policies.ThresholdPolicy.EQUALIZE_OPPORTUNITY:
            self.group_specific_thresholds = (
                threshold_policies.equality_of_opportunity_thresholds(
                    group_predictions=self._recursively_apply_score_transform(
                        training_corpus.get_features(
                            stratify_by=self.params.group_key)),
                    group_labels=training_corpus.get_labels(
                        stratify_by=self.params.group_key),
                    group_weights=None,
                    cost_matrix=self.params.cost_matrix))