当前位置: 首页 > news >正文

数据分析 | 随机森林如何确定参数空间的搜索范围

1. 随机森林超参数

        极其重要的三个超参数是必须要调整的,一般再加上两到三个其他超参数进行优化即可。

2. 学习曲线确定n_estimators搜索范围

        首先导入必要的库,使用sklearn自带的房价预测数据集:

import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.datasets import fetch_california_housing
from sklearn.ensemble import RandomForestRegressor
from sklearn.model_selection import KFold
from sklearn.model_selection import cross_validate
import matplotlib.pyplot as plt
housing = fetch_california_housing()
# 特征数据
X = housing.data[:, [0, 1, 2, 3, 4, 5, 6, 7]]
# 目标变量(房价)
y = housing.target

        初始化以及5折交叉验证计算RMSE:

trainRMSE = np.array([])
testRMSE = np.array([])
trainSTD = np.array([])
testSTD = np.array([])Option = range(5,101,5)
for n_estimators in Option :reg_f = RandomForestRegressor(n_estimators=n_estimators, random_state=1412)# 交叉验证输出结果cv = KFold(n_splits=5,shuffle=True,random_state=1412)result_f = cross_validate(reg_f,X,y,cv=cv,scoring="neg_mean_squared_error",return_train_score=True,n_jobs=-1)# 根据输出的MSE进行RMSE计算train = abs(result_f["train_score"])**0.5test = abs(result_f["test_score"])**0.5trainRMSE = np.append(trainRMSE,train.mean())testRMSE = np.append(testRMSE,test.mean())trainSTD = np.append(trainSTD,train.std())testSTD = np.append(testSTD,test.std())

        定义绘图函数: 

def plotCVresult(Option,trainRMSE,testRMSE,trainSTD,testSTD) :xaxis = Option# RMSEplt.plot(xaxis, trainRMSE,color='k',label='RandomForestTrain')plt.plot(xaxis, testRMSE, color='red', label='RandomForestTest')# 将标准差围绕在RMSE旁边,区间越大表示模型越不稳定plt.plot(xaxis, trainRMSE + trainSTD, color='k', linestyle='dotted')plt.plot(xaxis, trainRMSE - trainSTD, color='k', linestyle='dotted')plt.plot(xaxis, testRMSE + testSTD, color='red', linestyle='dotted')plt.plot(xaxis, testRMSE - testSTD, color='red', linestyle='dotted')plt.xticks([*xaxis])plt.legend(loc=1)plt.xlabel('n_estimators')plt.ylabel('RMSE')plt.title('Learning Curve')plt.show()plotCVresult(Option,trainRMSE,testRMSE,trainSTD,testSTD)

        输出结果如下: 

 3. 使用Tree模块判断max_depth搜索范围

        只需在输出的最小值和最大值之间进行搜索即可。

reg_f = RandomForestRegressor(n_estimators=100,random_state=1412)
reg_f = reg_f.fit(X,y)
d = pd.Series([],dtype="int64")
for idx,t in enumerate(reg_f.estimators_) :d[idx] = t.tree_.max_depth
print('决策树的最大深度的最小值为:',d.min())
print('决策树的最大深度的最大值为:',d.max())

        输出结果为:

4. 使用Tree模块判断min_weight_fraction_leaf搜索范围

reg_f = RandomForestRegressor(n_estimators=100,random_state=1412)
reg_f = reg_f.fit(X,y)
n = pd.Series([],dtype="int64")
for idx,t in enumerate(reg_f.estimators_) :n[idx] = t.tree_.weighted_n_node_samples
meann = np.zeros(20)
for i in range(0,20) :meann[i] = n[i].mean()
print('决策树分枝所需最小样本权重的最小值为:',meann.min())
print('决策树分枝所需最小样本权重的最大值为:',meann.max())
print('决策树分枝所需最小样本权重的平均值为:',meann.mean())

        输出结果为:

 5. 使用Tree模块判断min_sample_split搜索范围

reg_f = RandomForestRegressor(n_estimators=20,random_state=1412)
reg_f = reg_f.fit(X,y)
s = pd.Series([],dtype="int64")
for idx,t in enumerate(reg_f.estimators_) :s[idx] = t.tree_.n_node_samples
meann = np.zeros(20)
for i in range(0,20) :meann[i] = s[i].mean()
print('决策树需要最小样本的最小值为:',meann.min())
print('决策树需要最小样本的最大值为:',meann.max())
print('决策树需要最小样本的平均值为:',meann.mean())

        输出结果为:

 

 

http://www.lryc.cn/news/126836.html

相关文章:

  • 5G+AI数字化智能工厂建设解决方案PPT
  • Windows配置编译ffmpeg +音视频地址
  • C语言 常用工具型API --------system()
  • 车规级半导体分类(汽车芯片介绍)
  • opencv图像轮廓检测
  • 诚迈科技荣膺小米“最佳供应商奖”
  • 分布式 - 消息队列Kafka:Kafka 消费者的消费位移
  • H3C QoS打标签和限速配置案例
  • 带curl的docker镜像image
  • Hadoop数据迁移distcp
  • QT-Mysql数据库图形化接口
  • LeetCode150道面试经典题-- 合并两个有序链表(简单)
  • GitHub 如何部署写好的H5静态页面
  • SharkTeam:Worldcoin运营数据及业务安全分析
  • C语言编程练习
  • vue入门(增查改!)
  • 移动端身份证识别技术的应用,告别手动录入证件信息
  • 网络通信原理TCP字段解析(第四十七课)
  • uniapp微信小程序消息订阅快速上手
  • MySQL 根据多字段查询重复数据
  • Markdown编辑器 Mac版Typora功能介绍
  • el-form自定义校验规则
  • xml对象与字符串互换
  • 单例模式和多例模式和工厂模式
  • 【网络架构】华为hw交换机网络高可用网络架构拓扑图以及配置
  • 信也科技一面凉经
  • AI商业化如何落地?看设计师如何利用AI细化工作流
  • 论文阅读 - Understanding Diffusion Models: A Unified Perspective
  • [Python进阶] 定制类:模拟篇
  • HTML5 游戏开发实战 | 五子棋