How to visualise a tree model Multiclass Classification?

How to visualise a tree model Multiclass Classification?

How to visualise a tree model Multiclass Classification?

This recipe helps you visualise a tree model Multiclass Classification

In [2]:
## How to visualise a tree model - Multiclass Classification
## DataSet: skleran.datasets.load_wine()
def Snippet_180():
    print(format('How to visualise a tree model - Multiclass Classification','*^82'))

    import warnings

    # load libraries 
    from sklearn import datasets
    from sklearn import metrics
    from sklearn.model_selection import train_test_split
    import matplotlib.pyplot as plt'ggplot')

    from sklearn import tree
    from sklearn.externals.six import StringIO
    import pydotplus

    # load the wine datasets
    dataset = datasets.load_wine()
    X =; y =
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25)

    # fit a tree.DecisionTreeClassifier() model to the data
    model = tree.DecisionTreeClassifier(), y_train)
    print(); print(model)

    # make predictions
    expected_y  = y_test
    predicted_y = model.predict(X_test)

    # summarize the fit of the model
    print(); print('tree.DecisionTreeClassifier(): ')
    print(); print(metrics.classification_report(expected_y, predicted_y,
    print(); print(metrics.confusion_matrix(expected_y, predicted_y))

    # Create a dot file
    dotfile = open("", 'w')
    tree.export_graphviz(model, out_file = dotfile, feature_names = dataset.feature_names)

    # Create pdf and png from the dot data
    dot_data = StringIO()
    tree.export_graphviz(model, out_file=dot_data,  filled=True, rounded=True,
                         special_characters=True, feature_names = dataset.feature_names)
    graph = pydotplus.graph_from_dot_data(dot_data.getvalue())


    import matplotlib.image as mpimg
    img = mpimg.imread('tree.png')
    #img = misc.imread("tree.png")
************How to visualise a tree model - Multiclass Classification*************

DecisionTreeClassifier(class_weight=None, criterion='gini', max_depth=None,
            max_features=None, max_leaf_nodes=None,
            min_impurity_decrease=0.0, min_impurity_split=None,
            min_samples_leaf=1, min_samples_split=2,
            min_weight_fraction_leaf=0.0, presort=False, random_state=None,


              precision    recall  f1-score   support

     class_0       0.85      0.92      0.88        12
     class_1       0.89      0.85      0.87        20
     class_2       0.92      0.92      0.92        13

   micro avg       0.89      0.89      0.89        45
   macro avg       0.89      0.90      0.89        45
weighted avg       0.89      0.89      0.89        45

[[11  1  0]
 [ 2 17  1]
 [ 0  1 12]]

Relevant Projects

Data Science Project on Wine Quality Prediction in R
In this R data science project, we will explore wine dataset to assess red wine quality. The objective of this data science project is to explore which chemical properties will influence the quality of red wines.

Predict Credit Default | Give Me Some Credit Kaggle
In this data science project, you will predict borrowers chance of defaulting on credit loans by building a credit score prediction model.

Zillow’s Home Value Prediction (Zestimate)
Data Science Project in R -Build a machine learning algorithm to predict the future sale prices of homes.

Human Activity Recognition Using Smartphones Data Set
In this deep learning project, you will build a classification system where to precisely identify human fitness activities.

PySpark Tutorial - Learn to use Apache Spark with Python
PySpark Project-Get a handle on using Python with Spark through this hands-on data processing spark python tutorial.

Choosing the right Time Series Forecasting Methods
There are different time series forecasting methods to forecast stock price, demand etc. In this machine learning project, you will learn to determine which forecasting method to be used when and how to apply with time series forecasting example.

Data Science Project-TalkingData AdTracking Fraud Detection
Machine Learning Project in R-Detect fraudulent click traffic for mobile app ads using R data science programming language.

Solving Multiple Classification use cases Using H2O
In this project, we are going to talk about H2O and functionality in terms of building Machine Learning models.

Predict Churn for a Telecom company using Logistic Regression
Machine Learning Project in R- Predict the customer churn of telecom sector and find out the key drivers that lead to churn. Learn how the logistic regression model using R can be used to identify the customer churn in telecom dataset.

Mercari Price Suggestion Challenge Data Science Project
Data Science Project in Python- Build a machine learning algorithm that automatically suggests the right product prices.