Pipelines, Composition & Meta-Estimators
Pipeline, ColumnTransformer, TransformedTargetRegressor, multiclass/multioutput wrappers, and bagging/voting/stacking meta-ensembles.
OchreML provides full scikit-learn-compatible pipeline, column-routing, target-transformation, and meta-estimator composition across ochreml.pipeline, ochreml.compose, ochreml.multiclass, ochreml.multioutput, and ochreml.ensemble (all lazily importable from ochreml). (*≧ω≦*)
Overview
| Export | Submodule | Description |
|---|---|---|
Pipeline / make_pipeline | ochreml.pipeline | Sequential transform chain with a final estimator and step__param routing |
ColumnTransformer / make_column_transformer | ochreml.compose | Applies distinct transformers to column subsets of arrays or Pandas/Polars DataFrames (.dit tag 36) |
make_column_selector | ochreml.compose | Callable column selector by regex pattern, dtype_include, or dtype_exclude |
TransformedTargetRegressor | ochreml.compose | Regresses on a transformed target y and auto-inverts predictions (.dit tag 37) |
OneVsRestClassifier | ochreml.multiclass | Fits one binary classifier per class for multiclass classification |
MultiOutputRegressor | ochreml.multioutput | Fits one cloned regressor per target column for multi-target regression |
BaggingClassifier / BaggingRegressor | ochreml.ensemble | Bootstrap-aggregating ensemble meta-estimators |
VotingClassifier | ochreml.ensemble | Hard majority-rule or soft probability-weighted voting classifier |
StackingClassifier | ochreml.ensemble | Out-of-fold stacked generalization with a final meta-classifier |
Pipeline & make_pipeline
Chains a sequence of (name, transformer) steps ending in an optional final estimator. Intermediate steps must implement fit and transform (or be "passthrough" / None). Supports indexing by name (pipe["scaler"]), integer (pipe[0]), or slice (pipe[:-1]), nested get_params/set_params using step__param, fit parameter routing (pipe.fit(X, y, clf__sample_weight=w)), and .dit persistence.
from ochreml.pipeline import Pipeline, make_pipeline
from ochreml.preprocessing import StandardScaler
from ochreml.linear_model import LogisticRegression
pipe = Pipeline(
steps=[
("scaler", StandardScaler()),
("clf", LogisticRegression(max_iter=500)),
],
memory=None,
verbose=False,
)
# Shorthand with auto-generated lowercase step names
quick_pipe = make_pipeline(StandardScaler(), LogisticRegression(max_iter=500))- Methods:
fit(X, y=None, **fit_params),fit_transform(X, y=None, **fit_params),fit_predict(X, y=None, **fit_params),transform(X),predict(X, **predict_params),predict_proba(X, **predict_params),decision_function(X),score(X, y=None, sample_weight=None),get_params(deep=True),set_params(**params),save(path),load(path, device=None) - Properties:
named_steps,classes_,n_features_in_,feature_names_in_
ColumnTransformer, make_column_transformer & make_column_selector
Applies separate transformers to subsets of columns in NumPy arrays, Pandas DataFrames, or Polars DataFrames, then concatenates the outputs into a dense array or sparse CSR matrix (controlled by sparse_threshold).
from ochreml.compose import (
ColumnTransformer,
make_column_transformer,
make_column_selector,
)
from ochreml.preprocessing import StandardScaler, OneHotEncoder
ct = ColumnTransformer(
transformers=[
("num", StandardScaler(), make_column_selector(dtype_include="number")),
("cat", OneHotEncoder(handle_unknown="ignore"), make_column_selector(dtype_include="object")),
],
remainder="drop",
sparse_threshold=0.3,
n_jobs=None,
transformer_weights=None,
verbose=False,
verbose_feature_names_out=True,
force_int_remainder_cols=True,
)
# Auto-named shorthand
ct_short = make_column_transformer(
(StandardScaler(), [0, 1]),
(OneHotEncoder(), [2]),
remainder="passthrough",
)make_column_selector Signature
make_column_selector(pattern=None, *, dtype_include=None, dtype_exclude=None)ColumnTransformer Methods & Attributes
- Methods:
fit(X, y=None),fit_transform(X, y=None),transform(X),get_feature_names_out(input_features=None),save(path),load(path, device=None) - Attributes:
transformers_,named_transformers_,output_indices_,sparse_output_,n_features_in_,feature_names_in_
TransformedTargetRegressor
Transforms the regression target y before fitting regressor (defaults to ochreml.LinearRegression()) and applies inverse_transform to predictions automatically.
import numpy as np
from ochreml.compose import TransformedTargetRegressor
from ochreml.linear_model import Ridge
ttr = TransformedTargetRegressor(
regressor=Ridge(alpha=1.0),
transformer=None,
func=np.log1p,
inverse_func=np.expm1,
check_inverse=True,
)
ttr.fit(X, y)
y_pred = ttr.predict(X_test)- Parameters:
regressor=None, keyword-onlytransformer=None,func=None,inverse_func=None,check_inverse=True - Methods:
fit(X, y, **fit_params),predict(X, **predict_params),score(X, y, sample_weight=None),save(path),load(path, device=None) - Attributes:
regressor_,transformer_,n_features_in_
OneVsRestClassifier
Located in ochreml.multiclass (and lazily exported from ochreml). Fits one binary classifier per class (or a single binary classifier when n_classes_ == 2).
from ochreml.multiclass import OneVsRestClassifier
from ochreml.svm import LinearSVC
ovr = OneVsRestClassifier(estimator=LinearSVC(), n_jobs=None, verbose=0)
ovr.fit(X, y)- Parameters:
estimator, keyword-onlyn_jobs=None,verbose=0 - Methods:
fit(X, y),predict(X),predict_proba(X),decision_function(X),score(X, y, sample_weight=None),get_params(deep=True),set_params(**params),save(path),load(path, device=None) - Attributes:
estimators_,classes_,n_classes_,n_features_in_
MultiOutputRegressor
Located in ochreml.multioutput (and lazily exported from ochreml). Fits one cloned regressor per column of a 2D target matrix y of shape (n_samples, n_outputs).
from ochreml.multioutput import MultiOutputRegressor
from ochreml.linear_model import Ridge
mor = MultiOutputRegressor(estimator=Ridge(alpha=1.0), n_jobs=None)
mor.fit(X, Y_multi)- Parameters:
estimator, keyword-onlyn_jobs=None - Methods:
fit(X, y, sample_weight=None),predict(X),score(X, y, sample_weight=None),get_params(deep=True),set_params(**params),save(path),load(path, device=None) - Attributes:
estimators_,n_outputs_,n_features_in_
Ensemble Meta-Estimators (ochreml.ensemble)
BaggingClassifier & BaggingRegressor
Fits n_estimators clones of estimator on random sample/feature subsets and averages predicted probabilities (BaggingClassifier, default base DecisionTreeClassifier) or predictions (BaggingRegressor, default base DecisionTreeRegressor).
from ochreml.ensemble import BaggingClassifier, BaggingRegressor
bag_clf = BaggingClassifier(
estimator=None,
n_estimators=10,
max_samples=1.0,
max_features=1.0,
bootstrap=True,
bootstrap_features=False,
random_state=42,
n_jobs=None,
verbose=0,
)
bag_reg = BaggingRegressor(
estimator=None,
n_estimators=10,
max_samples=1.0,
max_features=1.0,
bootstrap=True,
bootstrap_features=False,
random_state=42,
n_jobs=None,
verbose=0,
)- Methods:
fit(X, y, sample_weight=None),predict(X),predict_proba(X)(classifier only),score(X, y, sample_weight=None),get_params(deep=True),set_params(**params),save(path),load(path, device=None) - Attributes:
estimators_,estimators_features_,n_features_in_(plusclasses_,n_classes_onBaggingClassifier)
VotingClassifier
Combines conceptually different classifiers via "hard" majority-rule voting or "soft" weighted probability averaging. Supports name__param parameter routing and "drop" / None entries.
from ochreml.ensemble import VotingClassifier
from ochreml.linear_model import LogisticRegression
from ochreml.naive_bayes import GaussianNB
voting = VotingClassifier(
estimators=[("lr", LogisticRegression()), ("nb", GaussianNB())],
voting="soft",
weights=[2.0, 1.0],
n_jobs=None,
flatten_transform=True,
verbose=False,
)- Methods:
fit(X, y, sample_weight=None),predict(X),predict_proba(X)(voting="soft"only),score(X, y, sample_weight=None),get_params(deep=True),set_params(**params),save(path),load(path, device=None) - Attributes:
estimators_,named_estimators_,classes_,n_features_in_
StackingClassifier
Generates leak-free out-of-fold predictions from base estimators using cv, then trains final_estimator (defaults to ochreml.LogisticRegression()) on the stacked meta-features (optionally concatenated with original X when passthrough=True).
from ochreml.ensemble import StackingClassifier, RandomForestClassifier
from ochreml.linear_model import LogisticRegression
from ochreml.naive_bayes import GaussianNB
stack = StackingClassifier(
estimators=[
("rf", RandomForestClassifier(n_estimators=20, random_state=0)),
("nb", GaussianNB()),
],
final_estimator=LogisticRegression(),
cv=5,
stack_method="auto",
n_jobs=None,
passthrough=False,
verbose=0,
)- Parameters:
estimators,final_estimator=None, keyword-onlycv=5,stack_method="auto"("auto","predict_proba","decision_function","predict"),n_jobs=None,passthrough=False,verbose=0 - Methods:
fit(X, y, sample_weight=None),transform(X),predict(X),predict_proba(X),decision_function(X),score(X, y, sample_weight=None),get_params(deep=True),set_params(**params),save(path),load(path, device=None) - Attributes:
estimators_,named_estimators_,final_estimator_,classes_,n_features_in_