One class classification using fitcsvm

5 views (last 30 days)
Nainsi Gupta
Nainsi Gupta on 30 May 2025
Edited: Hitesh on 2 Jun 2025
Classifier<- svm(PhaseI_Data,type='one-classification',kernel = "radial",
nu= nu,gamma= gamma, scale = F)
Above written code is from R. where nu is penalty factor, gamma is kernel bandwidth in radial kernel.
Can someone help me to write equivalent code in matlab.
I have tried multiple times but it is giving different results in both.

Answers (1)

Hitesh
Hitesh on 2 Jun 2025
Edited: Hitesh on 2 Jun 2025
Hi Nainsi,
MATLAB uses the "fitcsvm function". However, "fitcsvm"does not support one-class classification directly. You need to use the "fitcsvm" with 'OutlierFraction', which is equivalent to 1 - nu in R.
Kindly refer to the following code as example to implement "One class classification using fitcsvm".
% Assume PhaseI_Data is an n-by-d matrix (observations x features)
% and already normalized/scaled similarly to how it was used in R.
% Example dummy data: 100 points in 2D
PhaseI_Data = randn(100, 2); % Normally distributed data
% Convert R gamma to MATLAB's kernel scale:
% R: k(x, y) = exp(-gamma * ||x - y||^2)
% MATLAB: uses 'KernelScale', which is sigma such that
% k(x, y) = exp(-||x - y||^2 / (2*sigma^2))
gamma = 0.1; % or whatever value you used in R
sigma = sqrt(1 / (2 * gamma));
% MATLAB One-Class SVM equivalent:
SVMModel = fitcsvm(PhaseI_Data, ones(size(PhaseI_Data,1),1), ...
'KernelFunction', 'rbf', ...
'KernelScale', sigma, ...
'OutlierFraction', 1 - 0.1, ...
'Standardize', false);
For more information regarding "fitcsvm", kindly refer to the following MATLAB documentation:

Categories

Find more on Get Started with MATLAB in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!