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

📄 classorder.java

📁 代码是一个分类器的实现,其中使用了部分weka的源代码。可以将项目导入eclipse运行
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
   *   * @param order the class order   */  public void setClassOrder(int order){    m_ClassOrder = order;  }  /**    * Returns the Capabilities of this filter.   *   * @return            the capabilities of this object   * @see               Capabilities   */  public Capabilities getCapabilities() {    Capabilities result = super.getCapabilities();    // attributes    result.enableAllAttributes();    result.enable(Capability.MISSING_VALUES);        // class    result.enable(Capability.NOMINAL_CLASS);        return result;  }      /**   * Sets the format of the input instances.   *   * @param instanceInfo an Instances object containing the input instance   * structure (any instances contained in the object are ignored - only the   * structure is required).   * @return true if the outputFormat may be collected immediately   * @throws Exception if no class index set or class not nominal   */  public boolean setInputFormat(Instances instanceInfo) throws Exception {         super.setInputFormat(new Instances(instanceInfo, 0));	    m_ClassAttribute = instanceInfo.classAttribute();	    m_Random = new Random(m_Seed);    m_Converter = null;        int numClasses = instanceInfo.numClasses();    m_ClassCounts = new double[numClasses];	    return false;  }          /**   * Input an instance for filtering. Ordinarily the instance is processed   * and made available for output immediately. Some filters require all   * instances be read before producing output.   *   * @param instance the input instance   * @return true if the filtered instance may now be   * collected with output().   * @throws IllegalStateException if no input format has been defined.   */  public boolean input(Instance instance) {	    if (getInputFormat() == null) {      throw new IllegalStateException("No input instance format defined");    }    if (m_NewBatch) {      resetQueue();      m_NewBatch = false;         }	        // In case some one use this routine in testing,     // although he/she should not do so    if(m_Converter != null){      Instance datum = (Instance)instance.copy();      if (!datum.isMissing(m_ClassAttribute)){	datum.setClassValue((double)m_Converter[(int)datum.classValue()]);      }      push(datum);      return true;    }        if (!instance.isMissing(m_ClassAttribute)) {      m_ClassCounts[(int)instance.classValue()] += instance.weight();    }    bufferInput(instance);    return false;  }    /**   * Signify that this batch of input to the filter is finished. If   * the filter requires all instances prior to filtering, output()   * may now be called to retrieve the filtered instances. Any   * subsequent instances filtered should be filtered based on setting   * obtained from the first batch (unless the inputFormat has been   * re-assigned or new options have been set). This implementation    * sorts the class values and provide class counts in the output format   *   * @return true if there are instances pending output   * @throws IllegalStateException if no input structure has been defined,   * @throws Exception if there was a problem finishing the batch.   */  public boolean batchFinished() throws Exception {    Instances data = getInputFormat();    if (data == null)      throw new IllegalStateException("No input instance format defined");    if (m_Converter == null) {      // Get randomized indices and class counts       int[] randomIndices = new int[m_ClassCounts.length];      for (int i = 0; i < randomIndices.length; i++) {	randomIndices[i] = i;      }      for (int j = randomIndices.length - 1; j > 0; j--) {	int toSwap = m_Random.nextInt(j + 1);	int tmpIndex = randomIndices[j];	randomIndices[j] = randomIndices[toSwap];	randomIndices[toSwap] = tmpIndex;      }            double[] randomizedCounts = new double[m_ClassCounts.length];      for (int i = 0; i < randomizedCounts.length; i++) {	randomizedCounts[i] = m_ClassCounts[randomIndices[i]];      }       // Create new order. For the moment m_Converter converts new indices      // into old ones.      if (m_ClassOrder == RANDOM) {	m_Converter = randomIndices;	m_ClassCounts = randomizedCounts;      } else {	int[] sorted = Utils.sort(randomizedCounts);	m_Converter = new int[sorted.length];	if (m_ClassOrder == FREQ_ASCEND) {	  for (int i = 0; i < sorted.length; i++) {	    m_Converter[i] = randomIndices[sorted[i]];	  }	} else if (m_ClassOrder == FREQ_DESCEND) {	  for (int i = 0; i < sorted.length; i++) {	    m_Converter[i] = randomIndices[sorted[sorted.length - i - 1]];	  }	} else {	  throw new IllegalArgumentException("Class order not defined!");	}		// Change class counts	double[] tmp2 = new double[m_ClassCounts.length];	for (int i = 0; i < m_Converter.length; i++) {	  tmp2[i] = m_ClassCounts[m_Converter[i]];	}	m_ClassCounts = tmp2;      }            // Change the class values      FastVector values = new FastVector(data.classAttribute().numValues());      for (int i = 0; i < data.numClasses(); i++) {	values.addElement(data.classAttribute().value(m_Converter[i]));      }      FastVector newVec = new FastVector(data.numAttributes());      for (int i = 0; i < data.numAttributes(); i++) {	if (i == data.classIndex()) {	  newVec.addElement(new Attribute(data.classAttribute().name(), values, 					  data.classAttribute().getMetadata()));	} else {	  newVec.addElement(data.attribute(i));	}      }      Instances newInsts = new Instances(data.relationName(), newVec, 0);      newInsts.setClassIndex(data.classIndex());      setOutputFormat(newInsts);      // From now on we need m_Converter to convert old indices into new ones      int[] temp = new int[m_Converter.length];      for (int i = 0; i < temp.length; i++) {	temp[m_Converter[i]] = i;      }      m_Converter = temp;      // Process all instances      for(int xyz=0; xyz<data.numInstances(); xyz++){	Instance datum = data.instance(xyz);	if (!datum.isMissing(datum.classIndex())) {	  datum.setClassValue((double)m_Converter[(int)datum.classValue()]);	}	push(datum);      }    }    flushInput();    m_NewBatch = true;    return (numPendingOutput() != 0);  }      /**   * Get the class distribution of the sorted class values.  If class is numeric   * it returns null    *   * @return the class counts   */  public double[] getClassCounts(){     if(m_ClassAttribute.isNominal())      return m_ClassCounts;     else      return null;  }  /**   * Convert the given class distribution back to the distributions   * with the original internal class index   *    * @param before the given class distribution   * @return the distribution converted back   */  public double[] distributionsByOriginalIndex (double[] before){    double[] after = new double[m_Converter.length];    for(int i=0; i < m_Converter.length; i++)       after[i] = before[m_Converter[i]];        return after;  }  /**   * Return the original internal class value given the randomized    * class value, i.e. the string presentations of the two indices   * are the same.  It's useful when the filter is used within a classifier     * so that the filtering procedure should be transparent to the    * evaluation   *   * @param value the given value   * @return the original internal value, -1 if not found   * @throws Exception if the coverter table is not set yet   */  public double originalValue(double value)throws Exception{    if(m_Converter == null)      throw new IllegalStateException("Coverter table not defined yet!");	    for(int i=0; i < m_Converter.length; i++)      if((int)value == m_Converter[i])	return (double)i;    return -1;  }     /**   * Main method for testing this class.   *   * @param argv should contain arguments to the filter: use -h for help   */  public static void main(String [] argv) {    runFilter(new ClassOrder(), argv);  }}

⌨️ 快捷键说明

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