Representations and serving

class stagecoachml.PredictionEncoder(estimator, *, method='auto')[source]

Bases: TransformerMixin, BaseEstimator

Expose prediction scores as a two-dimensional representation.

Parameters:
  • estimator (Any) – A scikit-learn predictor, cloned before fitting.

  • method (str) – "auto" chooses predict_proba for classifiers and predict for regressors. Explicit "decision_function" is also supported; margins are never converted into probabilities.

estimator_

Fitted predictor.

method_

Resolved prediction method.

classes_

Class order when the predictor exposes classes.

__init__(estimator, *, method='auto')[source]
fit(X, y, sample_weight=None)[source]

Fit the predictor on upstream data.

Parameters:
  • X (Any) – Upstream feature matrix.

  • y (Any) – Training targets.

  • sample_weight (ndarray | None) – Optional weights forwarded to the predictor.

Returns:

The fitted encoder.

Raises:

ValueError – If the selected prediction method is unsupported.

Return type:

PredictionEncoder

transform(X)[source]

Produce numeric scores in stable column order.

Parameters:

X (Any) – Upstream feature matrix.

Returns:

A matrix of predictions, probabilities, or raw margins.

Return type:

ndarray

set_fit_request(*, sample_weight='$UNCHANGED$')

Configure whether metadata should be requested to be passed to the fit method.

Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with enable_metadata_routing=True (see sklearn.set_config()). Please check the User Guide on how the routing mechanism works.

The options for each parameter are:

  • True: metadata is requested, and passed to fit if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to fit.

  • None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.

  • str: metadata should be passed to the meta-estimator with this given alias instead of the original name.

The default (sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.

Added in version 1.3.

Parameters

sample_weightstr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED

Metadata routing for sample_weight parameter in fit.

Returns

selfobject

The updated object.

class stagecoachml.EncodedState(values, encoder_id, entity_ids=None, feature_times=None)[source]

Bases: object

A versioned, dense representation batch with optional entity and time metadata.

Arrays are copied and made read-only. Store this object alongside its fitted model using ordinary Python serialization. The encoder identifier binds state to a particular model fit, including after serialization.

Parameters:
  • values (ndarray) – Finite numeric matrix, one row per entity or observation.

  • encoder_id (str) – Identifier of the fitted encoder that produced these values.

  • entity_ids (ndarray | None) – Optional unique, nonmissing identifiers in representation order.

  • feature_times (ndarray | None) – Optional feature cutoff times in Unix seconds, scalar or one per representation. These describe input freshness, not write time.

values: ndarray
encoder_id: str
entity_ids: ndarray | None = None
feature_times: ndarray | None = None
rows_for(entity_ids)[source]

Map candidate entity identifiers to representation rows, preserving order.

Parameters:

entity_ids (object) – One entity identifier per candidate; repeats are allowed.

Returns:

Integer row indices suitable for predict_from_state.

Raises:

ValueError – If state has no entity identifiers or a candidate is missing.

Return type:

ndarray

select(indices, *, as_of=None, max_age=None)[source]

Select rows and optionally enforce feature freshness.

Parameters:
  • indices (object) – Positional representation rows, one per candidate.

  • as_of (object) – Request times in Unix seconds, scalar or one per candidate. Providing this rejects representations from the future.

  • max_age (float | None) – Maximum allowed age in seconds; requires as_of and stored feature times. The boundary is inclusive.

Returns:

Representation rows aligned with the candidates.

Raises:

ValueError – If indices, timestamps, or freshness constraints are invalid.

Return type:

ndarray

__init__(values, encoder_id, entity_ids=None, feature_times=None)
class stagecoachml.OnlinePredictor(estimator, encoder_id, n_encoded_features, n_online_features, online_names=None, residual=False)[source]

Bases: object

Serve a fitted downstream estimator without loading its upstream encoder.

Obtain this object from model.predictor_ after fitting. Serialize it with the prepared state for deployment in an independent process.

Parameters:
  • estimator (Any) – Fitted downstream estimator.

  • encoder_id (str) – Accepted fitted encoder identifier.

  • n_encoded_features (int) – Expected representation width.

  • n_online_features (int) – Expected number of online input columns.

  • online_names (tuple | None) – Fitted online column names, if trained on a DataFrame.

  • residual (bool) – Whether to add the first representation column to predictions.

estimator: Any
encoder_id: str
n_encoded_features: int
n_online_features: int
online_names: tuple | None = None
residual: bool = False
predict(X_online, state, *, state_indices=None, as_of=None, max_age=None)[source]

Score online inputs using an explicitly supplied representation batch.

Parameters:
  • X_online (Any) – Numeric online features, one row per candidate.

  • state (EncodedState) – State from the matching encoder fit.

  • state_indices (Any) – Optional representation row per candidate.

  • as_of (Any) – Optional request times in Unix seconds, scalar or per candidate.

  • max_age (float | None) – Optional maximum feature age in seconds.

Returns:

Predicted labels or regression values.

Return type:

ndarray

predict_proba(X_online, state, *, state_indices=None, as_of=None, max_age=None)[source]

Return probabilities in the fitted downstream estimator’s class order.

Parameters:
  • X_online (Any) – Numeric online features, one row per candidate.

  • state (EncodedState) – State from the matching encoder fit.

  • state_indices (Any) – Optional representation row per candidate.

  • as_of (Any) – Optional request times in Unix seconds, scalar or per candidate.

  • max_age (float | None) – Optional maximum feature age in seconds.

Returns:

Probabilities with one column per class, including binary problems.

Return type:

ndarray

__init__(estimator, encoder_id, n_encoded_features, n_online_features, online_names=None, residual=False)