Search in sources :

Example 6 with MultivariateGaussianDistribution

use of smile.stat.distribution.MultivariateGaussianDistribution in project smile by haifengl.

the class KDTreeSpeedTest method testToy.

/**
     * Test of nearest method, of class KDTree.
     */
@Test
public void testToy() {
    System.out.println("toy data");
    long start = System.currentTimeMillis();
    double[] mu1 = { 1.0, 1.0, 1.0 };
    double[][] sigma1 = { { 1.0, 0.0, 0.0 }, { 0.0, 1.0, 0.0 }, { 0.0, 0.0, 1.0 } };
    double[] mu2 = { -2.0, -2.0, -2.0 };
    double[][] sigma2 = { { 1.0, 0.3, 0.8 }, { 0.3, 1.0, 0.5 }, { 0.8, 0.5, 1.0 } };
    double[] mu3 = { 4.0, 2.0, 3.0 };
    double[][] sigma3 = { { 1.0, 0.8, 0.3 }, { 0.8, 1.0, 0.5 }, { 0.3, 0.5, 1.0 } };
    double[] mu4 = { 3.0, 5.0, 1.0 };
    double[][] sigma4 = { { 1.0, 0.5, 0.5 }, { 0.5, 1.0, 0.5 }, { 0.5, 0.5, 1.0 } };
    double[][] data = new double[10000][];
    MultivariateGaussianDistribution g1 = new MultivariateGaussianDistribution(mu1, sigma1);
    for (int i = 0; i < 2000; i++) {
        data[i] = g1.rand();
    }
    MultivariateGaussianDistribution g2 = new MultivariateGaussianDistribution(mu2, sigma2);
    for (int i = 0; i < 3000; i++) {
        data[2000 + i] = g2.rand();
    }
    MultivariateGaussianDistribution g3 = new MultivariateGaussianDistribution(mu3, sigma3);
    for (int i = 0; i < 3000; i++) {
        data[5000 + i] = g3.rand();
    }
    MultivariateGaussianDistribution g4 = new MultivariateGaussianDistribution(mu4, sigma4);
    for (int i = 0; i < 2000; i++) {
        data[8000 + i] = g4.rand();
    }
    double time = (System.currentTimeMillis() - start) / 1000.0;
    System.out.format("Generating toy data (four Gaussians): %.2fs%n", time);
    start = System.currentTimeMillis();
    KDTree<double[]> kdtree = new KDTree<>(data, data);
    time = (System.currentTimeMillis() - start) / 1000.0;
    System.out.format("Building KD-tree: %.2fs%n", time);
    start = System.currentTimeMillis();
    for (int i = 0; i < 1000; i++) {
        kdtree.nearest(data[Math.randomInt(data.length)]);
    }
    time = (System.currentTimeMillis() - start) / 1000.0;
    System.out.format("NN: %.2fs%n", time);
    start = System.currentTimeMillis();
    for (int i = 0; i < 1000; i++) {
        kdtree.knn(data[Math.randomInt(data.length)], 10);
    }
    time = (System.currentTimeMillis() - start) / 1000.0;
    System.out.format("10-NN: %.2fs%n", time);
    start = System.currentTimeMillis();
    List<Neighbor<double[], double[]>> n = new ArrayList<>();
    for (int i = 0; i < 1000; i++) {
        kdtree.range(data[Math.randomInt(data.length)], 1.0, n);
        n.clear();
    }
    time = (System.currentTimeMillis() - start) / 1000.0;
    System.out.format("Range: %.2fs%n", time);
}
Also used : MultivariateGaussianDistribution(smile.stat.distribution.MultivariateGaussianDistribution) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 7 with MultivariateGaussianDistribution

use of smile.stat.distribution.MultivariateGaussianDistribution in project smile by haifengl.

the class DBScanTest method testToy.

/**
     * Test of learn method, of class DBScan.
     */
