我正在使用示例s
我导入(.dta
文件)中的一些数据到python 3.我做了一些机器学习分析。我偶然发现了用scikit-learn
和graphviz
绘制决策树的简洁方法。
这是我的工作(在一些包和库导入等旁边):
# import data and give a little overview
sample = pd.read_stata('sample_data.dta')
s = sample
# some learning
X = np.array((s.X).reshape(100, 1))
y = np.array(s.y)
X_train, X_test, y_train, y_test = tts(X, y, test_size = .8)
my_tree = tree.DecisionTreeClassifier()
clf = my_tree.fit(X_train, y_train)
from IPython.display import Image
dot_data = StringIO()
tree.export_graphviz(clf, out_file=dot_data,
feature_names=s.feature_names,
class_names=s.target_names,
filled=True, rounded=True,
special_characters=True)
graph = pydot.graph_from_dot_data(dot_data.getvalue())
Image(graph.create_png())
一切都很好,直到我想要绘制树形图。我收到以下错误:
AttributeError: 'DataFrame' object has no attribute 'feature_names'
如何将feature_names
分配给s
?