I am following Müller & Guido's Introduction to Machine Learning, and have encountered an error I do not know how to fix.
My code looks like this:
import os
import graphviz
import sklearn
import IPython
import matplotlib
import numpy as np
import scipy as sp
import pandas as pd
from matplotlib import pyplot as plt
from mglearn import plots
from sklearn.tree import DecisionTreeClassifier, export_graphviz, DecisionTreeRegressor
from sklearn.datasets import load_breast_cancer, make_moons
from sklearn.ensemble import RandomForestClassifier
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
# (1) Run a single decision tree on the breast cancer dataset
cancer = load_breast_cancer()
X_train, X_test, y_train, y_test = train_test_split(cancer.data, cancer.target, stratify=cancer.target, random_state=42)
# Accuracy
tree = DecisionTreeClassifier(max_depth=4, random_state=0).fit(X_train, y_train)
print("Accuracy on training set: {:.3f}".format(tree.score(X_train, y_train)))
print("Accuracy on test set: {:.3f}".format(tree.score(X_test, y_test)))
# Vizualization of the descision tree built on the Breast Cancer dataset
export_graphviz(tree, out_file="tree.dot", class_names=["malignant", "benign"], feature_names=cancer.feature_names,
impurity=False, filled=True)
with open("tree.dot") as f:
dot_graph = f.read()
display(graphviz.Source.dot_graph)
And it gives me this output:
Out[] Accuracy on training set: 0.988
Accuracy on test set: 0.951
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_17928/1605121895.py in <module>
5 dot_graph = f.read()
6
----> 7 display(graphviz.Source.dot_graph)
AttributeError: type object 'Source' has no attribute 'dot_graph'
I am not sure how to fix this, as I have only followed the book. All help is appreciated!