How to test a single test case in Weka, entered by a User?

こ雲淡風輕ζ 提交于 2019-12-25 13:25:07

问题


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

Here is my code.

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



    }

}

回答1:


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


来源:https://stackoverflow.com/questions/33257198/how-to-test-a-single-test-case-in-weka-entered-by-a-user

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