-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Validate feature support vector 🚀 closes #14
- Loading branch information
Showing
4 changed files
with
124 additions
and
21 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
from dataclasses import dataclass | ||
|
||
import numpy as np | ||
from fseval.pipeline.estimator import Estimator | ||
from fseval.types import IncompatibilityError | ||
from omegaconf import MISSING | ||
from sklearn.feature_selection import SelectFromModel | ||
|
||
from .._experiment import Experiment | ||
from ._config import RankAndValidatePipeline | ||
from ._subset_validator import SubsetValidator | ||
|
||
|
||
@dataclass | ||
class SupportValidator(SubsetValidator): | ||
"""Validates a feature support vector, i.e. a feature subset.""" | ||
|
||
bootstrap_state: int = MISSING | ||
n_features_to_select: int = -1 # disable | ||
|
||
def _prepare_data(self, X, y): | ||
feature_support = getattr(self.ranker, "feature_support_", None) | ||
assert feature_support is not None, "ranker must have support attribute" | ||
assert isinstance( | ||
feature_support, np.ndarray | ||
), "feature support array must be a numpy ndarray" | ||
|
||
# make sure support vector is boolean-valued | ||
feature_support = feature_support.astype(bool) | ||
self.subset_size = np.sum(feature_support) | ||
|
||
# select feature subset | ||
X = X[:, feature_support] | ||
|
||
return X, y | ||
|
||
@property | ||
def _cache_filename(self): | ||
override = f"bootstrap_state={self.bootstrap_state}" | ||
filename = f"support[{override}].pickle" | ||
|
||
return filename | ||
|
||
def score(self, X, y, **kwargs): | ||
score = super(SubsetValidator, self).score(X, y) | ||
score["subset_size"] = self.subset_size | ||
score["fit_time"] = self.validator.fit_time_ | ||
return score |
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