# -*-coding:utf-8-*- from matplotlib import pyplot as plt from pandas.core.common import random_state import time from sklearn.datasets import load_diabetes from sklearn.metrics import mean_squared_error, mean_absolute_error from sklearn.model_selection import RandomizedSearchCV, train_test_split from scipy.stats import randint from sklearn.tree import DecisionTreeRegressor, plot_tree import matplotlib.pyplot as plt import numpy as np import time # 加载糖尿病数据集 diabetes_data = load_diabetes() X = diabetes_data.data y = diabetes_data.target # 将数据集分割为训练集和测试集 X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) # 定义参数网格 param_dist = { 'criterion': ['mse', 'friedman_mse'], 'max_depth': randint(5, 30), 'min_samples_split': randint(2, 20), 'min_samples_leaf': randint(5, 20), 'max_features': ['auto', 'sqrt'] } # 创建回归决策树模型 regressor = DecisionTreeRegressor(random_state=50) start_time = time.time() # 使用随机搜索进行参数调优 random_search = RandomizedSearchCV(estimator=regressor, param_distributions=param_dist, n_iter=100, cv=5, scoring='neg_mean_squared_error') random_search.fit(X_train, y_train) end_time = time.time() # 输出最优参数和对应的评分 print("最优参数:", random_search.best_params_) print("最优评分:", -random_search.best_score_) training_time = end_time - start_time print("训练时间:", training_time, "秒") # 使用最优参数重新训练模型 best_regressor = random_search.best_estimator_ best_regressor.fit(X_train, y_train) # 可视化决策树 plt.figure(figsize=(20,10)) plot_tree(best_regressor, filled=True, feature_names=diabetes_data.feature_names) plt.draw() plt.show() # 预测测试集结果 y_pred = best_regressor.predict(X_test) # 计算均方误差(MSE) mse = mean_squared_error(y_test, y_pred) print("均方误差(MSE):", mse) # 计算均方根误差(RMSE) rmse = mean_squared_error(y_test, y_pred, squared=False) print("均方根误差(RMSE):", rmse) # 计算平均绝对误差(MAE) mae = mean_absolute_error(y_test, y_pred) print("平均绝对误差(MAE):", mae) # 计算决定系数(R^2) r_squared = best_regressor.score(X_test, y_test) print("决定系数(R^2):", r_squared) # 绘制预测值和实际值的散点图 plt.rcParams['font.sans-serif'] = ['SimSun', 'Songti SC', 'Microsoft YaHei'] plt.scatter(y_test, y_pred) plt.xlabel("实际值") plt.ylabel("预测值") # 计算并绘制趋势线或回归线 trendline_x = np.linspace(np.min(y_test), np.max(y_test), 100) trendline_y = trendline_x plt.plot(trendline_x, trendline_y, color='red') plt.show()
04-05
690
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
07-17
3万+
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交