Example #1
0
  /**
   * Convert a single instance over. The converted instance is added to the end of the output queue.
   *
   * @param instance the instance to convert
   * @exception Exception if an error occurs
   */
  private 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)) {

          // Just subtract the mean if the standard deviation is zero
          if (m_StdDevs[j] > 0) {
            value = (vals[j] - m_Means[j]) / m_StdDevs[j];
          } else {
            value = vals[j] - m_Means[j];
          }
          if (Double.isNaN(value)) {
            throw new Exception(
                "A NaN value was generated "
                    + "while standardizing attribute "
                    + 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)) {

          // Just subtract the mean if the standard deviation is zero
          if (m_StdDevs[j] > 0) {
            vals[j] = (vals[j] - m_Means[j]) / m_StdDevs[j];
          } else {
            vals[j] = (vals[j] - m_Means[j]);
          }
          if (Double.isNaN(vals[j])) {
            throw new Exception(
                "A NaN value was generated "
                    + "while standardizing attribute "
                    + instance.attribute(j).name());
          }
        }
      }
      inst = new Instance(instance.weight(), vals);
    }
    inst.setDataset(instance.dataset());
    push(inst);
  }