Search in sources :

Example 61 with DenseLocalOnHeapMatrix

use of org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix 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 62 with DenseLocalOnHeapMatrix

use of org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix 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 63 with DenseLocalOnHeapMatrix

use of org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix 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 64 with DenseLocalOnHeapMatrix

use of org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix 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)

Example 65 with DenseLocalOnHeapMatrix

use of org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix in project ignite by apache.

the class LUDecompositionTest method solveMtx.

/**
 */
@Test
public void solveMtx() throws Exception {
    Matrix sol = new LUDecomposition(new PivotedMatrixView(testMatrix)).solve(new DenseLocalOnHeapMatrix(testMatrix.rowSize(), testMatrix.rowSize()));
    assertEquals("Wrong solution matrix row size.", testMatrix.rowSize(), sol.rowSize());
    assertEquals("Wrong solution matrix column size.", testMatrix.rowSize(), sol.columnSize());
    for (int row = 0; row < sol.rowSize(); row++) for (int col = 0; col < sol.columnSize(); col++) assertEquals("Unexpected P value at (" + row + "," + col + ").", 0d, sol.getX(row, col), 0.0000001d);
}
Also used : PivotedMatrixView(org.apache.ignite.ml.math.impls.matrix.PivotedMatrixView) Matrix(org.apache.ignite.ml.math.Matrix) DenseLocalOnHeapMatrix(org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix) DenseLocalOnHeapMatrix(org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix) Test(org.junit.Test)

Aggregations

DenseLocalOnHeapMatrix (org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix)67 Test (org.junit.Test)33 Matrix (org.apache.ignite.ml.math.Matrix)28 DenseLocalOnHeapVector (org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector)19 Vector (org.apache.ignite.ml.math.Vector)17 EuclideanDistance (org.apache.ignite.ml.math.distances.EuclideanDistance)14 ArrayList (java.util.ArrayList)8 DistanceMeasure (org.apache.ignite.ml.math.distances.DistanceMeasure)6 Random (java.util.Random)5 MathIllegalArgumentException (org.apache.ignite.ml.math.exceptions.MathIllegalArgumentException)5 Arrays (java.util.Arrays)4 Collections (java.util.Collections)4 Comparator (java.util.Comparator)4 MLPArchitecture (org.apache.ignite.ml.nn.architecture.MLPArchitecture)4 Assert.assertEquals (org.junit.Assert.assertEquals)4 Assert.assertTrue (org.junit.Assert.assertTrue)4 Ignite (org.apache.ignite.Ignite)3 KMeansLocalClusterer (org.apache.ignite.ml.clustering.KMeansLocalClusterer)3 BaseFuzzyCMeansClusterer (org.apache.ignite.ml.clustering.BaseFuzzyCMeansClusterer)2 FuzzyCMeansLocalClusterer (org.apache.ignite.ml.clustering.FuzzyCMeansLocalClusterer)2