【预测模型】基于matlab SVM用电量预测【含Matlab源码 103期】

253 阅读2分钟

一、简介

二、源代码

clear all;
clc ;
close all
%得到文件路径,找到所有.dat格式的文件
sh = xlsread('data.xlsx','Sheet1','B2:B38');
addpath('./libsvm-3.20');%%添加工具箱
%extract data
[m,n] = size(sh);
n1 = round(length(sh)*0.7);%训练样本大小
ts = sh(1:n1,1);%训练数据
tsx = sh(n1+1:m,1);%测试数据
original = sh(n1+1:end,:);%原始测试数据
%归一化处理
[TS,TSps] = mapminmax(ts);
[TSX,TSXps] = mapminmax(tsx);
[TSX_zong,TSXps_zong] = mapminmax(sh);
%split the data into training and testing

train_label = TS(1:n1,:);%训练数据标签
train_data = TS(1:n1,:);%训练数据
test_label = TSX(1:end,:);%测试数据标签
test_data = TSX(1:end,:);%测试数据
test_label_zong = TSX_zong(1:end,:);%测试数据标签
test_data_zong = TSX_zong(1:end,:);%测试数据
% Find the optimize value of c,g paramter
% Approximately choose the parameters:
% the scale of c is 2^(-5),2^(-4),...,2^(10)
% the scale of g is 2^(-5),2^(-4),...,2^(5)
[bestmse,bestc,bestg] = svmregress(train_label,train_label,-5,10,-5,5,3,1,1,0.0005);

% Display the approximate result
disp('Display the approximate result');
str = sprintf( 'Best Cross Validation MSE = %g Best c = %g Best g = %g',bestmse,bestc,bestg);
disp(str);
%Do training by using svmtrain of libsvm
cmd = ['-c ', num2str(bestc), ' -g ', num2str(bestg) , ' -s 3 -p 0.01'];
model = svmtrain(train_label,train_data,cmd);%训练
%Do predicting by using svmpredict of libsvm
 clear all;
clc ;
close all
%得到文件路径,找到所有.dat格式的文件
sh = xlsread('data.xlsx','Sheet1','B2:B38');
addpath('./libsvm-3.20');%%添加工具箱
%extract data
[m,n] = size(sh);
n1 = round(length(sh)*0.7);%训练样本大小
ts = sh(1:n1,1);%训练数据
tsx = sh(n1+1:m,1);%测试数据
original = sh(n1+1:end,:);%原始测试数据
%归一化处理
[TS,TSps] = mapminmax(ts);
[TSX,TSXps] = mapminmax(tsx);
[TSX_zong,TSXps_zong] = mapminmax(sh);
%split the data into training and testing

train_label = TS(1:n1,:);%训练数据标签
train_data = TS(1:n1,:);%训练数据
test_label = TSX(1:end,:);%测试数据标签
test_data = TSX(1:end,:);%测试数据
test_label_zong = TSX_zong(1:end,:);%测试数据标签
test_data_zong = TSX_zong(1:end,:);%测试数据
% Find the optimize value of c,g paramter
% Approximately choose the parameters:
% the scale of c is 2^(-5),2^(-4),...,2^(10)
% the scale of g is 2^(-5),2^(-4),...,2^(5)
[bestmse,bestc,bestg] = svmregress(train_label,train_label,-5,10,-5,5,3,1,1,0.0005);

% Display the approximate result
disp('Display the approximate result');
str = sprintf( 'Best Cross Validation MSE = %g Best c = %g Best g = %g',bestmse,bestc,bestg);
disp(str);
%Do training by using svmtrain of libsvm
cmd = ['-c ', num2str(bestc), ' -g ', num2str(bestg) , ' -s 3 -p 0.01'];
model = svmtrain(train_label,train_data,cmd);%训练
%Do predicting by using svmpredict of libsvm

三、运行结果

在这里插入图片描述

四、备注

版本:2014a