Example #1
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;
  }
Example #2
0
  protected static void escribeSalida(
      String nombreFichero,
      String instanciasIN[],
      String instanciasOUT[],
      Attribute entradas[],
      Attribute salida,
      int nEntradas,
      String relation) {

    String cadena = "";
    int i, j, k;
    int aux;

    /* Printing input attributes */
    cadena += "@relation " + relation + "\n";
    for (i = 0; i < nEntradas; i++) {
      cadena += "@attribute " + entradas[i].getName() + " ";
      if (entradas[i].getType() == Attribute.NOMINAL) {
        cadena += "{";
        for (j = 0; j < entradas[i].getNominalValuesList().size(); j++) {
          cadena += (String) entradas[i].getNominalValuesList().elementAt(j);
          if (j < entradas[i].getNominalValuesList().size() - 1) {
            cadena += ", ";
          }
        }
        cadena += "}\n";
      } else {
        if (entradas[i].getType() == Attribute.INTEGER) {
          cadena += "integer";
          cadena +=
              " ["
                  + String.valueOf((int) entradas[i].getMinAttribute())
                  + ", "
                  + String.valueOf((int) entradas[i].getMaxAttribute())
                  + "]\n";
        } else {
          cadena += "real";
          cadena +=
              " ["
                  + String.valueOf(entradas[i].getMinAttribute())
                  + ", "
                  + String.valueOf(entradas[i].getMaxAttribute())
                  + "]\n";
        }
      }
    }

    /* Printing output attribute */
    cadena += "@attribute " + salida.getName() + " ";
    if (salida.getType() == Attribute.NOMINAL) {
      cadena += "{";
      for (j = 0; j < salida.getNominalValuesList().size(); j++) {
        cadena += (String) salida.getNominalValuesList().elementAt(j);
        if (j < salida.getNominalValuesList().size() - 1) {
          cadena += ", ";
        }
      }
      cadena += "}\n";
    } else {
      cadena +=
          "integer ["
              + String.valueOf((int) salida.getMinAttribute())
              + ", "
              + String.valueOf((int) salida.getMaxAttribute())
              + "]\n";
    }

    /* Printing the data */
    cadena += "@data\n";

    Fichero.escribeFichero(nombreFichero, cadena);
    cadena = "";
    for (i = 0; i < instanciasIN.length; i++) {
      cadena += instanciasIN[i] + " " + instanciasOUT[i];

      cadena += "\n";
    }
    Fichero.AnadirtoFichero(nombreFichero, cadena);
  }
Example #3
0
  /**
   * Writes results
   *
   * @param nombreFichero Name of the output file
   * @param salidaKNN Instances to write
   * @param prediccion Instances to mantain
   * @param entradas Input attributes characteristics
   * @param salida Output attribute characteristics
   * @param nEntradas Number of input attributes
   * @param relation Name of the data set
   */
  public static void escribeSalida(
      String nombreFichero,
      int[][] salidaKNN,
      int[][] prediccion,
      Attribute entradas[],
      Attribute salida,
      int nEntradas,
      String relation) {

    int n_ejemplos, n_salidas = 0;
    String cadena = "";
    int i, j;

    /*Printing input attributes*/
    cadena += "@relation " + relation + "\n";
    for (i = 0; i < nEntradas; i++) {
      cadena += "@attribute " + entradas[i].getName() + " ";
      if (entradas[i].getType() == Attribute.NOMINAL) {
        cadena += "{";
        for (j = 0; j < entradas[i].getNominalValuesList().size(); j++) {
          cadena += (String) entradas[i].getNominalValuesList().elementAt(j);
          if (j < entradas[i].getNominalValuesList().size() - 1) {
            cadena += ", ";
          }
        }
        cadena += "}\n";
      } else {
        if (entradas[i].getType() == Attribute.INTEGER) {
          cadena += "integer";
        } else {
          cadena += "real";
        }
        cadena +=
            " ["
                + String.valueOf(entradas[i].getMinAttribute())
                + ", "
                + String.valueOf(entradas[i].getMaxAttribute())
                + "]\n";
      }
    }

    /*Printing output attribute*/
    cadena += "@attribute " + salida.getName() + " ";
    if (salida.getType() == Attribute.NOMINAL) {
      cadena += "{";
      for (j = 0; j < salida.getNominalValuesList().size(); j++) {
        cadena += (String) salida.getNominalValuesList().elementAt(j);
        if (j < salida.getNominalValuesList().size() - 1) {
          cadena += ", ";
        }
      }
      cadena += "}\n";
    } else {
      cadena +=
          "integer ["
              + String.valueOf(salida.getMinAttribute())
              + ", "
              + String.valueOf(salida.getMaxAttribute())
              + "]\n";
    }

    /*Printing the data*/
    cadena += "@data\n";

    Fichero.escribeFichero(nombreFichero, cadena);

    n_ejemplos = salidaKNN.length;

    if (n_ejemplos > 0) n_salidas = salidaKNN[0].length;

    for (i = 0; i < n_ejemplos; i++) {
      cadena = "";
      for (j = 0; j < n_salidas; j++) cadena += "" + salidaKNN[i][j] + " ";
      for (j = 0; j < n_salidas; j++) cadena += "" + prediccion[i][j] + " ";
      cadena += "\n";
      Fichero.AnadirtoFichero(nombreFichero, cadena);
    }
  } // end-method