Probabilities as a vector or as a ZipMap#

A classifier usually returns a matrix of probabilities. By default, sklearn-onnx converts that matrix into a list of dictionaries where each probabily is mapped to its class id or name. That mechanism retains the class names. This conversion increases the prediction time and is not always needed. Let’s see how to deactivate this behaviour on the Iris example.

Train a model and convert it#

from timeit import repeat
import numpy
import sklearn
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
import onnxruntime as rt
import onnx
import skl2onnx
from skl2onnx.common.data_types import FloatTensorType
from skl2onnx import convert_sklearn
from sklearn.linear_model import LogisticRegression

iris = load_iris()
X, y =,
X_train, X_test, y_train, y_test = train_test_split(X, y)
clr = LogisticRegression(max_iter=500), y_train)

initial_type = [('float_input', FloatTensorType([None, 4]))]
onx = convert_sklearn(clr, initial_types=initial_type,

Output type#

Let’s confirm the output type of the probabilities is a list of dictionaries with onnxruntime.

sess = rt.InferenceSession(onx.SerializeToString())
res =, {'float_input': X_test.astype(numpy.float32)})
print("probabilities type:", type(res[1]))
print("type for the first observations:", type(res[1][0]))
[{0: 0.9724591970443726, 1: 0.027540581300854683, 2: 2.352435330976732e-07}, {0: 0.9776268601417542, 1: 0.022372985258698463, 2: 1.9038986920349998e-07}]
probabilities type: <class 'list'>
type for the first observations: <class 'dict'>

Without ZipMap#

Let’s remove the ZipMap operator.

initial_type = [('float_input', FloatTensorType([None, 4]))]
options = {id(clr): {'zipmap': False}}
onx2 = convert_sklearn(clr, initial_types=initial_type, options=options,

sess2 = rt.InferenceSession(onx2.SerializeToString())
res2 =, {'float_input': X_test.astype(numpy.float32)})
print("probabilities type:", type(res2[1]))
print("type for the first observations:", type(res2[1][0]))
[[9.7245920e-01 2.7540581e-02 2.3524353e-07]
 [9.7762686e-01 2.2372985e-02 1.9038987e-07]]
probabilities type: <class 'numpy.ndarray'>
type for the first observations: <class 'numpy.ndarray'>

One output per class#

This options removes the final operator ZipMap and splits the probabilities into columns. The final model produces one output for the label, and one output per class.

options = {id(clr): {'zipmap': 'columns'}}
onx3 = convert_sklearn(clr, initial_types=initial_type, options=options,

sess3 = rt.InferenceSession(onx3.SerializeToString())
res3 =, {'float_input': X_test.astype(numpy.float32)})
for i, out in enumerate(sess3.get_outputs()):
    print("output: '{}' shape={} values={}...".format(, res3[i].shape, res3[i][:2]))
output: 'output_label' shape=(38,) values=[0 0]...
output: 'i0' shape=(38,) values=[0.9724592  0.97762686]...
output: 'i1' shape=(38,) values=[0.02754058 0.02237299]...
output: 'i2' shape=(38,) values=[2.3524353e-07 1.9038987e-07]...

Let’s compare prediction time#

X32 = X_test.astype(numpy.float32)

print("Time with ZipMap:")
print(repeat(lambda:, {'float_input': X32}),
             number=100, repeat=10))

print("Time without ZipMap:")
print(repeat(lambda:, {'float_input': X32}),
             number=100, repeat=10))

print("Time without ZipMap but with columns:")
print(repeat(lambda:, {'float_input': X32}),
             number=100, repeat=10))

# The prediction is much faster without ZipMap
# on this example.
# The optimisation is even faster when the classes
# are described with strings and not integers
# as the final result (list of dictionaries) may copy
# many times the same information with onnxruntime.
Time with ZipMap:
[0.012372604999882242, 0.004819102000055864, 0.002799700999958077, 0.005126302000007854, 0.004604101999802879, 0.0027419010000357957, 0.0030426010000610404, 0.0026854010000079143, 0.0026163009999891074, 0.0026090009998824826]
Time without ZipMap:
[0.0016827009999360598, 0.0011974000001373497, 0.0012046009999266971, 0.0011644000001069799, 0.0011607010001171147, 0.0012080000001333246, 0.001181700999950408, 0.0011657999998533342, 0.0013147009999556758, 0.0015800999999555643]
Time without ZipMap but with columns:
[0.00204040099993108, 0.0020714009999664995, 0.0020063009999375936, 0.001988400999834994, 0.0019787999999607564, 0.0019756009999127855, 0.0021800010001697956, 0.002363000999821452, 0.0021929009999439586, 0.002378501000066535]

Versions used for this example

print("numpy:", numpy.__version__)
print("scikit-learn:", sklearn.__version__)
print("onnx: ", onnx.__version__)
print("onnxruntime: ", rt.__version__)
print("skl2onnx: ", skl2onnx.__version__)
numpy: 1.23.5
scikit-learn: 1.3.dev0
onnx:  1.14.0
onnxruntime:  1.15.0+cpu
skl2onnx:  1.14.0

Total running time of the script: ( 0 minutes 0.153 seconds)

Gallery generated by Sphinx-Gallery