【问题标题】:AttributeError: module 'pydotplus' has no attribute 'graph_from_dot_data'AttributeError:模块“pydotplus”没有属性“graph_from_dot_data”
【发布时间】:2021-03-24 09:20:59
【问题描述】:

我正在尝试根据从 AttributeError: module 'pydotplus' has no attribute 'Node' 找到的文章绘制示例决策树。但是,我仍然收到属性错误:

# Load libraries
import pandas as pd
from sklearn.tree import DecisionTreeClassifier # Import Decision Tree Classifier
from sklearn.model_selection import train_test_split # Import train_test_split function
from sklearn import metrics #Import scikit-learn metrics module for accuracy calculation


col_names = ['Pregnancies', 'Glucose', 'BloodPressure', 'SkinThickness', 'Insulin', 'BMI', 'DiabetesPedigreeFunction', 'Age', 'Outcome']
# load dataset
pima = pd.read_csv("diabetes.csv", header=None, names=col_names)



#split dataset in features and target variable
feature_cols = ['Pregnancies', 'Insulin', 'BMI', 'Age','Glucose','BloodPressure','SkinThickness']
X = pima[feature_cols] # Features
y = pima['Outcome']# Target variable


# Split dataset into training set and test set
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=1) # 70% training and 30% test


# Create Decision Tree classifer object
clf = DecisionTreeClassifier()

# Train Decision Tree Classifer
clf = clf.fit(X_train,y_train)


from sklearn.tree import export_graphviz
from six import StringIO
from IPython.display import Image
import pydotplus
from sklearn.tree import DecisionTreeRegressor

decision_tree = DecisionTreeRegressor(max_depth=3)
decision_tree.fit(X_train, y_train)

# Predict values for train and test
# train_predictions = decision_tree.predict(X_train)
# test_predictions = decision_tree.predict(X_test)



dot_data = StringIO()

export_graphviz(decision_tree, out_file=dot_data,  filled=True, rounded=True, special_characters=True)
graph = pydotplus.graph_from_dot_data(dot_data.getvalue())
graph.write_png('decision_tree.png')
Image(graph.create_png())

错误是

Traceback (most recent call last):
  File "D:/treePlotter/test.py", line 49, in <module>
    graph = pydotplus.graph_from_dot_data(dot_data.getvalue())
AttributeError: module 'pydotplus' has no attribute 'graph_from_dot_data'

谁能给我解释一下是什么问题?万分感谢! Pycharm 3.8

【问题讨论】:

    标签: python attributeerror pydotplus


    【解决方案1】:

    pydotplus 中,graph_from_dot_datapydotplus.graphviz 包中。

    使用

    pydotplus.graphviz.graph_from_dot_data(...)

    【讨论】:

      猜你喜欢
      • 2020-01-02
      • 2016-05-19
      • 2018-04-14
      • 2019-02-18
      • 1970-01-01
      • 2020-01-01
      • 2019-07-20
      • 2021-11-05
      相关资源
      最近更新 更多