Exemple #1
0
 public Vector getRangos() {
   Vector salida = new Vector();
   double[][] rangos = this.devuelveRangos();
   for (int i = 0; i < nVars; i++) {
     Vector rango = new Vector();
     if (this.getTiposIndex2(i).equals("real")) {
       // if (this.getTiposIndex(i).equals("real")) {
       rango.add(new Double(rangos[i][0]));
       rango.add(new Double(rangos[i][1]));
       /*}else if(this.getTiposIndex2(i).equals("integer")){
          rango.add(new Integer((int)rangos[i][0]));
          rango.add(new Integer((int)rangos[i][1]));
       */
     } else {
       if (i == nVars - 1) {
         for (int j = 0; j < Attributes.getOutputAttribute(0).getNumNominalValues(); j++) {
           rango.add(Attributes.getOutputAttribute(0).getNominalValue(j));
         }
       } else {
         for (int j = 0; j < Attributes.getAttribute(i).getNumNominalValues(); j++) {
           rango.add(Attributes.getAttribute(i).getNominalValue(j));
         }
       }
     }
     salida.add(rango);
   }
   return salida;
 }
Exemple #2
0
  /**
   * Returs the type of the dataset 0-Modelling, 1-Clasiffication, 2-Clustering
   *
   * @return type of the dataset 0-Modelling, 1-Clasiffication, 2-Clustering
   */
  public int datasetType() {

    if (Attributes.getOutputNumAttributes() >= 1) {
      if (Attributes.hasNominalAttributes()) return (1);
      else return (0);
    } else return (2);
  }
Exemple #3
0
 /* Devuelve el tipo del atributo de indice index */
 public String getTiposIndex(int index) {
   int tipo = Attributes.getAttribute(index).getType();
   if ((tipo == Attributes.getAttribute(0).INTEGER) || (tipo == Attributes.getAttribute(0).REAL)) {
     return "real";
   }
   return "enumerado";
 }
Exemple #4
0
  /** Creates the total selector's set for get all the possible rules */
  private Complejo hazSelectores(Dataset train) {

    Complejo almacenSelectores;
    int nClases = train.getnclases();
    almacenSelectores =
        new Complejo(nClases); // Aqui voy a almacenar los selectores (numVariable,operador,valor)
    Attribute[] atributos = null;
    int num_atributos, type;
    Vector nominalValues;
    atributos = Attributes.getAttributes();
    num_atributos = Attributes.getNumAttributes();
    Selector s;

    for (int i = 0; i < train.getnentradas(); i++) {
      type = atributos[i].getType();
      switch (type) {
        case 0: // NOMINAL
          nominalValues = atributos[i].getNominalValuesList();
          // System.out.print("{");
          for (int j = 0; j < nominalValues.size(); j++) {
            // System.out.print ((String)nominalValues.elementAt(j)+"  ");
            s = new Selector(i, 0, (String) nominalValues.elementAt(j), true); // [atr,op,valor]
            // incluimos tb los valores en double para facilitar algunas funciones
            s.setValor((double) j);
            almacenSelectores.addSelector(s);
            // s.print();
          }
          // System.out.println("}");
          break;
      }
      // System.out.println(num_atributos);
    }
    return almacenSelectores;
  }
Exemple #5
0
  /**
   * Generating the header of the output file @ param p PrintStream
   *
   * @return Nothing
   */
  private void CopyHeaderTest(PrintStream p) {

    // Header of the output file
    p.println("@relation " + Attributes.getRelationName());
    p.print(Attributes.getInputAttributesHeader());
    p.print(Attributes.getOutputAttributesHeader());
    p.println(Attributes.getInputHeader());
    p.println(Attributes.getOutputHeader());
    p.println("@data");
  }
Exemple #6
0
 /**
  * It copies the header of the dataset
  *
  * @return String A string containing all the data-set information
  */
 public String copyHeader() {
   String p = new String("");
   p = "@relation " + Attributes.getRelationName() + "\n";
   p += Attributes.getInputAttributesHeader();
   p += Attributes.getOutputAttributesHeader();
   p += Attributes.getInputHeader() + "\n";
   p += Attributes.getOutputHeader() + "\n";
   p += "@data\n";
   return p;
 }
