/** * 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; }
/** * 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 { ArrayList<Attribute> attributes = new ArrayList<Attribute>(); Instances metaFormat; for (int k = 0; k < m_Classifiers.length; k++) { Classifier classifier = (Classifier) getClassifier(k); String name = classifier.getClass().getName() + "-" + (k+1); if (m_BaseFormat.classAttribute().isNumeric()) { attributes.add(new Attribute(name)); } else { for (int j = 0; j < m_BaseFormat.classAttribute().numValues(); j++) { attributes.add( new Attribute( name + ":" + m_BaseFormat.classAttribute().value(j))); } } } attributes.add((Attribute) m_BaseFormat.classAttribute().copy()); metaFormat = new Instances("Meta format", attributes, 0); metaFormat.setClassIndex(metaFormat.numAttributes() - 1); return metaFormat; }
/** * Determines the format of 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 { ArrayList<Attribute> attributes = new ArrayList<Attribute>(); Instances metaFormat; for (int k = 0; k < m_Classifiers.length; k++) { Classifier classifier = getClassifier(k); String name = classifier.getClass().getName() + "-" + (k + 1); if (m_BaseFormat.classAttribute().isNumeric()) { attributes.add(new Attribute(name)); } else { for (int j = 0; j < m_BaseFormat.classAttribute().numValues(); j++) { attributes.add(new Attribute(name + ":" + m_BaseFormat.classAttribute().value(j))); } } } attributes.add((Attribute) m_BaseFormat.classAttribute().copy()); metaFormat = new Instances("Meta format", attributes, 0); metaFormat.setClassIndex(metaFormat.numAttributes() - 1); return metaFormat; }
/** * Makes a set of level-1 instances from the given instances. More efficient if at least one base classifier * implements efficient batch prediction. Requires all base classifiers to implement BatchPredictor. * * @param instances the instances to be transformed * @return the level-1 instances * @throws Exception if the instance generation fails */ protected Instances metaInstances(Instances instances) throws Exception { double[][][] predictions = new double[m_Classifiers.length][][]; for (int k = 0; k < m_Classifiers.length; k++) { predictions[k] = ((BatchPredictor) getClassifier(k)).distributionsForInstances(instances); } Instances metaData = new Instances(m_MetaFormat, 0); for (int l = 0; l < instances.numInstances(); l++) { double[] values = new double[m_MetaFormat.numAttributes()]; int i = 0; for (int k = 0; k < m_Classifiers.length; k++) { if (m_BaseFormat.classAttribute().isNumeric()) { values[i++] = predictions[k][l][0]; } else { System.arraycopy(predictions[k][l], 0, values, i, predictions[k][l].length); i += predictions[k][l].length; } } values[i] = instances.instance(l).classValue(); metaData.add(new DenseInstance(1, values)); } return metaData; }
/** * 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 DenseInstance(1, values); metaInstance.setDataset(m_MetaFormat); return metaInstance; }
/** * 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 DenseInstance(1, values); metaInstance.setDataset(m_MetaFormat); return metaInstance; }