Sklearn export_text is actually sklearn.tree.export package of sklearn. Sklearn export_text gives an explainable view of the decision tree over a feature. In this article, We will firstly create a random decision tree and then we will export it, into text format.
Sklearn export_text: Step By step –
Step 1 (Prerequisites): Decision Tree Creation –
Here we will create a random decision tree with the help of sci-kit learn library. We will use the iris dataset for decision tree creation.
from sklearn.tree import DecisionTreeClassifier from sklearn.datasets import load_iris iris = load_iris() X = iris['data'] y = ['setosa']*50+['versicolor']*50+['virginica']*50 decision_tree = DecisionTreeClassifier(random_state=0, max_depth=3) decision_tree = decision_tree.fit(X, y)
- Firstly, We need to import the
DecisionTreeClassifierclassifier from sklearn.tree module. For the dataset part, We need to invoke the load_iris() function.
- Secondly, The X, Y are training and testing datasets.
- DecisionTreeClassifier is a constructer class for the decision tree. Here the max-depth argument is for defining the label of the decision tree.
Step 2: Invoking sklearn export_text –
Once we have created the decision tree, We can export the decision tree into textual format. But to achieve this, We need to import export_text from sklearn.tree.export package. After it, We will invoke the export_text() function by passing the decision tree object as an argument. Here is the syntax for that.
from sklearn.tree.export import export_text r = export_text(decision_tree, feature_names=iris['feature_names'],decimals=0, show_weights=True) print(r)
we can easily solve the mystery of the decision tree with the above self-explanatory export_text() function. Here show_weights are set are True. It will give more info about each node. Let’s run the complete code together and check the output.
As in the above tree text version, We can see the classes with weights. The discriminatory feature is also very clear in the text file.
Other Parameters of export_text() –
We have used decision_tree, feature_names, decimals, and show_weights in the earlier section. We have max_depth and spacing as an optional parameter. Here the default value for max_depth is 10 and spacing is 3. decimals is also an optional parameter and its default value is 2.
I hope you must like this article. Please subscribe for more interesting articles.
Data Science Learner Team
Join our list
Subscribe to our mailing list and get interesting stuff and updates to your email inbox.