机器学习入门-随机森林预测温度-不同参数对结果的影响调参 1.RandomedSearchCV(随机参数组的选择) 2.GridSearchCV(网格参数搜索) 3.pprint(顺序打印) 4.rf.get_params(获得当前的输入参数)
使用了RamdomedSearchCV迭代100次,从参数组里面选择出当前最佳的参数组合
在RamdomedSearchCV的基础上,使用GridSearchCV在上面最佳参数的周围选择一些合适的参数组合,进行参数的微调
1. RandomedSearchCV(estimator=rf, param_distributions=param_random, cv=3, verbose=2,random_state=42, n_iter=100) # 随机选择参数组合
参数说明:estimator使用的模型, param_distributions表示待选的参数组合,cv表示交叉验证的次数,verbose表示打印的详细程度,random_state表示随机种子, n_iter迭代的次数
2.GridSearchCV(estimator = rf, param_grid=grid_param, cv=3, verbose=2)
参数说明:estimator使用的模型, param_grid 待选择的参数组合, cv交叉验证的次数,verbose打印的详细程度
3. pprint(rf.get_params())
参数说明:pprint按顺序进行打印, rf.get_params() 表示获得随机森林模型的当前输入参数
代码:
第一步:导入数据
第二步:对数据的文本标签进行one-hot编码
第三步:提取特征和标签
第四步:使用train_test_split将数据分为训练集和测试集
第五步:构建随机森林训练集进行训练
第六步:获得模型特征重要性进行排序,选取前5重要性的特征rf.feature_importances_
第七步:重新构建随机森林的模型
第八步:使用RandomedSearchCV() 进行参数组的随机选择
第九步:根据获得的参数组,使用GridSearchCV() 进行参数组附近的选择,从而对参数组进行微调
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
import time # 第一步读取数据
data = pd.read_csv('data/temps_extended.csv')
# 第二步:对文本标签使用one-hot编码
data = pd.get_dummies(data)
# 第三步:提取特征和标签
X = data.drop('actual', axis=1)
feature_names = np.array(X.columns)
y = np.array(data['actual'])
X = np.array(X)
# 第四步:使用train_test_split进行样本的拆分
train_x, test_x, train_y, test_y = train_test_split(X, y, test_size=0.3, random_state=42) # 第五步:建立模型和预测
rf = RandomForestRegressor(random_state=42, n_estimators=1000)
rf.fit(train_x, train_y)
pre_y = rf.predict(test_x)
# MSE
mse = round(abs(pre_y - test_y).mean(), 2)
error = abs(pre_y - test_y).mean()
# MAPE
mape = round(((1 - abs(pre_y - test_y) / test_y)*100).mean(), 2)
print(mse, mape) # 第六步:选取特征重要性加和达到95%的特征
# 获得特征重要性的得分
feature_importances = rf.feature_importances_
# 将特征重要性得分和特征名进行组合
feature_importances_names = [(feature_name, feature_importance) for feature_name, feature_importance in
zip(feature_names, feature_importances)]
# 对特征重要性进行按照特征得分进行排序
feature_importances_names = sorted(feature_importances_names, key=lambda x: x[1], reverse=True)
# 获得排序后的特征名
feature_importances_n = [x[0] for x in feature_importances_names]
# 获得排序后的特征重要性得分
feature_importances_v = [x[1] for x in feature_importances_names]
feature_importances_v_add = np.cumsum(feature_importances_v)
little_feature_name = feature_importances_n[:np.where([feature_importances_v_add > 0.95])[1][0]+1] # 第七步:选择重要性前5的特征重新建立模型
X = data[little_feature_name].values
y = data['actual'].values # 使用train_test_split进行样本的拆分
train_x, test_x, train_y, test_y = train_test_split(X, y, test_size=0.3, random_state=42)
rf = RandomForestRegressor(random_state=42, n_estimators=1000) # 第八步:使用RandomizedSearchCV随机选择参数组合 # 使用pprint打印rf的参数
from pprint import pprint
pprint(rf.get_params()) from sklearn.model_selection import RandomizedSearchCV
#树的个数
n_estimators = [int(x) for x in range(200, 2000, 100)]
min_samples_leaf = [2, 4, 6]
min_samples_split = [1, 2, 4]
max_features = ['auto', 'sqrt']
bootstrap = [True, False]
max_depth = [int(x) for x in range(10, 100, 10)]
param_random = {
'n_estimators': n_estimators,
'max_depth': max_depth,
'max_features': max_features,
'min_samples_leaf': min_samples_leaf,
'min_samples_split': min_samples_split,
'bootstrap': bootstrap
} rf = RandomForestRegressor()
rf_random = RandomizedSearchCV(estimator=rf, param_distributions=param_random, cv=3, verbose=2,
random_state=42)
rf_random.fit(train_x, train_y)
# 获得最好的训练模型
best_estimator = rf_random.best_estimator_
# 定义用于计算误差和准确度的函数
def Calculation_accuracy(estimator, test_x, test_y):
pre_y = estimator.predict(test_x)
error = abs(pre_y - test_y).mean()
accuraccy = ((1 - abs(pre_y - test_y)/test_y)*100).mean()
return error, accuraccy
# 计算损失值和准确度
error, accuraccy = Calculation_accuracy(best_estimator, test_x, test_y)
print(error, accuraccy)
# 打印最好的参数组合
print(rf_random.best_params_)
# 最好的参数组合 {'n_estimators': 800, 'min_samples_split': 4, 'min_samples_leaf': 4, 'max_features': 'auto',
# 'max_depth': 10, 'bootstrap': 'True'} # 第九步:根据RandomizedSearchCV获得参数,使用GridSearchCV进行参数的微调
from sklearn.model_selection import GridSearchCV n_estimators = [600, 800, 1000]
min_samples_split = [4]
min_samples_leaf = [4]
max_depth = [8, 10, 12]
grid_param = {
'n_estimators': n_estimators,
'min_samples_split': min_samples_split,
'min_samples_leaf': min_samples_leaf,
'max_depth': max_depth
}
rf = RandomForestRegressor()
rf_grid = GridSearchCV(rf, param_grid=grid_param, cv=3, verbose=2)
rf_grid.fit(train_x, train_y)
best_estimator = rf_grid.best_estimator_
error, accuraccy = Calculation_accuracy(best_estimator, test_x, test_y)
print(error, accuraccy)
print(rf_grid.best_params_)
# {'max_depth': 8, 'min_samples_leaf': 4, 'min_samples_split': 4, 'n_estimators': 1000}
机器学习入门-随机森林预测温度-不同参数对结果的影响调参 1.RandomedSearchCV(随机参数组的选择) 2.GridSearchCV(网格参数搜索) 3.pprint(顺序打印) 4.rf.get_params(获得当前的输入参数)的更多相关文章
- 100天搞定机器学习|Day56 随机森林工作原理及调参实战(信用卡欺诈预测)
本文是对100天搞定机器学习|Day33-34 随机森林的补充 前文对随机森林的概念.工作原理.使用方法做了简单介绍,并提供了分类和回归的实例. 本期我们重点讲一下: 1.集成学习.Bagging和随 ...
- 机器学习之路:python 集成回归模型 随机森林回归RandomForestRegressor 极端随机森林回归ExtraTreesRegressor GradientBoostingRegressor回归 预测波士顿房价
python3 学习机器学习api 使用了三种集成回归模型 git: https://github.com/linyi0604/MachineLearning 代码: from sklearn.dat ...
- paper 56 :机器学习中的算法:决策树模型组合之随机森林(Random Forest)
周五的组会如约而至,讨论了一个比较感兴趣的话题,就是使用SVM和随机森林来训练图像,这样的目的就是 在图像特征之间建立内在的联系,这个model的训练,着实需要好好的研究一下,下面是我们需要准备的入门 ...
- R语言︱机器学习模型评估方案(以随机森林算法为例)
笔者寄语:本文中大多内容来自<数据挖掘之道>,本文为读书笔记.在刚刚接触机器学习的时候,觉得在监督学习之后,做一个混淆矩阵就已经足够,但是完整的机器学习解决方案并不会如此草率.需要完整的评 ...
- Python机器学习笔记——随机森林算法
随机森林算法的理论知识 随机森林是一种有监督学习算法,是以决策树为基学习器的集成学习算法.随机森林非常简单,易于实现,计算开销也很小,但是它在分类和回归上表现出非常惊人的性能,因此,随机森林被誉为“代 ...
- SIGAI机器学习第十九集 随机森林
讲授集成学习的概念,Bootstrap抽样,Bagging算法,随机森林的原理,训练算法,包外误差,计算变量的重要性,实际应用 大纲: 集成学习简介 Boostrap抽样 Bagging算法 随机森林 ...
- 机器学习之Bagging与随机森林笔记
集成学习通过将多个学习器进行结合,常可获得比单一学习器显著优越的泛化性能.这对“弱学习器”尤为明显,因此集成学习的很多理论研究都是针对弱学习器进行的,而基学习器有时也被直接称为弱学习器.虽然从理论上来 ...
- 机器学习回顾篇(12):集成学习之Bagging与随机森林
.caret, .dropup > .btn > .caret { border-top-color: #000 !important; } .label { border: 1px so ...
- 什么是机器学习的分类算法?【K-近邻算法(KNN)、交叉验证、朴素贝叶斯算法、决策树、随机森林】
1.K-近邻算法(KNN) 1.1 定义 (KNN,K-NearestNeighbor) 如果一个样本在特征空间中的k个最相似(即特征空间中最邻近)的样本中的大多数属于某一个类别,则该样本也属于这个类 ...
随机推荐
- JavaWeb学习总结(二)-修改Tomcat服务器的端口(半年之后再总结)
一.Tomcat服务器端口的配置 Tomcat的所有配置都放在conf文件夹之中,里面的server.xml文件是配置的核心文件(hibernate.cfg.xml是核心配置文件). 如果想修改Tom ...
- adb学习笔记
一.adb实现原理 adb的目的是想仅在PC端执行adb操作来获取手机里面的文件或向手机内部发送文件.这是通过Ubuntu中adb操作作为客户端与Ubuntu中运行的adb service交互,Ubu ...
- nuclio kubernetes 部署
一张参考架构图: 从图中可以看到nuclio可以运行到docker 以及kubernetes中 提供了kubernetes 部署的脚本 安装 创建命名空间 kubectl create namespa ...
- search bar 创建的一些文章
1. http://blog.csdn.net/oscarxie/article/details/1434608 2. http://blog.csdn.net/oscarxie/articl ...
- nyoj 单调递增子序列(二)
单调递增子序列(二) 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 给定一整型数列{a1,a2...,an}(0<n<=100000),找出单调递增最长 ...
- 01.ubuntu14.04安装HI3518EV200 SDK的过程
转载,侵删 1.海思SDK安装编译 Hi3518EV200_SDK是基于Hi3518EV200_DMEB的软件开发包,包含了在Linux相关应用开发时使用的各种工具及其源代码,是用户开发中最基本的软件 ...
- phper必知必会(一)
1.http返回状态 200:成功,服务器已经成功处理了请求,并正常返回了提供请求的网页 301:永久移动,服务器会将请求转移到新的服务器地址 302:临时移动 401:未授权请求,请求需要身份移动 ...
- 【Android】Android版本和API Level对应关系
API Level Notes Android 4.4 19 KITKAT Platform Highlights Android 4.3 18 JELLY_BEAN_MR2 Platform Hig ...
- keepalived的配置详解(非常详细)
keepalived的配置详解(非常详细) 2017-01-22 15:24 2997人阅读 评论(0) 收藏 举报 分类: 运维学习(25) 转载自:http://blog.csdn.net ...
- android webview setcookie 设置cookie
CookieSyncManager.createInstance(mWebView.getContext()); CookieManager cookieManager = CookieManager ...