Ejemplo n.º 1
0
  /**
   * This private method extract the dataset and the method's parameters from the KEEL environment,
   * calculates the centroids using the KMeans class and print out the results with the validation
   * dataset.
   *
   * @param tty unused boolean parameter, kept for compatibility
   * @param pc ProcessConfig object to obtain the train and test datasets and the method's
   *     parameters.
   */
  private void clustering_kmeans(boolean tty, ProcessConfig pc) {

    try {

      String linea;
      ProcessDataset pd = new ProcessDataset();

      linea = (String) pc.parInputData.get(ProcessConfig.IndexTrain);

      if (pc.parNewFormat) pd.processClusterDataset(linea, true);
      else pd.procesa_clustering_old(linea);

      int ndatos = pd.getNdata(); // Number of examples
      int nvariables = pd.getNvariables(); // Number of variables
      int nentradas = pd.getNinputs(); // Number of inputs
      pd.showDatasetStatistics();

      System.out.println("Number of examples=" + ndatos);
      System.out.println("Number of inputs=" + nentradas);

      double[][] X = pd.getX(); // Input data

      double[] emaximo = pd.getImaximum(); // Maximum and Minimum for input data
      double[] eminimo = pd.getIminimum();
      int[] neparticion = new int[nentradas];

      int s;
      s = pc.parNClusters;

      KMeans KM = new KMeans(X, s, rand);
      double fallos = 0;
      try {
        for (int i = 0; i < X.length; i++) {
          int clase = KM.nearestCentroid(X[i]);
          // System.out.println("pattern="+i+" cluster="+clase);
        }
      } catch (Exception e) {
        System.out.println(e.toString());
      }

      // Clusters in the test set
      ProcessDataset pdt = new ProcessDataset();
      int nprueba, npentradas, npvariables;
      linea = (String) pc.parInputData.get(ProcessConfig.IndexTestKMeans);

      if (pc.parNewFormat) pdt.processClusterDataset(linea, false);
      else pdt.procesa_clustering_old(linea);

      nprueba = pdt.getNdata();
      npvariables = pdt.getNvariables();
      npentradas = pdt.getNinputs();
      pdt.showDatasetStatistics();

      if (npentradas != nentradas) throw new IOException("Error in test file");

      double[][] Xp = pdt.getX();
      int[] Co = new int[Xp.length];

      // Test set is classified
      try {
        for (int i = 0; i < Xp.length; i++) {
          Co[i] = KM.nearestCentroid(Xp[i]);
          // System.out.println("pattern test="+i+" cluster="+Co[i]);
        }

      } catch (Exception e) {
        System.out.println(e.toString());
      }

      // Output format for clustering algorithms
      pc.results(Xp, Co);
      KM.print();

    } catch (FileNotFoundException e) {
      System.err.println(e + " Training data not found");
    } catch (IOException e) {
      System.err.println(e + " Read error");
    }
  }
Ejemplo n.º 2
0
  /**
   * Main Function
   *
   * @param args the Command line arguments. Only one is processed: the name of the file containing
   *     the
   *     <p>parameters
   */
  public static void main(String[] args) throws IOException {

    double[][] X;

    double[][] Y;

    int nInpt, nOutpl, ndata, i, j;

    Rbfn net;

    try {

      // Help required

      if (args.length > 0) {

        if (args[0].equals("--help")
            || args[0].equals("-help")
            || args[0].equals("-h")
            || args[0].equals("-?")) {

          doHelp();

          return;
        }
      }

      System.out.println("- Executing doRbfnDec " + args.length);

      // Reading parameters

      String paramFile = (args.length > 0) ? args[0] : "parameters.txt";

      setParameters(paramFile);

      System.out.println("    - Parameters file: " + paramFile);

      // Random generator setup

      if (reallySeed) {
        Randomize.setSeed((long) seed);
      }

      // Reading Training dataset

      ProcDataset Dtrn = new ProcDataset(trnFile, true);

      // Training
      System.out.println("Modeling Dataset");
      Dtrn.processModelDataset();
      nInpt = Dtrn.getninputs();
      nOutpl = 1; // PD.getnvariables()-nInpt;
      ndata = Dtrn.getndata();
      Y = new double[ndata][1];
      X = Dtrn.getX();
      double[] auxY;
      auxY = Dtrn.getY();
      for (i = 0; i < ndata; i++) Y[i][0] = auxY[i];
      // Building and training the net
      net = new Rbfn(nNeuronsIni, X, ndata, nInpt, nOutpl);
      net.decremental(X, Y, ndata, percent, alfa);
      double[] obtained = new double[ndata];
      net.testModeling(X, ndata, obtained);
      Dtrn.generateResultsModeling(outTrnFile, auxY, obtained);
      // TEST
      ProcDataset Dtst = new ProcDataset(tstFile, false);
      Dtst.processModelDataset();
      nInpt = Dtst.getninputs();
      nOutpl = 1; // PD.getnvariables()-nInpt;
      ndata = Dtst.getndata();
      X = Dtst.getX();
      auxY = Dtst.getY();
      Y = new double[ndata][1];
      for (i = 0; i < ndata; i++) Y[i][0] = auxY[i];
      obtained = new double[ndata];
      net.testModeling(X, ndata, obtained);
      Dtst.generateResultsModeling(outTstFile, auxY, obtained);
      RBFUtils.createOutputFile(trnFile, outRbfFile);
      net.printRbfn(outRbfFile);

      if (Dtrn.datasetType() == 2) System.out.println("This is not a clustering algorithm");

      System.out.println(
          "- End of doRbfnDec. See results in output files named according to "
              + paramFile
              + " parameters file.");

    } catch (Exception e) {

      throw new InternalError(e.toString());
    }
  }