コード例 #1
0
    def classify_instance(self, query, predictions):
        """Predicts the class label of the corresponding query sample.

        If self.mode == "all", the majority voting scheme is used to aggregate the predictions of all classifiers with
        the max competence level estimate.

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

        predictions : array of shape = [n_samples, n_classifiers]
                      Contains the predictions of all base classifier for all samples in the query array

        Returns
        -------
        The predicted label of the query
        """
        competences = self.estimate_competence(query, predictions=predictions)

        if self.selection_method != 'all':
            # only one classifier is selected
            clf_index = self.select(competences)
            predicted_label = predictions[clf_index]
        else:
            # Selected ensemble of classifiers is combined using Majority Voting
            indices = self.select(competences)
            votes = np.atleast_2d(predictions[indices])
            predicted_label = majority_voting_rule(votes)

        return predicted_label
コード例 #2
0
ファイル: des_clustering.py プロジェクト: xiangfasong/DESlib
    def classify_with_ds(self, query, predictions, probabilities=None):
        """Predicts the label of the corresponding query sample.

        Parameters
        ----------
        query : array of shape = [n_features]
                The test sample.

        predictions : array of shape = [n_samples, n_classifiers]
                      Predictions of the base classifiers for all test examples.

        probabilities : array of shape = [n_samples, n_classifiers, n_classes]
                        Probabilities estimates of each base classifier for all test examples.

        Returns
        -------
        predicted_label : array of shape = [n_samples]
                          Predicted class label for each test example.
        """
        if query.ndim < 2:
            query = query.reshape(1, -1)

        if predictions.ndim < 2:
            predictions = predictions.reshape(1, -1)

        if query.shape[0] != predictions.shape[0]:
            raise ValueError('The arrays query and predictions must have the same number of samples. query.shape is {}'
                             'and predictions.shape is {}' .format(query.shape, predictions.shape))

        selected_classifiers = self.select(query)
        votes = predictions[np.arange(predictions.shape[0])[:, None], selected_classifiers]
        predicted_label = majority_voting_rule(votes)

        return predicted_label
