Skip to content

check_cv

yohou.model_selection.split.check_cv(cv=5, forecasting_horizon=1)

Input checker utility for building a cross-validator.

Parameters

Name Type Description Default
cv int, cross-validation generator or an iterable

Determines the cross-validation splitting strategy. Possible inputs for cv are: - None, to use the default 5-fold time series cross validation, - integer, to specify the number of folds in a time series Splitter, - BaseSplitter instance, - An iterable yielding (train, test) splits as arrays of indices.

5
forecasting_horizon int >= 1

Horizon to forecast recursively.

1

Returns

Name Type Description
checked_cv a cross-validator instance.

The return value is a cross-validator which generates the train/test splits via the split method.

Source Code

Show/Hide source
def check_cv(
    cv: int | BaseSplitter | Iterable[tuple[np.ndarray[Any, Any], np.ndarray[Any, Any]]] | None = 5,
    forecasting_horizon: int = 1,
) -> BaseSplitter:
    """Input checker utility for building a cross-validator.

    Parameters
    ----------
    cv : int, cross-validation generator or an iterable, default=5
        Determines the cross-validation splitting strategy.
        Possible inputs for cv are:
        - None, to use the default 5-fold time series cross validation,
        - integer, to specify the number of folds in a time series `Splitter`,
        - `BaseSplitter` instance,
        - An iterable yielding (train, test) splits as arrays of indices.
    forecasting_horizon : int >= 1, default=1
        Horizon to forecast recursively.

    Returns
    -------
    checked_cv : a cross-validator instance.
        The return value is a cross-validator which generates the train/test
        splits via the ``split`` method.

    """
    cv = 5 if cv is None else cv
    if isinstance(cv, numbers.Integral):
        return ExpandingWindowSplitter(int(cv), test_size=forecasting_horizon)

    if not isinstance(cv, BaseSplitter):
        raise ValueError("Expected cv as an integer or a splitter object (from yohou.model_selection).")

    return cv