【问题标题】:Decision Tree AttributeError: module 'sklearn.tree' has no attribute 'plot_tree' Error in Jupyter Notebook决策树 AttributeError:模块“sklearn.tree”在 Jupyter Notebook 中没有属性“plot_tree”错误
【发布时间】:2020-08-27 06:07:20
【问题描述】:

我想为我的数据可视化显示决策树图。但是控制台出现错误。

AttributeError: module 'sklearn.tree' has no attribute 'plot_tree'

虽然我通过!pip install -U scikit-learn!pip install --upgrade sklearn安装了额外的模块,但还是无法解决错误。

我该如何解决这个问题?

下面是我的代码

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)

【问题讨论】:

  • 你运行的是什么版本的 scikit learn?
  • @Mike print(sklearn.__version__) -> 0.20.1
  • 我相信你需要运行 0.22
  • @Mike 好的,我该如何解决?
  • 你运行的是python的anaconda发行版还是标准发行版

标签: python machine-learning decision-tree


【解决方案1】:

这是我的答案。

在更新 Anaconda Navigator 中定义的所有包后,我编写的代码 sn-p 可以完美运行。

【讨论】:

    猜你喜欢
    • 2021-09-25
    • 1970-01-01
    • 2020-12-28
    • 1970-01-01
    • 2021-05-27
    • 2021-12-30
    • 1970-01-01
    • 2020-12-12
    • 2018-09-26
    相关资源
    最近更新 更多