-
-
Notifications
You must be signed in to change notification settings - Fork 25.8k
sklearn.tree.export_text failing when feature_names supplied #26265
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Could you please post a minimal reproducible? (something we can copy paste in its entirety to produce the issue). |
@NickKanellos From the error message, it seems that the feature names you passed in is an array, but as documented,
|
@adrinjalali I can give a piece of code that possibly produces the same error: from sklearn.datasets import load_iris
from sklearn.tree import DecisionTreeClassifier
from sklearn.tree import export_text
import numpy as np
import sklearn; print("Sklearn version:", sklearn.__version__)
# Loading data, make feature_names a numpy array on purpose
iris = load_iris()
X, y = iris["data"], iris["target"]
feature_names = np.array(iris["feature_names"])
# Training decision tree
decision_tree = DecisionTreeClassifier(random_state=0, max_depth=2).fit(X, y)
# Exporting text
r = export_text(decision_tree, feature_names=feature_names)
print(r) With the current stable version (1.2.2 or earlier), we will have
In the current development branch (main branch), the parameters are validated (see #25867) for
However, though scikit-learn/sklearn/tree/_export.py Line 1040 in 4caa4ab
to if feature_names is not None: , and changing the linescikit-learn/sklearn/tree/_export.py Line 927 in 4caa4ab
to "feature_names": ["array-like", None], and then running the same code snippet as above, we will be able get the desired result such that
If this is the desired behavior, I'm happy to make a PR for this.
|
we're not really doing anything which definitely requires feature names to be a list, I'm happy with a fix which would accept an array like, and convert it to a list or an array. @Charlie-XIAO would you have time to submit a PR for your suggestions? |
@adrinjalali Sure, I will do it right now. |
/take |
This comment was marked as spam.
This comment was marked as spam.
@Nazish12345 Thanks for your advice, but as you can see above, I've already opened a PR so that both |
folks, I'm not sure why this works for
but not for
Can anyone help?
The text was updated successfully, but these errors were encountered: