/** * Main method for testing this class. * * @param argv should contain the following arguments: * -t training file [-T test file] [-c class index] */ public static void main(String [] argv) { runClassifier(new Stacking(), argv); } }
/** * Output a representation of this classifier * * @return a string representation of the classifier */ public String toString() { if (m_Classifiers.length == 0) { return "Stacking: No base schemes entered."; } if (m_MetaClassifier == null) { return "Stacking: No meta scheme selected."; } if (m_MetaFormat == null) { return "Stacking: No model built yet."; } String result = "Stacking\n\nBase classifiers\n\n"; for (int i = 0; i < m_Classifiers.length; i++) { result += getClassifier(i).toString() +"\n\n"; } result += "\n\nMeta classifier\n\n"; result += m_MetaClassifier.toString(); return result; }
/** * Gets the current settings of the Classifier. * * @return an array of strings suitable for passing to setOptions */ public String [] getOptions() { String [] superOptions = super.getOptions(); String [] options = new String [superOptions.length + 4]; int current = 0; options[current++] = "-X"; options[current++] = "" + getNumFolds(); options[current++] = "-M"; options[current++] = getMetaClassifier().getClass().getName() + " " + Utils.joinOptions(((OptionHandler)getMetaClassifier()).getOptions()); System.arraycopy(superOptions, 0, options, current, superOptions.length); return options; }
Stacking nb=new Stacking(); SMO smo=new SMO(); Classifier[] stackoptions = new Classifier[1]; stackoptions[0] = smo; nb.setClassifiers(stackoptions);
throws Exception { Instances metaData = metaFormat(newData); m_MetaFormat = new Instances(metaData, 0); for (int j = 0; j < m_NumFolds; j++) { buildClassifiers(train); if (baseClassifiersImplementMoreEfficientBatchPrediction()) { metaData.addAll(metaInstances(test)); } else { for (int i = 0; i < test.numInstances(); i++) { metaData.add(metaInstance(test.instance(i)));
getCapabilities().testWithFail(data); generateMetaLevel(newData, random); buildClassifiers(newData);
throws Exception { Instances metaData = metaFormat(newData); m_MetaFormat = new Instances(metaData, 0); for (int j = 0; j < m_NumFolds; j++) { buildClassifiers(train); metaData.add(metaInstance(test.instance(i)));
/** Creates a default Stacking */ public Classifier getClassifier() { return new Stacking(); }
/** * Returns an enumeration describing the available options. * * @return an enumeration of all the available options. */ public Enumeration<Option> listOptions() { Vector<Option> newVector = new Vector<Option>(2); newVector.addElement(new Option(metaOption(), "M", 0, "-M <scheme specification>")); newVector.addElement(new Option("\tSets the number of cross-validation folds.", "X", 1, "-X <number of folds>")); newVector.addAll(Collections.list(super.listOptions())); if (getMetaClassifier() instanceof OptionHandler) { newVector.addElement(new Option("", "", 0, "\nOptions specific to meta classifier " + getMetaClassifier().getClass().getName() + ":")); newVector.addAll(Collections.list(((OptionHandler) getMetaClassifier()).listOptions())); } return newVector.elements(); }
/** * Returns class probabilities for all given instances if the class is nominal or corresponding predicted * numeric values if the class is numeric. The meta classifier must implement BatchPredictor, otherwise an * exception will be thrown. * * @param instances the instance sto be classified * @return the distributions * @throws Exception if instances could not be classified successfully */ public double[][] distributionsForInstances(Instances instances) throws Exception { Instances data; if (!baseClassifiersImplementMoreEfficientBatchPrediction()) { data = new Instances(m_MetaFormat, 0); for (Instance inst : instances) { data.add(metaInstance(inst)); } } else { data = metaInstances(instances); } return ((BatchPredictor)m_MetaClassifier).distributionsForInstances(data); }
/** * Returns true if the meta classifier or any of the base classifiers are able to generate batch predictions * efficiently and all of them implement BatchPredictor. * * @return true if batch prediction can be done efficiently */ public boolean implementsMoreEfficientBatchPrediction() { if (!(getMetaClassifier() instanceof BatchPredictor)) { return super.implementsMoreEfficientBatchPrediction(); } boolean atLeastOneIsEfficient = false; for (Classifier c : getClassifiers()) { if (!(c instanceof BatchPredictor)) { return super.implementsMoreEfficientBatchPrediction(); } atLeastOneIsEfficient |= ((BatchPredictor) c).implementsMoreEfficientBatchPrediction(); } return atLeastOneIsEfficient || ((BatchPredictor) getMetaClassifier()).implementsMoreEfficientBatchPrediction(); }
@Override public void postExecution() throws Exception { super.postExecution(); if (getMetaClassifier() instanceof CommandlineRunnable) { ((CommandlineRunnable) getMetaClassifier()).postExecution(); } }
/** * Returns combined capabilities of the base classifiers, i.e., the * capabilities all of them have in common. * * @return the capabilities of the base classifiers */ public Capabilities getCapabilities() { Capabilities result; result = super.getCapabilities(); result.setMinimumNumberInstances(getNumFolds()); return result; }
/** * Returns a string describing classifier * * @return a description suitable for * displaying in the explorer/experimenter gui */ public String globalInfo() { return "Combines several classifiers using the stacking method. Can do classification or regression.\n\n" + "For more information, see\n\n" + getTechnicalInformation().toString(); }
/** * Returns estimated class probabilities for the given instance if the class is nominal and a * one-element array containing the numeric prediction if the class is numeric. * * @param instance the instance to be classified * @return the distribution * @throws Exception if instance could not be classified successfully */ public double[] distributionForInstance(Instance instance) throws Exception { return m_MetaClassifier.distributionForInstance(metaInstance(instance)); }
Stacking nb=new Stacking(); SMO smo=new SMO(); Classifier[] stackoptions = new Classifier[] {smo}; nb.setClassifiers(stackoptions);
getCapabilities().testWithFail(data); generateMetaLevel(newData, random); buildClassifiers(newData);
/** Creates a default Stacking */ public Classifier getClassifier() { return new Stacking(); }
/** * Returns an enumeration describing the available options. * * @return an enumeration of all the available options. */ public Enumeration<Option> listOptions() { Vector<Option> newVector = new Vector<Option>(2); newVector.addElement(new Option( metaOption(), "M", 0, "-M <scheme specification>")); newVector.addElement(new Option( "\tSets the number of cross-validation folds.", "X", 1, "-X <number of folds>")); newVector.addAll(Collections.list(super.listOptions())); if (getMetaClassifier() instanceof OptionHandler) { newVector.addElement(new Option( "", "", 0, "\nOptions specific to meta classifier " + getMetaClassifier().getClass().getName() + ":")); newVector.addAll(Collections.list(((OptionHandler)getMetaClassifier()).listOptions())); } return newVector.elements(); }
@Override public void preExecution() throws Exception { super.preExecution(); if (getMetaClassifier() instanceof CommandlineRunnable) { ((CommandlineRunnable) getMetaClassifier()).preExecution(); } }