MATLAB中利用最小二乘支持向量机LS-SVM做图像分类的代码


%都是已经调试好的,直接可用。
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];
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值