function test
clc;
matPos = csvread('pos.dat');
noPos= size(matPos,1);
noFeature = size(matPos,2);
labelPos= ones(noPos,1);
% matPos= [matPos labelPos ];
matNeg = csvread('neg.dat');
noNeg= size(matNeg,1);
labelNeg= -1*ones(noNeg,1);
% matNeg= [matNeg labelNeg ];
% svmStruct = svmtrain(featureInTrain,featureOutTrain,'kernel_function','linear' , 'options' ,smo_opts); %,'rbf_sigma',100,'boxconstraint',25
noFold=5;
c = cvpartition([labelPos ; labelNeg],'kfold', noFold);
strArray = java_array('java.lang.String', 2);
strArray(1) = java.lang.String('1');
strArray(2) = java.lang.String('-1');
myorder = cell(strArray)
f = @(xtr,ytr,xte,yte) confusionmat(yte,@(xtr,ytr,xte)crossfun(xtr,ytr,xte, exp(z(1)),exp(z(2))),'order', [1 -1] );
cfMat = crossval(f,[matPos; matNeg], [ labelPos ; labelNeg],'partition',c);
cfMat = reshape(sum(cfMat),3,3)
minfn = @(z)crossval('mcr',[matPos; matNeg], [ labelPos ; labelNeg],...
'Predfun', @(xtrain,ytrain,xtest)crossfun(xtrain,ytrain,xtest, exp(z(1)),exp(z(2))), ...
'partition',c );
opts = optimset('TolX',5e-4,'TolFun',5e-4);
[searchmin fval] = fminsearch(minfn,randn(2,1),opts)
load('fisheriris');
y = species;
X = meas;
order = unique(y); % Order of the group labels
cp = cvpartition(y,'k',10); % Stratified cross-validation
f = @(xtr,ytr,xte,yte)confusionmat(yte,classify(xte,xtr,ytr),'order',order);
cfMat = crossval(f,X,y,'partition',cp);
cfMat = reshape(sum(cfMat),3,3)
display('done')
end
function yfit = crossfun(xtrain,ytrain,xtest, rbf_sigma,boxconstraint)
% Train the model on xtrain, ytrain,
% and get predictions of class of xtest
svmStruct = svmtrain(xtrain,ytrain, 'boxconstraint',boxconstraint, ...
'Kernel_Function','rbf','rbf_sigma',rbf_sigma );
%'kernel_function','linear' , 'options' ,smo_opts );
% 'Kernel_Function','rbf','rbf_sigma',rbf_sigma, );
yfit = svmclassify(svmStruct,xtest);
yfit
end
Comments
Post a Comment