use of smile.plot.PlotCanvas 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 smile.plot.PlotCanvas 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 smile.plot.PlotCanvas 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 smile.plot.PlotCanvas 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;
}
use of smile.plot.PlotCanvas in project smile by haifengl.
the class BIRCHDemo method learn.
@Override
public JComponent learn() {
try {
B = Integer.parseInt(BNumberField.getText().trim());
if (B < 2) {
JOptionPane.showMessageDialog(this, "Invalid B: " + B, ERROR, JOptionPane.ERROR_MESSAGE);
return null;
}
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "Invalid B: " + BNumberField.getText(), ERROR, JOptionPane.ERROR_MESSAGE);
return null;
}
try {
T = Double.parseDouble(TNumberField.getText().trim());
if (T <= 0) {
JOptionPane.showMessageDialog(this, "Invalid T: " + T, ERROR, JOptionPane.ERROR_MESSAGE);
return null;
}
} catch (Exception e) {
JOptionPane.showMessageDialog(this, "Invalid T: " + TNumberField.getText(), ERROR, JOptionPane.ERROR_MESSAGE);
return null;
}
try {
minPts = Integer.parseInt(minPtsNumberField.getText().trim());
if (minPts < 0) {
JOptionPane.showMessageDialog(this, "Invalid minPts: " + minPts, ERROR, JOptionPane.ERROR_MESSAGE);
return null;
}
} catch (Exception e) {
JOptionPane.showMessageDialog(this, "Invalid minPts: " + minPtsNumberField.getText(), ERROR, JOptionPane.ERROR_MESSAGE);
return null;
}
long clock = System.currentTimeMillis();
BIRCH birch = new BIRCH(2, B, T);
for (int i = 0; i < dataset[datasetIndex].length; i++) birch.add(dataset[datasetIndex][i]);
if (birch.partition(clusterNumber, minPts) < clusterNumber) {
JOptionPane.showMessageDialog(this, "The number of non-outlier leaves is less than " + clusterNumber + ". Try larger T.", "ERROR", JOptionPane.ERROR_MESSAGE);
return null;
}
int[] membership = new int[dataset[datasetIndex].length];
int[] clusterSize = new int[clusterNumber];
for (int i = 0; i < dataset[datasetIndex].length; i++) {
membership[i] = birch.predict(dataset[datasetIndex][i]);
if (membership[i] != Clustering.OUTLIER) {
clusterSize[membership[i]]++;
}
}
System.out.format("BIRCH clusterings %d samples in %dms\n", dataset[datasetIndex].length, System.currentTimeMillis() - clock);
PlotCanvas plot = ScatterPlot.plot(birch.centroids(), '@');
for (int k = 0; k < clusterNumber; k++) {
if (clusterSize[k] > 0) {
double[][] cluster = new double[clusterSize[k]][];
for (int i = 0, j = 0; i < dataset[datasetIndex].length; i++) {
if (membership[i] == k) {
cluster[j++] = dataset[datasetIndex][i];
}
}
plot.points(cluster, pointLegend, Palette.COLORS[k % Palette.COLORS.length]);
}
}
plot.points(birch.centroids(), '@');
return plot;
}
Aggregations