Visualising a model gives a better representation of how the model is working. Tree models are one of the easiest to visualise.
So this recipe is a short example of how we can visualise a tree model - Multiclass Classification.
from sklearn import datasets from sklearn import metrics from sklearn.model_selection import train_test_split import matplotlib.pyplot as plt plt.style.use("ggplot") from sklearn import tree from sklearn.externals.six import StringIO import pydotplus
Here we have imported various modules like datasets, StringIO and test_train_split from differnt libraries. We will understand the use of these later while using it in the in the code snipet.
For now just have a look on these imports.
Here we have used datasets to load the inbuilt wine dataset and we have created objects X and y to store the data and the target value respectively.
dataset = datasets.load_wine()
X = dataset.data; y = dataset.target
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25)
Here, we are using DecisionTreeClassifier as a Machine Learning model to fit the data.
model = tree.DecisionTreeClassifier()
Now we have predicted the output by passing X_test and also stored real target in expected_y.
expected_y = y_test
predicted_y = model.predict(X_test)
Here we have printed classification report and confusion matrix for the classifier.
We have created a dot file for the tree and used it to create png and pdf. By using matplotlib we have created a image of the tree with the conditions used by the model.
dotfile = open("tree.dot", "w")
tree.export_graphviz(model, out_file = dotfile, feature_names = dataset.feature_names)
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")
As an output we get: