Skip to content
FacebookTwitterLinkedinYouTubeGitHubSubscribeEmailRSS
Close
Beyond Knowledge Innovation

Beyond Knowledge Innovation

Where Data Unveils Possibilities

  • Home
  • AI & ML Insights
  • Machine Learning
    • Supervised Learning
      • Introduction
      • Regression
      • Classification
    • Unsupervised Learning
      • Introduction
      • Clustering
      • Association
      • Dimensionality Reduction
    • Reinforcement Learning
    • Generative AI
  • Knowledge Base
    • Introduction To Python
    • Introduction To Data
    • Introduction to EDA
  • References
HomeImplementationSupervised LearningClassificationVisualizing the Decision Tree
Classification

Visualizing the Decision Tree

March 7, 2024March 7, 2024CEO 187 views

To visualize a decision tree in scikit-learn, you can use the plot_tree function from the sklearn.tree module. This function allows you to generate a visual representation of the decision tree.

Here’s a simple example:

from sklearn import tree
plt.figure(figsize=(20,30))
tree.plot_tree(model, feature_names=feature_names, filled=True, fontsize=9, node_ids=True, class_names=True)
plt.show()

To show the decision tree as text in scikit-learn, you can use the export_text function from the sklearn.tree module. This function generates a textual representation of the decision tree.

Here’s an example:

from sklearn.tree import DecisionTreeClassifier, export_text
from sklearn.model_selection import train_test_split

# Create a decision tree classifier (you can replace this with your own model)
clf = DecisionTreeClassifier()

# Assuming X_train and y_train are your training data
clf.fit(X_train, y_train)

# Generate a text representation of the decision tree
tree_text = export_text(clf, feature_names=list(X_train.columns), show_weights=True)

# Print the text representation
print(tree_text)

decision tree, plot, tree

Post navigation

Previous Post
Previous post: Python scikit-learn library for Decision Tree model
Next Post
Next post: Feature Importance in Decision Tree

You Might Also Like

No image
BaggingClassifier from Scikit-Learn
April 7, 2024 Comments Off on BaggingClassifier from Scikit-Learn
No image
Post-pruning Decision Tree with Cost Complexity Parameter…
March 8, 2024 Comments Off on Post-pruning Decision Tree with Cost Complexity Parameter ccp_alpha
No image
Pre-pruning Decision Tree – GridSearch for Hyperparameter…
March 8, 2024 Comments Off on Pre-pruning Decision Tree – GridSearch for Hyperparameter tuning
No image
Pre-pruning Decision Tree – depth restricted
March 8, 2024 Comments Off on Pre-pruning Decision Tree – depth restricted
No image
Feature Importance in Decision Tree
March 7, 2024 Comments Off on Feature Importance in Decision Tree
  • Recent
  • Popular
  • Random
  • No image
    8 months ago Low-Rank Factorization
  • No image
    8 months ago Perturbation Test for a Regression Model
  • No image
    8 months ago Calibration Curve for Classification Models
  • No image
    March 15, 20240Single linkage hierarchical clustering
  • No image
    April 17, 20240XGBoost (eXtreme Gradient Boosting)
  • No image
    April 17, 20240Gradient Boosting
  • No image
    March 7, 2024Python scikit-learn library for Decision Tree model
  • No image
    March 5, 2024Receiver Operating Characteristic (ROC) and Area Under…
  • No image
    March 11, 2024What is Mahalanobis Distance
  • Implementation (55)
    • EDA (4)
    • Neural Networks (10)
    • Supervised Learning (26)
      • Classification (17)
      • Linear Regression (8)
    • Unsupervised Learning (11)
      • Clustering (8)
      • Dimensionality Reduction (3)
  • Knowledge Base (44)
    • Python (27)
    • Statistics (6)
June 2025
M T W T F S S
 1
2345678
9101112131415
16171819202122
23242526272829
30  
« Oct    

We are on

FacebookTwitterLinkedinYouTubeGitHubSubscribeEmailRSS

Subscribe

© 2025 Beyond Knowledge Innovation
FacebookTwitterLinkedinYouTubeGitHubSubscribeEmailRSS