Kernel functions of Support Vector Machines in MATLAB

What is a Support Vector Machine (SVM)?

Support Vector Machine Classifier is an effective supervised machine learning algorithm, that has its application in classification and regression analysis. 

How it works?

An SVM maps training data to points in space, so that hyperplane can separate the data points into group. (The line that separates the classes is known as hyperplane). The objective of the SVM is to create the best decision boundary as to maximize the width of the gap between the two categories. Then test data are mapped into that same space and predicted to which category they belongbased on which side of the gap they fall.

Applications:

Signal processing, language processing, medical diagnosis, image and speech classification and so on.

Kernel functions:

SVM is a type of machine learning algorithm, which works based on the kernel function. A kernel function transforms the input data into required form for further processing. Kernel function maps the data into higher dimensional space, expecting the classes are easier to separate after transformation by potentially simplifying a complex non-linear decision boundaries to linear ones.

Standard kernel functions:

1. Gaussian or Radial Basis Function (RBF)
2. Linear
3. Polynomial
4. Sigmoid



MATLAB Code for classification with different kernel functions

clc; clear; close all; %% load data load ionosphere data=X';         %features label=categorical(Y); %labels %% now partition data into training and testing set n = length(label); h = cvpartition(n,'Holdout',0.2); %partition idxTrain = training(h); dataTrain = data(:,idxTrain); %train data labelTrain=label(idxTrain); % train label idxNew = test(h); datatest = data(:,idxNew); % test data labelTest=label(idxNew); % test label %% train the classifier with training data
%sigmoid model_sigmoid = fitcsvm(X,Y,'KernelFunction','mysigmoid','Standardize',true);
% RBF      model_rbf = fitcsvm(X,Y,'KernelFunction','rbf'); 
% linear                                                       
model_linear = fitcsvm(X,Y,'KernelFunction','linear');
% polynomial
model_polynomial = fitcsvm(X,Y,'KernelFunction','polynomial');                             %% predict the classifier output for test data
% predict the output (sigmoid) ypred_sigmoid=predict(model_sigmoid,datatest');  
% calculate accuracy of prediction                                                   Accuracy_sigmoid=(sum(ypred_sigmoid==labelTest))/length(labelTest)*100;
% predict the output (RBF)         ypred_rbf=predict(model_rbf,datatest');
% calculate accuracy of prediction                                                            Accuracy_rbf=(sum(ypred_rbf==labelTest))/length(labelTest)*100;
% predict the output (linear)                         ypred_linear=predict(model_linear,datatest');
% calculate accuracy of prediction                                                          Accuracy_linear=(sum(ypred_linear==labelTest))/length(labelTest)*100; 
% predict the output (polynomial)               
ypred_polynomial=predict(model_polynomial,datatest');
% calculate accuracy of prediction
Accuracy_polynomial=(sum(ypred_polynomial==labelTest))/length(labelTest)*100; % print the result fprintf('Accuracy of SVM (sigmoid) = %d\n',Accuracy_sigmoid) fprintf('Accuracy of SVM (RBF) = %d\n',Accuracy_rbf) fprintf('Accuracy of SVM (linear) = %d\n',Accuracy_linear) fprintf('Accuracy of SVM (polynomial) = %d\n',Accuracy_polynomial)

Comments

Post a Comment

Popular Posts