use of smile.plot.PlotCanvas in project smile by haifengl.
the class ClassificationDemo method actionPerformed.
@Override
public void actionPerformed(ActionEvent e) {
if ("startButton".equals(e.getActionCommand())) {
Thread thread = new Thread(this);
thread.start();
} else if ("datasetBox".equals(e.getActionCommand())) {
datasetIndex = datasetBox.getSelectedIndex();
if (dataset[datasetIndex] == null) {
DelimitedTextParser parser = new DelimitedTextParser();
parser.setDelimiter("[\t ]+");
parser.setResponseIndex(new NominalAttribute("class"), 0);
try {
dataset[datasetIndex] = parser.parse(datasetName[datasetIndex], smile.data.parser.IOUtils.getTestDataFile(datasource[datasetIndex]));
} catch (Exception ex) {
JOptionPane.showMessageDialog(null, "Failed to load dataset.", "ERROR", JOptionPane.ERROR_MESSAGE);
System.err.println(ex);
}
}
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]);
}
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 CLARANSDemo method learn.
@Override
public JComponent learn() {
try {
numLocal = Integer.parseInt(numLocalField.getText().trim());
if (numLocal < 5) {
JOptionPane.showMessageDialog(this, "Toll smal NumLocal: " + numLocal, ERROR, JOptionPane.ERROR_MESSAGE);
return null;
}
} catch (Exception e) {
JOptionPane.showMessageDialog(this, "Invalid NumLocal: " + numLocalField.getText(), ERROR, JOptionPane.ERROR_MESSAGE);
return null;
}
try {
maxNeighbor = Integer.parseInt(maxNeighborField.getText().trim());
if (maxNeighbor < 5) {
JOptionPane.showMessageDialog(this, "Too small MaxNeighbor: " + maxNeighbor, ERROR, JOptionPane.ERROR_MESSAGE);
return null;
}
} catch (Exception e) {
JOptionPane.showMessageDialog(this, "Invalid MaxNeighbor: " + maxNeighborField.getText(), ERROR, JOptionPane.ERROR_MESSAGE);
return null;
}
long clock = System.currentTimeMillis();
CLARANS<double[]> clarans = new CLARANS<>(dataset[datasetIndex], new EuclideanDistance(), clusterNumber, maxNeighbor, numLocal);
System.out.format("CLARANS clusterings %d samples in %dms\n", dataset[datasetIndex].length, System.currentTimeMillis() - clock);
PlotCanvas plot = ScatterPlot.plot(clarans.medoids(), '@');
for (int k = 0; k < clusterNumber; k++) {
if (clarans.getClusterSize()[k] > 0) {
double[][] cluster = new double[clarans.getClusterSize()[k]][];
for (int i = 0, j = 0; i < dataset[datasetIndex].length; i++) {
if (clarans.getClusterLabel()[i] == k) {
cluster[j++] = dataset[datasetIndex][i];
}
}
plot.points(cluster, pointLegend, Palette.COLORS[k % Palette.COLORS.length]);
}
}
plot.points(clarans.medoids(), '@');
return plot;
}
use of smile.plot.PlotCanvas in project smile by haifengl.
the class DeterministicAnnealingDemo method learn.
@Override
public JComponent learn() {
try {
alpha = Double.parseDouble(alphaField.getText().trim());
if (alpha <= 0 || alpha >= 1) {
JOptionPane.showMessageDialog(this, "Invalid alpha: " + alpha, "Error", JOptionPane.ERROR_MESSAGE);
return null;
}
} catch (Exception e) {
JOptionPane.showMessageDialog(this, "Invalid K: " + alphaField.getText(), "Error", JOptionPane.ERROR_MESSAGE);
return null;
}
long clock = System.currentTimeMillis();
DeterministicAnnealing annealing = new DeterministicAnnealing(dataset[datasetIndex], clusterNumber, alpha);
System.out.format("Deterministic Annealing clusterings %d samples in %dms\n", dataset[datasetIndex].length, System.currentTimeMillis() - clock);
PlotCanvas plot = ScatterPlot.plot(annealing.centroids(), '@');
for (int k = 0; k < clusterNumber; k++) {
if (annealing.getClusterSize()[k] > 0) {
double[][] cluster = new double[annealing.getClusterSize()[k]][];
for (int i = 0, j = 0; i < dataset[datasetIndex].length; i++) {
if (annealing.getClusterLabel()[i] == k) {
cluster[j++] = dataset[datasetIndex][i];
}
}
plot.points(cluster, pointLegend, Palette.COLORS[k % Palette.COLORS.length]);
}
}
plot.points(annealing.centroids(), '@');
return plot;
}
use of smile.plot.PlotCanvas in project smile by haifengl.
the class SOMDemo method learn.
@Override
public JComponent learn() {
try {
width = Integer.parseInt(widthField.getText().trim());
if (width < 1) {
JOptionPane.showMessageDialog(this, "Invalid width: " + width, "Error", JOptionPane.ERROR_MESSAGE);
return null;
}
} catch (Exception e) {
JOptionPane.showMessageDialog(this, "Invalid width: " + widthField.getText(), "Error", JOptionPane.ERROR_MESSAGE);
return null;
}
try {
height = Integer.parseInt(heightField.getText().trim());
if (height < 1) {
JOptionPane.showMessageDialog(this, "Invalid height: " + height, "Error", JOptionPane.ERROR_MESSAGE);
return null;
}
} catch (Exception e) {
JOptionPane.showMessageDialog(this, "Invalid height: " + heightField.getText(), "Error", JOptionPane.ERROR_MESSAGE);
return null;
}
long clock = System.currentTimeMillis();
SOM som = new SOM(dataset[datasetIndex], width, height);
System.out.format("SOM clusterings %d samples in %dms\n", dataset[datasetIndex].length, System.currentTimeMillis() - clock);
JPanel pane = new JPanel(new GridLayout(2, 3));
PlotCanvas plot = ScatterPlot.plot(dataset[datasetIndex], pointLegend);
plot.grid(som.map());
plot.setTitle("SOM Grid");
pane.add(plot);
int[] membership = som.partition(clusterNumber);
int[] clusterSize = new int[clusterNumber];
for (int i = 0; i < membership.length; i++) {
clusterSize[membership[i]]++;
}
plot = ScatterPlot.plot(dataset[datasetIndex], pointLegend);
plot.setTitle("Hierarchical Clustering");
for (int k = 0; k < clusterNumber; k++) {
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]);
}
pane.add(plot);
double[][] umatrix = som.umatrix();
double[] umatrix1 = new double[umatrix.length * umatrix[0].length];
for (int i = 0, k = 0; i < umatrix.length; i++) {
for (int j = 0; j < umatrix[i].length; j++, k++) umatrix1[k] = umatrix[i][j];
}
plot = Histogram.plot(null, umatrix1, 20);
plot.setTitle("U-Matrix Histogram");
pane.add(plot);
GaussianMixture mixture = new GaussianMixture(umatrix1);
double w = (Math.max(umatrix1) - Math.min(umatrix1)) / 24;
double[][] p = new double[50][2];
for (int i = 0; i < p.length; i++) {
p[i][0] = Math.min(umatrix1) + i * w;
p[i][1] = mixture.p(p[i][0]) * w;
}
plot.line(p, Color.RED);
plot = Hexmap.plot(umatrix, Palette.jet(256));
plot.setTitle("U-Matrix");
pane.add(plot);
/*
double[][] x = new double[height][width];
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
x[i][j] = som.getMap()[i][j][0];
}
}
plot = PlotCanvas.hexmap(x, Palette.jet(256));
plot.setTitle("X");
pane.add(plot);
double[][] y = new double[height][width];
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
y[i][j] = som.getMap()[i][j][1];
}
}
plot = PlotCanvas.hexmap(y, Palette.jet(256));
plot.setTitle("Y");
pane.add(plot);
*/
return pane;
}
use of smile.plot.PlotCanvas in project smile by haifengl.
the class PCADemo method learn.
@Override
public JComponent learn() {
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;
}
boolean cor = corBox.getSelectedIndex() != 0;
long clock = System.currentTimeMillis();
PCA pca = new PCA(data, cor);
System.out.format("Learn PCA from %d samples in %dms\n", data.length, System.currentTimeMillis() - clock);
JPanel pane = new JPanel(new GridLayout(1, 2));
PlotCanvas scree = PlotCanvas.screeplot(pca);
scree.setTitle("Variance");
pane.add(scree);
pca.setProjection(3);
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("Scatter Plot");
pane.add(plot);
return pane;
}
Aggregations