Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use one-class SVM in matlab?

Tags:

matlab

svm

I labeled some pixels of a picture as foreground and the rest of them are unlabeled yet. I want to use SVM and the labeled pixels's properties like color as SVM input to label the remaining pixels as background or foreground. Is that possible with one-class as input? Or do I need some pixels labeled as background (two-class input)?
Thanks in advance.
Edit: I found
http://scikit-learn.org/stable/auto_examples/svm/plot_oneclass.html
and
http://www.csie.ntu.edu.tw/~cjlin/libsvm/
For one-class SVM, but I dont know how to use it in matlab.

like image 406
Majid Ramzani Avatar asked Oct 24 '25 03:10

Majid Ramzani


1 Answers

Setting up LIBSVM in Matlab is described in the README file contained in the official package, which can be downloaded here

After you installed LIBSVM for your Matlab version, you can train an SVM model with the following:

matlab> model = svmtrain(training_label_vector, training_instance_matrix [, 'libsvm_options']);

Explanation (taken from the README)

 -training_label_vector:
        An m by 1 vector of training labels (type must be double).
    -training_instance_matrix:
        An m by n matrix of m training instances with n features.
        It can be dense or sparse (type must be double).
    -libsvm_options:
        A string of training options in the same format as that of LIBSVM.

The training options are:

-s svm_type : set type of SVM (default 0)
    0 -- C-SVC      (multi-class classification)
    1 -- nu-SVC     (multi-class classification)
    2 -- one-class SVM
    3 -- epsilon-SVR    (regression)
    4 -- nu-SVR     (regression)
-t kernel_type : set type of kernel function (default 2)
    0 -- linear: u'*v
    1 -- polynomial: (gamma*u'*v + coef0)^degree
    2 -- radial basis function: exp(-gamma*|u-v|^2)
    3 -- sigmoid: tanh(gamma*u'*v + coef0)
    4 -- precomputed kernel (kernel values in training_set_file)
-d degree : set degree in kernel function (default 3)
-g gamma : set gamma in kernel function (default 1/num_features)
-r coef0 : set coef0 in kernel function (default 0)
-c cost : set the parameter C of C-SVC, epsilon-SVR, and nu-SVR (default 1)
-n nu : set the parameter nu of nu-SVC, one-class SVM, and nu-SVR (default 0.5)
-p epsilon : set the epsilon in loss function of epsilon-SVR (default 0.1)
-m cachesize : set cache memory size in MB (default 100)
-e epsilon : set tolerance of termination criterion (default 0.001)
-h shrinking : whether to use the shrinking heuristics, 0 or 1 (default 1)
-b probability_estimates : whether to train a SVC or SVR model for probability estimates, 0 or 1 (default 0)
-wi weight : set the parameter C of class i to weight*C, for C-SVC (default 1)
-v n : n-fold cross validation mode
-q : quiet mode (no outputs)

If you want to train a One-Class-SVM (e.g. for Anomaly-Detection), you have to chose -s 2 as an option.

In addition the parameter nu might be interesting in the tuning of your trained SVM as well as the appropriate kernel parameters for the selected kernel type (for example via grid-search).

To train a One-Class-SVM via LIBSVM you should only provide data, which belongs to the under-represented class.

Nevertheless for your kind of problem (since you are not going to do some kind of anomaly detection and features / samples are not rare), you should go for a normal two-class SVM.

like image 167
rzo1 Avatar answered Oct 26 '25 23:10

rzo1



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!