Search in sources :

Example 1 with DataStreamGenerator

use of org.apache.ignite.ml.util.generators.DataStreamGenerator in project ignite by apache.

the class ParametricVectorGeneratorExample method main.

/**
 * Run example.
 *
 * @param args Args.
 */
public static void main(String... args) throws IOException {
    // Example of Archimedean spiral.
    DataStreamGenerator spiral = new ParametricVectorGenerator(// 't' will be in [-50, 50] range
    new UniformRandomProducer(-50, 50), t -> Math.cos(Math.abs(t)) * Math.abs(t), t -> Math.sin(Math.abs(t)) * Math.abs(t)).asDataStream();
    Tracer.showClassificationDatasetHtml("Spiral", spiral, 20000, 0, 1, false);
    // Example of heart shape.
    DataStreamGenerator heart = new ParametricVectorGenerator(new UniformRandomProducer(-50, 50), t -> 16 * Math.pow(Math.sin(t), 3), t -> 13 * Math.cos(t) - 5 * Math.cos(2 * t) - 2 * Math.cos(3 * t) - Math.cos(4 * t)).asDataStream();
    Tracer.showClassificationDatasetHtml("Heart", heart, 2000, 0, 1, false);
    // Example of butterfly-like shape.
    DataStreamGenerator butterfly = new ParametricVectorGenerator(// 't' will be in [-100, 100] range
    new UniformRandomProducer(-100, 100), t -> 10 * Math.sin(t) * (Math.exp(Math.cos(t)) - 2 * Math.cos(4 * t) - Math.pow(Math.sin(t / 12), 5)), t -> 10 * Math.cos(t) * (Math.exp(Math.cos(t)) - 2 * Math.cos(4 * t) - Math.pow(Math.sin(t / 12), 5))).asDataStream();
    Tracer.showClassificationDatasetHtml("Butterfly", butterfly, 2000, 0, 1, false);
    System.out.flush();
}
Also used : DataStreamGenerator(org.apache.ignite.ml.util.generators.DataStreamGenerator) ParametricVectorGenerator(org.apache.ignite.ml.util.generators.primitives.vector.ParametricVectorGenerator) IOException(java.io.IOException) Tracer(org.apache.ignite.ml.math.Tracer) UniformRandomProducer(org.apache.ignite.ml.util.generators.primitives.scalar.UniformRandomProducer) UniformRandomProducer(org.apache.ignite.ml.util.generators.primitives.scalar.UniformRandomProducer) ParametricVectorGenerator(org.apache.ignite.ml.util.generators.primitives.vector.ParametricVectorGenerator) DataStreamGenerator(org.apache.ignite.ml.util.generators.DataStreamGenerator)

Example 2 with DataStreamGenerator

use of org.apache.ignite.ml.util.generators.DataStreamGenerator in project ignite by apache.

the class VectorGeneratorPrimitivesExample method main.

/**
 * Run example.
 *
 * @param args Args.
 */
