Skip to content

treelite.sklearn.export_model should preserve sklearn-compatible child estimator class metadata for random forest classifiers #663

Description

@coderabbitai

Summary

treelite.sklearn.export_model should export scikit-learn-compatible classifier metadata for child estimators in exported random forest classifiers.

Today, cuML needs a local workaround when converting a RandomForestClassifier to scikit-learn: after calling the CPU export path, it manually patches each exported child estimator so that:

  • the forest-level classes_ retains the original class labels
  • each child estimator's classes_ uses sklearn-style encoded class indices ([0, 1, ..., n_classes - 1])
  • each child estimator's n_classes_ matches the parent classifier

This behavior is currently implemented downstream in RAPIDS cuML, but it appears to be a responsibility that belongs in Treelite's sklearn export layer.

Rationale

scikit-learn stores classifier metadata differently at the forest level vs. the individual child trees. If treelite.sklearn.export_model exports the same classes_ metadata everywhere, downstream projects need ad hoc fixups to restore sklearn-compatible child estimator state.

Moving this logic upstream would:

  • keep sklearn-export behavior consistent at the source
  • remove downstream patching/hacks in RAPIDS cuML
  • improve compatibility for any Treelite consumer exporting sklearn random forest classifiers

Affected area

  • treelite.sklearn.export_model
  • sklearn random forest classifier export path
  • exported child estimator metadata (classes_, n_classes_)

Observed downstream workaround

RAPIDS cuML PR #8291 adds a post-processing step in python/cuml/cuml/ensemble/randomforestclassifier.py to patch exported child estimators after CPU export.

PR: NVIDIA/cuml#8291
Review comment: NVIDIA/cuml#8291 (comment)

Requested by: @csadorf

Expected behavior / acceptance criteria

  • Exported sklearn random forest classifiers preserve the original labels at the forest level (forest.classes_)
  • Exported child estimators use sklearn-compatible encoded labels for estimator.classes_
  • Exported child estimators expose the correct estimator.n_classes_
  • Behavior matches a natively fitted scikit-learn RandomForestClassifier, including cases with non-contiguous class labels
  • Downstream projects such as cuML can remove local metadata patching once they adopt the updated Treelite behavior

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions