Package Yuce;import Java.io.file;import weka.classifiers.classifier;import weka.classifiers.evaluation;import Weka.classifiers.trees.j48;import Weka.core.instance;import Weka.core.instances;import Weka.core.converters.ArffLoader; Public classtestclassification { Public Static voidMain (string[] args) {Try{File Inputfile=NewFile ("E:\\develop/weka-3-6/data/weather.numeric.arff"); Arffloader Loader=NewArffloader (); Loader.setfile (Inputfile); Instances Instrain=Loader.getdataset (); Instrain.setclassindex (Instrain.numattributes ()-1); Inputfile=NewFile ("E:\\develop/weka-3-6/data/weather.numeric.arff"); Loader.setfile (Inputfile); Instances instest=Loader.getdataset (); Instest.setclassindex (Instest.numattributes ()-1); Doublesum =instest.numinstances (); intright =0; Classifier Clas=NewJ48 (); //Classifier clas = new Weka.classifiers.bayes.BayesNet ();Clas.buildclassifier (Instrain); for(inti =0; i < sum; i++) { if(Clas.classifyinstance (Instest.instance (i)) = =instest.instance (i). Classvalue ()) { Right++; } System. out. println (Clas.classifyinstance (Instest.instance (i) +":"+instest.instance (i). Classvalue ()); System. out. println ("Classindex:"+instest.instance (i). Classindex ()); } System. out. println ("classification accuracy rate:"+right/sum); } Catch(Exception e) {e.printstacktrace (); } }}
Data dataset:
@relation contact-Lenses@attribute Age {young, pre-presbyopic, presbyopic} @attribute spectacle-Prescrip {myope, hypermetrope} @attribute astigmatism {No, yes} @attribute tear-prod-Rate {reduced, normal} @attribute Contact-Lenses {soft, hard, none} @data%% -instances%Young,myope,no,reduced,noneyoung,myope,no,normal,softyoung,myope,yes,reduced,noneyoung,myope,yes,normal, Hardyoung,hypermetrope,no,reduced,noneyoung,hypermetrope,no,normal,softyoung,hypermetrope,yes,reduced, Noneyoung,hypermetrope,yes,normal,hardpre-Presbyopic,myope,no,reduced,nonepre-Presbyopic,myope,no,normal,softpre-Presbyopic,myope,yes,reduced,nonepre-Presbyopic,myope,yes,normal,hardpre-Presbyopic,hypermetrope,no,reduced,nonepre-Presbyopic,hypermetrope,no,normal,softpre-Presbyopic,hypermetrope,yes,reduced,nonepre-Presbyopic,hypermetrope,yes,normal,nonepresbyopic,myope,no,reduced,nonepresbyopic,myope,no,normal, Nonepresbyopic,myope,yes,reduced,nonepresbyopic,myope,yes,normal,hardpresbyopic,hypermetrope,no,reduced, Nonepresbyopic,hypermetrope,no,normal,softpresbyopic,hypermetrope,yes,reduced,nonepresbyopic,hypermetrope,yes, Normal,none
Weka Code calls