I try to understand and learn to use libsvm to training, classify and recognize thing but first time, it’s hard to understand it without reading well about SVM.
Then I face again the issue understanding the libsvm that I’m going to use, here the way I go:
I choose Java to use with libsvm so here one of my practice on libsvm
Download
I download version 3.17 (tar.gz) from following site:
http://www.csie.ntu.edu.tw/~cjlin/libsvm/index.html
Compile
You can compile all java files at:
- libsvm-3.17\java\libsvm
- libsvm-3.17\java
But my case, I used pre-compile libsvm: libsvm.jar from the package
Learn with example svmguide1
libsvm comes with following methods:
– ‘svm-train’
– ‘svm-predict’
– ‘svm-scale’
Read at Installation and data format to understand these functions.
Example with svmguide1
- Download the training and testing data at libsvm dataset
- I place the files in folder: guide1, here is my folder structures
.java │ batch-svm-predict.bat (to create) │ batch-svm-train.bat (to create) │ libsvm.jar │ Makefile │ svm_predict.java │ svm_scale.java │ svm_toy.java │ svm_train.java │ test_applet.html │ ├───guide1 (I created) │ svmguide1 │ svmguide1.model │ svmguide1.predict │ svmguide1.t │ ├───libsvm │ svm.java │ svm.m4 │ svm_model.java │ svm_node.java │ svm_parameter.java │ svm_print_interface.java │ svm_problem.java
- I write a batch script to run: svm-training: batch-svm-train.bat
@echo off setlocal title Batch SVM Training with libsvm java -classpath libsvm.jar svm_train guide1/svmguide1 guide1/svmguide1.model Pause endlocal
- Run svm-training on the svmguid1 file:
D:\libsvm-3.17\java>batch-svm-train.bat .....* optimization finished, #iter = 5371 nu = 0.6061503521187195 obj = -1061.528918183956, rho = -0.49526608854956916 nSV = 3053, nBSV = 722 Total nSV = 3053 Press any key to continue . . .
- Write batch file for svm-predict for testing file: batch-svm-predict.bat
@echo off setlocal title Batch SVM Predict with libsvm java -classpath libsvm.jar svm_predict guide1/svmguide1.t guide1/svmguide1.model guide1/svmguide1.predict Pause endlocal
- Run svm-predict on testing file: svmguide1.t with recent generated model
D:\libsvm-3.17\java>batch-svm-predict.bat Accuracy = 66.925% (2677/4000) (classification)
Result
So here, I got result of accuracy = 66.925%
That’s a beginner guide to get into libsvm with Java that’s you might need it as me at the first time.
Next… I need to understand how a training/testing data generated.
Updated
- Sample Testing Package in GitHub
13 replies on “Using libsvm with Java, Compiling, Training, Predicting – Test”
Anything more to share or correct, please help to reply in comment.
Above is work for me.
Nice article. how a training/testing data generated? Any update on this.
I was considering to use libsvm in my research thesis but it was changed after I could not manage to handle training/testing related to my issue, I have switched to use: Accord.NET and write in C# instead.
Of course, I’m still interesting with libsvm, I will be back.
how to generate libsvm format data from text to predict the class in java ?
Hi,
Sorry to be so late to reply. I though first to try something before but until now I can’t manage time enough to do so.
I don’t know if you already got your answer some where (please share if any) but if it is not, I only can suggest now to read the libsvm document to try out.
I hope I could answer your question sometimes if I can write an example.
Regards.
i need thode source code in C# his my email please “abeddjemaa.@gmail.com”
Thanks for your interest, currently I have only source code in Java, I haven’t written it in C# yet. But if you are using C#, I am using a framework called: Accord.NET which is supporting SVM well enough to use. Please visit: http://accord-framework.net/
send me the sorce code in java in my email adrress “djemaaabed@gmail.com” thanks advance
Please find the testing package of the article here: https://github.com/metrey/LearnLibSVM
How do i use libsvm from java code ?
I haven’t tried the full process but I think we can use it easily via including the dependency via maven:
http://mvnrepository.com/artifact/tw.edu.ntu.csie/libsvm
The rest, we need to see the doc, some of above classes (in my github) might help. Please share back if you could achieve it since currently I’m not yet back to work on this again.
How to create the model file? what’s its format?
I want to make a java program and run on console instead of using command prompt.
do you have any Idea how can i make a program using lib svm and what t pass at which function.