---
title: "scikit-learn pipelines"
url: https://recursiveai.org/onemodel/docs/guide-sklearn-pipelines/
description: "A scikit-learn Pipeline exported with skl2onnx (and onnxmltools for its XGBoost and LightGBM steps) runs as it is, with the exporters' default options…"
version: "1.5.0"
source: https://github.com/recursive-ai-oss/onemodel-csharp/blob/c4f8bdcd456e17d74b9b1d89cfc250e4ed4c8de6/docs/guide/sklearn-pipelines.md
---

# scikit-learn pipelines

A scikit-learn `Pipeline` exported with skl2onnx (and onnxmltools for its XGBoost and LightGBM steps) runs as it is, with the exporters' default options: `ColumnTransformer` blocks that impute, scale, one-hot or ordinal-encode their columns, `FeatureUnion`, nested pipelines, `CountVectorizer` and `TfidfVectorizer` text steps, and a final classifier or regressor, with the `ZipMap` skl2onnx puts after a classifier. The operators and their rules are in [ops-ml.md](https://recursiveai.org/onemodel/docs/ops-ml/) and [ops-strings.md](https://recursiveai.org/onemodel/docs/ops-strings/); the pipelines checked against ONNX Runtime are listed in the "Pipelines" section of ops-ml.md.

## Export

Nothing special: pass the columns' types as skl2onnx documents.

```python
from skl2onnx import to_onnx
onnx_model = to_onnx(pipeline, df[:1])     # a DataFrame: one graph input per column, typed from its dtype
open("pipeline.onnx", "wb").write(onnx_model.SerializeToString())
```

A `ColumnTransformer` pipeline fed a DataFrame has one input per column (`float`, `int64` or `string`, each `[N, 1]`), and a classifier has two outputs, `output_label` and `output_probability`. Look at `model.Session.Inputs` and `Outputs` for the names and kinds of your export.

## Run with named inputs

`ModelInputs` collects the inputs by name, of any mix of kinds; `Model.Run(inputs)` returns a `ModelOutputs` you read by name. The shape comes from the model's declaration when it leaves one extent open (`[N, 1]` columns need no shape argument); pass it to `Add` otherwise, and for scalars.

<!-- sample: pipeline-named-inputs -->
```csharp
using var pipeline = Model.Load(pipelineModel);   // skl2onnx: ColumnTransformer (imputer, scaler, one-hot) + LogisticRegression

using var inputs = new ModelInputs();
inputs.Add<float>("age", [34f, float.NaN, 52f]);
inputs.Add<float>("income", [48.5f, 61f, 20f]);
inputs.Add<long>("visits", [3, 0, 7]);
inputs.Add("city", ["paris", "rome", ""]);
inputs.Add("kind", ["red", "blue", "purple"]);

using ModelOutputs result = pipeline.Run(inputs);
long[] labels = result.Int64s("output_label");
IReadOnlyList<IReadOnlyDictionary<long, float>> probabilities = result.Int64Maps("output_probability");
Console.WriteLine($"row 0: class {labels[0]}, p = {probabilities[0][labels[0]]:P0}");
```

What the accessors return, by the output's `TensorInfo.Kind`:

| Output | Read with |
|---|---|
| Numbers (float, int64, boolean, ...) | `Floats(name)`, `Int64s(name)`, `Values<T>(name)`, `ShapeOf(name)` |
| String labels or strings | `Strings(name)` |
| `ZipMap` with integer or string class labels | `Int64Maps(name)`, `StringMaps(name)` |

A `Session` takes the same values as tensors: `ModelValues.Strings(values, shape)` makes a string tensor.

## Things to know

- **Text pipelines** export the `Tokenizer` operator of the `com.microsoft` domain; it is implemented, with the leftmost-longest rule ONNX Runtime applies.
- **Missing values** are `NaN` in float columns and the empty string in string columns, exactly as the imputers were fitted.
- **LinearSVC as the last step** of a pipeline is refused by skl2onnx 1.20.0 itself with its default options (the exporter's error is `Option 'zipmap' not in ...`). The documented `update_registered_converter` route, with the converter's options declared, exports it; `ops-ml.md` lists what was checked.
- **Map-typed inputs** (a `DictVectorizer` pipeline exported with a `map(string, float)` input) are session inputs made with `ModelValues.Map(...)`; on the happy path `Model.RunMap` runs a model whose one input is a map and returns its one output.
- Fast and exact mode, thread counts and the GPU package behave as for any model: the string and ML operators run on the CPU.
