sklearn.tree.export_text#
Ultima modificación: 2023-03-11 | YouTube
[1]:
from sklearn.datasets import load_iris
iris = load_iris()
X = iris['data']
y = iris['target']
[2]:
from sklearn.tree import DecisionTreeClassifier
decisionTreeClassifier = DecisionTreeClassifier(random_state=0, max_depth=2)
decisionTreeClassifier.fit(X, y)
[2]:
DecisionTreeClassifier(max_depth=2, random_state=0)In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
DecisionTreeClassifier(max_depth=2, random_state=0)
[3]:
from sklearn.tree import export_text
r = export_text(
# --------------------------------------------------------------------------
# The decision tree estimator to be exported. It can be an instance of
# DecisionTreeClassifier or DecisionTreeRegressor.
decision_tree=decisionTreeClassifier,
# --------------------------------------------------------------------------
# A list of length n_features containing the feature names. If None generic
# names will be used (“feature_0”, “feature_1”, …).
feature_names=iris['feature_names'],
# --------------------------------------------------------------------------
# Only the first max_depth levels of the tree are exported. Truncated
# branches will be marked with “…”.
max_depth=10,
# --------------------------------------------------------------------------
# Number of spaces between edges. The higher it is, the wider the result.
spacing=3,
# --------------------------------------------------------------------------
# Number of decimal digits to display.
decimals=2,
# --------------------------------------------------------------------------
# Number of decimal digits to display.
show_weights=False,
)
print(r)
|--- petal width (cm) <= 0.80
| |--- class: 0
|--- petal width (cm) > 0.80
| |--- petal width (cm) <= 1.75
| | |--- class: 1
| |--- petal width (cm) > 1.75
| | |--- class: 2