forecasting SVM code issue

I found an error "Index in position 1 exceeds array bounds (must not exceed 142)" while using the below code:
Facing problem to find the attached figure
% clc;clear;close all;
% data = xlsread('all_data.xls');
% N = 142;
% data_in = data(13:N,[5 6 8]);
% data_out = data(13:N,4);
% mdl = fitrsvm(data_in,data_out,'Standardize','on','KernelFunction','rbf','KernelScale','auto');
% predY = predict(mdl,data_in);
% plot(data_out)
% hold on
% plot(predY,'r')
clc;clear;close all;
addpath(genpath([pwd,'\libsvm-3.20'])); %adding svm path
A = 142;
data = xlsread('all_data.xls');
data_in = data(13:A,[5 6 8]);
data_out = data(13:A,4);
%%
n = length(data_in);
sigma_in = std(data_in);
mu_in = mean(data_in);
data_in_norm = (data_in - repmat(mu_in,n,1))./repmat(sigma_in,n,1);
sigma_out = std(data_out);
mu_out= mean (data_out);
data_out_norm = (data_out - mu_out)./sigma_out;
%%
model = svmtrain(data_out_norm, data_in_norm, '-s 3 -g 2 -t 2 -d 1 -p 0.1 -c 1');
y_pred_norm = svmpredict(data_out_norm, data_in_norm, model);
y_pred = y_pred_norm*sigma_out+mu_out;
%%
for i = 143:154
x = [data(i,9) data(i,6) data(i-12,4)];
x_norm = (x - mu_in)./sigma_in;
y_pred_norm(i-12) = svmpredict(0, x_norm, model);
y_pred(i-12) = y_pred_norm(i-12)*sigma_out+mu_out;
end
[~,ix]= sort(y_pred(end-11:end));
y_pred(end-11:end)=y_pred(end-11:end).*(0.6+((ix-1)/11));
for i = 155:168
x = [data(i,9) data(i,6) y_pred(i-13)];
x_norm = (x - mu_in)./sigma_in;
y_pred_norm(i-12) = svmpredict(0, x_norm, model);
y_pred(i-12) = y_pred_norm(i-12)*sigma_out+mu_out;
end
plot(data_out)
hold on
plot(y_pred,'*-r')

5 件のコメント

Walter Roberson
Walter Roberson 2021 年 12 月 28 日
Which line is the problem occuring on? What are the size of all of the variables mentioned in the line, and the value of any index mentioned in the line?
Note: libsvm is third-party code; it is no longer common that people have it readily available.
suleman sarwar
suleman sarwar 2021 年 12 月 28 日
Problem in these loops
for i = 143:154
x = [data(i,9) data(i,6) data(i-12,4)];
x_norm = (x - mu_in)./sigma_in;
y_pred_norm(i-12) = svmpredict(0, x_norm, model);
y_pred(i-12) = y_pred_norm(i-12)*sigma_out+mu_out;
end
[~,ix]= sort(y_pred(end-11:end));
y_pred(end-11:end)=y_pred(end-11:end).*(0.6+((ix-1)/11));
for i = 155:168
x = [data(i,9) data(i,6) y_pred(i-13)];
x_norm = (x - mu_in)./sigma_in;
y_pred_norm(i-12) = svmpredict(0, x_norm, model);
y_pred(i-12) = y_pred_norm(i-12)*sigma_out+mu_out;
end
Data length - 1 to 142 (for seasonal effect first 12 obs removed)
Training data start from - 13th Obs (Jan 2005 - Sep 2014)
Testing data - 130 obs to 142 obs (Oct 2014 - Oct 2015)
While running the loop, i got error "Index in position 1 exceeds array bounds (must not exceed 142)"
Moreover, the codes does not work to forecast next 12 months consumption (Nov 2015 to Oct 2016), as given in attached FIGURE.
Walter Roberson
Walter Roberson 2021 年 12 月 28 日
You say the data length is 142, but your second for loop starts trying to access from data(i,9) where i is 155.
Just before the first of those loops, please put in
whos data y_pred_norm y_pred
and show us the result.
suleman sarwar
suleman sarwar 2021 年 12 月 28 日
Dear Walter, thanks for helping
Walter Roberson
Walter Roberson 2021 年 12 月 28 日
if data has 142 rows and your loop starts at 143 then data(i, 9) is out of range.
If you want to use the last 12 for forecasting then index data(i-12,:)
But you would have the same problem with the second loop.

サインインしてコメントする。

回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeStatistics and Machine Learning Toolbox についてさらに検索

質問済み:

2021 年 12 月 28 日

コメント済み:

2021 年 12 月 28 日

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by