无法使用Java中的Weka访问用于ML分类的训练数据集

时间:2019-03-31 16:53:49

标签: java android machine-learning error-handling weka

我正在尝试在Java(特别是Android Studio)中使用Weka对实例进行分类。最初,我从Desktop Weka GUI中保存了一个模型,然后尝试将其导入到我的项目目录中。如果我是正确的话,这将无法正常工作,因为Weka JDK在PC和Android上是不同的。

现在,我正在尝试通过导入训练数据集在Android本身上训练模型(如我所见,没有其他选择)。这是我遇到问题的地方。当我运行“ Test.java”时,出现此错误,提示未指定我的源,指向第23行调用.loadDataset方法的地方。           java.io.IOException:未指定源 但是,很明显,我已经指定了一条路径。这是正确的路径吗?即我不确定自己在做什么错。我看过其他示例/博客,但没有详细介绍。

我的最终目标:使用weka开发的模型在android / java中训练模型并在android / java中对实例进行分类。

可以在以下链接找到我的代码:

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);

    }
}

我的数据集当前所在位置的屏幕截图: Current Location of dataset

请在您方便的时候尽快通知我。

1 个答案:

答案 0 :(得分:1)

详细且详细的答案位于this链接:

简而言之,您必须在res目录中创建一个原始文件夹。然后将任何文件保存在那里。您将根据它们的资源ID访问这些文件。