【问题标题】:random forest classifier visualization随机森林分类器可视化
【发布时间】:2019-08-11 19:00:22
【问题描述】:

我正在尝试可视化我的 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.

【问题讨论】:

    标签: python visualization random-forest


    【解决方案1】:

    您的模型必须经过训练才能具有任何表示。

    您可以通过发出这样的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)
    

    拟合后可以保存为图表

    【讨论】:

      猜你喜欢
      • 2018-02-18
      • 2018-05-20
      • 2020-08-08
      • 2018-03-05
      • 1970-01-01
      • 2019-09-05
      • 2013-09-22
      • 2020-07-02
      • 1970-01-01
      相关资源
      最近更新 更多