@Test
public void testToy() {
    System.out.println("Toy");
    double[] mu1 = { 1.0, 1.0, 1.0 };
    double[][] sigma1 = { { 1.0, 0.0, 0.0 }, { 0.0, 1.0, 0.0 }, { 0.0, 0.0, 1.0 } };
    double[] mu2 = { -2.0, -2.0, -2.0 };
    double[][] sigma2 = { { 1.0, 0.3, 0.8 }, { 0.3, 1.0, 0.5 }, { 0.8, 0.5, 1.0 } };
    double[] mu3 = { 4.0, 2.0, 3.0 };
    double[][] sigma3 = { { 1.0, 0.8, 0.3 }, { 0.8, 1.0, 0.5 }, { 0.3, 0.5, 1.0 } };
    double[] mu4 = { 3.0, 5.0, 1.0 };
    double[][] sigma4 = { { 1.0, 0.5, 0.5 }, { 0.5, 1.0, 0.5 }, { 0.5, 0.5, 1.0 } };
    double[][] data = new double[10000][];
    int[] label = new int[10000];
    MultivariateGaussianDistribution g1 = new MultivariateGaussianDistribution(mu1, sigma1);
    for (int i = 0; i < 2000; i++) {
        data[i] = g1.rand();
        label[i] = 0;
    }
    MultivariateGaussianDistribution g2 = new MultivariateGaussianDistribution(mu2, sigma2);
    for (int i = 0; i < 3000; i++) {
        data[2000 + i] = g2.rand();
        label[i] = 1;
    }
    MultivariateGaussianDistribution g3 = new MultivariateGaussianDistribution(mu3, sigma3);
    for (int i = 0; i < 3000; i++) {
        data[5000 + i] = g3.rand();
        label[i] = 2;
    }
    MultivariateGaussianDistribution g4 = new MultivariateGaussianDistribution(mu4, sigma4);
    for (int i = 0; i < 2000; i++) {
        data[8000 + i] = g4.rand();
        label[i] = 3;
    }
    DBScan<double[]> dbscan = new DBScan<>(data, new KDTree<>(data, data), 200, 0.8);
    System.out.println(dbscan);
    int[] size = dbscan.getClusterSize();
    int n = 0;
    for (int i = 0; i < size.length - 1; i++) {
        n += size[i];
    }
    int[] y1 = new int[n];
    int[] y2 = new int[n];
    for (int i = 0, j = 0; i < data.length; i++) {
        if (dbscan.getClusterLabel()[i] != Clustering.OUTLIER) {
            y1[j] = label[i];
            y2[j++] = dbscan.getClusterLabel()[i];
        }
    }
    AdjustedRandIndex ari = new AdjustedRandIndex();
    RandIndex rand = new RandIndex();
    double r = rand.measure(y1, y2);
    double r2 = ari.measure(y1, y2);
    System.out.println("The number of clusters: " + dbscan.getNumClusters());
    System.out.format("Training rand index = %.2f%%\tadjusted rand index = %.2f%%%n", 100.0 * r, 100.0 * r2);
    assertTrue(r > 0.40);
    assertTrue(r2 > 0.15);
}
Also used : MultivariateGaussianDistribution(smile.stat.distribution.MultivariateGaussianDistribution) RandIndex(smile.validation.RandIndex) AdjustedRandIndex(smile.validation.AdjustedRandIndex) AdjustedRandIndex(smile.validation.AdjustedRandIndex) Test(org.junit.Test)

Example 8 with MultivariateGaussianDistribution

use of smile.stat.distribution.MultivariateGaussianDistribution in project smile by haifengl.

the class ToyData method sample.

/**
     * Generate n samples from each class.
     */
public double[][] sample(int n) {
    double[][] samples = new double[2 * n][];
    MultivariateGaussianDistribution[] gauss = new MultivariateGaussianDistribution[k];
    for (int i = 0; i < k; i++) {
        gauss[i] = new MultivariateGaussianDistribution(m[i], v);
    }
    for (int i = 0; i < n; i++) {
        samples[i] = gauss[Math.random(prob)].rand();
    }
    for (int i = 0; i < k; i++) {
        gauss[i] = new MultivariateGaussianDistribution(m[k + i], v);
    }
    for (int i = 0; i < n; i++) {
        samples[n + i] = gauss[Math.random(prob)].rand();
    }
    return samples;
}
Also used : MultivariateGaussianDistribution(smile.stat.distribution.MultivariateGaussianDistribution)

Aggregations

MultivariateGaussianDistribution (smile.stat.distribution.MultivariateGaussianDistribution)8 Test (org.junit.Test)5 ArrayList (java.util.ArrayList)3 AdjustedRandIndex (smile.validation.AdjustedRandIndex)2 RandIndex (smile.validation.RandIndex)2 JFrame (javax.swing.JFrame)1 EuclideanDistance (smile.math.distance.EuclideanDistance)1 MultivariateGaussianMixture (smile.stat.distribution.MultivariateGaussianMixture)1