📄 stacking.java
字号:
System.arraycopy(superOptions, 0, options, current, superOptions.length); return options; } /** * Returns the tip text for this property * @return tip text for this property suitable for * displaying in the explorer/experimenter gui */ public String numFoldsTipText() { return "The number of folds used for cross-validation."; } /** * Gets the number of folds for the cross-validation. * * @return the number of folds for the cross-validation */ public int getNumFolds() { return m_NumFolds; } /** * Sets the number of folds for the cross-validation. * * @param numFolds the number of folds for the cross-validation * @throws Exception if parameter illegal */ public void setNumFolds(int numFolds) throws Exception { if (numFolds < 0) { throw new IllegalArgumentException("Stacking: Number of cross-validation " + "folds must be positive."); } m_NumFolds = numFolds; } /** * Returns the tip text for this property * @return tip text for this property suitable for * displaying in the explorer/experimenter gui */ public String metaClassifierTipText() { return "The meta classifiers to be used."; } /** * Adds meta classifier * * @param classifier the classifier with all options set. */ public void setMetaClassifier(Classifier classifier) { m_MetaClassifier = classifier; } /** * Gets the meta classifier. * * @return the meta classifier */ public Classifier getMetaClassifier() { return m_MetaClassifier; } /** * 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; } /** * Buildclassifier selects a classifier from the set of classifiers * by minimising error on the training data. * * @param data the training data to be used for generating the * boosted classifier. * @throws Exception if the classifier could not be built successfully */ public void buildClassifier(Instances data) throws Exception { if (m_MetaClassifier == null) { throw new IllegalArgumentException("No meta classifier has been set"); } // can classifier handle the data? getCapabilities().testWithFail(data); // remove instances with missing class Instances newData = new Instances(data); m_BaseFormat = new Instances(data, 0); newData.deleteWithMissingClass(); Random random = new Random(m_Seed); newData.randomize(random); if (newData.classAttribute().isNominal()) { newData.stratify(m_NumFolds); } // Create meta level generateMetaLevel(newData, random); // Rebuilt all the base classifiers on the full training data for (int i = 0; i < m_Classifiers.length; i++) { getClassifier(i).buildClassifier(newData); } } /** * Generates the meta data * * @param newData the data to work on * @param random the random number generator to use for cross-validation * @throws Exception if generation fails */ protected void generateMetaLevel(Instances newData, Random random) throws Exception { Instances metaData = metaFormat(newData); m_MetaFormat = new Instances(metaData, 0); for (int j = 0; j < m_NumFolds; j++) { Instances train = newData.trainCV(m_NumFolds, j, random); // Build base classifiers for (int i = 0; i < m_Classifiers.length; i++) { getClassifier(i).buildClassifier(train); } // Classify test instances and add to meta data Instances test = newData.testCV(m_NumFolds, j); for (int i = 0; i < test.numInstances(); i++) { metaData.add(metaInstance(test.instance(i))); } } m_MetaClassifier.buildClassifier(metaData); } /** * Returns class probabilities. * * @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)); } /** * 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; } /** * Makes the format for the level-1 data. * * @param instances the level-0 format * @return the format for the meta data * @throws Exception if the format generation fails */ protected Instances metaFormat(Instances instances) throws Exception { FastVector attributes = new FastVector(); Instances metaFormat; for (int k = 0; k < m_Classifiers.length; k++) { Classifier classifier = (Classifier) getClassifier(k); String name = classifier.getClass().getName(); if (m_BaseFormat.classAttribute().isNumeric()) { attributes.addElement(new Attribute(name)); } else { for (int j = 0; j < m_BaseFormat.classAttribute().numValues(); j++) { attributes.addElement(new Attribute(name + ":" + m_BaseFormat .classAttribute().value(j))); } } } attributes.addElement(m_BaseFormat.classAttribute().copy()); metaFormat = new Instances("Meta format", attributes, 0); metaFormat.setClassIndex(metaFormat.numAttributes() - 1); return metaFormat; } /** * Makes a level-1 instance from the given instance. * * @param instance the instance to be transformed * @return the level-1 instance * @throws Exception if the instance generation fails */ protected Instance metaInstance(Instance instance) throws Exception { double[] values = new double[m_MetaFormat.numAttributes()]; Instance metaInstance; int i = 0; for (int k = 0; k < m_Classifiers.length; k++) { Classifier classifier = getClassifier(k); if (m_BaseFormat.classAttribute().isNumeric()) { values[i++] = classifier.classifyInstance(instance); } else { double[] dist = classifier.distributionForInstance(instance); for (int j = 0; j < dist.length; j++) { values[i++] = dist[j]; } } } values[i] = instance.classValue(); metaInstance = new Instance(1, values); metaInstance.setDataset(m_MetaFormat); return metaInstance; } /** * 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); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -