normalize.java

来自「Weka」· Java 代码 · 共 570 行 · 第 1/2 页

JAVA
570
字号
	      if (Double.isNaN(m_MinArray[i])) {		m_MinArray[i] = m_MaxArray[i] = value[i];	      }	      else {		if (value[i] < m_MinArray[i])		  m_MinArray[i] = value[i];		if (value[i] > m_MaxArray[i])		  m_MaxArray[i] = value[i];	      }	    }	  }	}       }      // Convert pending input instances      for (int i = 0; i < input.numInstances(); i++)	convertInstance(input.instance(i));    }     // Free memory    flushInput();    m_NewBatch = true;    return (numPendingOutput() != 0);  }  /**   * Convert a single instance over. The converted instance is    * added to the end of the output queue.   *   * @param instance 	the instance to convert   * @throws Exception 	if conversion fails   */  protected void convertInstance(Instance instance) throws Exception {    Instance inst = null;    if (instance instanceof SparseInstance) {      double[] newVals = new double[instance.numAttributes()];      int[] newIndices = new int[instance.numAttributes()];      double[] vals = instance.toDoubleArray();      int ind = 0;      for (int j = 0; j < instance.numAttributes(); j++) {	double value;	if (instance.attribute(j).isNumeric() &&	    (!Instance.isMissingValue(vals[j])) &&	    (getInputFormat().classIndex() != j)) {	  if (Double.isNaN(m_MinArray[j]) ||	      (m_MaxArray[j] == m_MinArray[j])) {	    value = 0;	  }	  else {	    value = (vals[j] - m_MinArray[j]) / 	      (m_MaxArray[j] - m_MinArray[j]) * m_Scale + m_Translation;            if (Double.isNaN(value)) {              throw new Exception("A NaN value was generated "                                  + "while normalizing "                                   + instance.attribute(j).name());            }	  }	  if (value != 0.0) {	    newVals[ind] = value;	    newIndices[ind] = j;	    ind++;	  }	}	else {	  value = vals[j];	  if (value != 0.0) {	    newVals[ind] = value;	    newIndices[ind] = j;	    ind++;	  }	}      }	      double[] tempVals = new double[ind];      int[] tempInd = new int[ind];      System.arraycopy(newVals, 0, tempVals, 0, ind);      System.arraycopy(newIndices, 0, tempInd, 0, ind);      inst = new SparseInstance(instance.weight(), tempVals, tempInd,                                instance.numAttributes());    }    else {      double[] vals = instance.toDoubleArray();      for (int j = 0; j < getInputFormat().numAttributes(); j++) {	if (instance.attribute(j).isNumeric() &&	    (!Instance.isMissingValue(vals[j])) &&	    (getInputFormat().classIndex() != j)) {	  if (Double.isNaN(m_MinArray[j]) ||	      (m_MaxArray[j] == m_MinArray[j])) {	    vals[j] = 0;	  }	  else {	    vals[j] = (vals[j] - m_MinArray[j]) / 	      (m_MaxArray[j] - m_MinArray[j]) * m_Scale + m_Translation;            if (Double.isNaN(vals[j])) {              throw new Exception("A NaN value was generated "                                  + "while normalizing "                                   + instance.attribute(j).name());            }	  }	}      }	      inst = new Instance(instance.weight(), vals);    }    inst.setDataset(instance.dataset());    push(inst);  }    /**   * Returns a string that describes the filter as source. The   * filter will be contained in a class with the given name (there may   * be auxiliary classes),   * and will contain two methods with these signatures:   * <pre><code>   * // converts one row   * public static Object[] filter(Object[] i);   * // converts a full dataset (first dimension is row index)   * public static Object[][] filter(Object[][] i);   * </code></pre>   * where the array <code>i</code> contains elements that are either   * Double, String, with missing values represented as null. The generated   * code is public domain and comes with no warranty.   *   * @param className   the name that should be given to the source class.   * @param data	the dataset used for initializing the filter   * @return            the object source described by a string   * @throws Exception  if the source can't be computed   */  public String toSource(String className, Instances data) throws Exception {    StringBuffer        result;    boolean[]		process;    int			i;        result = new StringBuffer();        // determine what attributes were processed    process = new boolean[data.numAttributes()];    for (i = 0; i < data.numAttributes(); i++)       process[i] = (data.attribute(i).isNumeric() && (i != data.classIndex()));      result.append("class " + className + " {\n");    result.append("\n");    result.append("  /** lists which attributes will be processed */\n");    result.append("  protected final static boolean[] PROCESS = new boolean[]{" + Utils.arrayToString(process) + "};\n");    result.append("\n");    result.append("  /** the minimum values for numeric values */\n");    result.append("  protected final static double[] MIN = new double[]{" + Utils.arrayToString(m_MinArray).replaceAll("NaN", "Double.NaN") + "};\n");    result.append("\n");    result.append("  /** the maximum values for numeric values */\n");    result.append("  protected final static double[] MAX = new double[]{" + Utils.arrayToString(m_MaxArray) + "};\n");    result.append("\n");    result.append("  /** the scale factor */\n");    result.append("  protected final static double SCALE = " + m_Scale + ";\n");    result.append("\n");    result.append("  /** the translation */\n");    result.append("  protected final static double TRANSLATION = " + m_Translation + ";\n");    result.append("\n");    result.append("  /**\n");    result.append("   * filters a single row\n");    result.append("   * \n");    result.append("   * @param i the row to process\n");    result.append("   * @return the processed row\n");    result.append("   */\n");    result.append("  public static Object[] filter(Object[] i) {\n");    result.append("    Object[] result;\n");    result.append("\n");    result.append("    result = new Object[i.length];\n");    result.append("    for (int n = 0; n < i.length; n++) {\n");    result.append("      if (PROCESS[n] && (i[n] != null)) {\n");    result.append("        if (Double.isNaN(MIN[n]) || (MIN[n] == MAX[n]))\n");    result.append("          result[n] = 0;\n");    result.append("        else\n");    result.append("          result[n] = (((Double) i[n]) - MIN[n]) / (MAX[n] - MIN[n]) * SCALE + TRANSLATION;\n");    result.append("      }\n");    result.append("      else {\n");    result.append("        result[n] = i[n];\n");    result.append("      }\n");    result.append("    }\n");    result.append("\n");    result.append("    return result;\n");    result.append("  }\n");    result.append("\n");    result.append("  /**\n");    result.append("   * filters multiple rows\n");    result.append("   * \n");    result.append("   * @param i the rows to process\n");    result.append("   * @return the processed rows\n");    result.append("   */\n");    result.append("  public static Object[][] filter(Object[][] i) {\n");    result.append("    Object[][] result;\n");    result.append("\n");    result.append("    result = new Object[i.length][];\n");    result.append("    for (int n = 0; n < i.length; n++) {\n");    result.append("      result[n] = filter(i[n]);\n");    result.append("    }\n");    result.append("\n");    result.append("    return result;\n");    result.append("  }\n");    result.append("}\n");        return result.toString();  }  /**   * Returns the calculated minimum values for the attributes in the data.   *    * @return		the array with the minimum values   */  public double[] getMinArray() {    return m_MinArray;  }  /**   * Returns the calculated maximum values for the attributes in the data.   *    * @return		the array with the maximum values   */  public double[] getMaxArray() {    return m_MaxArray;  }  /**   * Returns the tip text for this property.   *   * @return 		tip text for this property suitable for   * 			displaying in the explorer/experimenter gui   */  public String scaleTipText() {    return "The factor for scaling the output range (default: 1).";  }  /**   * Get the scaling factor.   *   * @return 		the factor   */  public double getScale() {    return m_Scale;  }  /**   * Sets the scaling factor.   *   * @param value 	the scaling factor   */  public void setScale(double value) {    m_Scale = value;  }  /**   * Returns the tip text for this property.   *   * @return 		tip text for this property suitable for   * 			displaying in the explorer/experimenter gui   */  public String translationTipText() {    return "The translation of the output range (default: 0).";  }  /**   * Get the translation.   *   * @return 		the translation   */  public double getTranslation() {    return m_Translation;  }  /**   * Sets the translation.   *   * @param value 	the translation   */  public void setTranslation(double value) {    m_Translation = value;  }    /**   * Main method for running this filter.   *   * @param args 	should contain arguments to the filter, use -h for help   */  public static void main(String[] args) {    runFilter(new Normalize(), args);  }}

⌨️ 快捷键说明

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