use of smile.plot.PlotCanvas in project smile by haifengl.
the class GrowingNeuralGasDemo method learn.
@Override
public JComponent learn() {
long clock = System.currentTimeMillis();
GrowingNeuralGas gas = new GrowingNeuralGas(2, 0.05, 0.0006, 88, 200, 0.5, 0.9995);
for (int loop = 0; loop < 25; loop++) {
for (double[] x : dataset[datasetIndex]) {
gas.update(x);
}
}
gas.partition(clusterNumber);
System.out.format("Growing Neural Gas clusterings %d samples in %dms\n", dataset[datasetIndex].length, System.currentTimeMillis() - clock);
int[] membership = new int[dataset[datasetIndex].length];
int[] clusterSize = new int[clusterNumber];
for (int i = 0; i < dataset[datasetIndex].length; i++) {
membership[i] = gas.predict(dataset[datasetIndex][i]);
clusterSize[membership[i]]++;
}
GrowingNeuralGas.Neuron[] neurons = gas.neurons();
double[][] x = new double[neurons.length][];
for (int i = 0; i < x.length; i++) x[i] = neurons[i].w;
PlotCanvas plot = ScatterPlot.plot(x, '@');
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]);
}
}
for (int i = 0; i < neurons.length; i++) {
for (int j = 0; j < neurons[i].neighbors.length; j++) {
plot.line(neurons[i].w, neurons[i].neighbors[j].w);
}
}
plot.points(x, '@');
return plot;
}
use of smile.plot.PlotCanvas in project smile by haifengl.
the class ClassificationDemo method run.
@Override
public void run() {
startButton.setEnabled(false);
datasetBox.setEnabled(false);
double[][] data = dataset[datasetIndex].toArray(new double[dataset[datasetIndex].size()][]);
int[] label = dataset[datasetIndex].toArray(new int[dataset[datasetIndex].size()]);
if (data.length < 500) {
pointLegend = 'o';
} else {
pointLegend = '.';
}
PlotCanvas canvas = ScatterPlot.plot(data, pointLegend);
for (int i = 0; i < data.length; i++) {
canvas.point(pointLegend, Palette.COLORS[label[i]], data[i]);
}
double[] lower = canvas.getLowerBounds();
double[] upper = canvas.getUpperBounds();
double[] x = new double[50];
double step = (upper[0] - lower[0]) / x.length;
for (int i = 0; i < x.length; i++) {
x[i] = lower[0] + step * (i + 1);
}
double[] y = new double[50];
step = (upper[1] - lower[1]) / y.length;
for (int i = 0; i < y.length; i++) {
y[i] = lower[1] + step * (i + 1);
}
try {
double[][] f = learn(x, y);
if (f != null) {
for (int i = 0; i < y.length; i++) {
for (int j = 0; j < x.length; j++) {
double[] p = { x[j], y[i] };
canvas.point('.', Palette.COLORS[(int) f[i][j]], p);
}
}
double[] levels = { 0.5 };
Contour contour = new Contour(x, y, f, levels);
contour.showLevelValue(false);
canvas.add(contour);
BorderLayout layout = (BorderLayout) getLayout();
remove(layout.getLayoutComponent(BorderLayout.CENTER));
add(canvas, BorderLayout.CENTER);
validate();
}
} catch (Exception ex) {
System.err.println(ex);
}
startButton.setEnabled(true);
datasetBox.setEnabled(true);
}
use of smile.plot.PlotCanvas in project smile by haifengl.
the class ClassificationDemo method ancestorAdded.
@Override
public void ancestorAdded(AncestorEvent event) {
if (datasetBox.getSelectedIndex() != datasetIndex) {
datasetBox.setSelectedIndex(datasetIndex);
double[][] data = dataset[datasetIndex].toArray(new double[dataset[datasetIndex].size()][]);
int[] label = dataset[datasetIndex].toArray(new int[dataset[datasetIndex].size()]);
PlotCanvas canvas = ScatterPlot.plot(data, 'o');
for (int i = 0; i < data.length; i++) {
canvas.point('o', Palette.COLORS[label[i]], data[i]);
}
BorderLayout layout = (BorderLayout) getLayout();
remove(layout.getLayoutComponent(BorderLayout.CENTER));
add(canvas, BorderLayout.CENTER);
validate();
}
}
use of smile.plot.PlotCanvas in project smile by haifengl.
the class IsotonicMDSDemo 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();
IsotonicMDS isomds = new IsotonicMDS(data, 2);
System.out.format("Learn Kruskal's Nonmetric MDS (k=2) from %d samples in %dms\n", data.length, System.currentTimeMillis() - clock);
PlotCanvas plot = ScatterPlot.plot(isomds.getCoordinates(), labels);
plot.setTitle("Kruskal's Nonmetric MDS (k = 2)");
pane.add(plot);
clock = System.currentTimeMillis();
isomds = new IsotonicMDS(data, 3);
System.out.format("Learn Kruskal's Nonmetric MDS (k=3) from %d samples in %dms\n", data.length, System.currentTimeMillis() - clock);
plot = ScatterPlot.plot(isomds.getCoordinates(), labels);
plot.setTitle("Kruskal's Nonmetric MDS (k = 3)");
pane.add(plot);
return pane;
}
use of smile.plot.PlotCanvas in project smile by haifengl.
the class MDSDemo 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();
MDS mds = new MDS(data, 2);
System.out.format("Learn MDS (k=2) from %d samples in %dms\n", data.length, System.currentTimeMillis() - clock);
PlotCanvas plot = ScatterPlot.plot(mds.getCoordinates(), labels);
plot.setTitle("MDS (k = 2)");
pane.add(plot);
clock = System.currentTimeMillis();
mds = new MDS(data, 3);
System.out.format("Learn MDS (k=3) from %d samples in %dms\n", data.length, System.currentTimeMillis() - clock);
plot = ScatterPlot.plot(mds.getCoordinates(), labels);
plot.setTitle("MDS (k = 3)");
pane.add(plot);
return pane;
}
Aggregations