One class classification using fitcsvm

5 次查看(过去 30 天)
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