public static void main(String... args) throws IOException {
    // Vectors from ring-like distribution.
    VectorGenerator fullRing = VectorGeneratorPrimitives.ring(10, 0, 2 * Math.PI);
    // Vectors from ring's sector distribution.
    VectorGenerator partOfRing = VectorGeneratorPrimitives.ring(15, -Math.PI / 2, Math.PI);
    // Vectors from distribution having filled circle shape.
    VectorGenerator circle = VectorGeneratorPrimitives.circle(14.5);
    // Vectors from uniform distribution in n-dimensional space.
    VectorGenerator parallelogram = VectorGeneratorPrimitives.parallelogram(VectorUtils.of(10, 15));
    // Vectors from gaussian.
    VectorGenerator gauss = VectorGeneratorPrimitives.gauss(VectorUtils.of(0.0, 0.0), VectorUtils.of(10., 15.));
    Tracer.showClassificationDatasetHtml("Full ring", fullRing.asDataStream(), 1500, 0, 1, false);
    Tracer.showClassificationDatasetHtml("Sector", partOfRing.asDataStream(), 1500, 0, 1, false);
    Tracer.showClassificationDatasetHtml("Circle", circle.asDataStream(), 1500, 0, 1, false);
    Tracer.showClassificationDatasetHtml("Parallelogram", parallelogram.asDataStream(), 1500, 0, 1, false);
    Tracer.showClassificationDatasetHtml("Gauss", gauss.asDataStream(), 1500, 0, 1, false);
    // Using of rotate for generator.
    VectorGenerator rotatedParallelogram = parallelogram.rotate(-Math.PI / 8);
    Tracer.showClassificationDatasetHtml("Rotated Parallelogram", rotatedParallelogram.asDataStream(), 1500, 0, 1, false);
    // Sum of generators where vectors from first generator are summed with corresponding vectors from second generator.
    VectorGenerator gaussPlusRing = gauss.plus(fullRing);
    Tracer.showClassificationDatasetHtml("Gauss plus ring", gaussPlusRing.asDataStream(), 1500, 0, 1, false);
    // Example of vector generator filtering.
    VectorGenerator filteredCircle = circle.filter(v -> Math.abs(v.get(0)) > 5);
    Tracer.showClassificationDatasetHtml("Filtered circle", filteredCircle.asDataStream(), 1500, 0, 1, false);
    // Example of using map function for vector generator.
    VectorGenerator mappedCircle = circle.map(v -> v.get(1) < 0 ? v : v.times(VectorUtils.of(2, 4)));
    Tracer.showClassificationDatasetHtml("Mapped circle", mappedCircle.asDataStream(), 1500, 0, 1, false);
    // Example of generators concatenation where each vector of first generator are concatenated with corresponding
    // vector from second generator.
    DataStreamGenerator ringAndGauss = fullRing.concat(gauss).asDataStream();
    Tracer.showClassificationDatasetHtml("Ring and gauss [x1, x2]", ringAndGauss, 1500, 0, 1, false);
    Tracer.showClassificationDatasetHtml("Ring and gauss [x2, x3]", ringAndGauss, 1500, 1, 2, false);
    Tracer.showClassificationDatasetHtml("Ring and gauss [x3, x4]", ringAndGauss, 1500, 2, 3, false);
    Tracer.showClassificationDatasetHtml("Ring and gauss [x4, x1]", ringAndGauss, 1500, 3, 0, false);
    // Example of vector generator function noize.
    VectorGenerator noisifiedRing = fullRing.noisify(new DiscreteRandomProducer(0.1, 0.2, 0.3, 0.4));
    Tracer.showClassificationDatasetHtml("Noisified ring", noisifiedRing.asDataStream(), 1500, 0, 1, false);
    // Example of complex distribution with "axe" shape.
    VectorGenerator axeBlade = circle.filter(v -> Math.abs(v.get(1)) > 5.).rotate(Math.PI / 4).filter(v -> Math.abs(v.get(0)) > 1.5).rotate(-Math.PI / 2).filter(v -> Math.abs(v.get(0)) > 1.5).rotate(Math.PI / 4).filter(v -> Math.sqrt(v.getLengthSquared()) > 10).map(v -> Math.abs(v.get(0)) > 8 && Math.abs(v.get(1)) < 9 ? v.times(0.5) : v).rotate(Math.PI / 2);
    Tracer.showClassificationDatasetHtml("Axe blade", axeBlade.asDataStream(), 1500, 0, 1, false);
    System.out.flush();
}
Also used : DiscreteRandomProducer(org.apache.ignite.ml.util.generators.primitives.scalar.DiscreteRandomProducer) DataStreamGenerator(org.apache.ignite.ml.util.generators.DataStreamGenerator) VectorGenerator(org.apache.ignite.ml.util.generators.primitives.vector.VectorGenerator) VectorUtils(org.apache.ignite.ml.math.primitives.vector.VectorUtils) VectorGeneratorPrimitives(org.apache.ignite.ml.util.generators.primitives.vector.VectorGeneratorPrimitives) IOException(java.io.IOException) Tracer(org.apache.ignite.ml.math.Tracer) DiscreteRandomProducer(org.apache.ignite.ml.util.generators.primitives.scalar.DiscreteRandomProducer) DataStreamGenerator(org.apache.ignite.ml.util.generators.DataStreamGenerator) VectorGenerator(org.apache.ignite.ml.util.generators.primitives.vector.VectorGenerator)

Example 3 with DataStreamGenerator

use of org.apache.ignite.ml.util.generators.DataStreamGenerator in project ignite by apache.

the class GmmClusterizationExample method main.

/**
 * Runs example.
 *
 * @param args Command line arguments.
 */
