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

📄 wavelet.java

📁 代码是一个分类器的实现,其中使用了部分weka的源代码。可以将项目导入eclipse运行
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
    if (tmpOptions.length != 0) {      tmpStr        = tmpOptions[0];      tmpOptions[0] = "";      setFilter((Filter) Utils.forName(Filter.class, tmpStr, tmpOptions));    }    else {      filter = new MultiFilter();      ((MultiFilter) filter).setFilters(	  new Filter[]{	      new weka.filters.unsupervised.attribute.ReplaceMissingValues(),	      new weka.filters.unsupervised.attribute.Normalize()	      });      setFilter(filter);    }  }    /**   * Returns the tip text for this property   *    * @return 		tip text for this property suitable for   * 			displaying in the explorer/experimenter gui   */  public String filterTipText() {    return "The preprocessing filter to use.";  }  /**   * Set the preprocessing filter (only used for setup).   *   * @param value	the preprocessing filter.   */  public void setFilter(Filter value) {    m_Filter = value;  }  /**   * Get the preprocessing filter.   *   * @return 		the preprocessing filter   */  public Filter getFilter() {    return m_Filter;  }  /**   * Returns the tip text for this property   *    * @return 		tip text for this property suitable for   * 			displaying in the explorer/experimenter gui   */  public String algorithmTipText() {    return "Sets the type of algorithm to use.";  }  /**   * Sets the type of algorithm to use    *   * @param value 	the algorithm type   */  public void setAlgorithm(SelectedTag value) {    if (value.getTags() == TAGS_ALGORITHM) {      m_Algorithm = value.getSelectedTag().getID();    }  }  /**   * Gets the type of algorithm to use    *   * @return 		the current algorithm type.   */  public SelectedTag getAlgorithm() {    return new SelectedTag(m_Algorithm, TAGS_ALGORITHM);  }  /**   * Returns the tip text for this property   *    * @return 		tip text for this property suitable for   * 			displaying in the explorer/experimenter gui   */  public String paddingTipText() {    return "Sets the type of padding to use.";  }  /**   * Sets the type of Padding to use    *   * @param value 	the Padding type   */  public void setPadding(SelectedTag value) {    if (value.getTags() == TAGS_PADDING) {      m_Padding = value.getSelectedTag().getID();    }  }  /**   * Gets the type of Padding to use    *   * @return 		the current Padding type.   */  public SelectedTag getPadding() {    return new SelectedTag(m_Padding, TAGS_PADDING);  }  /**   * returns the next bigger number that's a power of 2. If the number is    * already a power of 2 then this will be returned. The number will be at   * least 2^2..   *    * @param n		the number to start from   * @return		the next bigger number   */  protected static int nextPowerOf2(int n) {    int		exp;        exp = (int) StrictMath.ceil(StrictMath.log(n) / StrictMath.log(2.0));    exp = StrictMath.max(2, exp);        return (int) StrictMath.pow(2, exp);  }    /**   * pads the data to conform to the necessary number of attributes   *    * @param data	the data to pad   * @return		the padded data   */  protected Instances pad(Instances data) {    Instances 		result;    int 		i;    int			n;    String 		prefix;    int			numAtts;    boolean		isLast;    int			index;    Vector<Integer>	padded;    int[]		indices;    FastVector		atts;    // determine number of padding attributes    switch (m_Padding) {      case PADDING_ZERO:	if (data.classIndex() > -1)	  numAtts = (nextPowerOf2(data.numAttributes() - 1) + 1) - data.numAttributes();	else	  numAtts = nextPowerOf2(data.numAttributes()) - data.numAttributes();	break;	      default:	throw new IllegalStateException(	    "Padding " + new SelectedTag(m_Algorithm, TAGS_PADDING) 	    + " not implemented!");    }    result = new Instances(data);    prefix = getAlgorithm().getSelectedTag().getReadable();    // any padding necessary?    if (numAtts > 0) {      // add padding attributes      isLast = (data.classIndex() == data.numAttributes() - 1);      padded = new Vector<Integer>();      for (i = 0; i < numAtts; i++) {	if (isLast)	  index = result.numAttributes() - 1;	else	  index = result.numAttributes();		result.insertAttributeAt(	    new Attribute(prefix + "_padding_" + (i+1)),	    index);		// record index	padded.add(new Integer(index));      }            // get padded indices      indices = new int[padded.size()];      for (i = 0; i < padded.size(); i++)	indices[i] = padded.get(i);            // determine number of padding attributes      switch (m_Padding) {	case PADDING_ZERO:	  for (i = 0; i < result.numInstances(); i++) {	    for (n = 0; n < indices.length; n++)	      result.instance(i).setValue(indices[n], 0);	  }	  break;      }    }    // rename all attributes apart from class    data = result;    atts = new FastVector();    n = 0;    for (i = 0; i < data.numAttributes(); i++) {      n++;      if (i == data.classIndex())	atts.addElement(new Attribute(data.attribute(i).name()));      else	atts.addElement(new Attribute(prefix + "_" + n));    }    // create new dataset    result = new Instances(data.relationName(), atts, data.numInstances());    result.setClassIndex(data.classIndex());    for (i = 0; i < data.numInstances(); i++)      result.add(new Instance(1.0, data.instance(i).toDoubleArray()));        return result;  }    /**   * Determines the output format based on the input format and returns    * this. In case the output format cannot be returned immediately, i.e.,   * immediateOutputFormat() returns false, then this method will be called   * from batchFinished().   *   * @param inputFormat     the input format to base the output format on   * @return                the output format   * @throws Exception      in case the determination goes wrong   * @see   #hasImmediateOutputFormat()   * @see   #batchFinished()   */  protected Instances determineOutputFormat(Instances inputFormat)     throws Exception {    return pad(new Instances(inputFormat, 0));  }    /**   * processes the instances using the HAAR algorithm   *   * @param instances   the data to process   * @return            the modified data   * @throws Exception  in case the processing goes wrong   */  protected Instances processHAAR(Instances instances) throws Exception {    Instances	result;    int		i;    int		n;    int		j;    int		clsIdx;    double[]	oldVal;    double[]	newVal;    int		level;    int		length;    double[]	clsVal;    String	clsName;    clsIdx  = instances.classIndex();    clsVal  = null;    clsName = "<noname>";    if (clsIdx > -1) {      clsVal  = instances.attributeToDoubleArray(clsIdx);      clsName = instances.classAttribute().name();      instances.setClassIndex(-1);      instances.deleteAttributeAt(clsIdx);    }    result = new Instances(instances, 0);    level  = (int) StrictMath.ceil(			StrictMath.log(instances.numAttributes())			/ StrictMath.log(2.0));        for (i = 0; i < instances.numInstances(); i++) {      oldVal = instances.instance(i).toDoubleArray();      newVal = new double[oldVal.length];            for (n = level; n > 0; n--) {	length = (int) StrictMath.pow(2, n - 1);		for (j = 0; j < length; j++) {	  newVal[j]          = (oldVal[j*2] + oldVal[j*2 + 1]) / StrictMath.sqrt(2);	  newVal[j + length] = (oldVal[j*2] - oldVal[j*2 + 1]) / StrictMath.sqrt(2);	}		System.arraycopy(newVal, 0, oldVal, 0, newVal.length);      }            // add new transformed instance      result.add(new Instance(1, newVal));    }    // add class again    if (clsIdx > -1) {      result.insertAttributeAt(new Attribute(clsName), clsIdx);      result.setClassIndex(clsIdx);      for (i = 0; i < clsVal.length; i++)	result.instance(i).setClassValue(clsVal[i]);    }        return result;  }  /**    * Returns the Capabilities of this filter.   *   * @return            the capabilities of this object   * @see               Capabilities   */  public Capabilities getCapabilities() {    Capabilities result = super.getCapabilities();    // attribute    result.enable(Capability.NUMERIC_ATTRIBUTES);    result.enable(Capability.DATE_ATTRIBUTES);    result.enable(Capability.MISSING_VALUES);        // class    result.enable(Capability.NUMERIC_CLASS);    result.enable(Capability.DATE_CLASS);    result.enable(Capability.NO_CLASS);        return result;  }    /**   * Processes the given data (may change the provided dataset) and returns   * the modified version. This method is called in batchFinished().   *   * @param instances   the data to process   * @return            the modified data   * @throws Exception  in case the processing goes wrong   * @see               #batchFinished()   */  protected Instances process(Instances instances) throws Exception {    if (!isFirstBatchDone())      m_Filter.setInputFormat(instances);    instances = Filter.useFilter(instances, m_Filter);        switch (m_Algorithm) {      case ALGORITHM_HAAR:	return processHAAR(pad(instances));      default:	throw new IllegalStateException(	    "Algorithm type '" + m_Algorithm + "' is not recognized!");    }  }  /**   * runs the filter with the given arguments   *   * @param args      the commandline arguments   */  public static void main(String[] args) {    runFilter(new Wavelet(), args);  }}

⌨️ 快捷键说明

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