[英]random forest classifier visualization
我正在尝试可视化我的 RandomForestClassifier 的结果
帮我找出这个错误
我的模型已经过训练,所以我不明白为什么我会得到这个。
rfc = RandomForestClassifier(n_estimators = 1000)
fit_rfc = rfc.fit(train_x, train_y)
dot_data = StringIO()
export_graphviz(fit_rfc, out_file = dot_data, feature_names = features,
filled = True, rounded=True)
graph = pydot.graph_from_dot_data(dot_data.getvalue())
Image(graph[0].create_png())
NotFittedError Traceback (most recent call last)
<ipython-input-101-5b7775a2ce71> in <module>
1 dot_data = StringIO()
----> 2 export_graphviz(fit_rfc, out_file = dot_data, feature_names = features, filled = True, rounded=True)
3
4 graph = pydot.graph_from_dot_data(dot_data.getvalue())
5 Image(graph[0].create_png())
~\Documents\Python\Anaconda\lib\site-packages\sklearn\tree\export.py in export_graphviz(decision_tree, out_file, max_depth, feature_names, class_names, label, filled, leaves_parallel, impurity, node_ids, proportion, rotate, rounded, special_characters, precision)
394 out_file.write('%d -> %d ;\n' % (parent, node_id))
395
--> 396 check_is_fitted(decision_tree, 'tree_')
397 own_file = False
398 return_string = False
~\Documents\Python\Anaconda\lib\site-packages\sklearn\utils\validation.py in check_is_fitted(estimator, attributes, msg, all_or_any)
949
950 if not all_or_any([hasattr(estimator, attr) for attr in attributes]):
--> 951 raise NotFittedError(msg % {'name': type(estimator).__name__})
952
953
NotFittedError: This RandomForestClassifier instance is not fitted yet. Call 'fit' with appropriate arguments before using this method.
您的模型必须经过训练才能具有任何表示。
您可以通过发出这样的fit
方法来做到这一点(替换您的数据):
X, y = make_classification(n_samples=1000, n_features=4,
n_informative=2, n_redundant=0,
random_state=0, shuffle=False)
clf = RandomForestClassifier(n_estimators=100, max_depth=2,
random_state=0)
clf.fit(X, y)
拟合后,您可以将其另存为图形
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.