Exemple #7
0
  /**
   * Compute the number of all possible conditions that could appear in a rule of a given data. For
   * nominal attributes, it's the number of values that could appear; for numeric attributes, it's
   * the number of values * 2, i.e. <= and >= are counted as different possible conditions.
   *
   * @return number of all conditions of the data
   */
  public double numAllConditions() {
    double total = 0;

    for (int i = 0; i < Attributes.getInputNumAttributes(); i++) {
      Attribute att = Attributes.getInputAttribute(i);
      if (att.getType() == Attribute.NOMINAL) total += (double) att.getNumNominalValues();
      else total += 2.0 * (double) numDistinctValues(i);
    }
    return total;
  }
Exemple #8
0
 static void checkDataset() {
   Attribute[] outputs = Attributes.getOutputAttributes();
   if (outputs.length != 1) {
     LogManager.printErr("Only datasets with one output are supported");
     System.exit(1);
   }
   if (outputs[0].getType() != Attribute.NOMINAL) {
     LogManager.printErr("Output attribute should be nominal");
     System.exit(1);
   }
   Parameters.numClasses = outputs[0].getNumNominalValues();
   Parameters.numAttributes = Attributes.getInputAttributes().length;
 }
Exemple #9
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();

    keel.Dataset.Attribute at;

    // store attribute,inputs and outputs of the header
    for (int j = 0; j < Attributes.getNumAttributes(); j++) {
      at = Attributes.getAttribute(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 Attribute(attributeName, j));
        Attribute att = (Attribute) 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 Attribute(attributeName, j));
          Attribute att = (Attribute) 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 Attribute(attributeName, attributeValues, j));
          Attribute att = (Attribute) attributes.elementAt(j);
          att.activate();
        }
      }
    } // for

    // set the index of the output class
    classIndex = Attributes.getNumAttributes() - 1;
  }
Exemple #10
0
  /**
   * Process a dataset for classification
   *
   * @return Nothing
   */
  public void processClassifierDataset() throws IOException {
    try {

      ndata = IS.getNumInstances();
      ninputs = Attributes.getInputNumAttributes();
      nvariables = ninputs + Attributes.getOutputNumAttributes();
      // Check that there is only one output variable and
      // it is nominal
      if (Attributes.getOutputNumAttributes() > 1) {
        System.out.println("This algorithm can not process MIMO datasets");
        System.out.println("All outputs but the first one will be removed");
      }
      boolean noOutputs = false;
      if (Attributes.getOutputNumAttributes() < 1) {
        System.out.println("This algorithm cannot process datasets without outputs");
        System.out.println("Zero-valued output generated");
        noOutputs = true;
      }
      // Initialice and fill our own tables
      X = new double[ndata][ninputs];
      missing = new boolean[ndata][ninputs];
      C = new int[ndata];
      // Maximum and minimum of inputs
      imax = new double[ninputs];
      imin = new double[ninputs];
      // Maximum and minimum for output data
      omax = 0;
      omin = 0;
      // All values are casted into double/integer
      nclasses = 0;
      for (int i = 0; i < X.length; i++) {
        Instance inst = IS.getInstance(i);
        for (int j = 0; j < ninputs; j++) {
          X[i][j] = IS.getInputNumericValue(i, j);
          missing[i][j] = inst.getInputMissingValues(j);
          if (X[i][j] > imax[j] || i == 0) imax[j] = X[i][j];
          if (X[i][j] < imin[j] || i == 0) imin[j] = X[i][j];
        }
        if (noOutputs) C[i] = 0;
        else C[i] = (int) IS.getOutputNumericValue(i, 0);
        if (C[i] > nclasses) nclasses = C[i];
      }
      nclasses++;
      System.out.println("Number of classes=" + nclasses);
    } catch (Exception e) {
      System.out.println("DBG: Exception in readSet");
      e.printStackTrace();
    }
  }
