随机森林分类器可视化

时间:2019-03-20 23:38:27

标签: python visualization random-forest

我正在尝试可视化我的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 :(得分:0)

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

您可以通过发出import pyaudio import wave, struct import numpy as np waveFile = waveFile.open(example, 'r') input=[] length = waveFile.getnframes() for i in range(0,length): waveData=waveFile.readframes(1) data = struct.unpack("<h", waveData) t=list(data) tmp[0]=tmp[0]*10 data = struct.pack("<h", tmp[i]) input.append(data) input=np.asarray(input) 这样的方法(替换您的数据)来做到这一点:

fit

拟合后,您可以将其另存为图形