SVM nonlinear MATLAB implementation

 

Application of the Gaussian kernel

Clear, no draw ROC curve
TiC
Load ( 'C: \ the Users \ Administrator \ Desktop \ Cancer.mat')
MU Cancer = (. 1: 444, :); = zi and Cancer (445: End, :);% organize data, The first column (2) as the label
cycle_N = 1;% cycle_N fold cross validation for
N = 10;% N = 10 off ten
C = 100;
[Nl, COL] = size (MU);
[N2 of, COL] size = (zi and);
Ql = Floor (Nl / N); Floor Q2 = (N2 of / N);
% the ACC = zeros (1,10); the MCC = zeros (1,10); Fl = zeros (1,10 );

%TP_ROC=zeros(1,10);FP_ROC=zeros(1,10);

for kk = 1: cycle_N% NN times for cross-training
for k = 1: N% N per cross training off
IF K ==. 1
pos_train MU = (. 1 + Ql: End, :); pos_test MU = (. 1: Ql, :);
non_train = zi and (Q2 +. 1: End, :); non_test = zi and (. 1: Q2, :);
ELSEIF K == N
pos_train MU = (. 1: Ql * (. 1-K), :) ; pos_test = MU (Ql * (. 1-K) + 1'd: End, :);
non_train = zi and (. 1: Q2 * (. 1-K), :); non_test = zi and (Q2 * (. 1-K) + 1'd : End, :);
the else
pos_train = [MU (. 1: Ql * (. 1-K), :); MU (K * Ql: End, :)];
non_train = [zi and (. 1: Q2 * (. 1-K ), :); zi and (K * Q2: End, :)];
pos_test = MU (Ql * (. 1-K) + 1'd: * K-Ql. 1, :);
non_test = zi and (Q2 * (. 1-K ) + 1'd: Q2-K *. 1, :);
End
pos_train_r = size (pos_train,. 1); non_train_r = size (non_train,. 1); n number of rows based negative training set of class%
train = [pos_train; non_train] ;% training set were combined to give
train_num = pos_train_r + non_train_r;% training set of data points
y1 = [ones (pos_train_r, 1 ); - ones (non_train_r, 1)]; tab% training points

pos_test_r = size (pos_test,. 1); non_test_r = size (non_test,. 1);
Test = [pos_test; non_test]; % were combined to give test set
test_num = pos_test_r + non_test_r; data points% test set
y2 = [ones (pos_test_r, 1 ); - ones (non_test_r, 1)];% test point label

% defined calculated kernel function
K = zeros (train_num, train_num);
for I =. 1: train_num
for J =. 1: train_num
K (I, J) = exp (- (NORM (Train (I,:) - Train (J, :), 2)) ^. 2/2); 2% di-norm comma
End
End
Q = diag (Y1) * K * diag (Y1);
F = -ones (train_num,. 1);
Aeq = Y1 ';% input value is assigned to the series
= 0 BEQ;
VLB = zeros (train_num,. 1); VUB = C * ones (train_num,. 1);
Alpha = quadprog (Q, F, [], [], Aeq, BEQ, VLB, VUB); secondary% end solver

% w = alpha '* diag ( y1) * train;% calculated together in the core
for i=1:train_num
if (alpha(i)>0 && alpha(i)<vub(i))
b=y1(i)-alpha'*diag(y1)*K(:,i); %计算b的一种方法
break;
end
end

%测试
for i=1:train_num
for j=1:test_num
K(i,j)=exp(-(norm(train(i,:)-test(j,:),2)).^2/2);
end
end
count=0;%TP=0;TN=0;FP=0;FN=0;
pre_Y=sign(alpha'*diag(y1)*K+b);%预测标签
for i=1:test_num
if pre_Y(i)==y2(i)
count=count+1;
% if y2(i)==1
% TP=TP+1;
% else
% TN=TN+1;
% end
% else
% if y2(i)==1
% FP=FP+1;
% else
% FN=FN+1;
% end
end
end
%TP_ROC(k)=TP;FP_ROC(k)=FP;
% The ACC (K) = (TP + the TN) / (TP + the TN + FN + the FP);
% the MCC (K) = (TP * the TN-the FP * FN) / sqrt ((TP + the FP) * (TP + FN ) * (the TN + the FP) * (the TN + FN));
% Fl (K) = 2 * TP / (2 * TP + the FP + FN);
pre_right (K) = COUNT / test_num;
DISP (pre_right (K) );
End
P = mean (pre_right);
End
DISP ( 'average ten-fold cross rate correctly:');
DISP (P);
% Plot (FP_ROC, TP_ROC, 'R->');

Guess you like

Origin www.cnblogs.com/xiaoxuexue/p/12162402.html