use of java.awt.GridLayout in project smile by haifengl.
the class RandomProjectionDemo method learn.
@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);
RandomProjection rp = new RandomProjection(data[0].length, 2, sparseBox.isSelected());
System.out.format("%d x %d Random Projection:\n", data[0].length, 3);
double[][] projection = rp.getProjection();
for (int i = 0; i < projection.length; i++) {
for (int j = 0; j < projection[i].length; j++) {
System.out.format("% .4f ", projection[i][j]);
}
System.out.println();
}
y = rp.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("Random Projection");
pane.add(plot);
rp = new RandomProjection(data[0].length, 3, sparseBox.isSelected());
System.out.format("%d x %d Random Projection:\n", data[0].length, 3);
projection = rp.getProjection();
for (int i = 0; i < projection.length; i++) {
for (int j = 0; j < projection[i].length; j++) {
System.out.format("% .4f ", projection[i][j]);
}
System.out.println();
}
y = rp.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("Random Projection");
pane.add(plot);
return pane;
}
use of java.awt.GridLayout in project smile by haifengl.
the class SammonMappingDemo method learn.
/**
* Execute the MDS algorithm and return a swing JComponent representing
* the clusters.
*/
public JComponent learn() {
JPanel pane = new JPanel(new GridLayout(1, 2));
double[][] data = dataset[datasetIndex].toArray(new double[dataset[datasetIndex].size()][]);
String[] labels = dataset[datasetIndex].toArray(new String[dataset[datasetIndex].size()]);
if (labels[0] == null) {
Attribute[] attr = dataset[datasetIndex].attributes();
labels = new String[attr.length];
for (int i = 0; i < labels.length; i++) {
labels[i] = attr[i].getName();
}
}
long clock = System.currentTimeMillis();
SammonMapping sammon = new SammonMapping(data, 2);
System.out.format("Learn Sammon's Mapping (k=2) from %d samples in %dms\n", data.length, System.currentTimeMillis() - clock);
PlotCanvas plot = ScatterPlot.plot(sammon.getCoordinates(), labels);
plot.setTitle("Sammon's Mapping (k = 2)");
pane.add(plot);
clock = System.currentTimeMillis();
sammon = new SammonMapping(data, 3);
System.out.format("Learn Sammon's Mapping (k=3) from %d samples in %dms\n", data.length, System.currentTimeMillis() - clock);
plot = ScatterPlot.plot(sammon.getCoordinates(), labels);
plot.setTitle("Sammon's Mapping (k = 3)");
pane.add(plot);
return pane;
}
use of java.awt.GridLayout in project smile by haifengl.
the class IsoMapDemo method learn.
@Override
public JComponent learn() {
JPanel pane = new JPanel(new GridLayout(1, 2));
double[][] data = dataset[datasetIndex].toArray(new double[dataset[datasetIndex].size()][]);
if (data.length > 1000) {
double[][] x = new double[1000][];
for (int i = 0; i < 1000; i++) x[i] = data[i];
data = x;
}
long clock = System.currentTimeMillis();
IsoMap isomap = new IsoMap(data, 2, k);
System.out.format("Learn IsoMap from %d samples in %dms\n", data.length, System.currentTimeMillis() - clock);
double[][] y = isomap.getCoordinates();
PlotCanvas plot = new PlotCanvas(Math.colMin(y), Math.colMax(y));
plot.points(y, 'o', Color.RED);
int n = y.length;
Graph graph = isomap.getNearestNeighborGraph();
for (int i = 0; i < n; i++) {
for (int j = 0; j < i; j++) {
if (graph.hasEdge(i, j)) {
plot.line(y[i], y[j]);
}
}
}
plot.setTitle("IsoMap");
pane.add(plot);
return pane;
}
use of java.awt.GridLayout in project smile by haifengl.
the class LLEDemo method learn.
@Override
public JComponent learn() {
JPanel pane = new JPanel(new GridLayout(1, 2));
double[][] data = dataset[datasetIndex].toArray(new double[dataset[datasetIndex].size()][]);
if (data.length > 1000) {
double[][] x = new double[1000][];
for (int i = 0; i < 1000; i++) x[i] = data[i];
data = x;
}
long clock = System.currentTimeMillis();
LLE lle = new LLE(data, 2, k);
System.out.format("Learn LLE from %d samples in %dms\n", data.length, System.currentTimeMillis() - clock);
double[][] y = lle.getCoordinates();
PlotCanvas plot = new PlotCanvas(Math.colMin(y), Math.colMax(y));
plot.points(y, 'o', Color.RED);
int n = y.length;
Graph graph = lle.getNearestNeighborGraph();
for (int i = 0; i < n; i++) {
for (int j = 0; j < i; j++) {
if (graph.hasEdge(i, j)) {
plot.line(y[i], y[j]);
}
}
}
plot.setTitle("LLE");
pane.add(plot);
return pane;
}
use of java.awt.GridLayout in project smile by haifengl.
the class GHADemo method learn.
@Override
public JComponent learn() {
JPanel pane = new JPanel(new GridLayout(2, 2));
double[][] data = Math.clone(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);
clock = System.currentTimeMillis();
GHA gha = new GHA(data[0].length, 2, 0.00001);
for (int iter = 1; iter <= 500; iter++) {
double error = 0.0;
for (int i = 0; i < data.length; i++) {
error += gha.learn(data[i]);
}
error /= data.length;
if (iter % 100 == 0) {
System.out.format("Iter %3d, Error = %.5g\n", iter, error);
}
}
System.out.format("Learn GHA from %d samples in %dms\n", data.length, System.currentTimeMillis() - clock);
y = gha.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("GHA");
pane.add(plot);
clock = System.currentTimeMillis();
gha = new GHA(data[0].length, 3, 0.00001);
for (int iter = 1; iter <= 500; iter++) {
double error = 0.0;
for (int i = 0; i < data.length; i++) {
error += gha.learn(data[i]);
}
error /= data.length;
if (iter % 100 == 0) {
System.out.format("Iter %3d, Error = %.5g\n", iter, error);
}
}
System.out.format("Learn GHA from %d samples in %dms\n", data.length, System.currentTimeMillis() - clock);
y = gha.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("GHA");
pane.add(plot);
return pane;
}
Aggregations