-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added select_good_bootstraps tests and reference, fixed model list in…
…dexing bug
- Loading branch information
1 parent
f84c12a
commit b471c12
Showing
5 changed files
with
48 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,24 @@ | ||
# Utility functions for bootstraps | ||
import numpy as np | ||
from contextualized.easy.wrappers import SKLearnWrapper | ||
|
||
|
||
def select_good_bootstraps(sklearn_wrapper, train_errs, tol=2, **kwargs): | ||
def select_good_bootstraps(sklearn_wrapper: SKLearnWrapper, train_errs: np.ndarray, tol: float = 2) -> SKLearnWrapper: | ||
""" | ||
Select bootstraps that are good for a given model. | ||
Parameters | ||
---------- | ||
sklearn_wrapper : contextualized.easy.wrappers.SKLearnWrapper | ||
train_errs : np.ndarray of shape (n_bootstraps, n_samples, n_outcomes) | ||
tol : float tolerance for the mean of the train_errs | ||
Args: | ||
sklearn_wrapper (contextualized.easy.wrappers.SKLearnWrapper): Wrapper for the sklearn model. | ||
train_errs (np.ndarray): Training errors for each bootstrap (n_bootstraps, n_samples, n_outcomes). | ||
tol (float): Only bootstraps with mean train_errs below tol * min(train_errs) are kept. | ||
Returns | ||
------- | ||
sklearn_wrapper : sklearn_wrapper with only selected bootstraps | ||
Returns: | ||
contextualized.easy.wrappers.SKLearnWrapper: The input model with only selected bootstraps. | ||
""" | ||
if len(train_errs.shape) == 2: | ||
train_errs = train_errs[:, :, None] | ||
|
||
train_errs_by_bootstrap = np.mean(train_errs, axis=(1, 2)) | ||
sklearn_wrapper.models = sklearn_wrapper.models[ | ||
train_errs_by_bootstrap < tol * np.min(train_errs_by_bootstrap) | ||
] | ||
train_errs_min = np.min(train_errs_by_bootstrap) | ||
sklearn_wrapper.models = [model for train_err, model in zip(train_errs_by_bootstrap, sklearn_wrapper.models) if train_err < train_errs_min * tol] | ||
sklearn_wrapper.n_bootstraps = len(sklearn_wrapper.models) | ||
return sklearn_wrapper |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters