class PredictionsMonitoring(object): def __init__(self): self.predictions = None self.proba_barplot = None def add_fold(self, predictions): if self.predictions is None: ids = Ids(deepcopy(predictions.ids.ids), deepcopy(predictions.ids.idents), deepcopy(predictions.ids.timestamps)) self.predictions = Predictions(deepcopy(predictions.values), ids, predictions.info.multiclass, deepcopy(predictions.all_probas), deepcopy(predictions.probas), deepcopy(predictions.scores), deepcopy(predictions.ground_truth)) else: self.predictions.union(predictions) def final_computations(self): # PredictionsBarplots only for probabilist binary models pred_info = self.predictions.info if not pred_info.multiclass and pred_info.with_probas: self.proba_barplot = ProbaBarplot(pred_info.with_ground_truth) self.proba_barplot.add_fold(self.predictions) def display(self, directory): if self.proba_barplot is not None: self.proba_barplot.display(directory)
def apply_pipeline(self, instances): num_instances = instances.num_instances() if num_instances == 0: return Predictions([], [], [], [], instances.ids) features = instances.features.get_values() predictions = self._predict(features) all_probas, probas = self._get_predicted_probas( features, num_instances) scores = self._get_predicted_scores(features, num_instances) return Predictions(list(predictions), all_probas, probas, scores, instances.ids)
def add_fold(self, predictions): if self.predictions is None: ids = Ids(deepcopy(predictions.ids.ids), deepcopy(predictions.ids.idents), deepcopy(predictions.ids.timestamps)) self.predictions = Predictions(deepcopy(predictions.values), ids, predictions.info.multiclass, deepcopy(predictions.all_probas), deepcopy(predictions.probas), deepcopy(predictions.scores), deepcopy(predictions.ground_truth)) else: self.predictions.union(predictions)
def apply_pipeline(self, instances): num_instances = instances.num_instances() if num_instances == 0: return Predictions([], [], [], [], instances.ids) features = instances.features.get_values() preprocessed_features = self.scaler.transform(features) predicted_scores = np.apply_along_axis(predict_score, 1, preprocessed_features, self.c, self.r) predicted_labels = predicted_scores > 0 predicted_proba = [None for i in range(num_instances)] all_predicted_proba = [None for i in range(num_instances)] return Predictions(predicted_labels, all_predicted_proba, predicted_proba, predicted_scores, instances.ids)
def apply_pipeline(self, instances): num_instances = instances.num_instances() if num_instances == 0: return Predictions([], instances.ids, False) features = instances.features.get_values() preprocessed_features = self.scaler.transform(features) predicted_scores = np.apply_along_axis(predict_score, 1, preprocessed_features, self.c, self.r) predicted_labels = predicted_scores > 0 return Predictions(predicted_labels, instances.ids, False, scores=predicted_scores)
def apply_pipeline(self, instances): num_instances = instances.num_instances() if num_instances == 0: return Predictions([], instances.ids, self.conf.multiclass) features = instances.features.get_values() predictions = self._predict(features) all_probas, probas = self._get_predicted_probas( features, num_instances) scores = self._get_predicted_scores(features, num_instances) return Predictions(predictions, instances.ids, self.conf.multiclass, all_probas=all_probas, probas=probas, scores=scores)
def _predict_matrix(self, matrix, instances_ids): values = self._predict_values(matrix) all_probas, probas = self._predict_probas(matrix) all_scores, scores = self._predict_scores(matrix) return Predictions(values, instances_ids, self.conf.multiclass, all_probas=all_probas, probas=probas, all_scores=all_scores, scores=scores)
def _detection(self, kind, classifier, instances, fold_id): global_exp, detection_exps = self._create_detection_exp( kind, classifier.conf, fold_id=fold_id) if not all(detection_exp.exp_conf.dataset_conf.has_ground_truth == detection_exps[0].exp_conf.dataset_conf.has_ground_truth for detection_exp in detection_exps): raise InvalidValidationDatasets( 'All the test datasets must contain ground truth ' 'annotations, or none of them.') if fold_id is None: exp = global_exp if global_exp is not None else detection_exps[0] self._set_detection_exp(kind, exp) predictions = None prediction_time = None for detection_exp in detection_exps: detection_exp.run(instances, classifier) if predictions is None: predictions = Predictions.deepcopy(detection_exp.predictions) predictions = detection_exp.predictions prediction_time = detection_exp.prediction_time else: predictions.union(detection_exp.predictions) prediction_time += detection_exp.prediction_time if global_exp is not None: global_exp.set_predictions(predictions, prediction_time) return predictions, prediction_time
def add_fold(self, predictions): self.num_added_folds += 1 if self.num_added_folds > self.num_folds: raise AddTooManyFolds() if self.predictions is None: if self.num_folds > 1: self.predictions = Predictions.deepcopy(predictions) else: self.predictions = predictions else: self.predictions.union(predictions)
def _predict_matrix(self, matrix, instances_ids): all_probas, probas = self._predict_probas(matrix) if all_probas is None and probas is None: all_scores, scores = self._predict_scores(matrix) if all_scores is None and scores is None: values = self._predict_values(matrix) else: values = self._predict_from_scores(matrix, all_scores, scores) else: all_scores, scores = None, None values = self._predict_from_probas(matrix, all_probas, probas) if probas is not None: probas = probas[:, 1] return Predictions(values, instances_ids, self.conf.multiclass, all_probas=all_probas, probas=probas, all_scores=all_scores, scores=scores)
def add_fold(self, predictions): if self.predictions is None: self.predictions = Predictions.deepcopy(predictions) else: self.predictions.union(predictions)
def apply_pipeline(self, instances): num_instances = instances.num_instances() if num_instances == 0: return Predictions(np.array([]), instances.ids, self.conf.multiclass) return self._predict(instances.features, instances.ids)