简体   繁体   English

决策树 AttributeError:模块“sklearn.tree”在 Jupyter Notebook 中没有属性“plot_tree”错误

[英]Decision Tree AttributeError: module 'sklearn.tree' has no attribute 'plot_tree' Error in Jupyter Notebook

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.虽然我通过!pip install -U scikit-learn!pip install --upgrade sklearn sklearn 安装了额外的模块,但无法解决错误。

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.更新 Anaconda Navigator 中定义的所有包后,我编写的代码片段完美运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM