最近,我注意到这里sklearn.tree.export_graphviz
记录了一种方法。
但是,我不知道如何将其应用于RandomForestClassifier
.
我尝试了以下幼稚的代码,但它不起作用,而且我不知道如何从 a 中获取其中一棵树RandomForestClassifier
:
print('Training...')
forest = RandomForestClassifier(n_estimators=100)
forest = forest.fit( train_data[0::,1::], train_data[0::,0] )
print('Predicting...')
output = forest.predict(test_data).astype(int)
if sys.version_info >= (3,0,0):
predictions_file = open("myfirstforest.csv", 'w', newline='')
else:
predictions_file = open("myfirstforest.csv", 'wb')
tree.export_graphviz(forest, out_file='tree.dot')