Categories
Java

Using libsvm with Java, Compiling, Training, Predicting – Test

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”

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.

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 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.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.