use of smile.stat.distribution.MultivariateGaussianDistribution in project smile by haifengl.
the class LSHSpeedTest 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();
LSH<double[]> lsh = new LSH<>(3, 5, 10, 4.0);
for (double[] x : data) {
lsh.put(x, x);
}
time = (System.currentTimeMillis() - start) / 1000.0;
System.out.format("Building LSH: %.2fs%n", time);
start = System.currentTimeMillis();
for (int i = 0; i < 1000; i++) {
lsh.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++) {
lsh.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++) {
lsh.range(data[Math.randomInt(data.length)], 1.0, n);
n.clear();
}
time = (System.currentTimeMillis() - start) / 1000.0;
System.out.format("Range: %.2fs%n", time);
}
use of smile.stat.distribution.MultivariateGaussianDistribution in project smile by haifengl.
the class LinearSearchSpeedTest method testToy.
/**
* Test of nearest method, of class LinearSearch.
*/
@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);
LinearSearch<double[]> naive = new LinearSearch<>(data, new EuclideanDistance());
start = System.currentTimeMillis();
for (int i = 0; i < 1000; i++) {
naive.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++) {
naive.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++) {
naive.range(data[Math.randomInt(data.length)], 1.0, n);
n.clear();
}
time = (System.currentTimeMillis() - start) / 1000.0;
System.out.format("Range: %.2fs%n", time);
}
use of smile.stat.distribution.MultivariateGaussianDistribution in project smile by haifengl.
the class DENCLUETest method testToy.
/**
* Test of learn method, of class DENCLUE.
*/
@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;
}
DENCLUE denclue = new DENCLUE(data, 0.8, 50);
AdjustedRandIndex ari = new AdjustedRandIndex();
RandIndex rand = new RandIndex();
double r = rand.measure(label, denclue.getClusterLabel());
double r2 = ari.measure(label, denclue.getClusterLabel());
System.out.println("The number of clusters: " + denclue.getNumClusters());
System.out.format("Training rand index = %.2f%%\tadjusted rand index = %.2f%%%n", 100.0 * r, 100.0 * r2);
assertTrue(r > 0.54);
assertTrue(r2 > 0.2);
}
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);
}
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;
}
Aggregations