コード例 #3
0
    def predict(self, X):
        """Predict the label of each sample in X and returns the predicted
        label.

        Parameters
        ----------
        X : array of shape (n_samples, n_features)
            The data to be classified

        Returns
        -------
        predicted_labels : array of shape (n_samples)
                           Predicted class 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]))

        votes = np.zeros((X.shape[0], self.n_classifiers_ensemble_))
        for idx, clf in enumerate(self.ensemble_):
            X_space = X[:, self.estimator_features_[self.clf_indices_[idx]]]
            votes[:, idx] = self._encode_base_labels(clf.predict(X_space))

        predicted_labels = majority_voting_rule(votes).astype(int)

        return self.classes_.take(predicted_labels)
コード例 #4
0
ファイル: base.py プロジェクト: vishalbelsare/DESlib
    def classify_with_ds(self,
                         predictions,
                         probabilities=None,
                         neighbors=None,
                         distances=None,
                         DFP_mask=None):
        """Predicts the class label of the corresponding query sample.

        If self.selection_method == "all", the majority voting scheme is used
        to aggregate the predictions of all classifiers with the max competence
        level estimates for each test examples.

        Parameters
        ----------
        predictions : array of shape (n_samples, n_classifiers)
            Predictions of the base classifiers for all test examples

        probabilities : array of shape (n_samples, n_classifiers, n_classes)
            Probabilities estimates of each base classifier for all test
            examples (For methods that always require probabilities from the
            base classifiers)

        neighbors : array of shape (n_samples, n_neighbors)
            Indices of the k nearest neighbors.
        distances : array of shape (n_samples, n_neighbors)
            Distances from the k nearest neighbors to the query

        DFP_mask : array of shape (n_samples, n_classifiers)
            Mask containing 1 for the selected base classifier and 0 otherwise.

        Returns
        -------
        predicted_label : array of shape (n_samples)
            The predicted label for each query
        """
        if predictions.ndim < 2:
            predictions = predictions.reshape(1, -1)
        competences = self.estimate_competence(neighbors,
                                               distances=distances,
                                               predictions=predictions)

        if self.DFP:
            competences = competences * DFP_mask

        if self.selection_method != 'all':
            # only one classifier is selected
            clf_index = self.select(competences)
            predicted_label = predictions[np.arange(predictions.shape[0]),
                                          clf_index]
        else:
            # Selected ensemble of classifiers is combined using Majority
            # Voting
            indices = self.select(competences)
            votes = np.ma.MaskedArray(predictions, ~indices)
            predicted_label = majority_voting_rule(votes)

        return predicted_label
コード例 #5
0
ファイル: des_clustering.py プロジェクト: world4jason/DESlib
    def classify_with_ds(self,
                         query,
                         predictions,
                         probabilities=None,
                         neighbors=None,
                         distances=None,
                         DFP_mask=None):
        """Predicts the label of the corresponding query sample.

        Parameters
        ----------
        query : array of shape = [n_features]
                The test sample.

        predictions : array of shape (n_samples, n_classifiers)
            Predictions of the base classifiers for all test examples.

        probabilities : array of shape (n_samples, n_classifiers, n_classes)
            Probabilities estimates of each base classifier for all test
            examples.

        neighbors : array of shape (n_samples, n_neighbors)
            Indices of the k nearest neighbors according for each test sample.

        distances : array of shape (n_samples, n_neighbors)
            Distances of the k nearest neighbors according for each test
            sample.

        DFP_mask : array of shape (n_samples, n_classifiers)
            Mask containing 1 for the selected base classifier and 0 otherwise.

        Returns
        -------
        predicted_label : array of shape (n_samples)
                          Predicted class label for each test example.
        """
        if query.ndim < 2:
            query = query.reshape(1, -1)

        if predictions.ndim < 2:
            predictions = predictions.reshape(1, -1)

        if query.shape[0] != predictions.shape[0]:
            raise ValueError(
                'The arrays query and predictions must have the same number'
                ' of samples. query.shape is {}'
                'and predictions.shape is {}'.format(query.shape,
                                                     predictions.shape))

        selected_classifiers = self.select(query)
        votes = predictions[np.arange(predictions.shape[0])[:, None],
                            selected_classifiers]
        predicted_label = majority_voting_rule(votes)

        return predicted_label
コード例 #6
0
    def classify_with_ds(self, query, predictions, probabilities=None, neighbors=None, distances=None, DFP_mask=None):
        """Predicts the label of the corresponding query sample.

        Parameters
        ----------
        query : array of shape = [n_samples, n_features]
                The test examples

        predictions : array of shape = [n_samples, n_classifiers]
                      Predictions of the base classifiers for all test examples

        probabilities : array of shape = [n_samples, n_classifiers, n_classes]
                        Probabilities estimates of each base classifier for all test examples.

        neighbors : array of shale = [n_samples, n_neighbors]
                    Indices of the k nearest neighbors according for each test sample

        distances : array of shale = [n_samples, n_neighbors]
                    Distances of the k nearest neighbors according for each test sample

        DFP_mask : array of shape = [n_samples, n_classifiers]
                   Mask containing 1 for the selected base classifier and 0 otherwise.

        Notes
        ------
        Different than other DES techniques, this method only select N candidates from the pool of classifiers.

        Returns
        -------
        predicted_label : array of shape = [n_samples]
                          Predicted class label for each test example.
        """
        if query.ndim < 2:
            query = query.reshape(1, -1)

        if predictions.ndim < 2:
            predictions = predictions.reshape(1, -1)

        if query.shape[0] != predictions.shape[0]:
            raise ValueError('The arrays query and predictions must have the same number of samples. query.shape is {}'
                             'and predictions.shape is {}' .format(query.shape, predictions.shape))

        accuracy = self.estimate_competence(query,
                                            neighbors=neighbors,
                                            predictions=predictions)

        if self.DFP:
            accuracy = accuracy * DFP_mask

        selected_classifiers = self.select(accuracy)
        votes = predictions[np.arange(predictions.shape[0])[:, None], selected_classifiers]
        predicted_label = majority_voting_rule(votes)

        return predicted_label
コード例 #7
0
    def classify_with_ds(self, query, predictions, probabilities=None):
        """Predicts the class label of the corresponding query sample.

        If self.selection_method == "all", the majority voting scheme is used to aggregate the predictions
        of all classifiers with the max competence level estimates for each test examples.

        Parameters
        ----------
        query : array of shape = [n_samples, n_features]
                The test examples.

        predictions : array of shape = [n_samples, n_classifiers]
                      Predictions of the base classifiers for all test examples.

        probabilities : array of shape = [n_samples, n_classifiers, n_classes]
                        Probabilities estimates of each base classifier for all test examples. (For methods that
                        always require probabilities from the base classifiers).

        Returns
        -------
        predicted_label : array of shape = [n_samples]
                          Predicted class label for each test example.
        """
        if query.ndim < 2:
            query = query.reshape(1, -1)

        if predictions.ndim < 2:
            predictions = predictions.reshape(1, -1)

        if query.shape[0] != predictions.shape[0]:
            raise ValueError(
                'The arrays query and predictions must have the same shape. query.shape is {}'
                'and predictions.shape is {}'.format(query.shape,
                                                     predictions.shape))

        competences = self.estimate_competence(query, predictions=predictions)

        if self.selection_method != 'all':
            # only one classifier is selected
            clf_index = self.select(competences)
            predicted_label = predictions[np.arange(predictions.shape[0]),
                                          clf_index]
        else:
            # Selected ensemble of classifiers is combined using Majority Voting
            indices = self.select(competences)
            votes = np.ma.MaskedArray(predictions, ~indices)
            predicted_label = majority_voting_rule(votes)

        return predicted_label
コード例 #8
0
ファイル: des_knn.py プロジェクト: marianaasouza/DESlib
    def classify_with_ds(self, query, predictions, probabilities=None):
        """Predicts the label of the corresponding query sample.

        Parameters
        ----------
        query : array of shape = [n_samples, n_features]
                The test examples

        predictions : array of shape = [n_samples, n_classifiers]
                      Predictions of the base classifiers for all test examples

        probabilities : array of shape = [n_samples, n_classifiers, n_classes]
                        Probabilities estimates of each base classifier for all test examples.

        Notes
        ------
        Different than other DES techniques, this method is based on a two stage selection, where
        first the most accurate classifier are selected, then the diversity information is used to get the most
        diverse ensemble for the probability estimation. Hence, the weighting mode is not defined. Also, the
        selected ensemble size is fixed (self.J), so there is no need to use masked arrays in this class.

        Returns
        -------
        predicted_label : array of shape = [n_samples]
                          Predicted class label for each test example.
        """

        if query.ndim < 2:
            query = query.reshape(1, -1)

        if predictions.ndim < 2:
            predictions = predictions.reshape(1, -1)

        if query.shape[0] != predictions.shape[0]:
            raise ValueError('The arrays query and predictions must have the same number of samples. query.shape is {}'
                             'and predictions.shape is {}' .format(query.shape, predictions.shape))

        accuracy, diversity = self.estimate_competence(query, predictions)

        if self.DFP:
            accuracy = accuracy * self.DFP_mask

        selected_classifiers = self.select(accuracy, diversity)
        votes = predictions[np.arange(predictions.shape[0])[:, None], selected_classifiers]
        predicted_label = majority_voting_rule(votes)

        return predicted_label
コード例 #9
0
    def classify_instance(self, query, predictions):
        """Predicts the label of the corresponding query sample.
        Returns the predicted label.

        Parameters
        ----------
        query : array of shape = [n_features]
                The test sample

        predictions : array of shape = [n_samples, n_classifiers]
                      Contains the predictions of all base classifier for all samples in the query array

        Returns
        -------
        predicted_label: The predicted label of the query
        """
        indices = self.select(query)
        votes = np.atleast_2d(predictions[indices])
        predicted_label = majority_voting_rule(votes)
        return predicted_label
コード例 #10
0
ファイル: base.py プロジェクト: qhduan/DESlib
    def classify_instance(self, query, predictions):
        """Predicts the label of the corresponding query sample.

        If self.mode == "selection", the selected ensemble is combined using the
        majority voting rule

        If self.mode == "weighting", all base classifiers are used for classification, however their influence
        in the final decision are weighted according to their estimated competence level. The weighted majority voting
        scheme is used to combine the decisions of the base classifiers.

        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 the
        weighted majority voting rule according to its competence level estimates.

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

        predictions : array of shape = [n_samples, n_classifiers]
                      Contains the predictions of all base classifier for all samples in the query array

        Returns
        -------
        predicted_label: The predicted label of the query
        """
        competences = self.estimate_competence(query)
        if self.mode == "selection":
            indices = self.select(competences)
            votes = np.atleast_2d(predictions[indices])
            predicted_label = majority_voting_rule(votes)

        elif self.mode == "weighting":
            votes = np.atleast_2d(predictions)
            predicted_label = weighted_majority_voting_rule(votes, competences)
        else:
            indices = self.select(competences)
            competences_ensemble = competences[indices]
            votes = np.atleast_2d(predictions[indices])
            predicted_label = weighted_majority_voting_rule(votes, competences_ensemble)

        return predicted_label
コード例 #11
0
    def predict(self, X):
        """Predict the label of each sample in X and returns the predicted
        label.

        Parameters
        ----------
        X : array of shape = [n_samples, n_features]
            The data to be classified

        Returns
        -------
        predicted_labels : array of shape = [n_samples]
                           Predicted class for each sample in X.
        """
        X = check_array(X)
        self._check_is_fitted()

        votes = np.zeros((X.shape[0], self.n_classifiers_ensemble_))
        for clf_index, clf in enumerate(self.ensemble_):
            votes[:, clf_index] = self._encode_base_labels(clf.predict(X))

        predicted_labels = majority_voting_rule(votes).astype(int)

        return self.classes_.take(predicted_labels)
コード例 #12
0
    def classify_with_ds(self,
                         query,
                         predictions,
                         probabilities=None,
                         neighbors=None,
                         distances=None,
                         DFP_mask=None):
        """Predicts the label of the corresponding query sample.

        If self.mode == "selection", the selected ensemble is combined using
        the majority voting rule

        If self.mode == "weighting", all base classifiers are used for
        classification, however their influence in the final decision are
        weighted according to their estimated competence level. The weighted
        majority voting scheme is used to combine the decisions of the
        base classifiers.

        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 the weighted
        majority voting rule according to its competence level estimates.

        Parameters
        ----------
        query : array of shape (n_samples, n_features)
                The test examples.

        predictions : array of shape (n_samples, n_classifiers)
                      Predictions of the base classifier for all test examples.

        probabilities : array of shape (n_samples, n_classifiers, n_classes)
            Probabilities estimates of each base classifier for all test
            examples. (For methods that always require probabilities from
            the base classifiers).

        neighbors : array of shape (n_samples, n_neighbors)
            Indices of the k nearest neighbors according for each test sample.

        distances : array of shape (n_samples, n_neighbors)
            Distances of the k nearest neighbors according for each test
            sample.

        DFP_mask : array of shape (n_samples, n_classifiers)
            Mask containing 1 for the selected base classifier and 0 otherwise.

        Returns
        -------
        predicted_label : array of shape (n_samples)
                          Predicted class label for each test example.
        """
        if query.ndim < 2:
            query = query.reshape(1, -1)

        if predictions.ndim < 2:
            predictions = predictions.reshape(1, -1)

        if query.shape[0] != predictions.shape[0]:
            raise ValueError(
                'The arrays query and predictions must have the same number'
                ' of samples. query.shape is {}'
                'and predictions.shape is {}'.format(query.shape,
                                                     predictions.shape))

        if self.needs_proba:
            competences = self.estimate_competence_from_proba(
                query,
                neighbors=neighbors,
                distances=distances,
                probabilities=probabilities)
        else:
            competences = self.estimate_competence(query,
                                                   neighbors=neighbors,
                                                   distances=distances,
                                                   predictions=predictions)

        if self.DFP:
            competences = competences * DFP_mask

        if self.mode == "selection":
            # The selected_classifiers matrix is used as a mask to remove
            # the predictions of certain base classifiers.
            selected_classifiers = self.select(competences)
            votes = np.ma.MaskedArray(predictions, ~selected_classifiers)
            predicted_label = majority_voting_rule(votes)

        elif self.mode == "weighting":
            votes = np.atleast_2d(predictions)
            predicted_label = weighted_majority_voting_rule(
                votes, competences, np.arange(self.n_classes_))
        else:
            selected_classifiers = self.select(competences)
            votes = np.ma.MaskedArray(predictions, ~selected_classifiers)
            predicted_label = weighted_majority_voting_rule(
                votes, competences, np.arange(self.n_classes_))

        return predicted_label