Base class for query-by-committee setup. Args: learner_list: List of ActiveLearner objects to form committee. query_strategy: Function to query labels. on_transformed: Whether to transform samples with the pipeline defined by each learner's estimator when
| 208 | |
| 209 | |
| 210 | class BaseCommittee(ABC, BaseEstimator): |
| 211 | """ |
| 212 | Base class for query-by-committee setup. |
| 213 | Args: |
| 214 | learner_list: List of ActiveLearner objects to form committee. |
| 215 | query_strategy: Function to query labels. |
| 216 | on_transformed: Whether to transform samples with the pipeline defined by each learner's estimator |
| 217 | when applying the query strategy. |
| 218 | """ |
| 219 | def __init__(self, learner_list: List[BaseLearner], query_strategy: Callable, on_transformed: bool = False) -> None: |
| 220 | assert type(learner_list) == list, 'learners must be supplied in a list' |
| 221 | |
| 222 | self.learner_list = learner_list |
| 223 | self.query_strategy = query_strategy |
| 224 | self.on_transformed = on_transformed |
| 225 | # TODO: update training data when using fit() and teach() methods |
| 226 | self.X_training = None |
| 227 | |
| 228 | def __iter__(self) -> Iterator[BaseLearner]: |
| 229 | for learner in self.learner_list: |
| 230 | yield learner |
| 231 | |
| 232 | def __len__(self) -> int: |
| 233 | return len(self.learner_list) |
| 234 | |
| 235 | def _add_training_data(self, X: modALinput, y: modALinput) -> None: |
| 236 | """ |
| 237 | Adds the new data and label to the known data for each learner, but does not retrain the model. |
| 238 | Args: |
| 239 | X: The new samples for which the labels are supplied by the expert. |
| 240 | y: Labels corresponding to the new instances in X. |
| 241 | Note: |
| 242 | If the learners have been fitted, the features in X have to agree with the training samples which the |
| 243 | classifier has seen. |
| 244 | """ |
| 245 | for learner in self.learner_list: |
| 246 | learner._add_training_data(X, y) |
| 247 | |
| 248 | def _fit_to_known(self, bootstrap: bool = False, **fit_kwargs) -> None: |
| 249 | """ |
| 250 | Fits all learners to the training data and labels provided to it so far. |
| 251 | Args: |
| 252 | bootstrap: If True, each estimator is trained on a bootstrapped dataset. Useful when |
| 253 | using bagging to build the ensemble. |
| 254 | **fit_kwargs: Keyword arguments to be passed to the fit method of the predictor. |
| 255 | """ |
| 256 | for learner in self.learner_list: |
| 257 | learner._fit_to_known(bootstrap=bootstrap, **fit_kwargs) |
| 258 | |
| 259 | def _fit_on_new(self, X: modALinput, y: modALinput, bootstrap: bool = False, **fit_kwargs) -> None: |
| 260 | """ |
| 261 | Fits all learners to the given data and labels. |
| 262 | Args: |
| 263 | X: The new samples for which the labels are supplied by the expert. |
| 264 | y: Labels corresponding to the new instances in X. |
| 265 | bootstrap: If True, the method trains the model on a set bootstrapped from X. |
| 266 | **fit_kwargs: Keyword arguments to be passed to the fit method of the predictor. |
| 267 | """ |
nothing calls this directly
no outgoing calls
no test coverage detected
searching dependent graphs…