基于随机森林的回归分析,随机森林工具箱,随机森林的详细原理

news2024/10/6 6:41:08

目录
背影
摘要
随机森林的基本定义
随机森林实现的步骤
基于随机森林的回归分析
随机森林回归分析完整代码及工具箱下载链接: 随机森林分类工具箱,分类随机森林,随机森林回归工具箱,回归随机森林资源-CSDN文库 https://download.csdn.net/download/abc991835105/88137234
效果图
结果分析
展望
参考论文

背影

传统的回归分析一般用最小二乘等进行拟合,局部优化能力有限,本文用随机森林进行回归分析,利用每个树的增强局部优化,从而更好的拟合自变量与因变量的关系,实现回归分析的提高
摘要
LSTM原理,MATALB编程长短期神经网络LSTM的飞行轨迹预测。

随机森林的基本定义

在机器学习中,随机森林是一个包含多个决策树的分类器, 并且其输出的类别是由个别树输出的类别的众数而定。 Leo Breiman和Adele Cutler发展出推论出随机森林的算法。 而 “Random Forests” 是他们的商标。 这个术语是1995年由贝尔实验室的Tin Kam Ho所提出的随机决策森林(random decision forests)而来的。这个方法则是结合 Breimans 的 “Bootstrap aggregating” 想法和 Ho 的"random subspace method"以建造决策树的集合。

训练方法

根据下列算法而建造每棵树 [1] :
用N来表示训练用例(样本)的个数,M表示特征数目。
输入特征数目m,用于确定决策树上一个节点的决策结果;其中m应远小于M。
从N个训练用例(样本)中以有放回抽样的方式,取样N次,形成一个训练集(即bootstrap取样),并用未抽到的用例(样本)作预测,评估其误差。
对于每一个节点,随机选择m个特征,决策树上每个节点的决定都是基于这些特征确定的。根据这m个特征,计算其最佳的分裂方式。
每棵树都会完整成长而不会剪枝,这有可能在建完一棵正常树状分类器后会被采用)

随机森林的优缺点

随机森林的优点有 [2] :
1)对于很多种资料,它可以产生高准确度的分类器;
2)它可以处理大量的输入变数;
3)它可以在决定类别时,评估变数的重要性;
4)在建造森林时,它可以在内部对于一般化后的误差产生不偏差的估计;
5)它包含一个好方法可以估计遗失的资料,并且,如果有很大一部分的资料遗失,仍可以维持准确度;
6)它提供一个实验方法,可以去侦测variable interactions;
7)对于不平衡的分类资料集来说,它可以平衡误差;
8)它计算各例中的亲近度,对于数据挖掘、侦测离群点(outlier)和将资料视觉化非常有用;
9)使用上述。它可被延伸应用在未标记的资料上,这类资料通常是使用非监督式聚类。也可侦测偏离者和观看资料;
10)学习过程是很快速的。

决策树的构建过程

要说随机森林,必须先讲决策树。决策树是一种基本的分类器,一般是将特征分为两类(决策树也可以用来回归,不过本文中暂且不表)。构建好的决策树呈树形结构,可以认为是if-then规则的集合,主要优点是模型具有可读性,分类速度快。
我们用选择量化工具的过程形象的展示一下决策树的构建。假设要选择一个优秀的量化工具来帮助我们更好的炒股,怎么选呢?
第一步:看看工具提供的数据是不是非常全面,数据不全面就不用。
第二步:看看工具提供的API是不是好用,API不好用就不用。
第三步:看看工具的回测过程是不是靠谱,不靠谱的回测出来的策略也不敢用啊。
第四步:看看工具支不支持模拟交易,光回测只是能让你判断策略在历史上有用没有,正式运行前起码需要一个模拟盘吧。
这样,通过将“数据是否全面”,“API是否易用”,“回测是否靠谱”,“是否支持模拟交易”将市场上的量化工具贴上两个标签,“使用”和“不使用”。
上面就是一个决策树的构建,逻辑可以用图1表示:
在这里插入图片描述

​基于MATLAB编程的随机森林回归分析

clc
close all

%compile everything
if strcmpi(computer,‘PCWIN’) |strcmpi(computer,‘PCWIN64’)
compile_windows
else
compile_linux
end

total_train_time=0;
total_test_time=0;

