API Reference /

workflow.py

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

On

class workflow.On

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").

Constructor
__init__(
    self,
    columns,
    transformer,
    remainder: str = 'passthrough',
)

Parameters

columns
str, list of str, list of int, or callable

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
object
A configured transformer instance (anything with fit_transform, or the fit and transform pair).
remainder
{"passthrough", "drop"} = "passthrough"
What to do with the columns that were not selected.

Attributes

columns_
list of int
Positions of the selected columns, resolved at fit time.
transformer_
object
The fitted transformer.

Notes

Selecting by name requires the incoming column names, which are known only for the pipeline's original input. Put name-based On steps first, before anything that changes the column layout; positional and type-based selection work anywhere.
python
>>> from tuiml import On
>>> from tuiml.preprocessing import SimpleImputer, StandardScaler
>>> On("number", StandardScaler())                      # doctest: +SKIP
>>> On(["age", "fare"], SimpleImputer())                # doctest: +SKIP

Methods

get_params (self, deep: bool=True) -> Dict[str, Any]

Return the constructor parameters.

Parameters
deep
bool = True
Included for API symmetry; On has no nested params to expose.
Returns
dict
The columns, transformer, and remainder parameters.
fit_transform (self, X, y=None)

Fit the wrapped transformer on the selected columns and transform.

Parameters
X
array-like of shape (n_samples, n_features)
Input data.
y
array-like of shape (n_samples,)
Target values, forwarded to the wrapped transformer.
Returns
np.ndarray
Transformed selected columns, followed by the passthrough columns.
transform (self, X)

Transform new data with the already-fitted transformer.

Parameters
X
array-like of shape (n_samples, n_features)
Input data.
Returns
np.ndarray
Transformed selected columns, followed by the passthrough columns.
__repr__ (self)

Workflow

class workflow.Workflow(Algorithm)

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.

Constructor
__init__(
    self,
    steps: Optional[List[Any]] = None,
)

Parameters

steps
list
The pipeline, in execution order. Each element is a configured component instance, or an explicit (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_
list
The fitted transformation steps. Set by fit.
model_
object
The fitted final model. Set by fit.
metrics_
dict or None
Held-out scores, when fit was given cv or test_size.
cv_results_
dict or None
Per-fold scores from cross-validation.
predictions_
np.ndarray or None
Predictions on the held-out split.
feature_names_in_
list of str or None
Column names of the training data, when known.
metadata_
dict
Details of the run, algorithm name, step names, evaluation method.

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.

Build from configured instances, then use it like any model:

python
>>> 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:

python
>>> 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:

python
>>> 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

named_steps (self) -> Dict[str, Any]

Steps by name, e.g. wf.named_steps["standardscaler"].

model (self) -> Any

The final step, the model, unfitted unless fit has run.

transformers (self) -> List[Tuple[str, Any]]

The (name, component) pairs before the model.

get_params (self, deep: bool=True) -> Dict[str, Any]

Return the pipeline's parameters.

Parameters
deep
bool = True
When True, also return each step's parameters under "<step_name>__<param>" keys.
Returns
dict
The parameters.
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
Workflow
Raises
ValueError
If a step name is unknown or a key is not a pipeline parameter.
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
str, DataFrame, ndarray, or Dataset
Training data. A string is a file path (csv, arff, parquet, json, excel, auto-detected) or a builtin dataset name such as "iris". Arrays and DataFrames are used directly.
y
array-like of shape (n_samples,)
Target values, for the fit(X, y) form.
target
str or array-like
Target column name when data is a file or DataFrame, or a separate target array.
features
list of str
Restrict the feature matrix to these named columns.
cv
int
Number of cross-validation folds. When given, scores are collected per fold into metrics_ and cv_results_.
test_size
float
Hold out this fraction to score into metrics_. Ignored when cv is given.
stratify
bool = True
Preserve class balance in the holdout split.
metrics
str or list of str = "auto"
Metric function names from tuiml.evaluation.metrics. "auto" picks metrics that suit the model's task.
random_seed
int
Seed for splits, folds, and any step that accepts one. Falls back to the global seed, then 42.
Returns
self
Workflow
Fitted on all the data given, with results on metrics_.
Raises
ValueError
If no data or no steps were provided.
predict (self, X) -> np.ndarray

Predict, applying the fitted transformations first.

Parameters
X
array-like of shape (n_samples, n_features)
Raw input, in the same shape as the training data.
Returns
np.ndarray
Predicted labels or values.
predict_proba (self, X) -> np.ndarray

Predict class probabilities, applying the fitted transformations first.

Parameters
X
array-like of shape (n_samples, n_features)
Raw input.
Returns
np.ndarray of shape (n_samples, n_classes)
Class probabilities.
Raises
AttributeError
If the final model has no predict_proba.
score (self, X, y) -> float

Return the final model's default score on transformed data.

Parameters
X
array-like of shape (n_samples, n_features)
Raw input.
y
array-like of shape (n_samples,)
True labels or values.
Returns
float
Accuracy for classifiers, R² for regressors.
evaluate (self, X, y, metrics: Union[str, List[str]]='auto') -> Dict[str, float]

Score the fitted pipeline on new data.

Parameters
X
array-like of shape (n_samples, n_features)
Raw input.
y
array-like of shape (n_samples,)
True labels or values.
metrics
str or list of str = "auto"
Metric function names, or "auto" to pick by task.
Returns
dict
Metric name to value.
to_config (self) -> Dict[str, Any]

Export the pipeline as a train spec.

Returns
dict
A spec with model and, when there are transformation steps, pipeline: each component as {"name": ..., "params": {...}}.
__repr__ (self)