Exemple #11
0
  /**
   * Process a dataset file for a clustering problem.
   *
   * @param nfexamples Name of the dataset file
   * @param train The dataset file is for training or for test
   * @throws java.io.IOException if there is any semantical, lexical or sintactical error in the
   *     input file.
   */
  public void processClusterDataset(String nfexamples, boolean train) throws IOException {

    try {

      // Load in memory a dataset that contains a classification problem
      IS.readSet(nfexamples, train);

      nData = IS.getNumInstances();
      nInputs = Attributes.getInputNumAttributes();
      nVariables = nInputs + Attributes.getOutputNumAttributes();

      if (Attributes.getOutputNumAttributes() != 0) {
        System.out.println("This algorithm can not process datasets with outputs");
        System.out.println("All outputs will be removed");
      }

      // Initialize and fill our own tables
      X = new double[nData][nInputs];
      missing = new boolean[nData][nInputs];

      // Maximum and minimum of inputs
      iMaximum = new double[nInputs];
      iMinimum = new double[nInputs];

      // Maximum and minimum for output data
      oMaximum = 0;
      oMinimum = 0;

      // All values are casted into double/integer
      nClasses = 0;
      for (int i = 0; i < X.length; i++) {
        Instance inst = IS.getInstance(i);
        for (int j = 0; j < nInputs; j++) {
          X[i][j] = IS.getInputNumericValue(i, j);
          missing[i][j] = inst.getInputMissingValues(j);
          if (X[i][j] > iMaximum[j] || i == 0) {
            iMaximum[j] = X[i][j];
          }
          if (X[i][j] < iMinimum[j] || i == 0) {
            iMinimum[j] = X[i][j];
          }
        }
      }

    } catch (Exception e) {
      System.out.println("DBG: Exception in readSet");
      e.printStackTrace();
    }
  }
Exemple #12
0
  /**
   * Returns a string representation of this SimpleRule
   *
   * @return a string representation of this SimpleRule.
   */
  public String toString() {
    // return
    // ""+Attributes.getAttribute(attribute).getName()+"="+Attributes.getAttribute(attribute).getNominalValue((int)value);
    String V = "";
    V += value;
    String operator_string = "<undef>";
    if (operator == GREATER) operator_string = ">";
    if (operator == LOWER) operator_string = "<=";
    if (operator == EQUAL) {
      operator_string = "=";
      V = Attributes.getAttribute(attribute).getNominalValue((int) value);
    }

    return "" + Attributes.getAttribute(attribute).getName() + operator_string + V;
  }
Exemple #13
0
  /**
   * Process a dataset for modelling
   *
   * @return Nothing
   */
  public void processModelDataset() throws IOException {
    try {

      // Load in memory a dataset that contains a classification problem
      // IS.readSet(nfejemplos,train);
      ndata = IS.getNumInstances();
      ninputs = Attributes.getInputNumAttributes();
      nvariables = ninputs + Attributes.getOutputNumAttributes();
      if (Attributes.getOutputNumAttributes() > 1) {
        System.out.println("This algorithm can not process MIMO datasets");
        System.out.println("All outputs but the first one will be removed");
      }
      boolean noOutputs = false;
      if (Attributes.getOutputNumAttributes() < 1) {
        System.out.println("This algorithm can not process datasets without outputs");
        System.out.println("Zero-valued output generated");
        noOutputs = true;
      }
      // Initialice and fill our own tables
      X = new double[ndata][ninputs];
      missing = new boolean[ndata][ninputs];
      Y = new double[ndata];
      // Maximum and minimum of inputs
      imax = new double[ninputs];
      imin = new double[ninputs];
      // Maximum and minimum for output data
      omax = 0;
      omin = 0;
      // All values are casted into double/integer
      nclasses = 0;
      for (int i = 0; i < X.length; i++) {
        Instance inst = IS.getInstance(i);
        for (int j = 0; j < ninputs; j++) {
          X[i][j] = IS.getInputNumericValue(i, j);
          missing[i][j] = inst.getInputMissingValues(j);
          if (X[i][j] > imax[j] || i == 0) imax[j] = X[i][j];
          if (X[i][j] < imin[j] || i == 0) imin[j] = X[i][j];
        }
        if (noOutputs) Y[i] = 0;
        else Y[i] = IS.getOutputNumericValue(i, 0);
        if (Y[i] > omax || i == 0) omax = Y[i];
        if (Y[i] < omin || i == 0) omin = Y[i];
      }
    } catch (Exception e) {
      System.out.println("DBG: Exception in readSet");
      e.printStackTrace();
    }
  }