%diabetes
load data/diabetes

%modify so that training data is NxD and labels are Nx1, where N=#of
%examples, D=# of features

X = diabetes.x;
Y = diabetes.y;

[N D] =size(X);
%randomly split into 400 examples for training and 42 for testing
randvector = randperm(N);

X_trn = X(randvector(1:400)😅;
Y_trn = Y(randvector(1:400));
X_tst = X(randvector(401:end)😅;
Y_tst = Y(randvector(401:end));

% example 1: simply use with the defaults
model = regRF_train(X_trn,Y_trn);
Y_hat = regRF_predict(X_tst,model);
fprintf(‘\nexample 1: MSE rate %f\n’, sum((Y_hat-Y_tst).^2));

% example 2: set to 100 trees
model = regRF_train(X_trn,Y_trn, 100);
Y_hat = regRF_predict(X_tst,model);
fprintf(‘\nexample 2: MSE rate %f\n’, sum((Y_hat-Y_tst).^2));

% example 3: set to 100 trees, mtry = 2
model = regRF_train(X_trn,Y_trn, 100,2);
Y_hat = regRF_predict(X_tst,model);
fprintf(‘\nexample 3: MSE rate %f\n’, sum((Y_hat-Y_tst).^2));

% example 4: set to defaults trees and mtry by specifying values as 0
model = regRF_train(X_trn,Y_trn, 0, 0);
Y_hat = regRF_predict(X_tst,model);
fprintf(‘\nexample 4: MSE rate %f\n’, sum((Y_hat-Y_tst).^2));

% % example 5: set sampling without replacement (default is with replacement)
extra_options.replace = 0 ;
model = regRF_train(X_trn,Y_trn, 100, 4, extra_options);
Y_hat = regRF_predict(X_tst,model);
fprintf(‘\nexample 5: MSE rate %f\n’, sum((Y_hat-Y_tst).^2));

% example 6: sampsize example
% extra_options.sampsize = Size(s) of sample to draw. For classification,
% if sampsize is a vector of the length the number of strata, then sampling is stratified by strata,
% and the elements of sampsize indicate the numbers to be drawn from the strata.
clear extra_options
extra_options.sampsize = size(X_trn,1)*2/3;

model = regRF_train(X_trn,Y_trn, 100, 4, extra_options);
Y_hat = regRF_predict(X_tst,model);
fprintf('\nexample 6: MSE rate %f\n',   sum((Y_hat-Y_tst).^2));

% example 7: nodesize
% extra_options.nodesize = Minimum size of terminal nodes. Setting this number larger causes smaller trees
% to be grown (and thus take less time). Note that the default values are different
% for classification (1) and regression (5).
clear extra_options
extra_options.nodesize = 7;

model = regRF_train(X_trn,Y_trn, 100, 4, extra_options);
Y_hat = regRF_predict(X_tst,model);
fprintf('\nexample 7: MSE rate %f\n',   sum((Y_hat-Y_tst).^2));

% example 8: calculating importance
clear extra_options
extra_options.importance = 1; %(0 = (Default) Don’t, 1=calculate)

model = regRF_train(X_trn,Y_trn, 100, 4, extra_options);
Y_hat = regRF_predict(X_tst,model);
fprintf('\nexample 8: MSE rate %f\n',   sum((Y_hat-Y_tst).^2));

%model will have 3 variables for importance importanceSD and localImp
%importance = a matrix with nclass + 2 (for classification) or two (for regression) columns.
%           For classification, the first nclass columns are the class-specific measures
%           computed as mean decrease in accuracy. The nclass + 1st column is the
%           mean decrease in accuracy over all classes. The last column is the mean decrease
%           in Gini index. For Regression, the first column is the mean decrease in
%           accuracy and the second the mean decrease in MSE. If importance=FALSE,
%           the last measure is still returned as a vector.
figure('Name','Importance Plots')
subplot(3,1,1);
bar(model.importance(:,end-1));xlabel('feature');ylabel('magnitude');
title('Mean decrease in Accuracy');

subplot(3,1,2);
bar(model.importance(:,end));xlabel('feature');ylabel('magnitude');
title('Mean decrease in Gini index');


%importanceSD = The ?standard errors? of the permutation-based importance measure. For classification,
%           a D by nclass + 1 matrix corresponding to the first nclass + 1
%           columns of the importance matrix. For regression, a length p vector.
model.importanceSD
subplot(3,1,3);
bar(model.importanceSD);xlabel('feature');ylabel('magnitude');
title('Std. errors of importance measure');

% example 9: calculating local importance
% extra_options.localImp = Should casewise importance measure be computed? (Setting this to TRUE will
% override importance.)
%localImp = a D by N matrix containing the casewise importance measures, the [i,j] element
% of which is the importance of i-th variable on the j-th case. NULL if
% localImp=FALSE.
clear extra_options
extra_options.localImp = 1; %(0 = (Default) Don’t, 1=calculate)

model = regRF_train(X_trn,Y_trn, 100, 4, extra_options);
Y_hat = regRF_predict(X_tst,model);
fprintf('\nexample 9: MSE rate %f\n',   sum((Y_hat-Y_tst).^2));

model.localImp

% example 10: calculating proximity
% extra_options.proximity = Should proximity measure among the rows be calculated?
clear extra_options
extra_options.proximity = 1; %(0 = (Default) Don’t, 1=calculate)

model = regRF_train(X_trn,Y_trn, 100, 4, extra_options);
Y_hat = regRF_predict(X_tst,model);
fprintf('\nexample 10: MSE rate %f\n',   sum((Y_hat-Y_tst).^2));

model.proximity

% example 11: use only OOB for proximity
% extra_options.oob_prox = Should proximity be calculated only on ‘out-of-bag’ data?
clear extra_options
extra_options.proximity = 1; %(0 = (Default) Don’t, 1=calculate)
extra_options.oob_prox = 0; %(Default = 1 if proximity is enabled, Don’t 0)

model = regRF_train(X_trn,Y_trn, 100, 4, extra_options);
Y_hat = regRF_predict(X_tst,model);
fprintf('\nexample 11: MSE rate %f\n',   sum((Y_hat-Y_tst).^2));

% example 12: to see what is going on behind the scenes
% extra_options.do_trace = If set to TRUE, give a more verbose output as randomForest is run. If set to
% some integer, then running output is printed for every
% do_trace trees.
clear extra_options
extra_options.do_trace = 1; %(Default = 0)

model = regRF_train(X_trn,Y_trn, 100, 4, extra_options);
Y_hat = regRF_predict(X_tst,model);
fprintf('\nexample 12: MSE rate %f\n',   sum((Y_hat-Y_tst).^2));

% example 13: to see what is going on behind the scenes
% extra_options.keep_inbag Should an n by ntree matrix be returned that keeps track of which samples are
% ‘in-bag’ in which trees (but not how many times, if sampling with replacement)

clear extra_options
extra_options.keep_inbag = 1; %(Default = 0)

model = regRF_train(X_trn,Y_trn, 100, 4, extra_options);
Y_hat = regRF_predict(X_tst,model);
fprintf('\nexample 13: MSE rate %f\n',   sum((Y_hat-Y_tst).^2));

model.inbag

% example 14: getting the OOB MSE rate. model will have mse field
model = regRF_train(X_trn,Y_trn);
Y_hat = regRF_predict(X_tst,model);
fprintf(‘\nexample 14: MSE rate %f\n’, sum((Y_hat-Y_tst).^2));

figure('Name','OOB error rate');
plot(model.mse); title('OOB MSE error rate');  xlabel('iteration (# trees)'); ylabel('OOB error rate');

%
% example 15: nPerm
% Number of times the OOB data are permuted per tree for assessing variable
% importance. Number larger than 1 gives slightly more stable estimate, but not
% very effective. Currently only implemented for regression.
clear extra_options
extra_options.importance=1;
extra_options.nPerm = 1; %(Default = 0)
model = regRF_train(X_trn,Y_trn,100,2,extra_options);
Y_hat = regRF_predict(X_tst,model);
fprintf(‘\nexample 15: MSE rate %f\n’, sum((Y_hat-Y_tst).^2));

figure('Name','Importance Plots nPerm=1')
subplot(2,1,1);
bar(model.importance(:,end-1));xlabel('feature');ylabel('magnitude');
title('Mean decrease in Accuracy');

subplot(2,1,2);
bar(model.importance(:,end));xlabel('feature');ylabel('magnitude');
title('Mean decrease in Gini index');

%let's now run with nPerm=3
clear extra_options
extra_options.importance=1;
extra_options.nPerm = 3; %(Default = 0)
model = regRF_train(X_trn,Y_trn,100,2,extra_options);
Y_hat = regRF_predict(X_tst,model);
fprintf('\nexample 15: MSE rate %f\n',   sum((Y_hat-Y_tst).^2));

figure('Name','Importance Plots nPerm=3')
subplot(2,1,1);
bar(model.importance(:,end-1));xlabel('feature');ylabel('magnitude');
title('Mean decrease in Accuracy');

subplot(2,1,2);
bar(model.importance(:,end));xlabel('feature');ylabel('magnitude');
title('Mean decrease in Gini index');

% example 16: corr_bias (not recommended to use)
clear extra_options
extra_options.corr_bias=1;
model = regRF_train(X_trn,Y_trn,100,2,extra_options);
Y_hat = regRF_predict(X_tst,model);
fprintf(‘\nexample 16: MSE rate %f\n’, sum((Y_hat-Y_tst).^2));

结果图

在这里插入图片描述
在这里插入图片描述

结果分析

从图中可以看出来,随机森林回归拟合收敛平滑迅速,拟合准确,泛发性好

展望

随机森林是一种很好的分类算法,也能用于回归分析,并且可以扩展和各种启发式算法结合,提高算法的随机森林的分类和回归能力,需要扩展的可以扫描二维码联系我

参考论文

百科

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/847000.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

C++11之右值引用

C11之右值引用 传统的C语法中就有引用的语法,而C11中新增了的 右值引用(rvalue reference)语法特性,所以从现在开始我们之前学习的引用就叫做左值引用(lvalue reference)。无论左值引用还是右值引用&#…

依赖管理插件

项目场景: 接手新项目 第一步: 检查项目 看文件中包管理工具是什么 包管理工具有很多种,不要拿到就npm安装依赖 问题描述 在项目下 安装pnpm总是报错 查看了网上没找到具体解决方案 解决方案: 全局安装pnpm 全局安装pnpm npm insta…

中电金信:ChatGPT一夜爆火,知识图谱何以应战?

随着ChatGPT的爆火出圈 人工智能再次迎来发展小高潮 那么作为此前搜索领域的主流技术 知识图谱前路又将如何呢? 事实上,ChatGPT也并非“万能”,作为黑箱模型,ChatGPT很难验证生成的知识是否准确。并且ChatGPT是通过概率模型执行推…

Java基础入门篇——Java变量类型的转换和运算符(七)

目录 一、变量类型 1.1自动类型转换(隐式转换) 1.2 强制类型转换(显式转换) 1.3类型转换的其他情况 二、运算符 2.1算术运算符 2.2比较运算符 2.3逻辑运算符 2.4位运算符 三、总结 在Java中,变量类型的转换…

\vendor\github.com\godror\orahlp.go:531:19: undefined: VersionInfo

…\goAdmin\vendor\github.com\godror\orahlp.go:531:19: undefined: VersionInfo 解决办法 降了go版本(go1.18),之前是go1.19 gorm版本不能用最新的,降至(gorm.io/gorm v1.21.16)就可以 修改交插编译参数 go env -w CGO_ENABLED1…

机器学习深度学习—语言模型和数据集

👨‍🎓作者简介:一位即将上大四,正专攻机器学习的保研er 🌌上期文章:机器学习&&深度学习——文本预处理 📚订阅专栏:机器学习&&深度学习 希望文章对你们有所帮助 语…

订单系统就该这么设计,稳的一批~

订单功能作为电商系统的核心功能,由于它同时涉及到前台商城和后台管理系统,它的设计可谓是非常重要的。就算不是电商系统中,只要是涉及到需要交易的项目,订单功能都具有很好的参考价值,说它是通用业务功能也不为过。今…

单元测试最终结果为Stopped状态(有报错后不继续往下执行)

之前跑了一下项目的单元测试,但是发现一旦有报错后单元测试就不继续往下执行,而且最终的结果是stopped状态,截图如下: 经过排查是因为项目启动的时候有如下的代码: 通过代码可以发现如果项目启动失败,则直接…

如何用python画动漫人物,python画卡通人物代码

大家好,小编来为大家解答以下问题,python画动漫人物代码 星空,如何用python画动漫人物,现在让我们一起来看看吧! 要寒假了,给孩子画一个卡通版蜘蛛侠 完整程序代码: from turtle import * speed…

鉴源实验室|公钥基础设施(PKI)在车联网中的应用

作者 | 付海涛 上海控安可信软件创新研究院汽车网络安全组 来源 | 鉴源实验室 01 PKI与车联网 1.1 PKI概述 公钥基础设施(PKI ,Public Key Infrastructure)是一种在现代数字环境中实现认证和加密的基本框架,主要用于保护网络交互和通信的安…

新手教程:5步掌握系统流程图绘制方法!

流程图通常用于管理、分析、设计许多不同领域的流程,是一个很有用的工具,能够帮助大家更轻松、更有效地解决问题。系统流程图是流程图的常见变体之一。 系统流程图是展示数据流以及决策如何影响周围事件的图表类型。 与其他类型的流程图一样,…

【沁恒蓝牙mesh】CH58x USB功能开发记录(一)

本文主要介绍基于【沁恒蓝牙mesh】CH58x USB功能,结合SDK提供的代码包分析USB的基本常识 【沁恒蓝牙mesh】CH58x USB功能开发记录(一) 1. USB基本常识1.1 **USB 设备类别:**1.2 **USB设备实现方法:**1.3 **CDC设备&…

【我们一起60天准备考研算法面试(大全)-第三十九天 39/60】【序列型DP】

专注 效率 记忆 预习 笔记 复习 做题 欢迎观看我的博客,如有问题交流,欢迎评论区留言,一定尽快回复!(大家可以去看我的专栏,是所有文章的目录)   文章字体风格: 红色文字表示&#…

PDF Expert 3.3 for mac

PDF Expert是一款专业的PDF编辑和阅读工具。它可以帮助用户在Mac、iPad和iPhone等设备上查看、注释、编辑、填写和签署PDF文档。 以下是PDF Expert的特点: PDF编辑:PDF Expert提供了丰富的PDF编辑功能,包括添加、删除、移动、旋转、缩放、裁…

【2.2】Java微服务:Hystrix的详解与使用

目录 分布式系统面临问题 Hystrix概念 Hystrix作用 降级 什么是降级 order服务导入Hystrix依赖(简单判断原则:谁调用远程谁加) 启动类添加注解 业务方法添加注解(冒号里填回调方法名,回调方法返回兜底数据&…

DETR不需要多尺度或局部性设计

文章目录 DETR Doesn’t Need Multi-Scale or Locality Design摘要本文方法Box-to-Pixel Relative Position Bias其他改进 实验结果 DETR Doesn’t Need Multi-Scale or Locality Design 摘要 提出了一种改进的DETR检测器,使用单尺度特征映射和全局交叉注意计算&a…

RFID系统数据编码方式仿真实现

RFID 技术简介 射频识别技术(RFID,即,Radio Frequency Identification)是一种非接触自动识别技术,它利用无线通信的方式自动的从目标中读取信息。   典型的RFID射频识别系统包括标签和读写器两部分。   标签是一块集…

学术资源加速

以下为可以加速访问的学术资源地址: github.comgithubusercontent.comgithubassets.comhuggingface.co 编辑 /etc/network_turbo vim /etc/network_turbo 内容格式参考如下: export no_proxylocalhost,127.0.0.1 export http_proxyhttp://127.0.0.…

5,二叉树【p6-p7】

二叉树 5.1二叉树5.1.1例1:用递归和非递归两种方式实现二叉树的先序、中序、后序遍历5.1.1.1递归序的先序、中序、后序遍历先序遍历:中序遍历:后序遍历: 5.1.1.2非递归序的先序、中序、后序遍历先序遍历:中序遍历&…

基于Java+SpringBoot+Vue的时间管理系统设计与实现(源码+LW+部署文档等)

博主介绍: 大家好,我是一名在Java圈混迹十余年的程序员,精通Java编程语言,同时也熟练掌握微信小程序、Python和Android等技术,能够为大家提供全方位的技术支持和交流。 我擅长在JavaWeb、SSH、SSM、SpringBoot等框架…