资源简介
利用Mosek优化工具实现SVM二分类性能,并在公共数据集Banana上实现,直观显示分类结果
代码片段和文件信息
% SVMTRAIN - Trains a support vector machine incrementally
% using the L1 soft margin approach developed by
% Cauwenberghs for two-class problems.
%
% Syntax: [abginduindX_mery_merRsQ] = svmtrain(XyCtypescale)
% [abginduindX_mery_merRsQ] = svmtrain(XyCtypescaleuind)
% (trains a new SVM on the given examples)
%
% [abginduindX_mery_merRsQ] = svmtrain(XyC)
% [abginduindX_mery_marRsQ] = svmtrain(XyCuind)
% (trains the current SVM in memory on the given examples)
%
% a: alpha coefficients
% b: bias
% g: partial derivatives of cost function w.r.t. alpha coefficients
% ind: cell array containing indices of margin error and reserve vectors
% ind{1}: indices of margin vectors
% ind{2}: indices of error vectors
% ind{3}: indices of reserve vectors
% uind: column vector of user-defined example indices (used for unlearning specified examples)
% X_mer: matrix of margin error and reserve vectors stored columnwise
% y_mer: column vector of class labels (-1/+1) for margin error and reserve vectors
% Rs: inverse of extended kernel matrix for margin vectors
% Q: extended kernel matrix for all vectors
% X: matrix of training vectors stored columnwise
% y: column vector of class labels (-1/+1) for training vectors
% C: soft-margin regularization parameter(s)
% dimensionality of C assumption
% 1-dimensional vector universal regularization parameter
% 2-dimensional vector class-conditional regularization parameters (-1/+1)
% n-dimensional vector regularization parameter per example
% (where n = # of examples)
% type: kernel type
% 1: linear kernel K(xy) = x‘*y
% 2-4: polynomial kernel K(xy) = (scale*x‘*y + 1)^type
% 5: Gaussian kernel with variance 1/(2*scale)
% scale: kernel scale
%
% Version 3.22e -- Comments to diehl@alumni.cmu.edu
%
clc;
clear;
close all
% function [abginduindXyRsQ] = svmtrain(X_newy_newC_newvarargin)
load banana
x = banana;
N = 200;
X_new = x‘;
y_new=[ones(N1);-ones(N1)];
%% plot all examples
figure;
T.X = x‘;
T.y = y_new;
ppatterns(T)
% index = [1:100 201:300]
% T.X = x‘;
% T.y = y_new;
% ppatterns(T)
% V1=axis;axis(1.2*V1)
% %获得增加部分样本
index1=find((x(1:2002)<=0)&(x(1:2001)<=0));%
in1 = setdiff(1:200index1);
index2=find((x(201:4002)<=0)&(x(201:4001)>=-1));
index2 = index2+N*ones(size(index21)1);
in2 = setdiff(201:400index2);
addx1 = x(index1:);
addx2 = x(index2:);
addindex = [index1‘index2‘];
in = [in1 in2]‘;
x_ini = x(in:);
y_ini = y_new(in);
% flags for example state
MARGIN = 1;
ERROR = 2;
RESERVE = 3;
UNLEARNED = 4;
new_model = 1;
C_new = 2;
uind_new = zeros(size(y_new));
type_new = 5;
% 1: linear kernel
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 7049 2018-09-20 22:58 GY_SVM_Mosek.m
文件 3399 2018-01-19 10:04 plot_svm2.m
文件 2635 2018-01-03 23:55 svm_online.m
评论
共有 条评论