www.gusucode.com > stats 源码程序 matlab案例代码 > stats/ComparePerformanceOfLinearAndNonlinearSVMExample.m
%% Compare the Performance of Linear and Nonlinear SVM % This exmaple shows how to use misclassification rates to compare two % implementations of SVM. %% % Randomly generate data from two bivariate Gaussian distributions with means % $\mu_1$ and $\mu_2$, and covariance matrices $\Sigma_1$ and $\Sigma_2$. % % $$\mu_1=\left[\matrix{1\cr 1}\right], \mu_2=\left[\matrix{3\cr 2}\right],\Sigma_1 = \left[\matrix{0.25 & 0\cr 0& 0.25}\right],\Sigma_2 = \left[\matrix{2 & 0\cr 0& 2}\right].$$ % Copyright 2015 The MathWorks, Inc. rng(1); % For reproducibility n = 1000; Mu = [1 1;3 2]; % Mean matrix Sigma = 0.25*eye(2); % Covariance matrices Sigma(:,:,2) = 2*eye(2); GMDist = gmdistribution(Mu,Sigma); [X,idx1] = random(GMDist,n); %% % For illustration, assign the class 1 sample units that are close to the % center of class 0 to class 0. Recode to a 0\1 response. idx2 = idx1 == 2 & sqrt(X(:,1).^2-1) < 1.25 & sqrt(X(:,2).^2-1) < 1.25; idx1(idx2,:) = 1; y = idx1 - 1; %% % Train the SVM classifier using the full data set. Use the default linear kernel option. EstMdlLIN = svmtrain(X,y,'showplot','true'); %% % The plot displays the linear decisiom boundary, and circles the support % vectors. %% % Train the SVM classifier using the full data set. Use the Gaussian radial basis kernel. EstMdlRBF = svmtrain(X,y,'showplot','true','kernel_function','rbf'); %% % The plot displays the nonlinear decision boundary, and circles the % support vectors. %% % Examine the training set misclassification rates for both algorithms. predLIN = svmclassify(EstMdlLIN,X); misclassLIN = sum(y~=predLIN)/n predRBF = svmclassify(EstMdlRBF,X); misclassRBF = sum(y~=predRBF)/n %% % |misclassLIN| > |misclassRBF|, therefore, the SVM that uses a Gaussian % radial basis kernel is a better classifier for the traning set. %% % There are a number of other steps you can take to improve the algorithm. % You can: % % * Adjust the standard deviation of the Gaussian kernel by setting |'rbf_sigma'|. % * Adjust the allowance given to sample units that cross the margin by % setting |'boxconstraint'|. % * Use a different kernel by setting |'kernel_function'| % %% % In order to properly assess the performance of a trained algorithm, it is % best practice to partition the data set into training and validation % sets. Examine the misclassification rates for both sets among all % algorithms, and choose the algorithm that performs the best.