Search in sources :

Example 31 with EuclideanDistance

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

the class FuzzyCMeansLocalClustererTest method equalWeightsOneDimension.

/**
 * Test FCM on points that forms three clusters on the line.
 */
@Test
public void equalWeightsOneDimension() {
    FuzzyCMeansLocalClusterer clusterer = new FuzzyCMeansLocalClusterer(new EuclideanDistance(), 2, BaseFuzzyCMeansClusterer.StopCondition.STABLE_CENTERS, 0.01, 10, null);
    double[][] points = new double[][] { { -10 }, { -9 }, { -8 }, { -7 }, { 7 }, { 8 }, { 9 }, { 10 }, { -1 }, { 0 }, { 1 } };
    DenseLocalOnHeapMatrix pntMatrix = new DenseLocalOnHeapMatrix(points);
    FuzzyCMeansModel mdl = clusterer.cluster(pntMatrix, 3);
    Vector[] centers = mdl.centers();
    Arrays.sort(centers, Comparator.comparing(vector -> vector.getX(0)));
    assertEquals(-8.5, centers[0].getX(0), 2);
    assertEquals(0, centers[1].getX(0), 2);
    assertEquals(8.5, centers[2].getX(0), 2);
}
Also used : Arrays(java.util.Arrays) Assert.assertTrue(org.junit.Assert.assertTrue) DistanceMeasure(org.apache.ignite.ml.math.distances.DistanceMeasure) Test(org.junit.Test) ArrayList(java.util.ArrayList) Vector(org.apache.ignite.ml.math.Vector) MathIllegalArgumentException(org.apache.ignite.ml.math.exceptions.MathIllegalArgumentException) DenseLocalOnHeapMatrix(org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix) EuclideanDistance(org.apache.ignite.ml.math.distances.EuclideanDistance) Comparator(java.util.Comparator) Collections(java.util.Collections) DenseLocalOnHeapVector(org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector) Assert.assertEquals(org.junit.Assert.assertEquals) EuclideanDistance(org.apache.ignite.ml.math.distances.EuclideanDistance) DenseLocalOnHeapMatrix(org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix) Vector(org.apache.ignite.ml.math.Vector) DenseLocalOnHeapVector(org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector) Test(org.junit.Test)

Example 32 with EuclideanDistance

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

the class FuzzyCMeansLocalClustererTest method checkCentersLocationOnSphere.

/**
 * Test FCM on points located on the circle.
 */
@Test
public void checkCentersLocationOnSphere() {
    FuzzyCMeansLocalClusterer clusterer = new FuzzyCMeansLocalClusterer(new EuclideanDistance(), 2, BaseFuzzyCMeansClusterer.StopCondition.STABLE_CENTERS, 0.01, 100, null);
    int numOfPoints = 650;
    double radius = 100.0;
    double[][] points = new double[numOfPoints][2];
    for (int i = 0; i < numOfPoints; i++) {
        points[i][0] = Math.cos(Math.PI * 2 * i / numOfPoints) * radius;
        points[i][1] = Math.sin(Math.PI * 2 * i / numOfPoints) * radius;
    }
    DenseLocalOnHeapMatrix pntMatrix = new DenseLocalOnHeapMatrix(points);
    int k = 10;
    FuzzyCMeansModel mdl = clusterer.cluster(pntMatrix, k);
    Vector sum = mdl.centers()[0];
    for (int i = 1; i < k; i++) sum = sum.plus(mdl.centers()[i]);
    assertEquals(0, sum.kNorm(1), 1);
}
Also used : EuclideanDistance(org.apache.ignite.ml.math.distances.EuclideanDistance) DenseLocalOnHeapMatrix(org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix) Vector(org.apache.ignite.ml.math.Vector) DenseLocalOnHeapVector(org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector) Test(org.junit.Test)

Example 33 with EuclideanDistance

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

the class FuzzyCMeansLocalClustererTest method testDifferentAmountsOfPointsAndWeights.

/**
 * Test FCM on different numbers of points and weights.
 */
@Test(expected = MathIllegalArgumentException.class)
public void testDifferentAmountsOfPointsAndWeights() {
    FuzzyCMeansLocalClusterer clusterer = new FuzzyCMeansLocalClusterer(new EuclideanDistance(), 2, BaseFuzzyCMeansClusterer.StopCondition.STABLE_CENTERS, 0.01, 10, null);
    double[][] points = new double[][] { { 1 }, { 2 }, { 3 }, { 4 } };
    ArrayList<Double> weights = new ArrayList<>();
    Collections.addAll(weights, 1.0, 34.0, 2.5, 5.0, 0.5);
    clusterer.cluster(new DenseLocalOnHeapMatrix(points), 2, weights);
}
Also used : EuclideanDistance(org.apache.ignite.ml.math.distances.EuclideanDistance) ArrayList(java.util.ArrayList) DenseLocalOnHeapMatrix(org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix) Test(org.junit.Test)

