All Downloads are FREE. Search and download functionalities are using the official Maven repository.

weka.classifiers.MultipleClassifiersCombiner Maven / Gradle / Ivy

Go to download

The Waikato Environment for Knowledge Analysis (WEKA), a machine learning workbench. This version represents the developer version, the "bleeding edge" of development, you could say. New functionality gets added to this version.

There is a newer version: 3.9.6
Show newest version
/*
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see .
 */

/*
 *    MultipleClassifiersCombiner.java
 *    Copyright (C) 2004-2012 University of Waikato, Hamilton, New Zealand
 *
 */

package weka.classifiers;

import java.util.Collections;
import java.util.Enumeration;
import java.util.Vector;

import weka.core.Capabilities;
import weka.core.Capabilities.Capability;
import weka.core.Option;
import weka.core.OptionHandler;
import weka.core.Utils;

/**
 * Abstract utility class for handling settings common to
 * meta classifiers that build an ensemble from multiple classifiers.
 *
 * @author Eibe Frank ([email protected])
 * @version $Revision: 10141 $
 */
public abstract class MultipleClassifiersCombiner extends AbstractClassifier {

  /** for serialization */
  private static final long serialVersionUID = 2776436621129422119L;

  /** Array for storing the generated base classifiers. */
  protected Classifier[] m_Classifiers = {
    new weka.classifiers.rules.ZeroR()
  };

  /**
   * Returns an enumeration describing the available options
   *
   * @return an enumeration of all the available options
   */
  public Enumeration

* * @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 { // Iterate through the schemes Vector classifiers = new Vector(); while (true) { String classifierString = Utils.getOption('B', options); if (classifierString.length() == 0) { break; } String [] classifierSpec = Utils.splitOptions(classifierString); if (classifierSpec.length == 0) { throw new IllegalArgumentException("Invalid classifier specification string"); } String classifierName = classifierSpec[0]; classifierSpec[0] = ""; classifiers.addElement(AbstractClassifier.forName(classifierName, classifierSpec)); } if (classifiers.size() == 0) { classifiers.addElement(new weka.classifiers.rules.ZeroR()); } Classifier [] classifiersArray = new Classifier [classifiers.size()]; for (int i = 0; i < classifiersArray.length; i++) { classifiersArray[i] = (Classifier) classifiers.elementAt(i); } setClassifiers(classifiersArray); super.setOptions(options); } /** * Gets the current settings of the Classifier. * * @return an array of strings suitable for passing to setOptions */ public String [] getOptions() { Vector options = new Vector(); for (int i = 0; i < m_Classifiers.length; i++) { options.add("-B"); options.add("" + getClassifierSpec(i)); } Collections.addAll(options, super.getOptions()); return options.toArray(new String[0]); } /** * Returns the tip text for this property * @return tip text for this property suitable for * displaying in the explorer/experimenter gui */ public String classifiersTipText() { return "The base classifiers to be used."; } /** * Sets the list of possible classifers to choose from. * * @param classifiers an array of classifiers with all options set. */ public void setClassifiers(Classifier [] classifiers) { m_Classifiers = classifiers; } /** * Gets the list of possible classifers to choose from. * * @return the array of Classifiers */ public Classifier [] getClassifiers() { return m_Classifiers; } /** * Gets a single classifier from the set of available classifiers. * * @param index the index of the classifier wanted * @return the Classifier */ public Classifier getClassifier(int index) { return m_Classifiers[index]; } /** * Gets the classifier specification string, which contains the class name of * the classifier and any options to the classifier * * @param index the index of the classifier string to retrieve, starting from * 0. * @return the classifier string, or the empty string if no classifier * has been assigned (or the index given is out of range). */ protected String getClassifierSpec(int index) { if (m_Classifiers.length < index) { return ""; } Classifier c = getClassifier(index); return c.getClass().getName() + " " + Utils.joinOptions(((OptionHandler)c).getOptions()); } /** * 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; int i; if (getClassifiers().length == 0) { result = new Capabilities(this); result.disableAll(); } else { result = (Capabilities) getClassifier(0).getCapabilities().clone(); for (i = 1; i < getClassifiers().length; i++) result.and(getClassifier(i).getCapabilities()); } // set dependencies for (Capability cap: Capability.values()) result.enableDependency(cap); result.setOwner(this); return result; } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy