Search code examples
javaandroidandroid-studioweka

Error trying to call .buildClassifier (Weka) in Java AndroidStudio


I am trying to use Weka in Android Studio. And I am stuck at the this line:

linearRegresion.buildClassifier(data);

Its red underlined and it says:

Unhandled Exception:Java.lang.Exception

I was trying other ways and with different dataSet and its allways underlined at .buildClassifier

    //test:
    NaiveBayes nB = new NaiveBayes();
    nB.buildClassifier(dataSet); //Unhandled exception

    Classifier cModel = (Classifier)new LinearRegression();
    cModel.buildClassifier(data); //Unhandled exception

Trying to fix this for hours, couldn't find solution on internet,I think I am just missing something maybe I just need to Import something more?

I was doing this from the tutorial so the code should work. Tutorial: http://www.ibm.com/developerworks/opensource/library/os-weka3/index.html

Whole code

import weka.*;
import weka.classifiers.trees.J48;
import weka.core.*;
import weka.classifiers.Classifier;
import weka.core.Instance;
import weka.core.Instances;
import weka.classifiers.functions.LinearRegression;
import weka.classifiers.bayes.NaiveBayes;

public class Meni extends Activity {
public void Weka(){
    //@ATTRIBUTE houseSize NUMERIC
    Attribute a1 = new Attribute("houseSize", 0);
    Attribute a2 = new Attribute("lotSize", 1);
    Attribute a3 = new Attribute("bedrooms", 2);
    Attribute a4 = new Attribute("granite", 3);
    Attribute a5 = new Attribute("bathroom", 4);
    Attribute a6 = new Attribute("sellingPrice", 5);

    //ArrayList attr = new ArrayList();
    FastVector attrs = new FastVector();
    attrs.addElement(a1);
    attrs.addElement(a2);
    attrs.addElement(a3);
    attrs.addElement(a4);
    attrs.addElement(a5);
    attrs.addElement(a6);

    //@DATA
    Instance i1  = new DenseInstance(6);
    i1.setValue(a1, 3529);
    i1.setValue(a2, 9191);
    i1.setValue(a3, 6);
    i1.setValue(a4, 0);
    i1.setValue(a5, 0);
    i1.setValue(a6, 205000);
    Instance i2  = new DenseInstance(6);
    i2.setValue(a1, 3247);
    i2.setValue(a2, 10061);
    i2.setValue(a3, 5);
    i2.setValue(a4, 1);
    i2.setValue(a5, 1);
    i2.setValue(a6, 224900);
    Instance i3  = new DenseInstance(6);
    i3.setValue(a1, 4032);
    i3.setValue(a2, 10150);
    i3.setValue(a3, 5);
    i3.setValue(a4, 0);
    i3.setValue(a5, 1);
    i3.setValue(a6, 197900);

    //set class Atribute
    dataSet.setClassIndex(dataSet.numAttributes()-1);

    //Creating model
    LinearRegression linearRegresion = new LinearRegression();
    linearRegresion.buildClassifier(data); //Unhandled exception :(

} }


Solution

  • It was problem with weka.jar library. Normal one that I download from weka site is not working with android.Now I downloaded modified one from user rjmanrsan: https://github.com/rjmarsan/Weka-for-Android Working now :)