Search in sources :

Example 86 with GridLayout

use of java.awt.GridLayout 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;
}
Also used : JPanel(javax.swing.JPanel) GridLayout(java.awt.GridLayout) Attribute(smile.data.Attribute) MDS(smile.mds.MDS) PlotCanvas(smile.plot.PlotCanvas)

Example 87 with GridLayout

use of java.awt.GridLayout in project smile by haifengl.

the class ApproximateStringSearchDemo method run.

@Override
public void run() {
    startButton.setEnabled(false);
    knnField.setEnabled(false);
    if (data == null) {
        System.out.print("Loading dataset...");
        List<String> words = new ArrayList<>();
        try {
            FileInputStream stream = new FileInputStream(smile.data.parser.IOUtils.getTestDataFile("index.noun"));
            BufferedReader input = new BufferedReader(new InputStreamReader(stream));
            String line = input.readLine();
            while (line != null) {
                if (!line.startsWith(" ")) {
                    String[] w = line.split("\\s");
                    words.add(w[0].replace('_', ' '));
                }
                line = input.readLine();
            }
        } catch (Exception e) {
            System.err.println(e);
        }
        data = words.toArray(new String[1]);
        System.out.println(words.size() + " words");
        System.out.println("Building searching data structure...");
        long time = System.currentTimeMillis();
        naive = new LinearSearch<>(data, new EditDistance(50, true));
        int naiveBuild = (int) (System.currentTimeMillis() - time) / 1000;
        time = System.currentTimeMillis();
        bktree = new BKTree<>(new EditDistance(50, true));
        bktree.add(data);
        int bktreeBuild = (int) (System.currentTimeMillis() - time) / 1000;
        time = System.currentTimeMillis();
        cover = new CoverTree<>(data, new EditDistance(50, true));
        int coverBuild = (int) (System.currentTimeMillis() - time) / 1000;
        double[] buildTime = { naiveBuild, bktreeBuild, coverBuild };
        PlotCanvas build = BarPlot.plot(buildTime, label);
        build.setTitle("Build Time");
        canvas.add(build);
        validate();
    }
    int[] perm = Math.permutate(data.length);
    System.out.println("Perform 1000 searches...");
    long time = System.currentTimeMillis();
    List<Neighbor<String, String>> neighbors = new ArrayList<>();
    for (int i = 0; i < 1000; i++) {
        naive.range(data[perm[i]], knn, neighbors);
        neighbors.clear();
    }
    int naiveSearch = (int) (System.currentTimeMillis() - time) / 1000;
    time = System.currentTimeMillis();
    for (int i = 0; i < 1000; i++) {
        bktree.range(data[perm[i]], knn, neighbors);
        neighbors.clear();
    }
    int kdtreeSearch = (int) (System.currentTimeMillis() - time) / 1000;
    time = System.currentTimeMillis();
    for (int i = 0; i < 1000; i++) {
        cover.range(data[perm[i]], knn, neighbors);
        neighbors.clear();
    }
    int coverSearch = (int) (System.currentTimeMillis() - time) / 1000;
    double[] searchTime = { naiveSearch, kdtreeSearch, coverSearch };
    PlotCanvas search = BarPlot.plot(searchTime, label);
    search.setTitle("Search Time of k = " + knn);
    canvas.add(search);
    if (canvas.getComponentCount() > 3)
        canvas.setLayout(new GridLayout(2, 2));
    validate();
    startButton.setEnabled(true);
    knnField.setEnabled(true);
}
Also used : InputStreamReader(java.io.InputStreamReader) ArrayList(java.util.ArrayList) Neighbor(smile.neighbor.Neighbor) FileInputStream(java.io.FileInputStream) GridLayout(java.awt.GridLayout) BufferedReader(java.io.BufferedReader) EditDistance(smile.math.distance.EditDistance) PlotCanvas(smile.plot.PlotCanvas)

Example 88 with GridLayout

