Pipelines: an ordered list of steps ending in a model.
A Workflow chains transformation steps and a final model into one object that behaves like a model itself: fit, predict, score, save. Because the whole pipeline is a single estimator, the fitted transformations always travel with the model, so inference can never accidentally skip them.
Steps are configured instances:
Workflow([StandardScaler(), RandomForestClassifier(n_estimators=100)])
This is the object-based level of the API, with imports, autocomplete, and type checking. The string-and-dict way of writing the same pipeline belongs to train, which resolves names through the hub and runs on this same engine.
Classes
Apply a transformer to a subset of columns.
Mixed-type tables need different treatment per column, scale the numbers, encode the categories. On wraps a transformer so it sees only the columns you name, and is itself an ordinary pipeline step:
Workflow([
On("number", StandardScaler()),
On("category", OneHotEncoder()),
RandomForestClassifier(),
])
Transformed columns come first in the output, followed by any columns not selected (unless remainder="drop").
__init__( self, columns, transformer, remainder: str = 'passthrough', )
Parameters
columns
Which columns to route:
- •
"number": every numeric column, detected from the data - •
"category": every non-numeric column - •
["age", "income"]: these named columns - •
[0, 3]: these column positions - •a callable taking the feature-name list and returning names/indices
transformer
fit_transform, or the fit and transform pair).
remainder
Attributes
columns_
transformer_
Notes
On steps first, before anything that changes the column layout; positional and type-based selection work anywhere.>>> from tuiml import On
>>> from tuiml.preprocessing import SimpleImputer, StandardScaler
>>> On("number", StandardScaler()) # doctest: +SKIP
>>> On(["age", "fare"], SimpleImputer()) # doctest: +SKIP
Methods
fit_transform
(self, X, y=None)
fit_transform
(self, X, y=None)
Fit the wrapped transformer on the selected columns and transform.
Parameters
X
y
Returns
np.ndarray
__repr__
(self)
An ordered pipeline of steps ending in a model.
A Workflow is a model: it exposes the same fit/predict/ score/save interface as any single algorithm, so it can be used anywhere one can, including as a step inside another Workflow, or as an entry in Benchmark.
Every step except the last transforms the data; the last one is the model.
__init__( self, steps: Optional[List[Any]] = None, )
Parameters
steps
(name, instance) tuple when you want to choose the step's name. Names are otherwise derived from the class name, lowercased. To build the same pipeline from strings and dicts, use train.
Attributes
steps_
fit.
model_
fit.
metrics_
fit was given cv or test_size.
cv_results_
predictions_
feature_names_in_
metadata_
Notes
fit always leaves the pipeline fitted on all the data it was given. Passing cv or test_size additionally measures held-out performance into metrics_ before that final fit.See Also
Build from configured instances, then use it like any model:
>>> from tuiml import Workflow
>>> from tuiml.preprocessing import StandardScaler
>>> from tuiml.algorithms.trees import RandomForestClassifier
>>> wf = Workflow([StandardScaler(), RandomForestClassifier(n_estimators=100)])
... # doctest: +SKIP
>>> wf.fit(X_train, y_train).predict(X_test) # doctest: +SKIP
Evaluate while fitting by naming a split:
>>> wf = wf.fit("iris", cv=10) # doctest: +SKIP
>>> wf.metrics_ # doctest: +SKIP
{'cv_accuracy_score_mean': 0.96, 'cv_accuracy_score_std': 0.02}
Mixed-type table with per-column steps:
>>> from tuiml import On, Workflow
>>> from tuiml.preprocessing import SimpleImputer, OneHotEncoder
>>> wf = Workflow([ # doctest: +SKIP
... On("number", SimpleImputer()),
... On("category", OneHotEncoder()),
... RandomForestClassifier(),
... ])
>>> wf.fit("sales.csv", target="label", cv=10) # doctest: +SKIP
Methods
set_params
(self, **params) -> 'Workflow'
set_params
(self, **params) -> 'Workflow'
Set parameters, including a step's own via step__param.
Parameters
**params
steps=[...] to replace the pipeline, or standardscaler__with_mean=False to reach into a step.
Returns
self
Raises
ValueError
fit
(self, data=None, y=None, *, target=None, features: Optional[List[str]]=None, cv: Optional[int]=None, test_size: Optional[float]=None, stratify: bool=True, metrics: Union[str, List[str]]='auto', random_seed: Optional[int]=None) -> 'Workflow'
fit
(self, data=None, y=None, *, target=None, features: Optional[List[str]]=None, cv: Optional[int]=None, test_size: Optional[float]=None, stratify: bool=True, metrics: Union[str, List[str]]='auto', random_seed: Optional[int]=None) -> 'Workflow'
Fit the pipeline, optionally measuring held-out performance first.
Parameters
data
"iris". Arrays and DataFrames are used directly.
y
fit(X, y) form.
target
data is a file or DataFrame, or a separate target array.
features
cv
metrics_ and cv_results_.
test_size
metrics_. Ignored when cv is given.
stratify
metrics
tuiml.evaluation.metrics. "auto" picks metrics that suit the model's task.
random_seed
Returns
self
metrics_.
Raises
ValueError
predict_proba
(self, X) -> np.ndarray
predict_proba
(self, X) -> np.ndarray
Predict class probabilities, applying the fitted transformations first.
Parameters
X
Returns
np.ndarray of shape (n_samples, n_classes)
Raises
AttributeError
predict_proba.
evaluate
(self, X, y, metrics: Union[str, List[str]]='auto') -> Dict[str, float]
evaluate
(self, X, y, metrics: Union[str, List[str]]='auto') -> Dict[str, float]
Score the fitted pipeline on new data.
Parameters
X
y
metrics
"auto" to pick by task.
Returns
dict
__repr__
(self)