⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 stacking.java

📁 wekaUT是 university texas austin 开发的基于weka的半指导学习(semi supervised learning)的分类器
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
    m_BaseClassifiers = classifiers;  }  /**   * Gets the list of possible classifers to choose from.   *   * @return the array of Classifiers   */  public Classifier [] getBaseClassifiers() {    return m_BaseClassifiers;  }  /**   * Gets the specific classifier from the set of base classifiers.   *   * @param index the index of the classifier to retrieve   * @return the classifier   */  public Classifier getBaseClassifier(int index) {        return m_BaseClassifiers[index];  }  /**   * 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;  }  /**   * 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.   * @exception Exception if the classifier could not be built successfully   */  public void buildClassifier(Instances data) throws Exception {    if (m_BaseClassifiers.length == 0) {      throw new Exception("No base classifiers have been set");    }    if (m_MetaClassifier == null) {      throw new Exception("No meta classifier has been set");    }    if (!(data.classAttribute().isNominal() ||	  data.classAttribute().isNumeric())) {      throw new Exception("Class attribute has to be nominal or numeric!");    }    Instances newData = new Instances(data);    m_BaseFormat = new Instances(data, 0);    newData.deleteWithMissingClass();    if (newData.numInstances() == 0) {      throw new Exception("No training instances without missing class!");    }    newData.randomize(new Random(m_Seed));    if (newData.classAttribute().isNominal())      newData.stratify(m_NumFolds);    int numClassifiers = m_BaseClassifiers.length;    // Create meta data    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);      // Build base classifiers      for (int i = 0; i < m_BaseClassifiers.length; i++) {	getBaseClassifier(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)));      }    }    // Rebuilt all the base classifiers on the full training data    for (int i = 0; i < numClassifiers; i++) {      getBaseClassifier(i).buildClassifier(newData);    }       // Build meta classifier    m_MetaClassifier.buildClassifier(metaData);  }  /**   * Classifies a given instance using the stacked classifier.   *   * @param instance the instance to be classified   * @exception Exception if instance could not be classified   * successfully   */  public double classifyInstance(Instance instance) throws Exception {    return m_MetaClassifier.classifyInstance(metaInstance(instance));  }  /**   * Output a representation of this classifier   */  public String toString() {    if (m_BaseClassifiers.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_BaseClassifiers.length; i++) {      result += getBaseClassifier(i).toString() +"\n\n";    }       result += "\n\nMeta classifier\n\n";    result += m_MetaClassifier.toString();    return result;  }  /**   * 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) {    try {      System.out.println(Evaluation.evaluateModel(new Stacking(), argv));    } catch (Exception e) {      System.err.println(e.getMessage());    }  }  /**   * Makes the format for the level-1 data.   *   * @param instances the level-0 format   * @return the format for the meta data   */  protected Instances metaFormat(Instances instances) throws Exception {    FastVector attributes = new FastVector();    Instances metaFormat;    Attribute attribute;    int i = 0;    for (int k = 0; k < m_BaseClassifiers.length; k++) {      Classifier classifier = (Classifier) getBaseClassifier(k);      String name = classifier.getClass().getName();      if (m_BaseFormat.classAttribute().isNumeric()) {	attributes.addElement(new Attribute(name));      } else {	if (classifier instanceof DistributionClassifier) {	  for (int j = 0; j < m_BaseFormat.classAttribute().numValues(); j++) {	    attributes.addElement(new Attribute(name + ":" + 						m_BaseFormat						.classAttribute().value(j)));	  }	} else {	  FastVector values = new FastVector();	  for (int j = 0; j < m_BaseFormat.classAttribute().numValues(); j++) {	    values.addElement(m_BaseFormat.classAttribute().value(j));	  }	  attributes.addElement(new Attribute(name, values));	}      }    }    attributes.addElement(m_BaseFormat.classAttribute());    metaFormat = new Instances("Meta format", attributes, 0);    metaFormat.setClassIndex(metaFormat.numAttributes() - 1);    return metaFormat;  }  /**   * 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 getBaseClassifierSpec(int index) {        if (m_BaseClassifiers.length < index) {      return "";    }    return getClassifierSpec(getBaseClassifier(index));  }  /**   * Gets the classifier specification string, which contains the class name of   * the classifier and any options to the classifier   *   * @param c the classifier   * @return the classifier specification string.   */  protected String getClassifierSpec(Classifier c) {        if (c instanceof OptionHandler) {      return c.getClass().getName() + " "	+ Utils.joinOptions(((OptionHandler)c).getOptions());    }    return c.getClass().getName();  }  /**   * Makes a level-1 instance from the given instance.   *    * @param instance the instance to be transformed   * @return the level-1 instance   */  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_BaseClassifiers.length; k++) {      Classifier classifier = getBaseClassifier(k);      if (m_BaseFormat.classAttribute().isNumeric()) {	values[i++] = classifier.classifyInstance(instance);      } else {	if (classifier instanceof DistributionClassifier) {	  double[] dist = ((DistributionClassifier)classifier).	    distributionForInstance(instance);	  for (int j = 0; j < dist.length; j++) {	    values[i++] = dist[j];	  }	} else {	  values[i++] = classifier.classifyInstance(instance);	}      }    }    values[i] = instance.classValue();    metaInstance = new Instance(1, values);    metaInstance.setDataset(m_MetaFormat);    return metaInstance;  }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -