/**
   * sdf The test main
   *
   * @param args ignored
   */
  public static void main(String[] args) {
    //        Instance[] instances =  new Instance[100];
    //        for (int i = 0; i < instances.length; i++) {
    //            double[] data = new double[2];
    //            data[0] = Math.sin(i/2.0);
    //            data[1] = (Math.random() - .5)*2;
    //            instances[i] = new Instance(data);
    //        }
    DataSet set = new DataSet(trainInstances);
    System.out.println("Before randomizing");
    System.out.println(set);
    //        Matrix projection = new RectangularMatrix(new double[][]{ {.6, .6}, {.4, .6}});
    Matrix projection = new RectangularMatrix(new double[][] {{.1, .1}, {.1, .1}});

    for (int i = 0; i < set.size(); i++) {
      Instance instance = set.get(i);
      instance.setData(projection.times(instance.getData()));
    }
    System.out.println("Before ICA");
    System.out.println(set);
    IndependentComponentAnalysis filter = new IndependentComponentAnalysis(set, 1);
    filter.filter(set);
    System.out.println("After ICA");
    System.out.println(set);
  }
  private static Instance[] initializeInstances(String dataFile, String labelFile) {

    // DataSetReader dsr = new CSVDataSetReader(new File("").getAbsolutePath() + "/src/opt/test/" +
    // dataFile);
    // DataSetReader lsr = new CSVDataSetReader(new File("").getAbsolutePath() + "/src/opt/test/" +
    // labelFile);

    URL d_path = IndepenentComponentAnalysisMyDataTest.class.getResource(dataFile);
    File df = new File(d_path.getFile());
    URL l_path = IndepenentComponentAnalysisMyDataTest.class.getResource(dataFile);
    File lf = new File(l_path.getFile());

    DataSetReader dsr = new CSVDataSetReader(df.toString());
    DataSetReader lsr = new CSVDataSetReader(lf.toString());
    DataSet ds;
    DataSet labs;

    try {
      ds = dsr.read();
      labs = lsr.read();
      Instance[] instances = ds.getInstances();
      Instance[] labels = labs.getInstances();

      //            for(int i = 0; i < instances.length; i++) {
      //                instances[i].setLabel(new Instance(labels[i].getData().get(0)));
      //            	//instances[i].setLabel(new Instance(labels[i].getData()));
      //            }

      return instances;
    } catch (Exception e) {
      System.out.println("Failed to read input file");
      return null;
    }
  }
Example #3
0
 private static DataSet run(RandomizedProjectionFilter rca, DataSet original) {
   System.out.println("The projection matrix");
   System.out.println(rca.getProjection());
   DataSet transformed = original.copy();
   rca.filter(transformed);
   return transformed;
 }
Example #4
0
 private static void writeArffHeader(DataSet dataSet, String outputFile) {
   MLAssignmentUtils.writeToFile(outputFile, "@Relation RCATransformed\n", false);
   Vector vector = dataSet.get(0).getData();
   for (int i = 0; i < vector.size(); i++) {
     MLAssignmentUtils.writeToFile(outputFile, "@attribute attr" + (i + 1) + " numeric\n", true);
   }
   MLAssignmentUtils.writeToFile(outputFile, "@data\n", true);
 }
Example #5
0
 private static void computeReconstructionError(DataSet original, DataSet reconstructed) {
   EuclideanDistance distanceFunction = new EuclideanDistance();
   double totalDataSetsDistance = distanceFunction.value(original, reconstructed);
   double averageInstanceDistane = totalDataSetsDistance / original.size();
   System.out.println(
       "Total reconstruction error as Euclidean distance between data sets: "
           + totalDataSetsDistance);
   System.out.println("Average Instance reconstruction error: " + averageInstanceDistane);
 }
Example #6
0
 private static void saveAsArff(DataSet dataSet, String outputFile, List<String> labels) {
   writeArffHeader(dataSet, outputFile);
   Instance[] instances = dataSet.getInstances();
   for (Instance instance : instances) {
     Vector vector = instance.getData();
     for (int i = 0; i < vector.size(); i++) {
       MLAssignmentUtils.writeToFile(outputFile, String.valueOf(vector.get(i)) + ",", true);
     }
     MLAssignmentUtils.writeToFile(outputFile, getLabel(instance.getLabel(), labels) + "\n", true);
   }
 }
