Unable to access training dataset for ML classification using Weka in Java

♀尐吖头ヾ 提交于 2019-12-06 16:16:44

问题


I am trying to classify an instance using Weka in Java (specifically Android Studio). Initially, I saved a model from the Desktop Weka GUI and tried to import it into my project directory. If I am correct, this won't work because the Weka JDKs are different on PC versus Android.

Now I am trying to train a model on the Android itself (as I see no other option) by importing the training dataset. Here is where I am running into problems. When I run "Test.java," I get this error saying that my source hasn't been specified pointing to line 23 where I invoke the .loadDataset method. java.io.IOException: No source has been specified But, clearly, I have specified a path. Is this the correct path? I.E. I am not sure what I am doing wrong. I have looked at other examples/blogs, but none go in detail.

My end goal: train a model in android/java and classify instances in android/java using weka-developed models.

My code can be found at these links:

ModelGenerator.java

package com.example.owner.introductoryapplication;

import android.support.v7.app.AppCompatActivity;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.logging.Level;
import java.util.logging.Logger;

import weka.classifiers.Classifier;
import weka.classifiers.Evaluation;
import weka.classifiers.functions.MultilayerPerceptron;
import weka.core.Instances;
import weka.core.SerializationHelper;
import weka.core.converters.ConverterUtils;

public class ModelGenerator
{
    //String trainingSetPath = "JavaTrainingSet.arff"; //com/example/owner/introductoryapplication/JavaTrainingSet.arff
    //String modelSavedPath = "com/example/owner/introductoryapplication/JavaTrainingSet.csv";

    //Loading dataset from ARFF file and save it to Instances object
    public Instances loadDataset(String path)
    {
        //Declaration and initialization of null training set
        Instances dataset = null;

        //Loading the dataset into the program
        try
        {
            //Read the dataset
            dataset = ConverterUtils.DataSource.read(path);
            if (dataset.classIndex() == -1) {
                dataset.setClassIndex(dataset.numAttributes() - 1);
            }
        } catch (Exception ex) {
            Logger.getLogger(ModelGenerator.class.getName()).log(Level.SEVERE, null, ex);
        }

        return dataset;
    }

    //Building classifier for training set using MultilayerPerceptron (Neural network)
    public Classifier buildClassifier(Instances traindataset) {
        MultilayerPerceptron m = new MultilayerPerceptron();

        try {
            m.buildClassifier(traindataset);

        } catch (Exception ex) {
            Logger.getLogger(ModelGenerator.class.getName()).log(Level.SEVERE, null, ex);
        }
        return m;
    }

    //Evaluating the accuracy for the generated model with test set
    public String evaluateModel(Classifier model, Instances traindataset, Instances testdataset) {
        Evaluation eval = null;
        try {
            // Evaluate classifier with test dataset
            eval = new Evaluation(traindataset);
            eval.evaluateModel(model, testdataset);
        } catch (Exception ex) {
            Logger.getLogger(ModelGenerator.class.getName()).log(Level.SEVERE, null, ex);
        }
        return eval.toSummaryString("", true);
    }

