如何在 Weka 中测试用户输入的单个测试用例? [英] How to test a single test case in Weka, entered by a User?

查看:35
本文介绍了如何在 Weka 中测试用户输入的单个测试用例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 Weka 还很陌生.我正在编写一个代码,其中我构建了一个 J48 来预测学生成绩的结果.现在我已经尝试使用 ARFF 文件测试模型,但我想实现用户输入的测试用例的分类.例如.我希望用户输入两个数值,它们是在两个科目(即 CS 和数学)中获得的分数,然后预测他们的最终结果,即通过或失败.结果将是类变量.

I am fairly new to Weka. I am doing a code where I am built a J48 to predict outcome of students' results. Now I have made attempts to test the model using an ARFF file but I want to achieve a classification of a test case which is entered by a user. E.g. I want the user to enter two numeric values which are marks obtained in two subjects i.e. CS and Maths and then predict their final outcome i.e. PASS or FAIL. Outcome would be the class variable.

我不知道如何创建一个实例来执行这样的事情

I dont know how to create an instance to perform something like this

double pred = tree.ClassifyInstance(testcase);      

这是我的代码.

import java.security.KeyStore;
import weka.classifiers.Classifier;
import weka.classifiers.Evaluation;
import weka.classifiers.trees.J48;
import weka.core.Attribute;
import weka.core.FastVector;
import weka.core.Instance;
import weka.core.Instances;
import weka.core.Utils;
import weka.core.converters.ConverterUtils.DataSource;

public class WekaTest {
    public static void main (String[] args) throws Exception
    {
        //load the train set
        DataSource source = new DataSource("train.arff");
        Instances train = source.getDataSet();
        //Set class variable i.e. Outcome
        train.setClassIndex(train.numAttributes()- 1);

        Classifier tree = new J48();
        tree.buildClassifier(train);
       // Instance test = new Instance();
       // Evaluation eval = new Evaluation(train);

     Attribute COMS1000 = new Attribute("COMS1000");
     Attribute MATH1001 = new Attribute("MATH1001");

        FastVector classVal = new FastVector(2);
        classVal.addElement("PASS");
        classVal.addElement("FAIL");


        FastVector testAttributes = new FastVector(3);

        testAttributes.addElement(COMS1000);
        testAttributes.addElement(MATH1001);
        testAttributes.addElement(classVal);

      Instance testcase  = new Instance(3);
        //testcase.setClassIndex(testcase.numAttributes()-1);

        testcase.setValue((Attribute)testAttributes.elementAt(0),60);
        testcase.setValue((Attribute)testAttributes.elementAt(1),70);
        testcase.setValue((Attribute)testAttributes.elementAt(2),"?");

        double pred = tree.classifyInstance(testcase)

         System.out.println(pred.value(Double.toString(pred)));



    }

}

推荐答案

这样的事情应该对你有用,基本上我们创建一个新实例并使用扫描仪为新实例获取用户输入(我还没有测试这个,让我知道它是否有效或有任何问题):

Something like this should work for you, basically we create a new instance and use a scanner to take user input for the new instance (I haven't test this, let me know if it works or if there are any problems):

double a, b;
Scanner s = new Scanner(System.in);
System.out.println("Please enter marks:");
a = s.nextDouble();
b = s.nextDouble();

Instance inst = new DenseInstance(3); 

inst.setValue(COMS1000, a); 
inst.setValue(MATH1001, b); 
inst.setClassMissing();

inst.setDataset(source); 

double pred = tree.classifyInstance(inst);
System.out.println(pred.value(Double.toString(pred)));

这篇关于如何在 Weka 中测试用户输入的单个测试用例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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