Exemple #14
0
  /** Method interface for Automatic Branch and Bound */
  public void ejecutar() {
    String resultado;
    int i, numFeatures;
    Date d;

    d = new Date();
    resultado =
        "RESULTS generated at "
            + String.valueOf((Date) d)
            + " \n--------------------------------------------------\n";
    resultado += "Algorithm Name: " + params.nameAlgorithm + "\n";

    /* call of ABB algorithm */
    runABB();

    resultado += "\nPARTITION Filename: " + params.trainFileNameInput + "\n---------------\n\n";
    resultado += "Features selected: \n";

    for (i = numFeatures = 0; i < features.length; i++)
      if (features[i] == true) {
        resultado += Attributes.getInputAttribute(i).getName() + " - ";
        numFeatures++;
      }

    resultado +=
        "\n\n"
            + String.valueOf(numFeatures)
            + " features of "
            + Attributes.getInputNumAttributes()
            + "\n\n";

    resultado +=
        "Error in test (using train for prediction): "
            + String.valueOf(data.validacionCruzada(features))
            + "\n";
    resultado +=
        "Error in test (using test for prediction): "
            + String.valueOf(data.LVOTest(features))
            + "\n";

    resultado += "---------------\n";

    System.out.println("Experiment completed successfully");

    /* creates the new training and test datasets only with the selected features */
    Files.writeFile(params.extraFileNameOutput, resultado);
    data.generarFicherosSalida(params.trainFileNameOutput, params.testFileNameOutput, features);
  }
Exemple #15
0
  /**
   * Generates output file for a clasification problem @ param Foutput Name of the output file @
   * param real Vector of outputs instances @ param obtained Vector of net outputs
   *
   * @return Nothing
   */
  public void generateResultsClasification(String Foutput, int[] real, int[] obtained) {

    // Output file, classification problems
    FileOutputStream out;
    PrintStream p;
    Attribute at = Attributes.getOutputAttribute(0);

    // Check whether the output value is nominal or integer
    boolean isNominal = (at.getType() == at.NOMINAL);
    try {
      out = new FileOutputStream(Foutput);
      p = new PrintStream(out);
      CopyHeaderTest(p);
      // System.out.println("Longitudes "+real.length+" "+obtained.length);
      for (int i = 0; i < real.length; i++) {
        // Write the label associated to the class number,
        // when the output is nominal
        if (isNominal)
          p.print(at.getNominalValue(real[i]) + " " + at.getNominalValue(obtained[i]) + "\n");
        else p.print(real[i] + " " + obtained[i] + "\n");
      }
      p.close();
    } catch (Exception e) {
      System.err.println("Error building file for results: " + Foutput);
    }
  }
Exemple #16
0
 /**
  * It returns the names of the variables
  *
  * @return String[] an array with the names of the variables
  */
 public String[] names() {
   String nombres[] = new String[nVars];
   for (int i = 0; i < nVars; i++) {
     nombres[i] = Attributes.getInputAttribute(i).getName();
   }
   return nombres;
 }
Exemple #17
0
  /**
   * Constructor.
   *
   * @throws Exception If the algorithm cannot be executed.
   */
  public C45(String trainfn, String testfn) throws Exception {
    try {

      // starts the time
      long startTime = System.currentTimeMillis();

      /* Sets the options of the execution from text file*/
      setOptions(trainfn, testfn);

      /* Initializes the dataset. */
      Attributes.clearAll();
      modelDataset = new Dataset(modelFileName, true);
      trainDataset = new Dataset(trainFileName, false);
      testDataset = new Dataset(testFileName, false);

      priorsProbabilities = new double[modelDataset.numClasses()];
      priorsProbabilities();
      marginCounts = new double[marginResolution + 1];

      // generate the tree
      generateTree(modelDataset);

      // printTrain();
      // printTest();
      // printResult();
    } catch (Exception e) {
      System.err.println(e.getMessage());
      System.exit(-1);
    }
  }
