Search in sources :

Example 21 with DenseLocalOnHeapMatrix

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

the class FuzzyCMeansLocalClustererTest method equalWeightsTwoDimensions.

/**
 * Test FCM on points that forms four clusters on the plane.
 */
@Test
public void equalWeightsTwoDimensions() {
    FuzzyCMeansLocalClusterer clusterer = new FuzzyCMeansLocalClusterer(new EuclideanDistance(), 2, BaseFuzzyCMeansClusterer.StopCondition.STABLE_CENTERS, 0.01, 20, null);
    double[][] points = new double[][] { { -10, -10 }, { -9, -11 }, { -10, -9 }, { -11, -9 }, { 10, 10 }, { 9, 11 }, { 10, 9 }, { 11, 9 }, { -10, 10 }, { -9, 11 }, { -10, 9 }, { -11, 9 }, { 10, -10 }, { 9, -11 }, { 10, -9 }, { 11, -9 } };
    DenseLocalOnHeapMatrix pntMatrix = new DenseLocalOnHeapMatrix(points);
    FuzzyCMeansModel mdl = clusterer.cluster(pntMatrix, 4);
    Vector[] centers = mdl.centers();
    Arrays.sort(centers, Comparator.comparing(vector -> Math.atan2(vector.get(1), vector.get(0))));
    DistanceMeasure measure = mdl.distanceMeasure();
    assertEquals(0, measure.compute(centers[0], new DenseLocalOnHeapVector(new double[] { -10, -10 })), 1);
    assertEquals(0, measure.compute(centers[1], new DenseLocalOnHeapVector(new double[] { 10, -10 })), 1);
    assertEquals(0, measure.compute(centers[2], new DenseLocalOnHeapVector(new double[] { 10, 10 })), 1);
    assertEquals(0, measure.compute(centers[3], new DenseLocalOnHeapVector(new double[] { -10, 10 })), 1);
}
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) DistanceMeasure(org.apache.ignite.ml.math.distances.DistanceMeasure) Test(org.junit.Test)

Example 22 with DenseLocalOnHeapMatrix

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

the class FuzzyCMeansLocalClustererTest method testIllegalNumberOfClusters.

/**
 * Test FCM on illegal number of clusters.
 */
@Test(expected = MathIllegalArgumentException.class)
public void testIllegalNumberOfClusters() {
    FuzzyCMeansLocalClusterer clusterer = new FuzzyCMeansLocalClusterer(new EuclideanDistance(), 2, BaseFuzzyCMeansClusterer.StopCondition.STABLE_CENTERS, 0.01, 10, null);
    double[][] points = new double[][] { { 1 }, { 2 }, { 3 }, { 4 } };
    clusterer.cluster(new DenseLocalOnHeapMatrix(points), 1);
}
Also used : EuclideanDistance(org.apache.ignite.ml.math.distances.EuclideanDistance) DenseLocalOnHeapMatrix(org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix) Test(org.junit.Test)

Example 23 with DenseLocalOnHeapMatrix

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

the class IgniteCholeskyDecompositionBenchmark method runCholeskyDecomposition.

/**
 * Based on CholeskyDecompositionTest.
 */
private void runCholeskyDecomposition() {
    final DataChanger.Scale scale = new DataChanger.Scale();
    Matrix m = new DenseLocalOnHeapMatrix(scale.mutate(new double[][] { { 2.0d, -1.0d, 0.0d }, { -1.0d, 2.0d, -1.0d }, { 0.0d, -1.0d, 2.0d } }));
    CholeskyDecomposition dec = new CholeskyDecomposition(m);
    dec.getL();
    dec.getLT();
    Matrix bs = new DenseLocalOnHeapMatrix(scale.mutate(new double[][] { { 4.0, -6.0, 7.0 }, { 1.0, 1.0, 1.0 } })).transpose();
    dec.solve(bs);
    Vector b = new DenseLocalOnHeapVector(scale.mutate(new double[] { 4.0, -6.0, 7.0 }));
    dec.solve(b);
    dec.destroy();
}
Also used : CholeskyDecomposition(org.apache.ignite.ml.math.decompositions.CholeskyDecomposition) DataChanger(org.apache.ignite.yardstick.ml.DataChanger) Matrix(org.apache.ignite.ml.math.Matrix) DenseLocalOnHeapMatrix(org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix) 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)

Example 24 with DenseLocalOnHeapMatrix

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

the class LUDecompositionTest method setUp.

/**
 */
@Before
public void setUp() {
    double[][] rawMatrix = new double[][] { { 2.0d, 1.0d, 1.0d, 0.0d }, { 4.0d, 3.0d, 3.0d, 1.0d }, { 8.0d, 7.0d, 9.0d, 5.0d }, { 6.0d, 7.0d, 9.0d, 8.0d } };
    double[][] rawL = { { 1.0d, 0.0d, 0.0d, 0.0d }, { 3.0d / 4.0d, 1.0d, 0.0d, 0.0d }, { 1.0d / 2.0d, -2.0d / 7.0d, 1.0d, 0.0d }, { 1.0d / 4.0d, -3.0d / 7.0d, 1.0d / 3.0d, 1.0d } };
    double[][] rawU = { { 8.0d, 7.0d, 9.0d, 5.0d }, { 0.0d, 7.0d / 4.0d, 9.0d / 4.0d, 17.0d / 4.0d }, { 0.0d, 0.0d, -6.0d / 7.0d, -2.0d / 7.0d }, { 0.0d, 0.0d, 0.0d, 2.0d / 3.0d } };
    double[][] rawP = new double[][] { { 0, 0, 1.0d, 0 }, { 0, 0, 0, 1.0d }, { 0, 1.0d, 0, 0 }, { 1.0d, 0, 0, 0 } };
    rawPivot = new int[] { 3, 4, 2, 1 };
    testMatrix = new DenseLocalOnHeapMatrix(rawMatrix);
    testL = new DenseLocalOnHeapMatrix(rawL);
    testU = new DenseLocalOnHeapMatrix(rawU);
    testP = new DenseLocalOnHeapMatrix(rawP);
}
Also used : DenseLocalOnHeapMatrix(org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix) Before(org.junit.Before)

Example 25 with DenseLocalOnHeapMatrix

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

the class SingularValueDecompositionTest method rowsLessThanColumnsTest.

/**
 */
@Test
public void rowsLessThanColumnsTest() {
    DenseLocalOnHeapMatrix m = new DenseLocalOnHeapMatrix(new double[][] { { 2.0d, -1.0d, 0.0d }, { -1.0d, 2.0d, -1.0d } });
    SingularValueDecomposition dec = new SingularValueDecomposition(m);
    assertEquals("Unexpected value for singular values size.", 2, dec.getSingularValues().length);
    Matrix s = dec.getS();
    Matrix u = dec.getU();
    Matrix v = dec.getV();
    Matrix covariance = dec.getCovariance(0.5);
    assertNotNull("Matrix s is expected to be not null.", s);
    assertNotNull("Matrix u is expected to be not null.", u);
    assertNotNull("Matrix v is expected to be not null.", v);
    assertNotNull("Covariance matrix is expected to be not null.", covariance);
    dec.destroy();
}
Also used : 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