这一篇介绍一下如何使用Ensemble的类,不过提醒一下,不要什么都来个Ensemble,对这种毫无想象力可言的做法,我真是有点无语。
我已经写了这么多篇了,我也有点累了,这篇也是有人发E-mail让我写的。大家就先自己看吧,至于是不是对的,我以后有机会了我看了,再告诉大家了。
package instanceTest; import java.io.FileReader; import weka.classifiers.Classifier;import weka.classifiers.bayes.NaiveBayes;import weka.classifiers.functions.LibSVM;import weka.classifiers.functions.SMO;import weka.classifiers.meta.AdaBoostM1;import weka.classifiers.meta.Vote;import weka.classifiers.trees.J48;import weka.core.Instances;import weka.core.SelectedTag; public class EnsembleTest{ private Instances m_instances = null; public void getFileInstances( String fileName ) throws Exception { FileReader frData = new FileReader( fileName ); m_instances = new Instances( frData ); m_instances.setClassIndex( m_instances.numAttributes() - 1 ); } public void AdaBoostClassify() throws Exception { LibSVM baseClassifier = new LibSVM(); AdaBoostM1 classifier = new AdaBoostM1(); classifier.setClassifier( baseClassifier ); classifier.buildClassifier( m_instances ); System.out.println( classifier.classifyInstance( m_instances.instance( 0 ) ) ); } public void VoteClassify() throws Exception { Classifier baseClassifiers[] = new Classifier[3]; baseClassifiers[0] = new J48(); baseClassifiers[1] = new NaiveBayes(); baseClassifiers[2] = new SMO(); Vote classifier = new Vote(); SelectedTag tag = new SelectedTag(Vote.MAJORITY_VOTING_RULE,Vote.TAGS_RULES); classifier.setCombinationRule( tag ); classifier.setClassifiers( baseClassifiers ); classifier.buildClassifier( m_instances ); System.out.println( classifier.classifyInstance( m_instances.instance( 0 ) ) ); } public static void main( String[] args ) throws Exception { EnsembleTest etest = new EnsembleTest(); etest.getFileInstances( "F://Program Files//Weka-3-6//data//contact-lenses.arff"); etest.AdaBoostClassify(); etest.VoteClassify(); }}