Exemple #18
0
  /**
   * Fill TableVar with the characteristics of the variables and creates characteristics and
   * intervals for the fuzzy sets
   *
   * @param size Number of variables of the dataset
   */
  public void Load(int size) {
    num_vars = size; // Stores the number of variables of the dataset
    var = new TypeVar[num_vars]; // Creates space for the structure

    // For each variable of the dataset
    for (int i = 0; i < num_vars; i++) {
      var[i] = new TypeVar(); // Creates space for the variable chars
      var[i].setName(Attributes.getInputAttribute(i).getName());

      if (Attributes.getInputAttribute(i).getType() == Attribute.NOMINAL) {
        var[i].setType('e');
        var[i].setContinuous(false);
        var[i].initValues(Attributes.getInputAttribute(i).getNominalValuesList());
        var[i].setMin(
            0); // Enumerated values are translated into values from 0 to number of elements - 1
        var[i].setMax(Attributes.getInputAttribute(i).getNumNominalValues() - 1);
        var[i].setNLabels(Attributes.getInputAttribute(i).getNumNominalValues());
        // Update max number of values for discrete vars
        if (var[i].getNLabels() > MaxValores) MaxValores = var[i].getNLabels();
      } else if (Attributes.getInputAttribute(i).getType() == Attribute.REAL) {
        // Real: Continuous type
        var[i].setType('r');
        var[i].setContinuous(true);
        var[i].setMin((float) Attributes.getInputAttribute(i).getMinAttribute());
        var[i].setMax((float) Attributes.getInputAttribute(i).getMaxAttribute());
        var[i].setNLabels(n_etiq);
        // Update the max number of labels for cont variables and number of values
        if (var[i].getNLabels() > MaxEtiquetas) MaxEtiquetas = var[i].getNLabels();
        if (var[i].getNLabels() > MaxValores) MaxValores = var[i].getNLabels();
      } else {
        // Integer: Continuous type
        var[i].setType('i');
        var[i].setContinuous(true);
        var[i].setMin((float) Attributes.getInputAttribute(i).getMinAttribute());
        var[i].setMax((float) Attributes.getInputAttribute(i).getMaxAttribute());
        var[i].setNLabels(n_etiq);
        // Update the max number of labels for cont variables and number of values
        if (var[i].getNLabels() > MaxEtiquetas) MaxEtiquetas = var[i].getNLabels();
        if (var[i].getNLabels() > MaxValores) MaxValores = var[i].getNLabels();
      }
    }

    // Creates "Fuzzy" characteristics and intervals
    BaseDatos = new Fuzzy[num_vars][MaxValores];
    for (int x = 0; x < num_vars; x++)
      for (int y = 0; y < MaxValores; y++) BaseDatos[x][y] = new Fuzzy();
  }
Exemple #19
0
  /**
   * Function to read an itemset and appends it to the dataset.
   *
   * @return True if the itemset was readed succesfully.
   */
  private boolean getItemsetFull() {

    // fill itemset
    for (int j = 0; j < IS.getNumInstances(); j++) {

      double[] itemset = new double[Attributes.getNumAttributes()];
      int index;

      // Get values for all input attributes.
      for (int i = 0; i < Attributes.getInputNumAttributes(); i++) {

        // check type and if there is null

        if (IS.getInstance(j).getInputMissingValues(i)) itemset[i] = Itemset.getMissingValue();
        else {
          if (Attributes.getInputAttribute(i).getType() == 0) // nominal
          {
            for (int k = 0; k < Attributes.getInputAttribute(i).getNumNominalValues(); k++)
              if (Attributes.getInputAttribute(i)
                  .getNominalValue(k)
                  .equals(IS.getInstance(j).getInputNominalValues(i))) itemset[i] = (double) k;
          } else // real and integer
          {
            itemset[i] = IS.getInstance(j).getInputRealValues(i);
          }
        } // else
      } // for

      // Get values for output attribute.
      int i = Attributes.getInputNumAttributes();

      // check type and if there is null
      if (IS.getInstance(j).getOutputMissingValues(0)) itemset[i] = Itemset.getMissingValue();
      else {
        if (Attributes.getOutputAttribute(0).getType() == 0) // nominal
        {
          for (int k = 0; k < Attributes.getOutputAttribute(0).getNumNominalValues(); k++)
            if (Attributes.getOutputAttribute(0)
                .getNominalValue(k)
                .equals(IS.getInstance(j).getOutputNominalValues(0))) itemset[i] = (double) k;
        } else // real and integer
        {
          itemset[i] = IS.getInstance(j).getOutputRealValues(0);
        }
      } // else

      // Add itemset to dataset
      addItemset(new Itemset(1, itemset));
    } // for

    return true;
  }
