Search in sources :

Example 1 with Tracer

use of org.apache.ignite.ml.math.Tracer in project ignite by apache.

the class KMeansLocalClustererExample method main.

/**
 * Executes example.
 *
 * @param args Command line arguments, none required.
 */
public static void main(String[] args) {
    // IMPL NOTE based on KMeansDistributedClustererTestSingleNode#testClusterizationOnDatasetWithObviousStructure
    System.out.println(">>> K-means local clusterer example started.");
    int ptsCnt = 10000;
    DenseLocalOnHeapMatrix points = new DenseLocalOnHeapMatrix(ptsCnt, 2);
    DatasetWithObviousStructure dataset = new DatasetWithObviousStructure(10000);
    List<Vector> massCenters = dataset.generate(points);
    EuclideanDistance dist = new EuclideanDistance();
    OrderedNodesComparator comp = new OrderedNodesComparator(dataset.centers().values().toArray(new Vector[] {}), dist);
    massCenters.sort(comp);
    KMeansLocalClusterer clusterer = new KMeansLocalClusterer(dist, 100, 1L);
    KMeansModel mdl = clusterer.cluster(points, 4);
    Vector[] resCenters = mdl.centers();
    Arrays.sort(resCenters, comp);
    System.out.println("Mass centers:");
    massCenters.forEach(Tracer::showAscii);
    System.out.println("Cluster centers:");
    Arrays.asList(resCenters).forEach(Tracer::showAscii);
    System.out.println("\n>>> K-means local clusterer example completed.");
}
Also used : EuclideanDistance(org.apache.ignite.ml.math.distances.EuclideanDistance) KMeansModel(org.apache.ignite.ml.clustering.KMeansModel) Tracer(org.apache.ignite.ml.math.Tracer) DenseLocalOnHeapMatrix(org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix) Vector(org.apache.ignite.ml.math.Vector) KMeansLocalClusterer(org.apache.ignite.ml.clustering.KMeansLocalClusterer)

Example 2 with Tracer

use of org.apache.ignite.ml.math.Tracer in project ignite by apache.

the class KMeansDistributedClustererExample method main.

/**
 * Executes example.
 *
 * @param args Command line arguments, none required.
 */
public static void main(String[] args) throws InterruptedException {
    // IMPL NOTE based on KMeansDistributedClustererTestSingleNode#testClusterizationOnDatasetWithObviousStructure
    System.out.println(">>> K-means distributed clusterer example started.");
    // Start ignite grid.
    try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) {
        System.out.println(">>> Ignite grid started.");
        // Create IgniteThread, we must work with SparseDistributedMatrix inside IgniteThread
        // because we create ignite cache internally.
        IgniteThread igniteThread = new IgniteThread(ignite.configuration().getIgniteInstanceName(), SparseDistributedMatrixExample.class.getSimpleName(), () -> {
            int ptsCnt = 10000;
            SparseDistributedMatrix points = new SparseDistributedMatrix(ptsCnt, 2, StorageConstants.ROW_STORAGE_MODE, StorageConstants.RANDOM_ACCESS_MODE);
            DatasetWithObviousStructure dataset = new DatasetWithObviousStructure(10000);
            List<Vector> massCenters = dataset.generate(points);
            EuclideanDistance dist = new EuclideanDistance();
            KMeansDistributedClusterer clusterer = new KMeansDistributedClusterer(dist, 3, 100, 1L);
            Vector[] resCenters = clusterer.cluster(points, 4).centers();
            System.out.println("Mass centers:");
            massCenters.forEach(Tracer::showAscii);
            System.out.println("Cluster centers:");
            Arrays.asList(resCenters).forEach(Tracer::showAscii);
            points.destroy();
            System.out.println("\n>>> K-means distributed clusterer example completed.");
        });
        igniteThread.start();
        igniteThread.join();
    }
}
Also used : SparseDistributedMatrix(org.apache.ignite.ml.math.impls.matrix.SparseDistributedMatrix) EuclideanDistance(org.apache.ignite.ml.math.distances.EuclideanDistance) SparseDistributedMatrixExample(org.apache.ignite.examples.ml.math.matrix.SparseDistributedMatrixExample) Tracer(org.apache.ignite.ml.math.Tracer) KMeansDistributedClusterer(org.apache.ignite.ml.clustering.KMeansDistributedClusterer) Ignite(org.apache.ignite.Ignite) IgniteThread(org.apache.ignite.thread.IgniteThread) Vector(org.apache.ignite.ml.math.Vector)

Aggregations

Tracer (org.apache.ignite.ml.math.Tracer)2 Vector (org.apache.ignite.ml.math.Vector)2 EuclideanDistance (org.apache.ignite.ml.math.distances.EuclideanDistance)2 Ignite (org.apache.ignite.Ignite)1 SparseDistributedMatrixExample (org.apache.ignite.examples.ml.math.matrix.SparseDistributedMatrixExample)1 KMeansDistributedClusterer (org.apache.ignite.ml.clustering.KMeansDistributedClusterer)1 KMeansLocalClusterer (org.apache.ignite.ml.clustering.KMeansLocalClusterer)1 KMeansModel (org.apache.ignite.ml.clustering.KMeansModel)1 DenseLocalOnHeapMatrix (org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix)1 SparseDistributedMatrix (org.apache.ignite.ml.math.impls.matrix.SparseDistributedMatrix)1 IgniteThread (org.apache.ignite.thread.IgniteThread)1