From scikit-learn to ONNX#
Function skl2onnx.to_onnx is the
main entrypoint to convert a scikit-learn pipeline into ONNX.
The same function was extended in this package into
to_onnx
to handle
dataframes, an extended list of supported converters, scorers.
It works exactly the same:
<<<
import numpy
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.cluster import KMeans
from mlprodict.onnx_conv import to_onnx
from mlprodict.onnxrt import OnnxInference
iris = load_iris()
X = iris.data.astype(numpy.float32)
X_train, X_test = train_test_split(X)
clr = KMeans(n_clusters=3)
clr.fit(X_train)
model_def = to_onnx(clr, X_train.astype(numpy.float32),
target_opset=12)
oinf = OnnxInference(model_def, runtime='python')
print(oinf.run({'X': X_test[:5]}))
>>>
somewhere/.local/lib/python3.9/site-packages/sklearn/cluster/_kmeans.py:870: FutureWarning: The default value of `n_init` will change from 10 to 'auto' in 1.4. Set the value of `n_init` explicitly to suppress the warning
warnings.warn(
{'label': array([1, 1, 0, 0, 1]), 'scores': array([[3.328, 0.305, 4.967],
[3.535, 0.469, 5.182],
[1.02 , 2.626, 2.716],
[0.725, 4.064, 1.177],
[3.346, 0.342, 4.904]], dtype=float32)}
This new version extends the conversion to scorers through
convert_scorer
.