Exemple #20
0
 public Vector getAtributos() {
   Vector salida = new Vector();
   for (int i = 0; i < nVars; i++) {
     String cadena = new String(Attributes.getAttribute(i).getName());
     salida.add(cadena);
   }
   return salida;
 }
Exemple #21
0
  /**
   * The main method of the class
   *
   * @param script Name of the configuration script
   */
  public KNN(Instance[] trainI, Instance[] testI) {

    // set parameters
    trainInst = trainI;
    testInst = testI;

    trainData = new double[trainInst.length][Parameters.numAttributes];
    for (int i = 0; i < trainInst.length; ++i)
      System.arraycopy(
          trainInst[i].getAllInputValues(), 0, trainData[i], 0, Parameters.numAttributes);

    testData = new double[testInst.length][Parameters.numAttributes];
    for (int i = 0; i < testInst.length; ++i)
      System.arraycopy(
          testInst[i].getAllInputValues(), 0, testData[i], 0, Parameters.numAttributes);

    k = Parameters.numNeighbors;

    if (Parameters.distanceType.equalsIgnoreCase("euclidean")) distanceType = EUCLIDEAN;
    else if (Parameters.distanceType.equalsIgnoreCase("manhattan")) distanceType = MANHATTAN;
    else if (Parameters.distanceType.equalsIgnoreCase("hvdm")) distanceType = HVDM;

    // read Data Files -----------------------
    inputAtt = Attributes.getInputNumAttributes();
    inputs = Attributes.getInputAttributes();
    output = Attributes.getOutputAttribute(0);

    // Normalize the datasets
    normalizeTrain();
    normalizeTest();

    // Get the number of classes
    nClasses = Attributes.getOutputAttribute(0).getNumNominalValues();

    // And the number of instances on each class
    nInstances = new int[nClasses];
    Arrays.fill(nInstances, 0);

    for (int i = 0; i < trainOutput.length; i++) nInstances[trainOutput[i]]++;

    // Initialization of auxiliary structures
    if (distanceType == HVDM) {
      stdDev = new double[inputAtt];
      calculateHVDM();
    }
  }
Exemple #22
0
  /**
   * Prints as a String the complex content (List->Attribute)
   *
   * @return a String the complex content
   */
  public String printString(int[] numValues) {

    String cad = "";
    for (int x = 0; x < compl.size(); x++) {
      Selector s = (Selector) compl.get(x);
      // cad += "Atr" + s.getAtributo() + " ";
      double[] values = s.getValues();
      String[] nValues = s.getNValues();
      // si para un atributo aparecen todos sus valores, la condicion es true
      // no imprimimos condicion
      if (values.length < numValues[s.getAttribute()]) {

        if ((x < (compl.size())) && (x > 0)) {
          cad += " AND ";
        }
        cad += attributeNames[s.getAttribute()] + " ";

        switch (s.getOperator()) {
          case 0:
            if (values.length > 1) cad += "in";
            else cad += "=";
            break;
          case 1:
            cad += "<>";
            break;
          case 2:
            cad += "<=";
            break;
          case 3:
            cad += ">";
            break;
        }

        if (Attributes.getInputAttribute(s.getAttribute()).getType() == Attribute.NOMINAL) {
          if (nValues.length > 1) {
            cad += " [" + nValues[0];
            for (int i = 1; i < nValues.length - 1; i++) {
              cad += " , " + nValues[i];
            }
            cad += " , " + nValues[nValues.length - 1] + "] ";
          } else {
            cad += " " + nValues[0] + "";
          }
        } else {
          if (values.length > 1) {
            cad += " [" + values[0];
            for (int i = 1; i < values.length - 1; i++) {
              cad += " , " + values[i];
            }
            cad += " , " + values[values.length - 1] + "]";
          } else {
            cad += " " + values[0] + "";
          }
        }
      }
    }
    return cad;
  }
Exemple #23
0
  /**
   * Converts a real value to its representation as Nominal in the data set
   *
   * @param real Real value
   * @param att Attribute in the data set
   * @return The HVDM distance
   */
  private int real2Nom(double real, int att) {

    int result = 0;

    // con TRAIN
    result = (int) (real * ((Attributes.getInputAttribute(att).getNominalValuesList().size()) - 1));

    return result;
  }
