Skip to content

cross_val_score

yohou.model_selection.validation.cross_val_score(forecaster, y, X_actual=None, forecasting_horizon=1, *, X_future=None, X_forecast=None, scoring, cv=5, predict_forecasting_horizon=None, predict_stride=None, n_jobs=None, verbose=0, pre_dispatch='2*n_jobs', error_score=np.nan)

Evaluate a forecaster by cross-validation and return test scores.

Parameters

Name Type Description Default
forecaster BaseForecaster

The forecaster to evaluate.

required
y DataFrame

Target time series with a "time" column.

required
X_actual DataFrame or None

Actual feature observations with a "time" column.

None
forecasting_horizon int

Number of time steps to forecast.

1
X_future DataFrame or None

Known future features with a "time" column.

None
X_forecast DataFrame or None

External forecasts with "vintage_time" and "time" columns.

None
scoring BaseScorer

Single scorer used to evaluate predictions. Dicts of scorers are rejected; use cross_validate for multi-metric evaluation.

required
cv int, BaseSplitter, or None

Cross-validation splitting strategy.

5
predict_forecasting_horizon int or None

Override forecasting horizon for observe_predict.

None
predict_stride int or None

Override stride for observe_predict.

None
n_jobs int or None

Number of parallel jobs.

None
verbose int

Verbosity level.

0
pre_dispatch str or int

Controls pre-dispatched jobs for parallel execution.

"2*n_jobs"
error_score float or 'raise'

Value to assign if an error occurs during fitting.

np.nan

Returns

Type Description
DataFrame

DataFrame with columns split (int, 0-indexed fold identifier) and score (float, test score per fold).

Source Code

Show/Hide source
def cross_val_score(
    forecaster: BaseForecaster,
    y: pl.DataFrame,
    X_actual: pl.DataFrame | None = None,
    forecasting_horizon: int = 1,
    *,
    X_future: pl.DataFrame | None = None,
    X_forecast: pl.DataFrame | None = None,
    scoring: BaseScorer,
    cv: int | BaseSplitter | None = 5,
    predict_forecasting_horizon: int | None = None,
    predict_stride: int | None = None,
    n_jobs: int | None = None,
    verbose: int = 0,
    pre_dispatch: str | int = "2*n_jobs",
    error_score: float | str = np.nan,
) -> pl.DataFrame:
    """Evaluate a forecaster by cross-validation and return test scores.

    Parameters
    ----------
    forecaster : BaseForecaster
        The forecaster to evaluate.
    y : pl.DataFrame
        Target time series with a ``"time"`` column.
    X_actual : pl.DataFrame or None, default=None
        Actual feature observations with a ``"time"`` column.
    forecasting_horizon : int, default=1
        Number of time steps to forecast.
    X_future : pl.DataFrame or None, default=None
        Known future features with a ``"time"`` column.
    X_forecast : pl.DataFrame or None, default=None
        External forecasts with ``"vintage_time"`` and ``"time"``
        columns.
    scoring : BaseScorer
        Single scorer used to evaluate predictions.  Dicts of scorers
        are rejected; use ``cross_validate`` for multi-metric
        evaluation.
    cv : int, BaseSplitter, or None, default=5
        Cross-validation splitting strategy.
    predict_forecasting_horizon : int or None, default=None
        Override forecasting horizon for ``observe_predict``.
    predict_stride : int or None, default=None
        Override stride for ``observe_predict``.
    n_jobs : int or None, default=None
        Number of parallel jobs.
    verbose : int, default=0
        Verbosity level.
    pre_dispatch : str or int, default="2*n_jobs"
        Controls pre-dispatched jobs for parallel execution.
    error_score : float or "raise", default=np.nan
        Value to assign if an error occurs during fitting.

    Returns
    -------
    pl.DataFrame
        DataFrame with columns ``split`` (int, 0-indexed fold
        identifier) and ``score`` (float, test score per fold).
    """
    if isinstance(scoring, dict):
        raise ValueError(
            "cross_val_score does not accept dict scoring. Use cross_validate for multi-metric evaluation."
        )

    cv_results = cast(
        pl.DataFrame,
        cross_validate(
            forecaster,
            y,
            X_actual=X_actual,
            forecasting_horizon=forecasting_horizon,
            X_future=X_future,
            X_forecast=X_forecast,
            scoring=scoring,
            cv=cv,
            n_jobs=n_jobs,
            verbose=verbose,
            pre_dispatch=pre_dispatch,
            predict_forecasting_horizon=predict_forecasting_horizon,
            predict_stride=predict_stride,
            error_score=error_score,
        ),
    )
    return cv_results.select("split", pl.col("test_score").alias("score"))

Tutorials

The following example notebooks use this component:

  • Cross-Validation for Time Series


    Evaluation-Search

    Evaluate forecasters with cross_val_score, cross_validate, and cross_val_predict using temporal splitters.

    View · Open in marimo