AttributeError:模块“pydotplus”没有属性“节点”

Posted

技术标签:

【中文标题】AttributeError:模块“pydotplus”没有属性“节点”【英文标题】:AttributeError: module 'pydotplus' has no attribute 'Node' 【发布时间】:2020-01-02 19:33:08 【问题描述】:

我正在尝试根据我在 DataCamp 上找到的文章绘制我的决策树:https://www.datacamp.com/community/tutorials/decision-tree-classification-python。但是,我收到一个属性错误:

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

decision_tree = DecisionTreeRegressor(max_depth=3)
decision_tree.fit(train_features, train_targets)

# Predict values for train and test
train_predictions = decision_tree.predict(train_features)
test_predictions = decision_tree.predict(test_features)



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())



AttributeError: module 'pydotplus' has no attribute 'Node'

有没有人知道我应该去哪里调查?

【问题讨论】:

与错误相关的行是什么? 它指的是pydotplus包:~\AppData\Local\Programs\Python\Python36\lib\site-packages\pydotplus\parser.py in push_node_stmt(s, loc, toks) 374 node_name = node_name[0] 375 --> 376 n = pydotplus.Node(str(node_name), **attrs) 377 return n 378 10 11 export_graphviz(decision_tree, out_file=dot_data, special_characters=True) ---> 12 graph = pydotplus.graph_from_dot_data(dot_data.getvalue( )) 13 graph.write_png('decision_tree.png') 14 Image(graph.create_png()) 【参考方案1】:

我刚刚尝试了教程中的代码,得到了一个不错的决策树图。我从here下载了数据集。

请尝试以下代码。我用python-3python-2.7 测试了它。

# 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 sklearn.externals.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())

输出:

【讨论】:

以上是关于AttributeError:模块“pydotplus”没有属性“节点”的主要内容,如果未能解决你的问题,请参考以下文章

AttributeError:模块'asyncio'没有属性'run'

AttributeError:模块'mysql'没有属性'connector'

AttributeError:“模块”对象没有属性“作者”

AttributeError:模块 'urllib' 没有属性 'parse'

AttributeError:模块“cupy”没有属性“cupyx”

AttributeError:“模块”对象没有属性