Пример #1
0
    def predict_proba_instance(self, query):
        """Predicts the posterior probabilities of the corresponding query sample.

        If self.mode == "all", get the probability estimates of the selected ensemble. Otherwise,
        the technique gets the probability estimates from the selected base classifier

        Parameters
        ----------
        query : array containing the test sample = [n_features]

        Returns
        -------
        predicted_proba : array = [n_classes] with the probability estimates for all classes
        """
        competences = self.estimate_competence(query)
        if self.selection_method != 'all':
            # only one classifier is selected
            clf_index = self.select(competences)
            predicted_proba = self.pool_classifiers[clf_index].predict_proba(
                query)
        else:
            # Selected ensemble of classifiers is combined using Majority Voting
            indices = self.select(competences)
            classifier_ensemble = self._get_classifier_ensemble(indices)
            predicted_proba = predict_proba_ensemble(classifier_ensemble,
                                                     query)

        return predicted_proba
Пример #2
0
    def predict_proba(self, X):
        """Estimates the posterior probabilities for sample in X.

         Parameters
         ----------
         X : array of shape (n_samples, n_features)
             The input data.

         Returns
         -------
         predicted_proba : array of shape (n_samples, n_classes)
                           Probabilities estimates for each sample in X.
         """
        self._check_is_fitted()
        X = check_array(X)
        if self.n_features_ != X.shape[1]:
            raise ValueError("Number of features of the model must "
                             "match the input. Model n_features is {0} and "
                             "input n_features is {1}."
                             "".format(self.n_features_, X.shape[1]))

        self._check_predict_proba()
        features = self.estimator_features_[self.clf_indices_]
        proba = predict_proba_ensemble(self.ensemble_, X, features)
        return proba
Пример #3
0
    def predict_proba(self, X):
        """Estimates the posterior probabilities for sample in X.

        Parameters
        ----------
        X : array of shape = [n_samples, n_features]
            The input data.

        Returns
        -------
        predicted_proba : array of shape = [n_samples, n_classes] with the
        probabilities estimates for each class in the classifier model.
        """
        # Check if the DS model was trained
        self._check_is_fitted()

        # Check if X is a valid input
        self._check_input_predict(X)

        n_samples = X.shape[0]
        predicted_proba = np.zeros((n_samples, self.n_classes))
        for index, instance in enumerate(X):
            # Do not use dynamic selection if all base classifiers agrees on the
            # same label.
            instance = instance.reshape(1, -1)
            if self._all_classifier_agree(instance):

                #  since it may have better probabilities estimates
                predicted_proba[index, :] = predict_proba_ensemble(self.pool_classifiers, instance)[0]

            else:

                # Check whether use the DFP or hardness information is used to compute the competence region
                if self.DFP or self.with_IH:
                    self.distances, self.neighbors = self._get_region_competence(instance)

                # If Instance hardness (IH) is used, check the hardness level of the region of competence
                # to decide between DS or the roc_algorithm classifier
                if self.with_IH and (self._hardness_region_competence(self.neighbors) <= self.IH_rate):
                    # use the KNN for prediction if the sample is located in a safe region.
                    # TODO: Optimize that to not re-calculate the neighborhood
                    predicted_proba[index, :] = self.roc_algorithm.predict_proba(instance)

                # Otherwise, use DS for classification
                else:
                    # Check if the dynamic frienemy pruning should be used
                    if self.DFP:
                            self.DFP_mask = self._frienemy_pruning()
                    else:
                            self.DFP_mask = np.ones(self.n_classifiers)

                    predicted_proba[index, :] = self.predict_proba_instance(instance)

        # Reset the neighbors and the distances as they are specific to a given query.
        self.neighbors = None
        self.distances = None
        return predicted_proba
    def predict_proba(self, X):
        """Estimates the posterior probabilities for sample in X.

         Parameters
         ----------
         X : array of shape = [n_samples, n_features]
             The input data.

         Returns
         -------
         predicted_proba : array of shape = [n_samples, n_classes]
                           Probabilities estimates for each sample in X.
         """
        self._check_is_fitted()
        self._check_predict_proba()
        proba = predict_proba_ensemble(self.ensemble_, X)
        return proba
Пример #5
0
    def predict_proba_instance(self, query):
        """Predicts the posterior probabilities of the corresponding query sample.

        If self.mode == "selection", the selected ensemble is used to estimate the probabilities. The average rule is
        used to give probabilities estimates.

        If self.mode == "weighting", all base classifiers are used for estimating the probabilities, however their
        influence in the final decision are weighted according to their estimated competence level. A weighted average
        method is used to give the probabilities estimates.

        If self.mode == "Hybrid",  A hybrid Dynamic selection and weighting approach is used. First an
        ensemble with the competent base classifiers are selected. Then, their decisions are aggregated using a
        weighted average rule to give the probabilities estimates.

        Parameters
        ----------
        query : array of shape = [n_features]
                The test sample
        Returns
        -------
        predicted_proba : array = [n_classes] with the probability estimates for all classes
        """
        competences = self.estimate_competence(query)
        if self.mode == "selection":
            indices = self.select(competences)
            classifier_ensemble = self._get_classifier_ensemble(indices)
            predicted_proba = predict_proba_ensemble(classifier_ensemble,
                                                     query)

        elif self.mode == "weighting":
            predicted_proba = predict_proba_ensemble_weighted(
                self.pool_classifiers, competences, query)
        else:
            indices = self.select(competences)
            competences_ensemble = competences[indices]
            classifier_ensemble = self._get_classifier_ensemble(indices)
            predicted_proba = predict_proba_ensemble_weighted(
                classifier_ensemble, competences_ensemble, query)

        return predicted_proba
Пример #6
0
def test_predict_proba():
    query = np.array([[1, -1]])
    ensemble_classifiers = create_pool_classifiers()
    predicted_proba = predict_proba_ensemble(ensemble_classifiers, query)
    assert np.isclose(predicted_proba, [0.61, 0.39]).all()