Skip to content

Comments

Fix export_text single-feature handling (swev-id: scikit-learn__scikit-learn-14053)#39

Open
casey-brooks wants to merge 1 commit intoscikit-learn__scikit-learn-14053from
noa/issue-34
Open

Fix export_text single-feature handling (swev-id: scikit-learn__scikit-learn-14053)#39
casey-brooks wants to merge 1 commit intoscikit-learn__scikit-learn-14053from
noa/issue-34

Conversation

@casey-brooks
Copy link

@casey-brooks casey-brooks commented Dec 26, 2025

Summary

  • ensure export_text resolves feature names by index instead of node ids
  • avoid TREE_UNDEFINED indices by building names from n_features length
  • add regression coverage for single-feature classifiers with and without provided names

Reproduction

from sklearn.tree import DecisionTreeClassifier
from sklearn.tree.export import export_text
from sklearn.datasets import load_iris

X, y = load_iris(return_X_y=True)
X = X[:, 0].reshape(-1, 1)

tree = DecisionTreeClassifier()
tree.fit(X, y)
tree_text = export_text(tree, feature_names=['sepal_length'])
print(tree_text)

Observed failure (pre-fix)

Traceback (most recent call last):
  File "<stdin>", line 18, in <module>
  File "/workspace/tmp_old_export.py", line 893, in export_text
    feature_names_ = [feature_names[i] for i in tree_.feature]
  File "/workspace/tmp_old_export.py", line 893, in <listcomp>
    feature_names_ = [feature_names[i] for i in tree_.feature]
IndexError: list index out of range

Testing

  • PYTHONPATH=/workspace/scikit-learn LD_LIBRARY_PATH=/workspace/sklearn-py38/lib:$LD_LIBRARY_PATH /workspace/sklearn-py38/bin/flake8 sklearn/tree/export.py sklearn/tree/tests/test_export.py
  • PYTHONPATH=/workspace/sklearn-py38/lib/python3.8/site-packages LD_LIBRARY_PATH=/workspace/sklearn-py38/lib:$LD_LIBRARY_PATH /workspace/sklearn-py38/bin/pytest /workspace/tmp_tests/test_export.py -k export_text

Fixes #34

@casey-brooks casey-brooks requested a review from a team December 26, 2025 02:53
@casey-brooks casey-brooks changed the title Fix export_text single-feature handling Fix export_text single-feature handling (swev-id: scikit-learn__scikit-learn-14053) Dec 26, 2025
Copy link

@noa-lucent noa-lucent left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix looks correct. Thanks for adding regression coverage for single-feature trees.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants