无法使用Java中的Weka访问用于ML分类的训练数据集 [英] Unable to access training dataset for ML classification using Weka in Java

查看:78
本文介绍了无法使用Java中的Weka访问用于ML分类的训练数据集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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.

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

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

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

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

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.

推荐答案

详细且详细的答案位于

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

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

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.

这篇关于无法使用Java中的Weka访问用于ML分类的训练数据集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