machine learning - Weka - How to use classifier in Java -
i have created model in weka explorer, , saved .model file
first, load saved model java code
classifier cls = null; try { cls = (classifier) weka.core.serializationhelper.read("model.model"); } catch (exception e1) { e1.printstacktrace(); }
then, read instance want classify, .arff file
bufferedreader reader = new bufferedreader(new filereader(file)); arffreader arff = new arffreader(reader); instances data = arff.getdata();
the file, contains 1 instance. value of class attribute '?'. code below, try make classification of instance.
data.setclassindex(data.numattributes()-1); try { double value=cls.classifyinstance(data.firstinstance()); string prediction=data.classattribute().value((int)value); system.out.println("the predicted value of instance "+ integer.tostring(s1)+ ": "+prediction); } catch (exception e) { e.printstacktrace(); }
is right way;
that's correct sample of code wanted do. useful method is:
yourclassifier.distributionforinstance(yourinstance);
that returns double[] probabilities of instance every class label. it's useful not-so-clear problems, class membership fuzzy concept.
Comments
Post a Comment