визуализация классификатора случайного леса - PullRequest
0 голосов
/ 21 марта 2019

Я пытаюсь визуализировать результаты моего 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.

1 Ответ

0 голосов
/ 21 марта 2019

Ваша модель должна быть обучена, чтобы иметь какое-либо представление.

Вы можете сделать это, выполнив 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)

Послепримерка вы можете сохранить как график

...