use of java.awt.GridLayout in project smile by haifengl.

the class HierarchicalClusteringDemo method learn.

@Override
public JComponent learn() {
    long clock = System.currentTimeMillis();
    double[][] data = dataset[datasetIndex];
    int n = data.length;
    double[][] proximity = new double[n][];
    for (int i = 0; i < n; i++) {
        proximity[i] = new double[i + 1];
        for (int j = 0; j < i; j++) proximity[i][j] = Math.distance(data[i], data[j]);
    }
    HierarchicalClustering hac = null;
    switch(linkageBox.getSelectedIndex()) {
        case 0:
            hac = new HierarchicalClustering(new SingleLinkage(proximity));
            break;
        case 1:
            hac = new HierarchicalClustering(new CompleteLinkage(proximity));
            break;
        case 2:
            hac = new HierarchicalClustering(new UPGMALinkage(proximity));
            break;
        case 3:
            hac = new HierarchicalClustering(new WPGMALinkage(proximity));
            break;
        case 4:
            hac = new HierarchicalClustering(new UPGMCLinkage(proximity));
            break;
        case 5:
            hac = new HierarchicalClustering(new WPGMCLinkage(proximity));
            break;
        case 6:
            hac = new HierarchicalClustering(new WardLinkage(proximity));
            break;
        default:
            throw new IllegalStateException("Unsupported Linkage");
    }
    System.out.format("Hierarchical clusterings %d samples in %dms\n", dataset[datasetIndex].length, System.currentTimeMillis() - clock);
    int[] membership = hac.partition(clusterNumber);
    int[] clusterSize = new int[clusterNumber];
    for (int i = 0; i < membership.length; i++) {
        clusterSize[membership[i]]++;
    }
    JPanel pane = new JPanel(new GridLayout(1, 3));
    PlotCanvas plot = ScatterPlot.plot(dataset[datasetIndex], pointLegend);
    plot.setTitle("Data");
    pane.add(plot);
    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]);
    }
    plot = Dendrogram.plot("Dendrogram", hac.getTree(), hac.getHeight());
    plot.setTitle("Dendrogram");
    pane.add(plot);
    return pane;
}
Also used : WPGMCLinkage(smile.clustering.linkage.WPGMCLinkage) JPanel(javax.swing.JPanel) CompleteLinkage(smile.clustering.linkage.CompleteLinkage) WardLinkage(smile.clustering.linkage.WardLinkage) HierarchicalClustering(smile.clustering.HierarchicalClustering) GridLayout(java.awt.GridLayout) SingleLinkage(smile.clustering.linkage.SingleLinkage) WPGMALinkage(smile.clustering.linkage.WPGMALinkage) UPGMALinkage(smile.clustering.linkage.UPGMALinkage) UPGMCLinkage(smile.clustering.linkage.UPGMCLinkage) PlotCanvas(smile.plot.PlotCanvas)

Example 89 with GridLayout

use of java.awt.GridLayout in project smile by haifengl.

the class LaplacianEigenmapDemo method learn.

@Override
public JComponent learn() {
    JPanel pane = new JPanel(new GridLayout(1, 2));
    try {
        sigma = Double.parseDouble(sigmaField.getText().trim());
    } catch (Exception ex) {
        JOptionPane.showMessageDialog(this, "Invalid t: " + sigmaField.getText(), "Error", JOptionPane.ERROR_MESSAGE);
        return pane;
    }
    sigmaField.setEnabled(false);
    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();
    LaplacianEigenmap eigenmap = new LaplacianEigenmap(data, 2, k, sigma);
    System.out.format("Learn Laplacian Eigenmap from %d samples in %dms\n", data.length, System.currentTimeMillis() - clock);
    double[][] y = eigenmap.getCoordinates();
    PlotCanvas plot = new PlotCanvas(Math.colMin(y), Math.colMax(y));
    plot.points(y, 'o', Color.RED);
    int n = y.length;
    Graph graph = eigenmap.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("Laplacian Eigenmap");
    pane.add(plot);
    sigmaField.setEnabled(true);
    return pane;
}
Also used : JPanel(javax.swing.JPanel) GridLayout(java.awt.GridLayout) Graph(smile.graph.Graph) LaplacianEigenmap(smile.manifold.LaplacianEigenmap) PlotCanvas(smile.plot.PlotCanvas)

