Search in sources :

Example 26 with PlotCanvas

use of smile.plot.PlotCanvas in project smile by haifengl.

the class MECDemo method learn.

@Override
public JComponent learn() {
    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();
    MEC<double[]> mec = new MEC<>(dataset[datasetIndex], new EuclideanDistance(), clusterNumber, range);
    System.out.format("MEC clusterings %d samples in %dms\n", dataset[datasetIndex].length, System.currentTimeMillis() - clock);
    PlotCanvas plot = ScatterPlot.plot(dataset[datasetIndex], pointLegend);
    for (int k = 0; k < mec.getNumClusters(); k++) {
        double[][] cluster = new double[mec.getClusterSize()[k]][];
        for (int i = 0, j = 0; i < dataset[datasetIndex].length; i++) {
            if (mec.getClusterLabel()[i] == k) {
                cluster[j++] = dataset[datasetIndex][i];
            }
        }
        plot.points(cluster, pointLegend, Palette.COLORS[k % Palette.COLORS.length]);
    }
    return plot;
}
Also used : EuclideanDistance(smile.math.distance.EuclideanDistance) MEC(smile.clustering.MEC) PlotCanvas(smile.plot.PlotCanvas)

Example 27 with PlotCanvas

use of smile.plot.PlotCanvas in project smile by haifengl.

the class NeuralGasDemo method learn.

@Override
public JComponent learn() {
    long clock = System.currentTimeMillis();
    NeuralGas gas = new NeuralGas(dataset[datasetIndex], clusterNumber);
    System.out.format("Neural Gas clusterings %d samples in %dms\n", dataset[datasetIndex].length, System.currentTimeMillis() - clock);
    PlotCanvas plot = ScatterPlot.plot(gas.centroids(), '@');
    for (int k = 0; k < clusterNumber; k++) {
        if (gas.getClusterSize()[k] > 0) {
            double[][] cluster = new double[gas.getClusterSize()[k]][];
            for (int i = 0, j = 0; i < dataset[datasetIndex].length; i++) {
                if (gas.getClusterLabel()[i] == k) {
                    cluster[j++] = dataset[datasetIndex][i];
                }
            }
            plot.points(cluster, pointLegend, Palette.COLORS[k % Palette.COLORS.length]);
        }
    }
    plot.points(gas.centroids(), '@');
    return plot;
}
Also used : NeuralGas(smile.vq.NeuralGas) PlotCanvas(smile.plot.PlotCanvas)

Example 28 with PlotCanvas

use of smile.plot.PlotCanvas in project smile by haifengl.

the class ExponentialFamilyMixtureDemo method main.

public static void main(String[] args) {
    // Mixture of Gaussian, Exponential, and Gamma.
    double[] data = new double[2000];
    GaussianDistribution gaussian = new GaussianDistribution(-2.0, 1.0);
    for (int i = 0; i < 500; i++) data[i] = gaussian.rand();
    ExponentialDistribution exp = new ExponentialDistribution(0.8);
    for (int i = 500; i < 1000; i++) data[i] = exp.rand();
    GammaDistribution gamma = new GammaDistribution(2.0, 3.0);
    for (int i = 1000; i < 2000; i++) data[i] = gamma.rand();
    List<Mixture.Component> m = new ArrayList<>();
    Mixture.Component c = new Mixture.Component();
    c.priori = 0.25;
    c.distribution = new GaussianDistribution(0.0, 1.0);
    m.add(c);
    c = new Mixture.Component();
    c.priori = 0.25;
    c.distribution = new ExponentialDistribution(1.0);
    m.add(c);
    c = new Mixture.Component();
    c.priori = 0.25;
    c.distribution = new GammaDistribution(1.0, 2.0);
    m.add(c);
    ExponentialFamilyMixture mixture = new ExponentialFamilyMixture(m, data);
    System.out.println(mixture);
    JFrame frame = new JFrame("Mixture of Exponential Family Distributions");
    PlotCanvas canvas = Histogram.plot(data, 50);
    frame.add(canvas);
    double width = (Math.max(data) - Math.min(data)) / 50;
    double[][] p = new double[400][2];
    for (int i = 0; i < p.length; i++) {
        p[i][0] = -10 + i * 0.1;
        p[i][1] = mixture.p(p[i][0]) * width;
    }
    canvas.line(p, Color.RED);
    frame.add(QQPlot.plot(data, mixture));
    frame.setVisible(true);
}
Also used : GaussianDistribution(smile.stat.distribution.GaussianDistribution) ExponentialFamilyMixture(smile.stat.distribution.ExponentialFamilyMixture) ExponentialDistribution(smile.stat.distribution.ExponentialDistribution) ArrayList(java.util.ArrayList) ExponentialFamilyMixture(smile.stat.distribution.ExponentialFamilyMixture) Mixture(smile.stat.distribution.Mixture) JFrame(javax.swing.JFrame) GammaDistribution(smile.stat.distribution.GammaDistribution) PlotCanvas(smile.plot.PlotCanvas)

Example 29 with PlotCanvas

use of smile.plot.PlotCanvas in project smile by haifengl.

the class NearestNeighborDemo method run.

