为什么我打印随机森林每个决策树结构,打印出来的节点value值的加和跟sample对不上(这是其中一个决策树的结构)
from sklearn.ensemble import RandomForestClassifier
from sklearn import tree
import graphviz
from sklearn.datasets import make_classification
import numpy as np
# 生成示例数据
X_train, y_train = make_classification(n_samples=100, n_features=4)
# 创建随机森林分类器
rf = RandomForestClassifier(n_estimators=10)
# 假设你有训练数据 X_train 和对应的目标变量 y_train
rf.fit(X_train, y_train)
# 获取每个决策树
estimators = rf.estimators_
# 遍历每个决策树并绘制结构图
for i, estimator in enumerate(estimators):
dot_data = tree.export_graphviz(estimator)
graph = graphviz.Source(dot_data)
graph.render(f"D:\\Files\\Study Program\\tree/tree_{i+1}", format="png")