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
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