Checks the class probabilities and reshapes it if not all labels are present in the classifier. Args: proba: The class probabilities of a classifier. known_labels: The class labels known by the classifier. all_labels: All class labels. Returns: Class pr
(proba: np.ndarray, known_labels: Sequence, all_labels: Sequence)
| 28 | |
| 29 | |
| 30 | def check_class_proba(proba: np.ndarray, known_labels: Sequence, all_labels: Sequence) -> np.ndarray: |
| 31 | """ |
| 32 | Checks the class probabilities and reshapes it if not all labels are present in the classifier. |
| 33 | |
| 34 | Args: |
| 35 | proba: The class probabilities of a classifier. |
| 36 | known_labels: The class labels known by the classifier. |
| 37 | all_labels: All class labels. |
| 38 | |
| 39 | Returns: |
| 40 | Class probabilities augmented such that the probability of all classes is present. If the classifier is unaware |
| 41 | of a particular class, all probabilities are zero. |
| 42 | """ |
| 43 | # TODO: rewrite this function using numpy.insert |
| 44 | |
| 45 | label_idx_map = -np.ones(len(all_labels), dtype='int') |
| 46 | |
| 47 | for known_label_idx, known_label in enumerate(known_labels): |
| 48 | # finds the position of label in all_labels |
| 49 | for label_idx, label in enumerate(all_labels): |
| 50 | if np.array_equal(label, known_label): |
| 51 | label_idx_map[label_idx] = known_label_idx |
| 52 | break |
| 53 | |
| 54 | aug_proba = np.hstack((proba, np.zeros(shape=(proba.shape[0], 1)))) |
| 55 | return aug_proba[:, label_idx_map] |
no outgoing calls
no test coverage detected
searching dependent graphs…