Python 3.x 雷多姆森林的树木结构太大,无法目测,如何浓缩?
我为一个分类问题训练了一个平衡森林,现在我想画一棵树。但是,当使用Python 3.x 雷多姆森林的树木结构太大,无法目测,如何浓缩?,python-3.x,scikit-learn,graphviz,decision-tree,Python 3.x,Scikit Learn,Graphviz,Decision Tree,我为一个分类问题训练了一个平衡森林,现在我想画一棵树。但是,当使用export_graphviz执行此操作时,树输出太大,我不希望所有树都显示,只显示一个随机树。我怎样才能做到这一点 #train balanced random forest brf = BalancedRandomForestClassifier(n_estimators=50, random_state=0) #fit model brf.fit(X_train, y_train) y_pred_brf = brf.pred
export_graphviz
执行此操作时,树输出太大,我不希望所有树都显示,只显示一个随机树。我怎样才能做到这一点
#train balanced random forest
brf = BalancedRandomForestClassifier(n_estimators=50, random_state=0)
#fit model
brf.fit(X_train, y_train)
y_pred_brf = brf.predict(X_test)
# Extract single tree
estimator = brf.estimators_[1]
# Export as dot file
export_graphviz(estimator,
out_file='tree.dot',
feature_names = ['feature_1','feature_2','feature_3'],
class_names =['fraud','no_fraud'],
rounded = True,
proportion = False,
precision = 2,
filled = True)
# Convert to png using system command (requires Graphviz)
from subprocess import call
call(['dot', '-Tpng', 'tree.dot', '-o', 'tree.png', '-Gdpi=100'])
# Display in jupyter notebook
from IPython.display import Image
Image(filename = 'tree.png')
我得到的输出是这个巨大的树结构(这个屏幕截图甚至没有显示整个树,它甚至更大!):我需要在模型中添加
max\u depth
,例如:
brf = BalancedRandomForestClassifier(n_estimators=50, random_state=0, max_depth = 2)
max_depth
影响显示的深度,而不是树的数量,树的数量在代码中已经是1。您的标题有误导性,我建议您相应地编辑它。@desertnaut,所以这是一个添加最大深度和n_估计量的问题。