import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestRegressor
from sklearn.tree import export_graphviz
import pydot
import os
读取数据集
data = pd.read_csv(‘your_dataset.csv’) # 请替换成你的数据集文件名
准备特征和标签
X = data.drop(‘target_column_name’, axis=1) # 请替换为你的目标列名
y = data[‘target_column_name’]
划分训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=42)
创建随机森林模型
rf = RandomForestRegressor(n_estimators=100, random_state=42)
rf.fit(X_train, y_train)
随机森林模型预测
predictions = rf.predict(X_test)
绘制随机森林中的一棵树
tree = rf.estimators_[0] # 获取第一棵树
export_graphviz(tree, out_file=‘tree.dot’, feature_names=X.columns, rounded=True, precision=1)
(graph, ) = pydot.graph_from_dot_file(‘tree.dot’)
graph.write_png(‘tree.png’)
打印模型评估指标等
…
显示树的可视化
plt.figure(figsize=(20, 10))
plt.imshow(plt.imread(‘tree.png’))
plt.axis(‘off’)
plt.show()