openquant.sb_bagging
Compiled from Rust (crates/pyopenquant). Signatures and types are from the shipped .pyi stubs; descriptions are the functions’ own docstrings, as help() shows them. All Python modules.
Classes
SequentiallyBootstrappedBaggingClassifier
class SequentiallyBootstrappedBaggingClassifier(
n_estimators: int = 10,
max_samples: float = 1.0,
max_features: float = 1.0,
bootstrap_features: bool = False,
oob_score: bool = False,
random_state: int = 42,
)Sequentially bootstrapped bagging classifier (AFML sections 4.5 and 6.3), as an object.
It has fit, predict and predict_proba, so it can be fitted on one set of rows and scored on another (for example, out of fold under purged cross-validation).
predict_proba returns one [P(y = 0), P(y = 1)] row per input row, in the order of classes_ ([0, 1]), as scikit-learn classifiers do. The probability of class 1 is the share of estimators voting 1, so predict is 1 exactly when it is at least 0.5.
**The sampling is real; the base learner is a sketch.** Each estimator draws max_samples labels with the sequential bootstrap (AFML section 4.5), which favours labels that overlap little with those already drawn, draws max_features feature indices and keeps only the first, and fits a fixed one-feature learner on the drawn rows: a stump whose threshold is the weighted mean of the feature over the drawn rows, predicting 1 on whichever side had the higher weighted rate of y == 1. The out-of-bag score stays optimistic when labels overlap, because a held-out label still overlaps drawn labels in time; score the model under purged cross-validation instead.
Parameters
n_estimators: int, default 10Number of estimators; must be positive (checked by
fit).max_samples: float, default 1.0Labels drawn per estimator, as a fraction of the rows of
x(rounded down); must be positive, at most 1.0, and resolve to at least one row (checked byfit).max_features: float, default 1.0Feature indices drawn per estimator, as a fraction of the columns of
x(rounded down); must be positive, at most 1.0, and resolve to at least one column (checked byfit). Only the first drawn index is used.bootstrap_features: bool, default FalseDraw feature indices with replacement instead of as a random permutation.
oob_score: bool, default FalseCompute the out-of-bag accuracy (
oob_score_) duringfit.random_state: int, default 42Seed; the same seed and inputs reproduce a fit.
SequentiallyBootstrappedBaggingClassifier.classes_
classes_: list[int]The class labels, [0, 1]: the column order of predict_proba.
SequentiallyBootstrappedBaggingClassifier.estimators_samples_
estimators_samples_: list[list[int]]Row indices each estimator was trained on, one list per estimator.
SequentiallyBootstrappedBaggingClassifier.fit
fit(
x: Sequence[Sequence[float]],
y: Sequence[int],
ind_mat: Sequence[Sequence[int]],
sample_weight: Sequence[float] | None = None,
) -> SelfFits the ensemble and returns the model, so calls can be chained.
Parameters
x: list[list[float]]Features, one inner list per label (observation) and one column per feature.
y: list[int]Class per row of
x: 1 is the positive class and any other value counts as 0.ind_mat: list[list[int]]Bars x labels indicator matrix (as
sampling.get_ind_matrixreturns); its columns correspond one-to-one with the rows ofx.sample_weight: list[float] | None, default NoneOne non-negative, finite weight per row of
x, not all zero; None weighs rows equally.
Returns
- SequentiallyBootstrappedBaggingClassifier
The fitted model (
self).
Raises
- ValueError
If the inputs are empty, ragged or disagree on the number of rows, or a setting or sample weight is out of range. A failed
fitleaves the model unfitted.
SequentiallyBootstrappedBaggingClassifier.n_estimators
n_estimators: intNumber of estimators set at construction.
SequentiallyBootstrappedBaggingClassifier.n_features_in_
n_features_in_: int | NoneColumn count seen by the last successful fit; None before one.
SequentiallyBootstrappedBaggingClassifier.oob_score_
oob_score_: float | NoneOut-of-bag accuracy from the last fit when oob_score=True, else None.
SequentiallyBootstrappedBaggingClassifier.predict
predict(x: Sequence[Sequence[float]]) -> list[int]Predicted class (0 or 1) for each row of x, by majority vote; ties go to 1.
Parameters
x: list[list[float]]Rows to score, with the column count of the matrix passed to
fit.
Returns
- list[int]
One class per row of
x.
Raises
- ValueError
If the model has not been fitted, or
xis empty, ragged, or has a different number of columns from the matrix passed tofit.
SequentiallyBootstrappedBaggingClassifier.predict_proba
predict_proba(x: Sequence[Sequence[float]]) -> list[list[float]][P(y = 0), P(y = 1)] for each row of x; the columns follow classes_.
P(y = 1) is the share of estimators voting 1.
Parameters
x: list[list[float]]Rows to score, with the column count of the matrix passed to
fit.
Returns
- list[list[float]]
One
[P(y = 0), P(y = 1)]row per row ofx; each row sums to 1.
Raises
- ValueError
If the model has not been fitted, or
xis empty, ragged, or has a different number of columns from the matrix passed tofit.
SequentiallyBootstrappedBaggingClassifier.random_state
random_state: intSeed set at construction.
Functions
fit_predict_sb_classifier
fit_predict_sb_classifier(
x: Sequence[Sequence[float]],
y: Sequence[int],
ind_mat: Sequence[Sequence[int]],
n_estimators: int = 10,
max_samples: float = 1.0,
max_features: float = 1.0,
random_state: int = 42,
sample_weight: Sequence[float] | None = None,
) -> AnyFits a sequentially bootstrapped bagging classifier and predicts the rows it was fitted on.
A one-call shortcut for SequentiallyBootstrappedBaggingClassifier(..., oob_score=True) followed by fit(x, y, ind_mat) and predict(x); use the class to score rows it was not fitted on. Predictions are a majority vote of the estimators, ties going to 1.
**The sampling is real; the base learner is a sketch.** Each estimator draws max_samples labels with the sequential bootstrap (AFML section 4.5), which favours labels that overlap little with those already drawn, draws max_features feature indices and keeps only the first, and fits a fixed one-feature learner on the drawn rows: a stump whose threshold is the weighted mean of the feature over the drawn rows, predicting 1 on whichever side had the higher weighted rate of y == 1. The out-of-bag score stays optimistic when labels overlap, because a held-out label still overlaps drawn labels in time; score the model under purged cross-validation instead.
Parameters
x: list[list[float]]Features, one inner list per label (observation) and one column per feature.
y: list[int]Class per row of
x: 1 is the positive class and any other value counts as 0, so map a -1/+1 encoding to 0/1 first.ind_mat: list[list[int]]Bars x labels indicator matrix (as
sampling.get_ind_matrixreturns): one inner list per bar, one 0/1 entry per label. Its columns correspond one-to-one with the rows ofx.n_estimators: int, default 10Number of estimators; must be positive.
max_samples: float, default 1.0Labels drawn per estimator, as a fraction of the rows of
x(rounded down); must be positive, at most 1.0, and resolve to at least one row.max_features: float, default 1.0Feature indices drawn per estimator, as a fraction of the columns of
x(rounded down); must be positive, at most 1.0, and resolve to at least one column. Only the first drawn index is used, so values above one column change only the random stream.random_state: int, default 42Seed; the same seed and inputs reproduce a fit.
sample_weight: list[float] | None, default NoneOne non-negative, finite weight per row of
x, not all zero; None weighs rows equally. A row drawn twice counts twice.
Returns
- dict[str, Any]
predictions: the predicted class (0 or 1) for each row ofx(in-sample).oob_score: out-of-bag accuracy over the rows that at least one estimator did not draw, each predicted by the estimators that did not draw it; None if every row was drawn by every estimator.
Raises
- ValueError
If
xis empty or ragged;y,sample_weightor the rows ofind_matdo not match the rows ofx, orind_matis empty;n_estimatorsis zero;max_samplesormax_featuresis out of range; or a sample weight is negative or not finite, or all weights are zero.
fit_predict_sb_regressor
fit_predict_sb_regressor(
x: Sequence[Sequence[float]],
y: Sequence[float],
ind_mat: Sequence[Sequence[int]],
n_estimators: int = 10,
max_samples: float = 1.0,
max_features: float = 1.0,
random_state: int = 42,
sample_weight: Sequence[float] | None = None,
) -> AnyFits a sequentially bootstrapped bagging regressor and predicts the rows it was fitted on.
Predictions are the mean of the estimators' predictions.
**The sampling is real; the base learner is a sketch.** Each estimator draws max_samples labels with the sequential bootstrap (AFML section 4.5), which favours labels that overlap little with those already drawn, draws max_features feature indices and keeps only the first, and fits a fixed one-feature learner on the drawn rows: a weighted least-squares line. The out-of-bag score stays optimistic when labels overlap, because a held-out label still overlaps drawn labels in time; score the model under purged cross-validation instead.
Parameters
x: list[list[float]]Features, one inner list per label (observation) and one column per feature.
y: list[float]Target per row of
x.ind_mat: list[list[int]]Bars x labels indicator matrix (as
sampling.get_ind_matrixreturns): one inner list per bar, one 0/1 entry per label. Its columns correspond one-to-one with the rows ofx.n_estimators: int, default 10Number of estimators; must be positive.
max_samples: float, default 1.0Labels drawn per estimator, as a fraction of the rows of
x(rounded down); must be positive, at most 1.0, and resolve to at least one row.max_features: float, default 1.0Feature indices drawn per estimator, as a fraction of the columns of
x(rounded down); must be positive, at most 1.0, and resolve to at least one column. Only the first drawn index is used, so values above one column change only the random stream.random_state: int, default 42Seed; the same seed and inputs reproduce a fit.
sample_weight: list[float] | None, default NoneOne non-negative, finite weight per row of
x, not all zero; None weighs rows equally. A row drawn twice counts twice.
Returns
- dict[str, Any]
predictions: the predicted value for each row ofx(in-sample).oob_score: out-of-bag R^2 over the rows that at least one estimator did not draw, each predicted by the mean of the estimators that did not draw it (0 when the scored targets are constant); None if every row was drawn by every estimator.
Raises
- ValueError
If
xis empty or ragged;y,sample_weightor the rows ofind_matdo not match the rows ofx, orind_matis empty;n_estimatorsis zero;max_samplesormax_featuresis out of range; or a sample weight is negative or not finite, or all weights are zero.