Search in sources :

Example 36 with EuclideanDistance

use of org.apache.ignite.ml.math.distances.EuclideanDistance in project ignite by apache.

the class KMeansDistributedClustererTestMultiNode method testClusterizationOnDatasetWithObviousStructure.

/**
 */
public void testClusterizationOnDatasetWithObviousStructure() throws IOException {
    IgniteUtils.setCurrentIgniteName(ignite.configuration().getIgniteInstanceName());
    int ptsCnt = 10000;
    int squareSideLen = 10000;
    Random rnd = new Random(123456L);
    // Let centers be in the vertices of square.
    Map<Integer, Vector> centers = new HashMap<>();
    centers.put(100, new DenseLocalOnHeapVector(new double[] { 0.0, 0.0 }));
    centers.put(900, new DenseLocalOnHeapVector(new double[] { squareSideLen, 0.0 }));
    centers.put(3000, new DenseLocalOnHeapVector(new double[] { 0.0, squareSideLen }));
    centers.put(6000, new DenseLocalOnHeapVector(new double[] { squareSideLen, squareSideLen }));
    SparseDistributedMatrix points = new SparseDistributedMatrix(ptsCnt, 2, StorageConstants.ROW_STORAGE_MODE, StorageConstants.RANDOM_ACCESS_MODE);
    List<Integer> permutation = IntStream.range(0, ptsCnt).boxed().collect(Collectors.toList());
    Collections.shuffle(permutation, rnd);
    int totalCnt = 0;
    for (Integer count : centers.keySet()) {
        for (int i = 0; i < count; i++) {
            Vector pnt = new DenseLocalOnHeapVector(2).assign(centers.get(count));
            // Perturbate point on random value.
            pnt.map(val -> val + rnd.nextDouble() * squareSideLen / 100);
            points.assignRow(permutation.get(totalCnt), pnt);
            totalCnt++;
        }
    }
    EuclideanDistance dist = new EuclideanDistance();
    KMeansDistributedClusterer clusterer = new KMeansDistributedClusterer(dist, 3, 100, 1L);
    clusterer.cluster(points, 4);
    points.destroy();
}
Also used : SparseDistributedMatrix(org.apache.ignite.ml.math.impls.matrix.SparseDistributedMatrix) EuclideanDistance(org.apache.ignite.ml.math.distances.EuclideanDistance) Random(java.util.Random) HashMap(java.util.HashMap) DenseLocalOnHeapVector(org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector) Vector(org.apache.ignite.ml.math.Vector) DenseLocalOnHeapVector(org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector)

Example 37 with EuclideanDistance

use of org.apache.ignite.ml.math.distances.EuclideanDistance in project ignite by apache.

the class KMeansDistributedClustererTestMultiNode method testPerformClusterAnalysisDegenerate.

/**
 */
public void testPerformClusterAnalysisDegenerate() {
    IgniteUtils.setCurrentIgniteName(ignite.configuration().getIgniteInstanceName());
    KMeansDistributedClusterer clusterer = new KMeansDistributedClusterer(new EuclideanDistance(), 1, 1, 1L);
    double[] v1 = new double[] { 1959, 325100 };
    double[] v2 = new double[] { 1960, 373200 };
    SparseDistributedMatrix points = new SparseDistributedMatrix(2, 2, StorageConstants.ROW_STORAGE_MODE, StorageConstants.RANDOM_ACCESS_MODE);
    points.setRow(0, v1);
    points.setRow(1, v2);
    clusterer.cluster(points, 1);
    points.destroy();
}
Also used : EuclideanDistance(org.apache.ignite.ml.math.distances.EuclideanDistance) SparseDistributedMatrix(org.apache.ignite.ml.math.impls.matrix.SparseDistributedMatrix)

Aggregations

EuclideanDistance (org.apache.ignite.ml.math.distances.EuclideanDistance)37 Vector (org.apache.ignite.ml.math.Vector)22 DenseLocalOnHeapVector (org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector)18 DenseLocalOnHeapMatrix (org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix)14 LabeledDataset (org.apache.ignite.ml.structures.LabeledDataset)13 DistanceMeasure (org.apache.ignite.ml.math.distances.DistanceMeasure)10 Test (org.junit.Test)10 KNNModel (org.apache.ignite.ml.knn.models.KNNModel)9 SparseDistributedMatrix (org.apache.ignite.ml.math.impls.matrix.SparseDistributedMatrix)9 ArrayList (java.util.ArrayList)6 IgniteThread (org.apache.ignite.thread.IgniteThread)6 Arrays (java.util.Arrays)5 Comparator (java.util.Comparator)5 Collections (java.util.Collections)4 Ignite (org.apache.ignite.Ignite)4 FuzzyCMeansModel (org.apache.ignite.ml.clustering.FuzzyCMeansModel)4 KNNMultipleLinearRegression (org.apache.ignite.ml.knn.regression.KNNMultipleLinearRegression)4 MathIllegalArgumentException (org.apache.ignite.ml.math.exceptions.MathIllegalArgumentException)4 SparseBlockDistributedVector (org.apache.ignite.ml.math.impls.vector.SparseBlockDistributedVector)4 Assert.assertEquals (org.junit.Assert.assertEquals)4