/** Creates a default Bagging */ public Classifier getClassifier() { return new Bagging(); }
/** * This method only accepts RandomTree arguments. * * @param newClassifier the RandomTree to use. * @exception if argument is not a RandomTree */ @Override @ProgrammaticProperty public void setClassifier(Classifier newClassifier) { if (!(newClassifier instanceof RandomTree)) { throw new IllegalArgumentException( "RandomForest: Argument of setClassifier() must be a RandomTree."); } super.setClassifier(newClassifier); }
/** * Builds the classifier to generate a partition. */ @Override public void generatePartition(Instances data) throws Exception { if (m_Classifier instanceof PartitionGenerator) buildClassifier(data); else throw new Exception("Classifier: " + getClassifierSpec() + " cannot generate a partition"); }
/** * Main method for testing this class. * * @param argv the options */ public static void main(String [] argv) { runClassifier(new Bagging(), argv); }
/** * Returns description of the bagged classifier. * * @return description of the bagged classifier as a string */ @Override public String toString() { if (m_Classifiers == null) { return "Bagging: No model built yet."; } StringBuffer text = new StringBuffer(); text.append("Bagging with " + getNumIterations() + " iterations and base learner\n\n" + getClassifierSpec()); if (getPrintClassifiers()) { text.append("All the base classifiers: \n\n"); for (int i = 0; i < m_Classifiers.length; i++) text.append(m_Classifiers[i].toString() + "\n\n"); } if (m_CalcOutOfBag) { text.append(m_OutOfBagEvaluationObject.toSummaryString("\n\n*** Out-of-bag estimates ***\n", getOutputOutOfBagComplexityStatistics())); } return text.toString(); }
/** * Gets the current settings of the Classifier. * * @return an array of strings suitable for passing to setOptions */ @Override public String [] getOptions() { Vector<String> options = new Vector<String>(); options.add("-P"); options.add("" + getBagSizePercent()); if (getCalcOutOfBag()) { options.add("-O"); } if (getStoreOutOfBagPredictions()) { options.add("-store-out-of-bag-predictions"); } if (getOutputOutOfBagComplexityStatistics()) { options.add("-output-out-of-bag-complexity-statistics"); } if (getRepresentCopiesUsingWeights()) { options.add("-represent-copies-using-weights"); } if (getPrintClassifiers()) { options.add("-print"); } Collections.addAll(options, super.getOptions()); return options.toArray(new String[0]); }
getCapabilities().testWithFail(data); if (getRepresentCopiesUsingWeights() && !(m_Classifier instanceof WeightedInstancesHandler)) { throw new IllegalArgumentException("Cannot represent copies using weights when " + "base learner in bagging does not implement " + buildClassifiers(); if (getCalcOutOfBag()) { m_OutOfBagEvaluationObject = new Evaluation(m_data); if (voteCount > 0) { votes[0] /= voteCount; m_OutOfBagEvaluationObject.evaluationForSingleInstance(votes, m_data.instance(i), getStoreOutOfBagPredictions()); if (sum > 0) { Utils.normalize(votes, sum); m_OutOfBagEvaluationObject.evaluationForSingleInstance(votes, m_data.instance(i), getStoreOutOfBagPredictions());
learner[1] = new CalibratedLabelRanking(new J48()); learnerName[1] = "CLR"; Bagging bagging = new Bagging(); bagging.setClassifier(new J48()); learner[2] = new BinaryRelevance(bagging); learnerName[2] = "BR";
/** * Constructor that sets base classifier for bagging to RandomTre and default * number of iterations to 100. */ public RandomForest() { RandomTree rTree = new RandomTree(); rTree.setDoNotCheckCapabilities(true); super.setClassifier(rTree); super.setRepresentCopiesUsingWeights(true); setNumIterations(defaultNumberOfIterations()); }
/** * Returns the number of elements in the partition. */ @Override public int numElements() throws Exception { if (m_Classifier instanceof PartitionGenerator) { int size = 0; for (int i = 0; i < m_Classifiers.length; i++) { size += ((PartitionGenerator)m_Classifiers[i]).numElements(); } return size; } else throw new Exception("Classifier: " + getClassifierSpec() + " cannot generate a partition"); }
/** * Gets the current settings of the Classifier. * * @return an array of strings suitable for passing to setOptions */ @Override public String [] getOptions() { Vector<String> options = new Vector<String>(); options.add("-P"); options.add("" + getBagSizePercent()); if (getCalcOutOfBag()) { options.add("-O"); } if (getStoreOutOfBagPredictions()) { options.add("-store-out-of-bag-predictions"); } if (getOutputOutOfBagComplexityStatistics()) { options.add("-output-out-of-bag-complexity-statistics"); } if (getRepresentCopiesUsingWeights()) { options.add("-represent-copies-using-weights"); } if (getPrintClassifiers()) { options.add("-print"); } Collections.addAll(options, super.getOptions()); return options.toArray(new String[0]); }
getCapabilities().testWithFail(data); if (getRepresentCopiesUsingWeights() && !(m_Classifier instanceof WeightedInstancesHandler)) { throw new IllegalArgumentException("Cannot represent copies using weights when " + "base learner in bagging does not implement " + buildClassifiers(); if (getCalcOutOfBag()) { m_OutOfBagEvaluationObject = new Evaluation(m_data); if (voteCount > 0) { votes[0] /= voteCount; m_OutOfBagEvaluationObject.evaluationForSingleInstance(votes, m_data.instance(i), getStoreOutOfBagPredictions()); if (sum > 0) { Utils.normalize(votes, sum); m_OutOfBagEvaluationObject.evaluationForSingleInstance(votes, m_data.instance(i), getStoreOutOfBagPredictions());
/** * Main method for testing this class. * * @param argv the options */ public static void main(String [] argv) { runClassifier(new Bagging(), argv); }
/** * Returns description of the bagged classifier. * * @return description of the bagged classifier as a string */ @Override public String toString() { if (m_Classifiers == null) { return "Bagging: No model built yet."; } StringBuffer text = new StringBuffer(); text.append("Bagging with " + getNumIterations() + " iterations and base learner\n\n" + getClassifierSpec()); if (getPrintClassifiers()) { text.append("All the base classifiers: \n\n"); for (int i = 0; i < m_Classifiers.length; i++) text.append(m_Classifiers[i].toString() + "\n\n"); } if (m_CalcOutOfBag) { text.append(m_OutOfBagEvaluationObject.toSummaryString("\n\n*** Out-of-bag estimates ***\n", getOutputOutOfBagComplexityStatistics())); } return text.toString(); }
/** * Constructor that sets base classifier for bagging to RandomTre and default * number of iterations to 100. */ public RandomForest() { RandomTree rTree = new RandomTree(); rTree.setDoNotCheckCapabilities(true); super.setClassifier(rTree); super.setRepresentCopiesUsingWeights(true); setNumIterations(defaultNumberOfIterations()); }
/** * Returns the number of elements in the partition. */ @Override public int numElements() throws Exception { if (m_Classifier instanceof PartitionGenerator) { int size = 0; for (int i = 0; i < m_Classifiers.length; i++) { size += ((PartitionGenerator)m_Classifiers[i]).numElements(); } return size; } else throw new Exception("Classifier: " + getClassifierSpec() + " cannot generate a partition"); }
/** Creates a default Bagging */ public Classifier getClassifier() { return new Bagging(); }
/** * Builds the classifier to generate a partition. */ @Override public void generatePartition(Instances data) throws Exception { if (m_Classifier instanceof PartitionGenerator) buildClassifier(data); else throw new Exception("Classifier: " + getClassifierSpec() + " cannot generate a partition"); }
/** * This method only accepts RandomTree arguments. * * @param newClassifier the RandomTree to use. * @exception if argument is not a RandomTree */ @Override @ProgrammaticProperty public void setClassifier(Classifier newClassifier) { if (!(newClassifier instanceof RandomTree)) { throw new IllegalArgumentException( "RandomForest: Argument of setClassifier() must be a RandomTree."); } super.setClassifier(newClassifier); }
/** * Computes an array that indicates leaf membership */ @Override public double[] getMembershipValues(Instance inst) throws Exception { if (m_Classifier instanceof PartitionGenerator) { ArrayList<double[]> al = new ArrayList<double[]>(); int size = 0; for (int i = 0; i < m_Classifiers.length; i++) { double[] r = ((PartitionGenerator)m_Classifiers[i]). getMembershipValues(inst); size += r.length; al.add(r); } double[] values = new double[size]; int pos = 0; for (double[] v: al) { System.arraycopy(v, 0, values, pos, v.length); pos += v.length; } return values; } else throw new Exception("Classifier: " + getClassifierSpec() + " cannot generate a partition"); }