Example #7
0
  public void filter(DataSet data) {
    int foldSize = data.size() / foldCount;
    Random rand = new Random();

    for (int currentFold = 0; currentFold < foldCount; currentFold++) {
      DataSet currentSet = new DataSet(new Instance[foldSize], data.getDescription());
      int i = 0;
      while (i < foldSize) {
        int position = rand.nextInt(data.size());
        Instance instance = data.get(position);
        if (instance != null && instance.getData() != null) {
          currentSet.set(i, instance);
          data.set(position, null);
          i++;
        }
      }
      this.folds.add(currentSet);
    }
  }
  public void run(int iterations) throws Exception {
    // 1) Construct data instances for training.  These will also be run
    //    through the network at the bottom to verify the output
    CSVDataSetReader reader = new CSVDataSetReader("data/letter_training_new.data");
    DataSet set = reader.read();
    LabelSplitFilter flt = new LabelSplitFilter();
    flt.filter(set);
    DataSetLabelBinarySeperator.seperateLabels(set);
    DataSetDescription desc = set.getDescription();
    DataSetDescription labelDesc = desc.getLabelDescription();

    // 2) Instantiate a network using the FeedForwardNeuralNetworkFactory.  This network
    //    will be our classifier.
    FeedForwardNeuralNetworkFactory factory = new FeedForwardNeuralNetworkFactory();
    // 2a) These numbers correspond to the number of nodes in each layer.
    //     This network has 4 input nodes, 3 hidden nodes in 1 layer, and 1 output node in the
    // output layer.
    FeedForwardNetwork network =
        factory.createClassificationNetwork(
            new int[] {
              desc.getAttributeCount(),
              factory.getOptimalHiddenLayerNodes(desc, labelDesc),
              labelDesc.getDiscreteRange()
            });

    // 3) Instantiate a measure, which is used to evaluate each possible set of weights.
    ErrorMeasure measure = new SumOfSquaresError();

    // 4) Instantiate a DataSet, which adapts a set of instances to the optimization problem.
    // DataSet set = new DataSet(patterns);

    // 5) Instantiate an optimization problem, which is used to specify the dataset, evaluation
    //    function, mutator and crossover function (for Genetic Algorithms), and any other
    //    parameters used in optimization.
    NeuralNetworkOptimizationProblem nno =
        new NeuralNetworkOptimizationProblem(set, network, measure);

    // 6) Instantiate a specific OptimizationAlgorithm, which defines how we pick our next potential
    //    hypothesis.
    OptimizationAlgorithm o = new RandomizedHillClimbing(nno);

    // 7) Instantiate a trainer.  The FixtIterationTrainer takes another trainer (in this case,
    //    an OptimizationAlgorithm) and executes it a specified number of times.
    FixedIterationTrainer fit = new FixedIterationTrainer(o, iterations);

    // 8) Run the trainer.  This may take a little while to run, depending on the
    // OptimizationAlgorithm,
    //    size of the data, and number of iterations.
    fit.train();

    // 9) Once training is done, get the optimal solution from the OptimizationAlgorithm.  These are
    // the
    //    optimal weights found for this network.
    Instance opt = o.getOptimal();
    network.setWeights(opt.getData());

    // 10) Run the training data through the network with the weights discovered through
    // optimization, and
    //    print out the expected label and result of the classifier for each instance.
    int[] labels = {0, 1};
    TestMetric acc = new AccuracyTestMetric();
    TestMetric cm = new ConfusionMatrixTestMetric(labels);
    Tester t = new NeuralNetworkTester(network, acc, cm);
    t.test(set.getInstances());

    acc.printResults();
  }
Example #9
0
 private static DataSet reconstruct(RandomizedProjectionFilter rca, DataSet transformed) {
   DataSet reconstructed = transformed.copy();
   rca.reverse(reconstructed);
   return reconstructed;
 }