2

In Java programming, we should firstly add weka.jar into our classpath, thus we can call all classify or cluster algorithms in WEKA in the form of the following codes,

import weka.classifiers.trees.RandomForest; 
...
RandomForest rf = new RandomForest(); // RandomForest object

But unfortunately, we can not use this way to import LibSVM algorithm, because there is not such class in weka.jar.

So, my question is How to import LibSVM into my Java code? Any help will be grateful :)

Yongfeng
  • 345
  • 1
  • 3
  • 15
  • LibSVM is it's only library so you will need to add the libsvm.jar to your project library. – Steve Jul 07 '17 at 14:39
  • See https://stackoverflow.com/questions/5223982/how-to-use-libsvm-with-weka-in-my-java-code which may be of help. – tale852150 Jul 07 '17 at 14:40
  • Thanks a lot, but these 2 methods do not work for me. a) `WekaPackageManager` will throw an exception of ClassNotFoundException; b) Link to `wlsvm.jar` is wrong, and I also can not find class LibSVM in `libsvm.jar` and `weka.jar`. So I doubt that these methods are really checked by authors :( – Yongfeng Jul 08 '17 at 02:46
  • I have tried versions 3.8.1 and 3.9.1 of weka, but I still can not find `weka.classifier.functions.LibSVM` T^T – Yongfeng Jul 08 '17 at 03:42

2 Answers2

2

Firstly, I'd like to say there are so many methods to solve the problem. The solution I mentioned is quite simple, but other answers from StackOverflow are not detailed descripted, with waste my too much time to verify. So I'm happy to share it with all WEKA beginners :)

a) Download the LibSVM.jar from Maven Repository Center. Note that this LibSVM.jar is different from the libsvm.jar developed by Chih-Chung Chang and Chih-Jen Lin;

b) Add the LibSVM.jar to the classpath of our Java project;

c) Call the classifier LibSVM when you need, see the following Java code.

import weka.classifiers.functions.LibSVM; // contained in LibSVM.jar

String path = "file/train.arff";
Instances train = DataSource.read(path); // load the dataset
train.setClassIndex(train.numAttribute()-1); // set class index

LibSVM svm = new LibSVM(); // load the svm classifier
svm.buildClassifier(train);

Evaluation eval = new Evaluation(train);
eval.crossValidateModel(svm, train, 10, new Random(1)); // 10-fold cross-validation
Yongfeng
  • 345
  • 1
  • 3
  • 15
1

See: https://weka.wikispaces.com/LibSVM

Use Weka's package manager to install the LibSVM. Suppose "weka.jar" is in your current folder, than run this:

java -cp weka.jar weka.core.WekaPackageManager -install-package LibSVM

During the installation, it shows:

[DefaultPackageManager] Tmp file: /tmp/LibSVM1.0.107382715397815864641.zip
[DefaultPackageManager] Installing: Description.props
[DefaultPackageManager] Installing: LibSVM.jar
[DefaultPackageManager] Installing: build_package.xml
...

You can see that "LibSVM.jar" is installed somewhere. In my case, it is at:

/home/john/wekafiles/packages/LibSVM/LibSVM.jar
Jun Wang
  • 879
  • 8
  • 10