示例#1
0
  /**
   * Returns the number of distinct values of a given attribute. Returns the number of instances if
   * the attribute is a string attribute. The value 'missing' is not counted.
   *
   * @param attIndex the attribute (index starts with 0)
   * @return the number of distinct values of a given attribute
   */
  public int numDistinctValues(int attIndex) {

    MyAttribute att = getAttribute(attIndex);
    if (att.isContinuous()) {
      double[] attVals = attributeToDoubleArray(attIndex);
      Utilities.mergeSort(attVals, attVals.length);
      double prev = 0;
      int counter = 0;
      for (int i = 0; i < size(); i++) {

        if (attVals[i] == Double.NaN) {
          break;
        }
        if ((i == 0) || (attVals[i] > prev)) {
          prev = attVals[i];
          counter++;
        }
      }
      return counter;
    } else {
      return att.numValues();
    }
  }
示例#2
0
  /** Function to stores header of a data file. */
  private void readHeader() {
    String attributeName;
    Vector attributeValues;
    int i;

    name = Attributes.getRelationName();

    // Create vectors to hold information temporarily.
    attributes = new Vector();

    Attribute at;

    // store attribute inputs and of the header
    for (int j = 0; j < Attributes.getInputNumAttributes(); j++) {
      at = Attributes.getInputAttribute(j);
      attributeName = at.getName();

      // check if it is real
      if (at.getType() == 2) {
        float min = (float) at.getMinAttribute();
        float max = (float) at.getMinAttribute();
        attributes.addElement(new MyAttribute(attributeName, j));
        MyAttribute att = (MyAttribute) attributes.elementAt(j);
        att.setRange(min, max);
        att.activate();
      } else {
        if (at.getType() == 1) // check if it is integer
        {
          int min = (int) at.getMinAttribute();
          int max = (int) at.getMinAttribute();
          attributes.addElement(new MyAttribute(attributeName, j));
          MyAttribute att = (MyAttribute) attributes.elementAt(j);
          att.setRange(min, max);
          att.activate();
        } else // it is nominal
        {
          attributeValues = new Vector();
          for (int k = 0; k < at.getNumNominalValues(); k++) {
            attributeValues.addElement(at.getNominalValue(k));
          }
          attributes.addElement(new MyAttribute(attributeName, attributeValues, j));
          MyAttribute att = (MyAttribute) attributes.elementAt(j);
          att.activate();
        }
      }
    } // for

    // store outputs of the header
    at = Attributes.getOutputAttribute(0);
    attributeName = at.getName();

    int j = Attributes.getNumAttributes() - 1;

    // check if it is real
    if (at.getType() == 2) {
      float min = (float) at.getMinAttribute();
      float max = (float) at.getMinAttribute();
      attributes.addElement(new MyAttribute(attributeName, j));
      MyAttribute att = (MyAttribute) attributes.elementAt(j);
      att.setRange(min, max);
      att.activate();
    } else {
      if (at.getType() == 1) // check if it is integer
      {
        int min = (int) at.getMinAttribute();
        int max = (int) at.getMinAttribute();
        attributes.addElement(new MyAttribute(attributeName, j));
        MyAttribute att = (MyAttribute) attributes.elementAt(j);
        att.setRange(min, max);
        att.activate();
      } else // it is nominal
      {
        attributeValues = new Vector();
        for (int k = 0; k < at.getNumNominalValues(); k++) {
          attributeValues.addElement(at.getNominalValue(k));
        }
        attributes.addElement(new MyAttribute(attributeName, attributeValues, j));
        MyAttribute att = (MyAttribute) attributes.elementAt(j);
        att.activate();
      }
    }

    // set the index of the output class
    classIndex = Attributes.getNumAttributes() - 1;
  }