public static void main(String[] args) {
    System.out.println();
    System.out.println(">>> GMM clustering algorithm over cached dataset usage example started.");
    // Start ignite grid.
    try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) {
        System.out.println(">>> Ignite grid started.");
        long seed = 0;
        IgniteCache<Integer, LabeledVector<Double>> dataCache = null;
        try {
            dataCache = ignite.createCache(new CacheConfiguration<Integer, LabeledVector<Double>>("GMM_EXAMPLE_CACHE").setAffinity(new RendezvousAffinityFunction(false, 10)));
            // Dataset consists of three gaussians where two from them are rotated onto PI/4.
            DataStreamGenerator dataStream = new VectorGeneratorsFamily.Builder().add(RandomProducer.vectorize(new GaussRandomProducer(0, 2., seed++), new GaussRandomProducer(0, 3., seed++)).rotate(Math.PI / 4).move(VectorUtils.of(10., 10.))).add(RandomProducer.vectorize(new GaussRandomProducer(0, 1., seed++), new GaussRandomProducer(0, 2., seed++)).rotate(-Math.PI / 4).move(VectorUtils.of(-10., 10.))).add(RandomProducer.vectorize(new GaussRandomProducer(0, 3., seed++), new GaussRandomProducer(0, 3., seed++)).move(VectorUtils.of(0., -10.))).build(seed++).asDataStream();
            AtomicInteger keyGen = new AtomicInteger();
            dataStream.fillCacheWithCustomKey(50000, dataCache, v -> keyGen.getAndIncrement());
            GmmTrainer trainer = new GmmTrainer(1);
            GmmModel mdl = trainer.withMaxCountIterations(10).withMaxCountOfClusters(4).withEnvironmentBuilder(LearningEnvironmentBuilder.defaultBuilder().withRNGSeed(seed)).fit(ignite, dataCache, new LabeledDummyVectorizer<>());
            System.out.println(">>> GMM means and covariances");
            for (int i = 0; i < mdl.countOfComponents(); i++) {
                MultivariateGaussianDistribution distribution = mdl.distributions().get(i);
                System.out.println();
                System.out.println("============");
                System.out.println("Component #" + i);
                System.out.println("============");
                System.out.println("Mean vector = ");
                Tracer.showAscii(distribution.mean());
                System.out.println();
                System.out.println("Covariance matrix = ");
                Tracer.showAscii(distribution.covariance());
            }
            System.out.println(">>>");
        } finally {
            if (dataCache != null)
                dataCache.destroy();
        }
    } finally {
        System.out.flush();
    }
}
Also used : LabeledVector(org.apache.ignite.ml.structures.LabeledVector) GmmModel(org.apache.ignite.ml.clustering.gmm.GmmModel) GaussRandomProducer(org.apache.ignite.ml.util.generators.primitives.scalar.GaussRandomProducer) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) GmmTrainer(org.apache.ignite.ml.clustering.gmm.GmmTrainer) MultivariateGaussianDistribution(org.apache.ignite.ml.math.stat.MultivariateGaussianDistribution) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DataStreamGenerator(org.apache.ignite.ml.util.generators.DataStreamGenerator) VectorGeneratorsFamily(org.apache.ignite.ml.util.generators.primitives.vector.VectorGeneratorsFamily) Ignite(org.apache.ignite.Ignite) RendezvousAffinityFunction(org.apache.ignite.cache.affinity.rendezvous.RendezvousAffinityFunction)

Aggregations

DataStreamGenerator (org.apache.ignite.ml.util.generators.DataStreamGenerator)3 IOException (java.io.IOException)2 Tracer (org.apache.ignite.ml.math.Tracer)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Ignite (org.apache.ignite.Ignite)1 RendezvousAffinityFunction (org.apache.ignite.cache.affinity.rendezvous.RendezvousAffinityFunction)1 GmmModel (org.apache.ignite.ml.clustering.gmm.GmmModel)1 GmmTrainer (org.apache.ignite.ml.clustering.gmm.GmmTrainer)1 VectorUtils (org.apache.ignite.ml.math.primitives.vector.VectorUtils)1 MultivariateGaussianDistribution (org.apache.ignite.ml.math.stat.MultivariateGaussianDistribution)1 LabeledVector (org.apache.ignite.ml.structures.LabeledVector)1 DiscreteRandomProducer (org.apache.ignite.ml.util.generators.primitives.scalar.DiscreteRandomProducer)1 GaussRandomProducer (org.apache.ignite.ml.util.generators.primitives.scalar.GaussRandomProducer)1 UniformRandomProducer (org.apache.ignite.ml.util.generators.primitives.scalar.UniformRandomProducer)1 ParametricVectorGenerator (org.apache.ignite.ml.util.generators.primitives.vector.ParametricVectorGenerator)1 VectorGenerator (org.apache.ignite.ml.util.generators.primitives.vector.VectorGenerator)1 VectorGeneratorPrimitives (org.apache.ignite.ml.util.generators.primitives.vector.VectorGeneratorPrimitives)1 VectorGeneratorsFamily (org.apache.ignite.ml.util.generators.primitives.vector.VectorGeneratorsFamily)1