@Override
public void run() {
    startButton.setEnabled(false);
    logNSlider.setEnabled(false);
    dimensionSlider.setEnabled(false);
    logN = logNSlider.getValue();
    dimension = dimensionSlider.getValue();
    System.out.println("Generating dataset...");
    int n = (int) Math.pow(10, logN);
    double[][] data = new double[n][];
    for (int i = 0; i < n; i++) {
        data[i] = new double[dimension];
        for (int j = 0; j < dimension; j++) {
            data[i][j] = Math.random();
        }
    }
    int[] perm = Math.permutate(n);
    System.out.println("Building searching data structure...");
    long time = System.currentTimeMillis();
    LinearSearch<double[]> naive = new LinearSearch<>(data, new EuclideanDistance());
    int naiveBuild = (int) (System.currentTimeMillis() - time);
    time = System.currentTimeMillis();
    KDTree<double[]> kdtree = new KDTree<>(data, data);
    int kdtreeBuild = (int) (System.currentTimeMillis() - time);
    time = System.currentTimeMillis();
    CoverTree<double[]> cover = new CoverTree<>(data, new EuclideanDistance());
    int coverBuild = (int) (System.currentTimeMillis() - time);
    System.out.println("Perform 100 searches...");
    int[] answer = new int[100];
    double radius = 0.0;
    time = System.currentTimeMillis();
    for (int i = 0; i < 100; i++) {
        Neighbor<double[], double[]> neighbor = naive.nearest(data[perm[i]]);
        answer[i] = neighbor.index;
        radius += neighbor.distance;
    }
    int naiveSearch = (int) (System.currentTimeMillis() - time);
    radius /= 100;
    time = System.currentTimeMillis();
    for (int i = 0; i < 100; i++) {
        kdtree.nearest(data[perm[i]]);
    }
    int kdtreeSearch = (int) (System.currentTimeMillis() - time);
    time = System.currentTimeMillis();
    for (int i = 0; i < 100; i++) {
        cover.nearest(data[perm[i]]);
    }
    int coverSearch = (int) (System.currentTimeMillis() - time);
    time = System.currentTimeMillis();
    LSH<double[]> lsh = new LSH<>(dimension, 5, (int) Math.ceil(Math.log2(dimension)), 4 * radius, 1017881);
    for (int i = 0; i < n; i++) {
        lsh.put(data[i], data[i]);
    }
    int lshBuild = (int) (System.currentTimeMillis() - time);
    double lshRecall = 0.0;
    time = System.currentTimeMillis();
    for (int i = 0; i < 100; i++) {
        if (lsh.nearest(data[perm[i]]).index == answer[i]) {
            lshRecall++;
        }
    }
    int lshSearch = (int) (System.currentTimeMillis() - time);
    lshRecall /= 100;
    System.out.format("The recall of LSH is %.1f%%\n", lshRecall * 100);
    time = System.currentTimeMillis();
    MPLSH<double[]> mplsh = new MPLSH<>(dimension, 5, (int) Math.ceil(Math.log2(n)), 4 * radius, 1017881);
    for (int i = 0; i < n; i++) {
        mplsh.put(data[i], data[i]);
    }
    double[][] train = new double[1000][];
    for (int i = 0; i < train.length; i++) {
        train[i] = data[perm[i]];
    }
    mplsh.learn(kdtree, train, 1.5 * radius);
    int mplshBuild = (int) (System.currentTimeMillis() - time);
    double mplshRecall = 0.0;
    time = System.currentTimeMillis();
    for (int i = 0; i < 100; i++) {
        if (mplsh.nearest(data[perm[i]], 0.95, 10).index == answer[i]) {
            mplshRecall++;
        }
    }
    int mplshSearch = (int) (System.currentTimeMillis() - time);
    mplshRecall /= 100;
    System.out.format("The recall of MPLSH is %.1f%%\n", mplshRecall * 100);
    canvas.removeAll();
    double[] buildTime = { naiveBuild, kdtreeBuild, coverBuild, lshBuild, mplshBuild };
    PlotCanvas build = BarPlot.plot(buildTime, label);
    build.setTitle("Build Time");
    canvas.add(build);
    double[] searchTime = { naiveSearch, kdtreeSearch, coverSearch, lshSearch, mplshSearch };
    PlotCanvas search = BarPlot.plot(searchTime, label);
    search.setTitle("Search Time");
    canvas.add(search);
    validate();
    startButton.setEnabled(true);
    logNSlider.setEnabled(true);
    dimensionSlider.setEnabled(true);
}
Also used : MPLSH(smile.neighbor.MPLSH) LSH(smile.neighbor.LSH) CoverTree(smile.neighbor.CoverTree) EuclideanDistance(smile.math.distance.EuclideanDistance) KDTree(smile.neighbor.KDTree) MPLSH(smile.neighbor.MPLSH) LinearSearch(smile.neighbor.LinearSearch) PlotCanvas(smile.plot.PlotCanvas)

Example 30 with PlotCanvas

use of smile.plot.PlotCanvas 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)

Aggregations

PlotCanvas (smile.plot.PlotCanvas)36 GridLayout (java.awt.GridLayout)16 JPanel (javax.swing.JPanel)15 EuclideanDistance (smile.math.distance.EuclideanDistance)6 ArrayList (java.util.ArrayList)4 PCA (smile.projection.PCA)4 BorderLayout (java.awt.BorderLayout)3 Attribute (smile.data.Attribute)3 Graph (smile.graph.Graph)3 CoverTree (smile.neighbor.CoverTree)3 KDTree (smile.neighbor.KDTree)3 LSH (smile.neighbor.LSH)3 LinearSearch (smile.neighbor.LinearSearch)3 MPLSH (smile.neighbor.MPLSH)3 Neighbor (smile.neighbor.Neighbor)3 JFrame (javax.swing.JFrame)2 NominalAttribute (smile.data.NominalAttribute)2 DelimitedTextParser (smile.data.parser.DelimitedTextParser)2 IsotonicMDS (smile.mds.IsotonicMDS)2 MDS (smile.mds.MDS)2