One class classification using fitcsvm

조회 수: 5 (최근 30일)
Nainsi Gupta
Nainsi Gupta 2025년 5월 30일
편집: Hitesh 2025년 6월 2일
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.

답변 (1개)

Hitesh
Hitesh 2025년 6월 2일
편집: Hitesh 2025년 6월 2일
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:

카테고리

Help CenterFile Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by