1 2 ```from typing import Any ``` 2 3 2 ```import numpy as np ``` 4 5 2 ```from snorkel.labeling.model.base_labeler import BaseLabeler ``` 6 7 8 2 ```class RandomVoter(BaseLabeler): ``` 9 ``` """Random vote label model. ``` 10 11 ``` Example ``` 12 ``` ------- ``` 13 ``` >>> L = np.array([[0, 0, -1], [-1, 0, 1], [1, -1, 0]]) ``` 14 ``` >>> random_voter = RandomVoter() ``` 15 ``` >>> predictions = random_voter.predict_proba(L) ``` 16 ``` """ ``` 17 18 2 ``` def predict_proba(self, L: np.ndarray) -> np.ndarray: ``` 19 ``` """ ``` 20 ``` Assign random votes to the data points. ``` 21 22 ``` Parameters ``` 23 ``` ---------- ``` 24 ``` L ``` 25 ``` An [n, m] matrix of labels ``` 26 27 ``` Returns ``` 28 ``` ------- ``` 29 ``` np.ndarray ``` 30 ``` A [n, k] array of probabilistic labels ``` 31 32 ``` Example ``` 33 ``` ------- ``` 34 ``` >>> L = np.array([[0, 0, -1], [-1, 0, 1], [1, -1, 0]]) ``` 35 ``` >>> random_voter = RandomVoter() ``` 36 ``` >>> predictions = random_voter.predict_proba(L) ``` 37 ``` """ ``` 38 2 ``` n = L.shape[0] ``` 39 2 ``` Y_p = np.random.rand(n, self.cardinality) ``` 40 2 ``` Y_p /= Y_p.sum(axis=1).reshape(-1, 1) ``` 41 2 ``` return Y_p ``` 42 43 44 2 ```class MajorityClassVoter(BaseLabeler): ``` 45 ``` """Majority class label model.""" ``` 46 47 2 ``` def fit( # type: ignore ``` 48 ``` self, balance: np.ndarray, *args: Any, **kwargs: Any ``` 49 ``` ) -> None: ``` 50 ``` """Train majority class model. ``` 51 52 ``` Set class balance for majority class label model. ``` 53 54 ``` Parameters ``` 55 ``` ---------- ``` 56 ``` balance ``` 57 ``` A [k] array of class probabilities ``` 58 ``` """ ``` 59 2 ``` self.balance = balance ``` 60 61 2 ``` def predict_proba(self, L: np.ndarray) -> np.ndarray: ``` 62 ``` """Predict probabilities using majority class. ``` 63 64 ``` Assign majority class vote to each datapoint. ``` 65 ``` In case of multiple majority classes, assign equal probabilities among them. ``` 66 67 68 ``` Parameters ``` 69 ``` ---------- ``` 70 ``` L ``` 71 ``` An [n, m] matrix of labels ``` 72 73 ``` Returns ``` 74 ``` ------- ``` 75 ``` np.ndarray ``` 76 ``` A [n, k] array of probabilistic labels ``` 77 78 ``` Example ``` 79 ``` ------- ``` 80 ``` >>> L = np.array([[0, 0, -1], [-1, 0, 1], [1, -1, 0]]) ``` 81 ``` >>> maj_class_voter = MajorityClassVoter() ``` 82 ``` >>> maj_class_voter.fit(balance=np.array([0.8, 0.2])) ``` 83 ``` >>> maj_class_voter.predict_proba(L) ``` 84 ``` array([[1., 0.], ``` 85 ``` [1., 0.], ``` 86 ``` [1., 0.]]) ``` 87 ``` """ ``` 88 2 ``` n = L.shape[0] ``` 89 2 ``` Y_p = np.zeros((n, self.cardinality)) ``` 90 2 ``` max_classes = np.where(self.balance == max(self.balance)) ``` 91 2 ``` for c in max_classes: ``` 92 2 ``` Y_p[:, c] = 1.0 ``` 93 2 ``` Y_p /= Y_p.sum(axis=1).reshape(-1, 1) ``` 94 2 ``` return Y_p ``` 95 96 97 2 ```class MajorityLabelVoter(BaseLabeler): ``` 98 ``` """Majority vote label model.""" ``` 99 100 2 ``` def predict_proba(self, L: np.ndarray) -> np.ndarray: ``` 101 ``` """Predict probabilities using majority vote. ``` 102 103 ``` Assign vote by calculating majority vote across all labeling functions. ``` 104 ``` In case of ties, non-integer probabilities are possible. ``` 105 106 ``` Parameters ``` 107 ``` ---------- ``` 108 ``` L ``` 109 ``` An [n, m] matrix of labels ``` 110 111 ``` Returns ``` 112 ``` ------- ``` 113 ``` np.ndarray ``` 114 ``` A [n, k] array of probabilistic labels ``` 115 116 ``` Example ``` 117 ``` ------- ``` 118 ``` >>> L = np.array([[0, 0, -1], [-1, 0, 1], [1, -1, 0]]) ``` 119 ``` >>> maj_voter = MajorityLabelVoter() ``` 120 ``` >>> maj_voter.predict_proba(L) ``` 121 ``` array([[1. , 0. ], ``` 122 ``` [0.5, 0.5], ``` 123 ``` [0.5, 0.5]]) ``` 124 ``` """ ``` 125 2 ``` n, m = L.shape ``` 126 2 ``` Y_p = np.zeros((n, self.cardinality)) ``` 127 2 ``` for i in range(n): ``` 128 2 ``` counts = np.zeros(self.cardinality) ``` 129 2 ``` for j in range(m): ``` 130 2 ``` if L[i, j] != -1: ``` 131 2 ``` counts[L[i, j]] += 1 ``` 132 2 ``` Y_p[i, :] = np.where(counts == max(counts), 1, 0) ``` 133 2 ``` Y_p /= Y_p.sum(axis=1).reshape(-1, 1) ``` 134 2 ``` return Y_p ```

Read our documentation on viewing source code .