matlab下利用隨機森林包做回歸擬合

2021-10-09 12:16:51 字數 3670 閱讀 2909

clear all;

clc;

winsize=5;

%% 資料讀入

[num,s]

=xlsread

("****");

time=s(

2:size

(s,1),

1);dt =

datetime

(time,

'inputformat'

,'yyyy/mm/dd hh:mm:ss');

for i =1:

size

(dt)if(

isnat(dt

(i)))dt

(i)=

datetime

(time

(i),

'format'

,'yyyy/mm/dd 00:00:00');

endenddtnum =

datenum

(dt)

; data =

zeros

(size

(dtnum,1)

,5);

data(:

,1)= dtnum;

%時間data(:

,2)=

num(:,

1);data(:

,3)=

num(:,

2);%h1

data(:

,4)=

num(:,

3);%h2

data(:

,5)=

num(:,

4);%h3

%% 劃分訓練集和測試集

a =randperm

(size

(dtnum,1)

);splitrate =

0.7;

%劃分比例

splitindex =

floor

(splitrate*

size

(dtnum,1)

);%劃分索引

train =

data(a

(1:splitindex),:

);test =

data(a

((splitindex+1)

:end),:

);i_train =

train(:

,1:2

);o_train_h1 =

train(:

,3);

o_train_h2 =

train(:

,4);

o_train_h3 =

train(:

,5);

% i_test =

test(:

,1:2

);% o_test_h1 =

test(:

,3);

% o_test_h2 =

test(:

,4);

% o_test_h3 =

test(:

,5);

%% h1 訓練及**

step =1;

tree_num=

500;

%樹的數目

for i=

1:step

model=

regrf_train

(i_train ,o_train_h1,tree_num)

; y_hat1 =

regrf_predict

(data(:

,1:2

), model)

;end

% 誤差

err1=

(y_hat1-

data(:

,3))

./data(:

,3);

y_hat11=

filter

(ones(1

,winsize)

/winsize,1,

[y_hat1;

zeros

(floor

(winsize/2)

,1)]

);y_hat1=

y_hat11

(round

(winsize/2)

:end);%

% h2 訓練及**

for i=

1:step

mode2=

regrf_train

(i_train ,o_train_h2,tree_num)

; y_hat2 =

regrf_predict

(data(:

,1:2

), mode2)

;end

% 誤差

err2=

(y_hat2-

data(:

,4))

./data(:

,4);

y_hat22=

filter

(ones(1

,winsize)

/winsize,1,

[y_hat2;

zeros

(floor

(winsize/2)

,1)]

);y_hat2=

y_hat22

(round

(winsize/2)

:end);%

% h3 訓練及**

for i=

1:step

mode3=

regrf_train

(i_train ,o_train_h3,tree_num)

; y_hat3 =

regrf_predict

(data(:

,1:2

), mode3)

;end

% 誤差

err3=

(y_hat3-

data(:

,5))

./data(:

,5);

y_hat33=

filter

(ones(1

,winsize)

/winsize,1,

[y_hat3;

zeros

(floor

(winsize/2)

,1)]

);y_hat3=

y_hat33

(round

(winsize/2)

:end);%

% 評價指

h1_r2 =1-

norm

(data(:

,3)-y_hat1)^2

/norm

(data(:

,3)-

mean

(data(:

,3))

)^2h2_r2 =1-

norm

(data(:

,4)-y_hat2)^2

/norm

(data(:

,4)-

mean

(data(:

,4))

)^2h3_r2 =1-

norm

(data(:

,5)-y_hat3)^2

/norm

(data(:

,5)-

mean

(data(:

,5))

)^2

04 12 scikit learn庫之隨機森林

目錄二 randomforestregressor 人工智慧從入門到放棄完整教程目錄 本文主要介紹隨機森林的兩個模型randomforestclassifier和randomforestregressor,這兩個模型調參包括兩部分,第一部分是bagging框架,第二部分是cart決策樹的引數。本文會...

04 12 scikit learn庫之隨機森林

目錄二 randomforestregressor 更新 更全的 機器學習 的更新 更有python go 資料結構與演算法 爬蟲 人工智慧教學等著你 本文主要介紹隨機森林的兩個模型randomforestclassifier和randomforestregressor,這兩個模型調參包括兩部分,第...

matlab隨機訊號處理

matlab中rand和randn是產生隨機數的命令,x rand 1,n 產生 0,1 區間均勻分布的長度為n的隨機訊號,x randn 1,n 產生長度為n且具有零均值和單位方差的正態分佈的隨機訊號。matlab中產生偽隨機數需要種子,把不同的種子用於不同的隨機數生成器產生不同的偽隨機數。bet...