forecasting SVM code issue
10 views (last 30 days)
Show older comments
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 Comments
Walter Roberson
on 28 Dec 2021
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.
Answers (0)
See Also
Categories
Find more on Statistics and Machine Learning Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!