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 class testClassification { public static void main(String[] args) { try { File inputfile = new File("E:\\Develop/Weka-3-6/data/weather.numeric.arff"); ArffLoader loader = new ArffLoader(); loader.setFile(inputfile); Instances insTrain = loader.getDataSet(); insTrain.setClassIndex(insTrain.numAttributes()-1); inputfile = new File("E:\\Develop/Weka-3-6/data/weather.numeric.arff"); loader.setFile(inputfile); Instances insTest = loader.getDataSet(); insTest.setClassIndex(insTest.numAttributes()-1); double sum = insTest.numInstances(); int right = 0; Classifier clas = new J48(); //Classifier clas = new weka.classifiers.bayes.BayesNet(); clas.buildClassifier(insTrain); for(int i = 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("分类准确率:"+right/sum); } catch (Exception e) { e.printStackTrace(); } } }
data数据集:
@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 % % 24 instances % young,myope,no,reduced,none young,myope,no,normal,soft young,myope,yes,reduced,none young,myope,yes,normal,hard young,hypermetrope,no,reduced,none young,hypermetrope,no,normal,soft young,hypermetrope,yes,reduced,none young,hypermetrope,yes,normal,hard pre-presbyopic,myope,no,reduced,none pre-presbyopic,myope,no,normal,soft pre-presbyopic,myope,yes,reduced,none pre-presbyopic,myope,yes,normal,hard pre-presbyopic,hypermetrope,no,reduced,none pre-presbyopic,hypermetrope,no,normal,soft pre-presbyopic,hypermetrope,yes,reduced,none pre-presbyopic,hypermetrope,yes,normal,none presbyopic,myope,no,reduced,none presbyopic,myope,no,normal,none presbyopic,myope,yes,reduced,none presbyopic,myope,yes,normal,hard presbyopic,hypermetrope,no,reduced,none presbyopic,hypermetrope,no,normal,soft presbyopic,hypermetrope,yes,reduced,none presbyopic,hypermetrope,yes,normal,none
时间: 2024-10-09 19:44:12