Example 90 with GridLayout

use of java.awt.GridLayout in project smile by haifengl.

the class DBScanDemo method learn.

@Override
public JComponent learn() {
    try {
        minPts = Integer.parseInt(minPtsField.getText().trim());
        if (minPts < 1) {
            JOptionPane.showMessageDialog(this, "Invalid MinPts: " + minPts, "Error", JOptionPane.ERROR_MESSAGE);
            return null;
        }
    } catch (Exception e) {
        JOptionPane.showMessageDialog(this, "Invalid MinPts: " + minPtsField.getText(), "Error", JOptionPane.ERROR_MESSAGE);
        return null;
    }
    try {
        range = Double.parseDouble(rangeField.getText().trim());
        if (range <= 0) {
            JOptionPane.showMessageDialog(this, "Invalid Range: " + range, "Error", JOptionPane.ERROR_MESSAGE);
            return null;
        }
    } catch (Exception e) {
        JOptionPane.showMessageDialog(this, "Invalid range: " + rangeField.getText(), "Error", JOptionPane.ERROR_MESSAGE);
        return null;
    }
    long clock = System.currentTimeMillis();
    DBScan<double[]> dbscan = new DBScan<>(dataset[datasetIndex], new EuclideanDistance(), minPts, range);
    System.out.format("DBSCAN clusterings %d samples in %dms\n", dataset[datasetIndex].length, System.currentTimeMillis() - clock);
    JPanel pane = new JPanel(new GridLayout(1, 2));
    PlotCanvas plot = ScatterPlot.plot(dataset[datasetIndex], pointLegend);
    for (int k = 0; k < dbscan.getNumClusters(); k++) {
        double[][] cluster = new double[dbscan.getClusterSize()[k]][];
        for (int i = 0, j = 0; i < dataset[datasetIndex].length; i++) {
            if (dbscan.getClusterLabel()[i] == k) {
                cluster[j++] = dataset[datasetIndex][i];
            }
        }
        plot.points(cluster, pointLegend, Palette.COLORS[k % Palette.COLORS.length]);
    }
    pane.add(plot);
    return pane;
}
Also used : EuclideanDistance(smile.math.distance.EuclideanDistance) JPanel(javax.swing.JPanel) GridLayout(java.awt.GridLayout) DBScan(smile.clustering.DBScan) PlotCanvas(smile.plot.PlotCanvas)

Aggregations

GridLayout (java.awt.GridLayout)142 JPanel (javax.swing.JPanel)106 JLabel (javax.swing.JLabel)54 BorderLayout (java.awt.BorderLayout)50 Dimension (java.awt.Dimension)32 JButton (javax.swing.JButton)32 ActionEvent (java.awt.event.ActionEvent)29 JScrollPane (javax.swing.JScrollPane)27 ActionListener (java.awt.event.ActionListener)25 JTextField (javax.swing.JTextField)23 Insets (java.awt.Insets)21 JCheckBox (javax.swing.JCheckBox)21 GridBagConstraints (java.awt.GridBagConstraints)17 GridBagLayout (java.awt.GridBagLayout)17 TitledBorder (javax.swing.border.TitledBorder)17 PlotCanvas (smile.plot.PlotCanvas)16 BoxLayout (javax.swing.BoxLayout)15 EmptyBorder (javax.swing.border.EmptyBorder)15 FlowLayout (java.awt.FlowLayout)13 ButtonGroup (javax.swing.ButtonGroup)10