Example 34 with EuclideanDistance

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

the class FuzzyCMeansLocalClustererTest method test2DLineClustering.

/**
 * Test FCM on points that forms the line located on the plane.
 */
@Test
public void test2DLineClustering() {
    FuzzyCMeansLocalClusterer clusterer = new FuzzyCMeansLocalClusterer(new EuclideanDistance(), 2, BaseFuzzyCMeansClusterer.StopCondition.STABLE_CENTERS, 0.01, 50, null);
    double[][] points = new double[][] { { 1, 2 }, { 3, 6 }, { 5, 10 } };
    DenseLocalOnHeapMatrix pntMatrix = new DenseLocalOnHeapMatrix(points);
    int k = 2;
    FuzzyCMeansModel mdl = clusterer.cluster(pntMatrix, k);
    Vector[] centers = mdl.centers();
    Arrays.sort(centers, Comparator.comparing(vector -> vector.getX(0)));
    Vector[] exp = { new DenseLocalOnHeapVector(new double[] { 1.5, 3 }), new DenseLocalOnHeapVector(new double[] { 4.5, 9 }) };
    for (int i = 0; i < k; i++) {
        Vector center = centers[i];
        for (int j = 0; j < 2; j++) assertEquals(exp[i].getX(j), center.getX(j), 0.5);
    }
}
Also used : Arrays(java.util.Arrays) Assert.assertTrue(org.junit.Assert.assertTrue) DistanceMeasure(org.apache.ignite.ml.math.distances.DistanceMeasure) Test(org.junit.Test) ArrayList(java.util.ArrayList) Vector(org.apache.ignite.ml.math.Vector) MathIllegalArgumentException(org.apache.ignite.ml.math.exceptions.MathIllegalArgumentException) DenseLocalOnHeapMatrix(org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix) EuclideanDistance(org.apache.ignite.ml.math.distances.EuclideanDistance) Comparator(java.util.Comparator) Collections(java.util.Collections) DenseLocalOnHeapVector(org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector) Assert.assertEquals(org.junit.Assert.assertEquals) EuclideanDistance(org.apache.ignite.ml.math.distances.EuclideanDistance) DenseLocalOnHeapVector(org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector) DenseLocalOnHeapMatrix(org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix) Vector(org.apache.ignite.ml.math.Vector) DenseLocalOnHeapVector(org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector) Test(org.junit.Test)

Example 35 with EuclideanDistance

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

the class FuzzyCMeansLocalClustererTest method differentWeightsOneDimension.

/**
 * Test FCM on points that have different weights.
 */
@Test
public void differentWeightsOneDimension() {
    FuzzyCMeansLocalClusterer clusterer = new FuzzyCMeansLocalClusterer(new EuclideanDistance(), 2, BaseFuzzyCMeansClusterer.StopCondition.STABLE_CENTERS, 0.01, 10, null);
    double[][] points = new double[][] { { 1 }, { 2 }, { 3 }, { 4 }, { 5 }, { 6 } };
    DenseLocalOnHeapMatrix pntMatrix = new DenseLocalOnHeapMatrix(points);
    ArrayList<Double> weights = new ArrayList<>();
    Collections.addAll(weights, 3.0, 2.0, 1.0, 1.0, 1.0, 1.0);
    Vector[] centers1 = clusterer.cluster(pntMatrix, 2).centers();
    Vector[] centers2 = clusterer.cluster(pntMatrix, 2, weights).centers();
    Arrays.sort(centers1, Comparator.comparing(vector -> vector.getX(0)));
    Arrays.sort(centers2, Comparator.comparing(vector -> vector.getX(0)));
    assertTrue(centers1[0].get(0) - centers2[0].get(0) > 0.5);
}
Also used : Arrays(java.util.Arrays) Assert.assertTrue(org.junit.Assert.assertTrue) DistanceMeasure(org.apache.ignite.ml.math.distances.DistanceMeasure) Test(org.junit.Test) ArrayList(java.util.ArrayList) Vector(org.apache.ignite.ml.math.Vector) MathIllegalArgumentException(org.apache.ignite.ml.math.exceptions.MathIllegalArgumentException) DenseLocalOnHeapMatrix(org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix) EuclideanDistance(org.apache.ignite.ml.math.distances.EuclideanDistance) Comparator(java.util.Comparator) Collections(java.util.Collections) DenseLocalOnHeapVector(org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector) Assert.assertEquals(org.junit.Assert.assertEquals) EuclideanDistance(org.apache.ignite.ml.math.distances.EuclideanDistance) ArrayList(java.util.ArrayList) DenseLocalOnHeapMatrix(org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix) Vector(org.apache.ignite.ml.math.Vector) DenseLocalOnHeapVector(org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector) Test(org.junit.Test)

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