Exemple #24
0
  /**
   * Creates a boolean array with all values to true
   *
   * @return returns a boolean vector with all values to true
   */
  private boolean[] startSolution() {
    boolean fv[];

    fv = new boolean[Attributes.getInputNumAttributes()];

    for (int i = 0; i < fv.length; i++) fv[i] = true;

    return fv;
  }
Exemple #25
0
  private void normalizarTest() {

    int i, j, cont = 0, k;
    Instance temp;
    boolean hecho;
    double caja[];
    StringTokenizer tokens;
    boolean nulls[];

    /* Check if dataset corresponding with a classification problem */

    if (Attributes.getOutputNumAttributes() < 1) {
      System.err.println(
          "This dataset haven´t outputs, so it not corresponding to a classification problem.");
      System.exit(-1);
    } else if (Attributes.getOutputNumAttributes() > 1) {
      System.err.println("This dataset have more of one output.");
      System.exit(-1);
    }

    if (Attributes.getOutputAttribute(0).getType() == Attribute.REAL) {
      System.err.println(
          "This dataset have an input attribute with floating values, so it not corresponding to a classification problem.");
      System.exit(-1);
    }

    datosTest = new double[test.getNumInstances()][Attributes.getInputNumAttributes()];
    clasesTest = new int[test.getNumInstances()];
    caja = new double[1];

    for (i = 0; i < test.getNumInstances(); i++) {
      temp = test.getInstance(i);
      nulls = temp.getInputMissingValues();
      datosTest[i] = test.getInstance(i).getAllInputValues();
      for (j = 0; j < nulls.length; j++) if (nulls[j]) datosTest[i][j] = 0.0;
      caja = test.getInstance(i).getAllOutputValues();
      clasesTest[i] = (int) caja[0];
      for (k = 0; k < datosTest[i].length; k++) {
        if (Attributes.getInputAttribute(k).getType() == Attribute.NOMINAL) {
          datosTest[i][k] /= Attributes.getInputAttribute(k).getNominalValuesList().size() - 1;
        } else {
          datosTest[i][k] -= Attributes.getInputAttribute(k).getMinAttribute();
          datosTest[i][k] /=
              Attributes.getInputAttribute(k).getMaxAttribute()
                  - Attributes.getInputAttribute(k).getMinAttribute();
        }
      }
    }
  }
Exemple #26
0
  private void normalizarReferencia() throws CheckException {

    int i, j, cont = 0, k;
    Instance temp;
    boolean hecho;
    double caja[];
    StringTokenizer tokens;
    boolean nulls[];

    /*Check if dataset corresponding with a classification problem*/

    if (Attributes.getOutputNumAttributes() < 1) {
      throw new CheckException(
          "This dataset haven´t outputs, so it not corresponding to a classification problem.");
    } else if (Attributes.getOutputNumAttributes() > 1) {
      throw new CheckException("This dataset have more of one output.");
    }

    if (Attributes.getOutputAttribute(0).getType() == Attribute.REAL) {
      throw new CheckException(
          "This dataset have an input attribute with floating values, so it not corresponding to a classification problem.");
    }

    datosReferencia = new double[referencia.getNumInstances()][Attributes.getInputNumAttributes()];
    clasesReferencia = new int[referencia.getNumInstances()];
    caja = new double[1];

    /*Get the number of instances that have a null value*/
    for (i = 0; i < referencia.getNumInstances(); i++) {
      temp = referencia.getInstance(i);
      nulls = temp.getInputMissingValues();
      datosReferencia[i] = referencia.getInstance(i).getAllInputValues();
      for (j = 0; j < nulls.length; j++) if (nulls[j]) datosReferencia[i][j] = 0.0;
      caja = referencia.getInstance(i).getAllOutputValues();
      clasesReferencia[i] = (int) caja[0];
      for (k = 0; k < datosReferencia[i].length; k++) {
        if (Attributes.getInputAttribute(k).getType() == Attribute.NOMINAL) {
          datosReferencia[i][k] /=
              Attributes.getInputAttribute(k).getNominalValuesList().size() - 1;
        } else {
          datosReferencia[i][k] -= Attributes.getInputAttribute(k).getMinAttribute();
          datosReferencia[i][k] /=
              Attributes.getInputAttribute(k).getMaxAttribute()
                  - Attributes.getInputAttribute(k).getMinAttribute();
        }
      }
    }
  }
