4
String filePath = new File("").getAbsolutePath();
DataSource source = new DataSource(filePath + "\\src\\data\\data.arff");
Instances dataset = source.getDataSet();
// set class
dataset.setClassIndex(0);

// build model
**LinearRegression lr = new LinearRegression();**
lr.buildClassifier(dataset);
System.out.println(lr);

Right after LinearRegression instantiation I get this error:

Exception in thread "main" java.lang.NoClassDefFoundError: no/uib/cipr/matrix/Matrix at weka_prediction.Main_LinearRegression.main(Main_LinearRegression.java:22) Caused by: java.lang.ClassNotFoundException: no.uib.cipr.matrix.Matrix at java.net.URLClassLoader.findClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) ... 1 more

I am using weka 3.8.

any ideas? thanks in advance

Confront
  • 488
  • 3
  • 7

4 Answers4

4

There is a problem with loading weka.core. You just need to go to the weka repository and download and add the following jars:

  • mtj.jar
  • arpack_combined_all.jar
  • core.jar

See more details here:

Adrian Muntean
  • 322
  • 2
  • 15
  • 1
    links are down so let me put it here how to do it: go to Weka official website (https://www.cs.waikato.ac.nz/ml/weka/downloading.html) or Google it if the link is down, go to Downloads and get appropriate archive. Unpack it. You will find a weka.jar lib. This is actually the lib you need to work with Java. But if you have this exception - extract weka.jar and get 3 jars from there `arpack_combined.jar`, `core.jar` and `mtj.jar` and add them to your project along with weka.jar. This is how I fixed this error for weka 3.8. Cheers! – Kirill Karmazin Jan 09 '19 at 14:24
0

Solved! instead of 3.8, I am now using 3.6 from here: http://grepcode.com/project/repo1.maven.org/maven2/nz.ac.waikato.cms.weka/weka-stable/

Confront
  • 488
  • 3
  • 7
0

For anyone who are trying to run Weka as a tool. When you downloaded the weka archive, unzip it, you will find the weka.jar. And then just do what @Kirill Karmazin said in the comment in here. unzip the 3 jars arpack_combined.jar, core.jar and mtj.jar and put them alongside with weka.jar.

Then run java -cp * weka.gui.GUIChooser to run the Weka GUI.

Charles Chen
  • 21
  • 1
  • 3
0

Rather than extracting the MTJ and arpack libraries, simply use Weka's package manager to sort this out for you. It will initialize package management and, if necessary, add these internal jars to its classpath automatically.

You do this by calling the loadPackages method of the weka.core.WekaPackageManager class.

Here is an example:

import weka.core.converters.ConverterUtils.DataSource;                                                                  
import weka.core.Instances;
import weka.core.WekaPackageManager;
import weka.classifiers.functions.LinearRegression;

public class LR {
  public static void main(String[] args) throws Exception {
    // load packages and MTJ/arpack libraries (if necessary)
    WekaPackageManager.loadPackages(false);
    // load dataset
    DataSource source = new DataSource(args[0]);
    Instances dataset = source.getDataSet();
    // set class
    dataset.setClassIndex(dataset.numAttributes() - 1);
    // build model
    LinearRegression lr = new LinearRegression();
    lr.buildClassifier(dataset);
    System.out.println(lr);
  }
}
fracpete
  • 2,448
  • 2
  • 12
  • 17