How to set weights in fitcsvm?
    11 visualizzazioni (ultimi 30 giorni)
  
       Mostra commenti meno recenti
    
Hello
I'm using the fitcsvm method from Matlab to train a SVM. The BoxConstraint parameter is the C (cost). Because I have high class imbalance (2 class classification problem), I would like to set different weights for each class.
How can this be done in fitcsvm?
In the LibSVM library there is an option to set different weights for each class...
0 Commenti
Risposte (1)
  Abhipsa
 il 3 Mar 2025
        To handle class imbalance in the “fitcsvm” function using the “Weights” parameter of the “BoxConstraint”, you can assign different weights to each observation based on its class.
This effectively gives more importance to the minority class during training.
The below code segment demonstrates how to achieve the same :
% Example data
X = [randn(100, 2); randn(20, 2) + 3]; % Features
Y = [ones(100, 1); -ones(20, 1)];      % Labels
% Calculate class weights
% Assuming class 1 is the majority class and class -1 is the minority
numClass1 = sum(Y == 1);
numClassMinus1 = sum(Y == -1);
% Assign weights inversely proportional to class frequency so as to give higher weightage to the minority class
weightClass1 = 1;
weightClassMinus1 = numClass1 / numClassMinus1;
% Create a weight vector for each observation
weights = ones(size(Y));
weights(Y == -1) = weightClassMinus1;
% Train the SVM with weights
SVMModel = fitcsvm(X, Y, 'Weights', weights);
% Display the model
disp(SVMModel);
For more details please refer to the following MathWorks documentation for "fitcsvm" for more details:
Hope this helps!
0 Commenti
Vedere anche
Categorie
				Scopri di più su Classification in Help Center e File Exchange
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