Exemple #27
0
  /**
   * Init a new set of instances
   *
   * @param nfexamples Name of the dataset file
   * @param train The dataset file is for training or for test
   */
  public ProcDataset(String nfexamples, boolean train) throws IOException {

    try {
      IS = new InstanceSet();
      IS.readSet(nfexamples, train);
      System.out.println("Dataset analyzed: " + Attributes.getRelationName());
    } catch (Exception e) {
      System.out.println("Exception in readSet");
      e.printStackTrace();
    }
  }
Exemple #28
0
  /** Muestra por pantalla la regla actual */
  public void mostrarRegla() {

    Attribute a[] = Attributes.getInputAttributes();
    Attribute s[] = Attributes.getOutputAttributes();

    System.out.print("Regla: ");
    for (int i = 0; i < this.antecedentes.size(); i++) {

      Atributo_valor av = antecedentes.get(i);

      System.out.print(
          "("
              + a[av.getAtributo()].getName()
              + ","
              + a[av.getAtributo()].getNominalValue(av.getValor().intValue())
              + ")");
      if (i < this.antecedentes.size() - 1) System.out.print(" & ");
    }
    System.out.println(" -> (" + s[0].getName() + "," + this.consecuente + ")");
    System.out.println("------------------------------------");
  }
Exemple #29
0
  /**
   * Computes the HVDM distance between two instances
   *
   * @param instance1 First instance
   * @param instance2 Second instance
   * @return The HVDM distance
   */
  private double HVDMDistance(double[] instance1, double[] instance2) {

    double result = 0.0;

    for (int i = 0; i < instance1.length; i++) {
      if (Attributes.getInputAttribute(i).getType() == Attribute.NOMINAL)
        result += nominalDistance[i][real2Nom(instance1[i], i)][real2Nom(instance2[i], i)];
      else result += Math.abs(instance1[i] - instance2[i]) / (4.0 * stdDev[i]);
    }
    result = Math.sqrt(result);

    return result;
  }
Exemple #30
0
  /**
   * It reads the whole input data-set and it stores each transaction in local array
   *
   * @param datasetFile String name of the file containing the data-set
   * @throws IOException If there occurs any problem with the reading of the data-set
   */
  public void readDataSet(String datasetFile) throws IOException {
    int i, j, k;

    try {
      // Load in memory a data-set that contains a Frequent Items Mining problem
      IS.readSet(datasetFile, true);
      this.nTrans = IS.getNumInstances();
      this.nInputs = Attributes.getInputNumAttributes();
      this.nOutputs = Attributes.getOutputNumAttributes();
      this.nVars = this.nInputs + this.nOutputs;

      // Initialize and fill our own tables
      trueTransactions = new double[nTrans][nVars];
      Amplitude = new double[nVars];
      missing = new boolean[nTrans][nVars];

      // Maximum and minimum of inputs
      emax = new double[nVars];
      emin = new double[nVars];
      for (i = 0; i < nVars; i++) {
        if (Attributes.getAttribute(i).getType() != myDataset.NOMINAL) {
          emax[i] = Attributes.getAttribute(i).getMaxAttribute();
          emin[i] = Attributes.getAttribute(i).getMinAttribute();
          this.Amplitude[i] = emax[i] - emin[i];
        } else {
          emin[i] = 0;
          emax[i] = Attributes.getAttribute(i).getNumNominalValues() - 1;
          this.Amplitude[i] = Attributes.getAttribute(i).getNumNominalValues();
        }
      }

      // All values are casted into double/integer
      for (i = 0; i < nTrans; i++) {
        Instance inst = IS.getInstance(i);

        for (j = 0; j < nInputs; j++) {
          trueTransactions[i][j] = IS.getInputNumericValue(i, j);
          missing[i][j] = inst.getInputMissingValues(j);
          if (missing[i][j]) {
            trueTransactions[i][j] = emin[j] - 1;
          }
        }

        if (this.nOutputs > 0) {
          for (k = 0; k < this.nOutputs; k++, j++) {
            trueTransactions[i][j] = IS.getOutputNumericValue(i, k);
          }
        }
      }
    } catch (Exception e) {
      System.out.println("DBG: Exception in readSet");
      e.printStackTrace();
    }
  }