sklink.utils.validation
Validation of the population table X and of the database parameter.
check_relational_X is the single place where X is interpreted: it resolves the key and cutoff columns (names or positions), separates the sample feature columns and records n_features_in_ and feature_names_in_.
Classes:
- ValidatedX:
Xsplit into entity keys, cutoffs and sample feature columns.
Functions:
- check_database: Check that
databaseis a usableDatabase. - check_relational_X: Validate the population table
Xof a relational estimator. - check_temporal_setup: Warn when a validation set-up does not match the time mode of a model.
sklink.utils.validation.ValidatedX
ValidatedX(n_samples: int, keys: list[np.ndarray] | None, key_missing: np.ndarray | None, cutoff: np.ndarray | None, feature_names: list[str], feature_positions: list[int], features: list[RawColumn]) -> NoneX split into entity keys, cutoffs and sample feature columns.
Attributes:
- n_samples (
int): Number of rows. - keys (
list of ndarray or None): Key columns in the order of the entity's primary key (relational mode). - key_missing (
ndarray of bool or None): Rows whose key has a missing component. - cutoff (
ndarray of int64 or None): Cutoff of every row in nanoseconds since the epoch (temporal mode). - feature_names (
list of str): Names of the sample feature columns. - feature_positions (
list of int): Positions of the sample feature columns inX. - features (
list of RawColumn): Sample feature columns.
Functions:
- take: Return the rows
rowsofX, validated like the whole.
sklink.utils.validation.ValidatedX.cutoff
cutoff: np.ndarray | Nonesklink.utils.validation.ValidatedX.feature_names
feature_names: list[str]sklink.utils.validation.ValidatedX.feature_positions
feature_positions: list[int]sklink.utils.validation.ValidatedX.features
features: list[RawColumn]sklink.utils.validation.ValidatedX.key_missing
key_missing: np.ndarray | Nonesklink.utils.validation.ValidatedX.keys
keys: list[np.ndarray] | Nonesklink.utils.validation.ValidatedX.n_samples
n_samples: intsklink.utils.validation.ValidatedX.take
take(rows: slice) -> ValidatedXReturn the rows rows of X, validated like the whole.
sklink.utils.validation.check_database
check_database(database: Any, *, entity: str | None = None) -> DatabaseCheck that database is a usable Database.
Parameters:
- database (
object): Value of an estimator'sdatabaseparameter. - entity (
str): Table that must exist in the database.
Returns:
- database (
Database):
Raises:
DetachedDatabaseError: If the estimator was detached withsklink.detach.TypeError: Ifdatabaseis not aDatabase.SchemaError: Ifentityis not a table of the database.
sklink.utils.validation.check_relational_X
check_relational_X(estimator: Any, X: Any, *, reset: bool, database: Database | None = None, entity: str | None = None, key: Any = None, time: Any = None) -> ValidatedXValidate the population table X of a relational estimator.
Parameters:
- estimator (
estimator): The estimator;n_features_in_andfeature_names_in_are recorded on it whenresetis true and checked otherwise. - X (
DataFrame or array-like of shape (n_samples, n_columns)): Population table. - reset (
bool): True infit, false intransformandpredict. - database (
Database or None):Nonemeans flat mode: every column is a feature. - entity (
str or None): The estimator'sentityparameter. - key (
(str, int, sequence or None)): The estimator'skeyparameter. - time (
(str, int or None)): The estimator'stimeparameter.
Returns:
- validated (
ValidatedX):
sklink.utils.validation.check_temporal_setup
check_temporal_setup(estimator: Any, cv: Any = None) -> NoneWarn when a validation set-up does not match the time mode of a model.
TemporalCutoffSplit never checks the estimator it validates. Call this function before a cross-validation or a search to catch the combinations that make its scores meaningless:
- a temporal split of a relational model in static mode (
time=None), which reads the rows after each cutoff and so scores optimistically; - a temporal model and a split that read different cutoff columns.
Parameters:
- estimator (
estimator): A relational estimator, or a pipeline or meta-estimator that holds some. Every nested estimator with adatabaseis inspected. - cv (
cross-validation generator): The splitter that will validateestimator.Nonechecks nothing.
Warns:
SklinkWarning: For each combination listed above.
Examples:
>>> import warnings
>>> from sklink.datasets import load_shop
>>> from sklink.feature_extraction import RelationalFeaturizer
>>> from sklink.model_selection import TemporalCutoffSplit
>>> from sklink.utils.validation import check_temporal_setup
>>> static = RelationalFeaturizer(load_shop().database, entity="customers")
>>> with warnings.catch_warnings(record=True) as caught:
... warnings.simplefilter("always")
... check_temporal_setup(static, TemporalCutoffSplit(time="cutoff"))
>>> print(caught[0].message)
TemporalCutoffSplit validates a static model: the estimator has time=None,
so it reads the rows after each cutoff and the scores are optimistic. Set
time='cutoff' on it.