Skip to content

Regression in RandomForestClassifier predictions for onnxruntime 1.26.0 #28557

@betatim

Description

@betatim

Describe the issue

I have a converted scikit-learn RandomForestClassifier where the predictions don't match with the "native" scikit-learn model. This worked in onnxruntime 1.25.1 but started failing in 1.26.0. I think this is a regression that might have been caused by the fix for #27533

I'm using these versions:

onnxruntime:  1.26.0
scikit-learn: 1.8.0
skl2onnx:     1.20.0

To reproduce

import numpy as np
import onnxruntime as ort
from sklearn.datasets import make_classification
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
from skl2onnx import convert_sklearn
from skl2onnx.common.data_types import FloatTensorType

X, y = make_classification(
    n_samples=500, n_features=10, n_informative=5, random_state=42
)
X = X.astype(np.float32)
X_train, X_test, y_train, _ = train_test_split(
    X, y, test_size=0.2, random_state=42
)

clf = RandomForestClassifier(n_estimators=20, max_depth=8, random_state=42)
clf.fit(X_train, y_train)

sk_preds = clf.predict(X_test)

onnx_model = convert_sklearn(
    clf,
    initial_types=[("float_input", FloatTensorType([None, 10]))],
    options={"zipmap": False},
)

sess = ort.InferenceSession(onnx_model.SerializeToString())
onnx_labels, onnx_probs = sess.run(None, {"float_input": X_test})

match_rate = np.mean(sk_preds == onnx_labels)
prob_row_sums = onnx_probs.sum(axis=1)

print(f"Prediction match rate: {match_rate:.4f} (expect 1.0)")
print(f"Probability row sums:  min={prob_row_sums.min():.4f} max={prob_row_sums.max():.4f} (expect 1.0)")
print(f"Sample probs[0]:       {onnx_probs[0]} (expect non-negative, sum to 1)")
print(f"Sample probs[4]:       {onnx_probs[4]} (expect non-negative, sum to 1)")

Urgency

This is a regression, something that used to work stopped working. There is no exception, so users have to have their own tests/checks to notice this.

There is a workaround available (pin to older version).

Platform

Linux

OS Version

6.17.0-23-generic

ONNX Runtime Installation

Released Package

ONNX Runtime Version or Commit ID

1.26.0

ONNX Runtime API

Python

Architecture

X64

Execution Provider

Default CPU

Execution Provider Library Version

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions