tic % 计时器
%% 清空环境变量
close all
clear
clc
%format compact
load('ISSL-Isomap.mat')
% load CMPE原始
% mappedX=X;
%% 数据提取
zc=mappedX(1:60,:);%特征输入
lie=mappedX(61:120,:);
mo=mappedX(121:180,:);
que=mappedX(181:240,:);
duan=mappedX(241:300,:);
mm=size(zc,1);
nn=20;
a=ones(mm,1);%行为总体样本数
b=2*ones(mm,1);
c=3*ones(mm,1);
d=4*ones(mm,1);
f=5*ones(mm,1);
n1=randperm(size(zc,1));
n2=randperm(size(lie,1));
n3=randperm(size(mo,1));
n4=randperm(size(que,1));
n5=randperm(size(duan,1));
train_wine = [zc(n1(1:nn),:);lie(n2(1:nn),:);mo(n3(1:nn),:);que(n4(1:nn),:);duan(n5(1:nn),:)];
% 相应的训练集的标签也要分离出来
train_wine_labels = [a(1:nn,:);b(1:nn,:);c(1:nn,:);d(1:nn,:);f(1:nn,:)];
% 将第一类的31-59,第二类的96-130,第三类的154-178做为测试集
test_wine = [zc(n1((nn+1):mm),:);lie(n2((nn+1):mm),:);mo(n3((nn+1):mm),:);que(n4((nn+1):mm),:);duan(n5((nn+1):mm),:)];
% 相应的测试集的标签也要分离出来
test_wine_labels = [a((nn+1):mm,:);b((nn+1):mm,:);c((nn+1):mm,:);d((nn+1):mm,:);f((nn+1):mm,:)];
%% 数据预处理
% 数据预处理,将训练集和测试集归一化到[0,1]区间
[mtrain,ntrain] = size(train_wine);
[mtest,ntest] = size(test_wine);
dataset = [train_wine;test_wine];
% mapminmax为MATLAB自带的归一化函数
[dataset_scale,ps] = mapminmax(dataset',0,1);
dataset_scale = dataset_scale';
train_wine = dataset_scale(1:mtrain,:);
test_wine = dataset_scale( (mtrain+1):(mtrain+mtest),: );
%% Default parameters 默认参数
n=10; % Population size, typically10 to 40
N_gen=150; % Number of generations
A=0.5; % Loudness (constant or decreasing)
r=0.5; % Pulse rate (constant or decreasing)
% This frequency range determines the scalings
% You should change these values if necessary
Qmin=0; % Frequency minimum
Qmax=2; % Frequency maximum
% Iteration parameters
N_iter=0; % Total number of function evaluations %这是什么意思???
% Dimension of the search variables
d=2; % Number of dimensions
% Lower limit/bounds/ a vector
Lb=[0.01,0.01]; % 参数取值下界
Ub=[100,100]; % 参数取值上界
% Initializing arrays
Q=zeros(n,1); % Frequency
v=zeros(n,d); % Velocities
% Initialize the population/solutions
% Output/display
disp(['Number of evaluations: ',num2str(N_iter)]);
disp(['Best =',num2str(best),' fmin=',num2str(fmin)]);
%% 利用最佳的参数进行SVM网络训练
cmd_gwosvm = ['-c ',num2str(best(:,1)),' -g ',num2str(best(:,2))];
model_gwosvm = svmtrain(train_wine_labels,train_wine,cmd_gwosvm);
%% SVM网络预测
[predict_label] = svmpredict(test_wine_labels,test_wine,model_gwosvm);
total = length(test_wine_labels);% 打印测试集分类准确率
right = length(find(predict_label == test_wine_labels));
Accuracy=right/total;
% disp('打印测试集分类准确率');
% str = sprintf( 'Accuracy = %g%% (%d/%d)',accuracy(1),right,total);
% disp(str);
%% 结果分析
% 测试集的实际分类和预测分类图
figure;
hold on;
plot(test_wine_labels,'o');
plot(predict_label,'r*');
xlabel('测试集样本','FontSize',12);
ylabel('类别标签','FontSize',12);
legend('实际测试集分类','预测测试集分类');
title('测试集的实际分类和预测分类图','FontSize',12);
grid on
snapnow
figure
plot(1:N_gen,AAA);
完整代码或者代写添加QQ2449341593
往期回顾>>>>>>
【lssvm预测】基于鲸鱼优化算法的lssvm数据预测matlab源码【Matlab 104期】【预测模型2】
【lstm预测】基于鲸鱼优化算法改进的lstm预测matlab源码【Matlab 105期】【预测模型3】
【SVM预测】灰狼算法优化svm支持向量机预测matlab源码【Matlab 107期】【预测模型5】
BP神经网络的预测【Matlab 108期】【预测模型6】
【lssvm预测模型】基于蝙蝠算法改进的最小二乘支持向量机lssvm预测【Matlab 109期】【预测模型7】