I want to show decision tree figure for my data visualization. But there is an errror appeared in the console.
AttributeError: module 'sklearn.tree' has no attribute 'plot_tree'
Although I install extra modules via !pip install -U scikit-learn
and !pip install --upgrade sklearn
, the error cannot be solved.
How can I fix the issue?
Here is my code shown below
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
from sklearn.model_selection import train_test_split
# from sklearn.tree import DecisionTreeClassifier
from sklearn import tree
data = pd.read_csv("files/data.csv")
data.drop(["id","Unnamed: 32"],axis=1,inplace=True)
data.diagnosis = [1 if each == "M" else 0 for each in data.diagnosis]
y = data["diagnosis"].values
x_data = data.drop(["diagnosis"],axis=1)
x = (x_data - np.min(x_data))/(np.max(x_data)-np.min(x_data))
x_train, x_test, y_train, y_test = train_test_split(x,y,test_size = 0.3,random_state=1)
dt = DecisionTreeClassifier()
dt.fit(x_train,y_train)
print("score: ", dt.score(x_test,y_test))
tree.plot_tree(dt,
feature_names = data.columns,
rounded = True,
filled = True,
class_names = ["diagnosis"],
impurity = True)
Here is my answer.
After updating all packages defined in Anaconda Navigator, the code snippet I wrote works flawlessly.