Example #1
0
  @Override
  public JComponent learn() {
    JPanel pane = new JPanel(new GridLayout(2, 2));
    double[][] data = dataset[datasetIndex].toArray(new double[dataset[datasetIndex].size()][]);
    String[] names = dataset[datasetIndex].toArray(new String[dataset[datasetIndex].size()]);
    if (names[0] == null) {
      names = null;
    }

    long clock = System.currentTimeMillis();
    PCA pca = new PCA(data, true);
    System.out.format(
        "Learn PCA from %d samples in %dms\n", data.length, System.currentTimeMillis() - clock);

    pca.setProjection(2);
    double[][] y = pca.project(data);

    PlotCanvas plot = new PlotCanvas(Math.colMin(y), Math.colMax(y));
    if (names != null) {
      plot.points(y, names);
    } else if (dataset[datasetIndex].response() != null) {
      int[] labels = dataset[datasetIndex].toArray(new int[dataset[datasetIndex].size()]);
      for (int i = 0; i < y.length; i++) {
        plot.point(pointLegend, Palette.COLORS[labels[i]], y[i]);
      }
    } else {
      plot.points(y, pointLegend);
    }

    plot.setTitle("PCA");
    pane.add(plot);

    pca.setProjection(3);
    y = pca.project(data);

    plot = new PlotCanvas(Math.colMin(y), Math.colMax(y));
    if (names != null) {
      plot.points(y, names);
    } else if (dataset[datasetIndex].response() != null) {
      int[] labels = dataset[datasetIndex].toArray(new int[dataset[datasetIndex].size()]);
      for (int i = 0; i < y.length; i++) {
        plot.point(pointLegend, Palette.COLORS[labels[i]], y[i]);
      }
    } else {
      plot.points(y, pointLegend);
    }

    plot.setTitle("PCA");
    pane.add(plot);

    PPCA ppca = new PPCA(data, 2);
    y = ppca.project(data);
    plot = new PlotCanvas(Math.colMin(y), Math.colMax(y));
    if (names != null) {
      plot.points(y, names);
    } else if (dataset[datasetIndex].response() != null) {
      int[] labels = dataset[datasetIndex].toArray(new int[dataset[datasetIndex].size()]);
      for (int i = 0; i < y.length; i++) {
        plot.point(pointLegend, Palette.COLORS[labels[i]], y[i]);
      }
    } else {
      plot.points(y, pointLegend);
    }

    plot.setTitle("PPCA");
    pane.add(plot);

    clock = System.currentTimeMillis();
    ppca = new PPCA(data, 3);
    System.out.format(
        "Learn PPCA from %d samples in %dms\n", data.length, System.currentTimeMillis() - clock);
    y = ppca.project(data);
    plot = new PlotCanvas(Math.colMin(y), Math.colMax(y));
    if (names != null) {
      plot.points(y, names);
    } else if (dataset[datasetIndex].response() != null) {
      int[] labels = dataset[datasetIndex].toArray(new int[dataset[datasetIndex].size()]);
      for (int i = 0; i < y.length; i++) {
        plot.point(pointLegend, Palette.COLORS[labels[i]], y[i]);
      }
    } else {
      plot.points(y, pointLegend);
    }

    plot.setTitle("PPCA");
    pane.add(plot);

    return pane;
  }
Example #2
0
  public ScatterPlotDemo() {
    super(new GridLayout(1, 2));

    double[][] data = new double[10][2];
    for (int j = 0; j < data.length; j++) {
      data[j][0] = 10000 + Math.random();
      data[j][1] = Math.random();
    }

    PlotCanvas canvas = ScatterPlot.plot(data);
    canvas.setTitle("2D Scatter Plot");
    canvas.setAxisLabels("X Axis", "A Long Label");
    add(canvas);

    data = new double[10][2];
    for (int j = 0; j < data.length; j++) {
      data[j][0] = 10001 + Math.random();
      data[j][1] = 1 + Math.random();
    }
    canvas.points(data, '*', Color.RED);

    data = new double[10][2];
    for (int j = 0; j < data.length; j++) {
      data[j][0] = 10002 + Math.random();
      data[j][1] = Math.random();
    }
    canvas.points(data, '+', Color.BLUE);

    data = new double[10][2];
    for (int j = 0; j < data.length; j++) {
      data[j][0] = 10000 + Math.random();
      data[j][1] = 2 + Math.random();
    }
    canvas.points(data, '-', Color.GREEN);

    data = new double[10][2];
    for (int j = 0; j < data.length; j++) {
      data[j][0] = 10001 + Math.random();
      data[j][1] = Math.random();
    }
    canvas.points(data, 'x', Color.CYAN);

    data = new double[10][2];
    for (int j = 0; j < data.length; j++) {
      data[j][0] = 10000 + Math.random();
      data[j][1] = 1 + Math.random();
    }
    canvas.points(data, '@', Color.MAGENTA);

    data = new double[10][2];
    for (int j = 0; j < data.length; j++) {
      data[j][0] = 10002 + Math.random();
      data[j][1] = Math.random();
    }
    canvas.points(data, 's', Color.ORANGE);

    data = new double[10][2];
    for (int j = 0; j < data.length; j++) {
      data[j][0] = 10000 + Math.random();
      data[j][1] = 2 + Math.random();
    }
    canvas.points(data, 'S', Color.PINK);

    data = new double[10][2];
    for (int j = 0; j < data.length; j++) {
      data[j][0] = 10002 + Math.random();
      data[j][1] = 2 + Math.random();
    }
    canvas.points(data, 'a', Color.YELLOW);

    data = new double[10][2];
    for (int j = 0; j < data.length; j++) {
      data[j][0] = 10001.5 + Math.random();
      data[j][1] = 1.5 + Math.random();
    }
    canvas.points(data, 'A', Color.DARK_GRAY);

    data = new double[100][2];
    for (int j = 0; j < data.length; j++) {
      data[j][0] = 10000 + 3 * Math.random();
      data[j][1] = 3 * Math.random();
    }
    canvas.points(data, '.', Color.BLACK);

    data = new double[100][3];
    for (int j = 0; j < data.length; j++) {
      data[j][0] = Math.random();
      data[j][1] = Math.random();
      data[j][2] = Math.random();
    }

    PlotCanvas canvas3d = ScatterPlot.plot(data);
    canvas3d.setTitle("3D Scatter Plot");
    add(canvas3d);
  }