%都是已经调试好的,直接可用。
function [model,Ytest,ypred] = LSSVM_Classify(trainFeatures,trainLabels,testFeatures,testLabels)
%% load 数据
% 导入训练样本及其标签
X=trainFeatures;
Y=trainLabels;
% 导入测试样本及其标签
Xt=testFeatures;
YY=testLabels;
type = 'classification';
L_fold = 10; % L-fold crossvalidation
%% 获得对象接口
model = initlssvm(X,Y,type,[],[],'RBF_kernel');%lin_kernel
%% 参数寻优
model = tunelssvm(model,'simplex','crossvalidatelssvm',{L_fold,'misclass'},'code_OneVsOne');
%% 模型训练和图形化显示
model = trainlssvm(model);
plotlssvm(model);
%% 利用训练模型进行预测
% Xt = 2.*rand(10,2)-1;
Ytest = simlssvm(model,Xt);
%% 利用公式计算真实值,并与模型预测结果进行对比
% YY = sign(sin(Xt(:,1))+Xt(:,2));
figure(1)
subplot(2,1,1)
plot(Xt,Ytest,'*'); title('模型预测结果','fontsize',13);
subplot(2,1,2)
plot(Xt,YY,'*'); title('公式计算结果','fontsize',13);
figure(2)
hold on;
plot(YY,'o');
plot(Ytest,'r*');
legend('实际测试集分类','预测测试集分类');
title('测试集的实际分类和预测分类图','FontSize',10);
% ypred = length(find(Ytest==YY))/length(YY);
ypred = length(find(Ytest==YY))/(length(Ytest)) * 100;
disp(['accuracy = ' ,num2str(ypred)])
%% ROC曲线
% latent variables are needed to make the ROC curve
% Y_latent = latentlssvm(model,X);
% [~,~,thresholds,oneMinusSpec,Sens]=roc(Y_latent,Y);
% [thresholds oneMinusSpec Sens];
MATLAB中利用最小二乘支持向量机LS-SVM做图像分类的代码
最新推荐文章于 2024-07-10 17:18:29 发布