    //Saving the generated model to a path to use it for future prediction
    public void saveModel(Classifier model, String modelpath) {
        try {
            SerializationHelper.write(modelpath, model);
        } catch (Exception ex) {
            Logger.getLogger(ModelGenerator.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

}

ModelClassifier.java

package com.example.owner.introductoryapplication;

import android.support.v7.app.AppCompatActivity;

import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import weka.classifiers.Classifier;
import weka.classifiers.functions.MultilayerPerceptron;
import weka.core.Attribute;
import weka.core.DenseInstance;
import weka.core.Instances;
import weka.core.SerializationHelper;

public class ModelClassifier
{
    private Attribute Age;
    private Attribute Height;
    private Attribute Weight;
    private Attribute UPDRS;
    private Attribute TUAG;
    private Attribute Speed;
    private Attribute Gender;

    private ArrayList attributes;
    private ArrayList classVal;
    private Instances dataRaw;


    public ModelClassifier() {
        Age = new Attribute("Age");
        Height = new Attribute("Height");
        Weight = new Attribute("Weight");
        UPDRS = new Attribute("UPDRS");
        TUAG = new Attribute("TUAG");
        Speed = new Attribute("Speed");
        Gender = new Attribute("Gender");

        attributes = new ArrayList();
        classVal = new ArrayList();
        classVal.add("PD");
        classVal.add("CO");

        attributes.add(Age);
        attributes.add(Height);
        attributes.add(Weight);
        attributes.add(UPDRS);
        attributes.add(TUAG);
        attributes.add(Speed);
        attributes.add(Gender);

        attributes.add(new Attribute("class", classVal));
        dataRaw = new Instances("TestInstances", attributes, 0);
        dataRaw.setClassIndex(dataRaw.numAttributes() - 1);
    }


    public Instances createInstance(double Age, double Height, double Weight, double UPDRS, double TUAG, double Speed, double Gender, double result) {
        dataRaw.clear();
        double[] instanceValue1 = new double[]{Age, Height, 0};
        dataRaw.add(new DenseInstance(1.0, instanceValue1));
        return dataRaw;
    }


    public String classifiy(Instances insts, String path) {
        String result = "Not classified!!";
        Classifier cls = null;
        try {
            cls = (MultilayerPerceptron) SerializationHelper.read(path);
            result = (String) classVal.get((int) cls.classifyInstance(insts.firstInstance()));
        } catch (Exception ex) {
            Logger.getLogger(ModelClassifier.class.getName()).log(Level.SEVERE, null, ex);
        }
        return result;
    }


    public Instances getInstance() {
        return dataRaw;
    }
}

Test.java

package com.example.owner.introductoryapplication;

import com.example.owner.introductoryapplication.ModelGenerator;
import com.example.owner.introductoryapplication.ModelClassifier;

import android.support.v7.app.AppCompatActivity;

import weka.classifiers.functions.MultilayerPerceptron;
import weka.core.Debug;
import weka.core.Instances;
import weka.filters.Filter;
import weka.filters.unsupervised.attribute.Normalize;

public class Test
{
    String DATASETPATH = "com/example/owner/introductoryapplication/JavaTrainingSet.arff";
    String MODElPATH = "com/example/owner/introductoryapplication/model.bin";

    public static void main(String[] args) throws Exception {

        ModelGenerator mg = new ModelGenerator();

        Instances dataset = mg.loadDataset("/com/example/owner/introductoryapplication/JavaTrainingSet.arff");

        Filter filter = new Normalize();

        // divide dataset to train dataset 80% and test dataset 20%
        int trainSize = (int) Math.round(dataset.numInstances() * 0.8);
        int testSize = dataset.numInstances() - trainSize;

        dataset.randomize(new Debug.Random(1));// if you comment this line the accuracy of the model will be droped from 96.6% to 80%

        //Normalize dataset
        filter.setInputFormat(dataset);
        Instances datasetnor = Filter.useFilter(dataset, filter);

        Instances traindataset = new Instances(datasetnor, 0, trainSize);
        Instances testdataset = new Instances(datasetnor, trainSize, testSize);

        // build classifier with train dataset
        MultilayerPerceptron ann = (MultilayerPerceptron) mg.buildClassifier(traindataset);

        // Evaluate classifier with test dataset
        String evalsummary = mg.evaluateModel(ann, traindataset, testdataset);
        System.out.println("Evaluation: " + evalsummary);

        //Save model
        mg.saveModel(ann, "/com/example/owner/introductoryapplication/model.bin");

        //classifiy a single instance
        ModelClassifier cls = new ModelClassifier();
        String classname = cls.classifiy(Filter.useFilter(cls.createInstance(50, 20, 30, 14, 16, 10.42, 2, 0), filter), "/com/example/owner/introductoryapplication/model.bin");
        System.out.println("\n The class name for the instance is: " + classname);
        System.out.println("\n The class name for the instance is: " + classname);

    }
}

A screenshot of where my dataset is right now:

Please let me know at your earliest convenience.


回答1:


Elaborate and in-detail answer is located at this link:

In short, you have to make a raw folder within the res directory. Then save whatever file there. You will access these files per their resource ID's.



来源:https://stackoverflow.com/questions/55443209/unable-to-access-training-dataset-for-ml-classification-using-weka-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!