📄 multiclassclassifier.java
字号:
.distributionForInstance(inst); } double[] probs = new double[inst.numClasses()]; for(int i = 0; i < m_ClassFilters.length; i++) { if (m_Classifiers[i] != null) { m_ClassFilters[i].input(inst); m_ClassFilters[i].batchFinished(); double [] current = ((DistributionClassifier)m_Classifiers[i]) .distributionForInstance(m_ClassFilters[i].output()); for (int j = 0; j < m_ClassAttribute.numValues(); j++) { if (m_ClassFilters[i].getValueRange().isInRange(j)) { probs[j] += current[1]; } else { probs[j] += current[0]; } } } } if (Utils.gr(Utils.sum(probs), 0)) { Utils.normalize(probs); return probs; } else { return m_ZeroR.distributionForInstance(inst); } } /** * Prints the classifiers. */ public String toString() { if (m_Classifiers == null) { return "MultiClassClassifier: No model built yet."; } StringBuffer text = new StringBuffer(); text.append("MultiClassClassifier\n\n"); for (int i = 0; i < m_Classifiers.length; i++) { text.append("Classifier ").append(i + 1); if (m_Classifiers[i] != null) { if ((m_ClassFilters != null) && (m_ClassFilters[i] != null)) { text.append(", using indicator values: "); text.append(m_ClassFilters[i].getValueRange()); } text.append('\n'); text.append(m_Classifiers[i].toString() + "\n"); } else { text.append(" Skipped (no training examples)\n"); } } return text.toString(); } /** * Returns an enumeration describing the available options * * @return an enumeration of all the available options */ public Enumeration listOptions() { Vector vec = new Vector(3); Object c; vec.addElement(new Option( "\tSets the error-correction mode. Valid values are 0 (no correction),\n" +"\t1 (random codes), and 2 (exhaustive code). (default 0)\n", "E", 1, "-E <num>")); vec.addElement(new Option( "\tSets the multiplier when using random codes. (default 2.0)", "R", 1, "-R <num>")); vec.addElement(new Option( "\tSets the base classifier.", "W", 1, "-W <base classifier>")); if (m_Classifier != null) { try { vec.addElement(new Option("", "", 0, "\nOptions specific to classifier " + m_Classifier.getClass().getName() + ":")); Enumeration enum = ((OptionHandler)m_Classifier).listOptions(); while (enum.hasMoreElements()) { vec.addElement(enum.nextElement()); } } catch (Exception e) { } } return vec.elements(); } /** * Parses a given list of options. Valid options are:<p> * * -E num <br> * Sets the error-correction mode. Valid values are 0 (no correction), * 1 (random codes), and 2 (exhaustive code). (default 0) <p> * * -R num <br> * Sets the multiplier when using random codes. (default 2.0)<p> * * -W classname <br> * Specify the full class name of a learner as the basis for * the multiclassclassifier (required).<p> * * @param options the list of options as an array of strings * @exception Exception if an option is not supported */ public void setOptions(String[] options) throws Exception { String errorString = Utils.getOption('E', options); if (errorString.length() != 0) { setErrorCorrectionMode(new SelectedTag(Integer.parseInt(errorString), TAGS_ERROR)); } else { setErrorCorrectionMode(new SelectedTag(ERROR_NONE, TAGS_ERROR)); } String rfactorString = Utils.getOption('R', options); if (rfactorString.length() != 0) { setRandomWidthFactor((new Double(rfactorString)).doubleValue()); } else { setRandomWidthFactor(2.0); } String classifierName = Utils.getOption('W', options); if (classifierName.length() == 0) { throw new Exception("A classifier must be specified with" + " the -W option."); } setDistributionClassifier((DistributionClassifier) Classifier.forName(classifierName, Utils.partitionOptions(options))); } /** * Gets the current settings of the Classifier. * * @return an array of strings suitable for passing to setOptions */ public String [] getOptions() { String [] classifierOptions = new String [0]; if ((m_Classifier != null) && (m_Classifier instanceof OptionHandler)) { classifierOptions = ((OptionHandler)m_Classifier).getOptions(); } String [] options = new String [classifierOptions.length + 7]; int current = 0; options[current++] = "-E"; options[current++] = "" + m_ErrorMode; options[current++] = "-R"; options[current++] = "" + m_RandomWidthFactor; if (getDistributionClassifier() != null) { options[current++] = "-W"; options[current++] = getDistributionClassifier().getClass().getName(); } options[current++] = "--"; System.arraycopy(classifierOptions, 0, options, current, classifierOptions.length); current += classifierOptions.length; while (current < options.length) { options[current++] = ""; } return options; } /** * @return a description of the classifier suitable for * displaying in the explorer/experimenter gui */ public String globalInfo() { return "A metaclassifier for handling multi-class datasets with 2-class " + "distribution classifiers. This classifier is also capable of " + "applying error correcting output codes for increased accuracy."; } /** * @return tip text for this property suitable for * displaying in the explorer/experimenter gui */ public String randomWidthFactorTipText() { return "Sets the width multiplier when using random codes. The number " + "of codes generated will be thus number multiplied by the number of " + "classes."; } /** * Gets the multiplier when generating random codes. Will generate * numClasses * m_RandomWidthFactor codes. * * @return the width multiplier */ public double getRandomWidthFactor() { return m_RandomWidthFactor; } /** * Sets the multiplier when generating random codes. Will generate * numClasses * m_RandomWidthFactor codes. * * @param newRandomWidthFactor the new width multiplier */ public void setRandomWidthFactor(double newRandomWidthFactor) { m_RandomWidthFactor = newRandomWidthFactor; } /** * @return tip text for this property suitable for * displaying in the explorer/experimenter gui */ public String errorCorrectionModeTipText() { return "Sets whether error correction will be used. The default method " + "is no error correction: one classifier will be built per class value. " + "Increased accuracy can be obtained by using error correcting output " + "codes. \"Random\" generates random output codes (the number of " + "which is determined by the number of classes and the width " + "multiplier). \"Exhaustive\" generates one classifier for each " + "(non-redundant) combination of class values - beware that this " + "increases exponentially in the number of class values. We have yet " + "to implement BCH codes (feel free to do so)."; } /** * Gets the error correction mode used. Will be one of * ERROR_NONE, ERROR_RANDOM, or ERROR_EXHAUSTIVE. * * @return the current error correction mode. */ public SelectedTag getErrorCorrectionMode() { return new SelectedTag(m_ErrorMode, TAGS_ERROR); } /** * Sets the error correction mode used. Will be one of * ERROR_NONE, ERROR_RANDOM, or ERROR_EXHAUSTIVE. * * @param newMethod the new error correction mode. */ public void setErrorCorrectionMode(SelectedTag newMethod) { if (newMethod.getTags() == TAGS_ERROR) { m_ErrorMode = newMethod.getSelectedTag().getID(); } } /** * @return tip text for this property suitable for * displaying in the explorer/experimenter gui */ public String distributionClassifierTipText() { return "Sets the DistributionClassifier used as the basis for " + "the multi-class classifier."; } /** * Set the base classifier. * * @param newClassifier the Classifier to use. */ public void setDistributionClassifier(DistributionClassifier newClassifier) { m_Classifier = newClassifier; } /** * Get the classifier used as the classifier * * @return the classifier used as the classifier */ public DistributionClassifier getDistributionClassifier() { return m_Classifier; } /** * Main method for testing this class. * * @param argv the options */ public static void main(String [] argv) { DistributionClassifier scheme; try { scheme = new MultiClassClassifier(); System.out.println(Evaluation.evaluateModel(scheme, argv)); } catch (Exception e) { System.err.println(e.getMessage()); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -