Search in sources :

Example 11 with Vector

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

the class MatrixImplementationsTest method testLikeVector.

/** */
@Test
public void testLikeVector() {
    consumeSampleMatrix((m, desc) -> {
        if (likeVectorTypesMap().containsKey(m.getClass())) {
            Vector likeVector = m.likeVector(m.columnSize());
            assertNotNull(likeVector);
            assertEquals("Unexpected value for " + desc, likeVector.size(), m.columnSize());
            return;
        }
        boolean expECaught = false;
        try {
            m.likeVector(1);
        } catch (UnsupportedOperationException uoe) {
            expECaught = true;
        }
        assertTrue("Expected exception was not caught for " + desc, expECaught);
    });
}
Also used : RandomVector(org.apache.ignite.ml.math.impls.vector.RandomVector) DenseLocalOffHeapVector(org.apache.ignite.ml.math.impls.vector.DenseLocalOffHeapVector) Vector(org.apache.ignite.ml.math.Vector) SparseLocalVector(org.apache.ignite.ml.math.impls.vector.SparseLocalVector) DenseLocalOnHeapVector(org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector) UnsupportedOperationException(org.apache.ignite.ml.math.exceptions.UnsupportedOperationException) Test(org.junit.Test) ExternalizeTest(org.apache.ignite.ml.math.ExternalizeTest)

Example 12 with Vector

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

the class MatrixImplementationsTest method testTimesVector.

/** */
@Test
public void testTimesVector() {
    consumeSampleMatrix((m, desc) -> {
        if (ignore(m.getClass()))
            return;
        double[][] data = fillAndReturn(m);
        double[] arr = fillArray(m.columnSize());
        Vector times = m.times(new DenseLocalOnHeapVector(arr));
        assertEquals("Unexpected vector size for " + desc, times.size(), m.rowSize());
        for (int i = 0; i < m.rowSize(); i++) {
            double exp = 0.0;
            for (int j = 0; j < m.columnSize(); j++) exp += arr[j] * data[i][j];
            assertEquals("Unexpected value for " + desc + " at " + i, times.get(i), exp, 0d);
        }
        testInvalidCardinality(() -> m.times(new DenseLocalOnHeapVector(m.columnSize() + 1)), desc);
    });
}
Also used : DenseLocalOnHeapVector(org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector) RandomVector(org.apache.ignite.ml.math.impls.vector.RandomVector) DenseLocalOffHeapVector(org.apache.ignite.ml.math.impls.vector.DenseLocalOffHeapVector) Vector(org.apache.ignite.ml.math.Vector) SparseLocalVector(org.apache.ignite.ml.math.impls.vector.SparseLocalVector) DenseLocalOnHeapVector(org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector) Test(org.junit.Test) ExternalizeTest(org.apache.ignite.ml.math.ExternalizeTest)

Example 13 with Vector

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

the class CacheVectorTest method testMax.

/** */
public void testMax() {
    IgniteUtils.setCurrentIgniteName(ignite.configuration().getIgniteInstanceName());
    IdentityValueMapper valMapper = new IdentityValueMapper();
    CacheVector<Integer, Double> cacheVector = new CacheVector<>(size, getCache(), keyMapper, valMapper);
    Vector testVec = new DenseLocalOnHeapVector(IntStream.range(0, size).asDoubleStream().toArray());
    cacheVector.assign(testVec);
    assertEquals("Unexpected value.", cacheVector.maxValue(), testVec.get(size - 1), 0d);
}
Also used : IdentityValueMapper(org.apache.ignite.ml.math.IdentityValueMapper) Vector(org.apache.ignite.ml.math.Vector)

Example 14 with Vector

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

the class KMeansDistributedClustererTest method testClusterizationOnDatasetWithObviousStructure.

/** */
@Test
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 }));
    int centersCnt = centers.size();
    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);
    Vector[] mc = new Vector[centersCnt];
    Arrays.fill(mc, VectorUtils.zeroes(2));
    int centIndex = 0;
    int totalCount = 0;
    List<Vector> massCenters = new ArrayList<>();
    for (Integer count : centers.keySet()) {
        for (int i = 0; i < count; i++) {
            DenseLocalOnHeapVector pnt = (DenseLocalOnHeapVector) new DenseLocalOnHeapVector(2).assign(centers.get(count));
            // pertrubate point on random value.
            pnt.map(val -> val + rnd.nextDouble() * squareSideLen / 100);
            mc[centIndex] = mc[centIndex].plus(pnt);
            points.assignRow(permutation.get(totalCount), pnt);
            totalCount++;
        }
        massCenters.add(mc[centIndex].times(1 / (double) count));
        centIndex++;
    }
    EuclideanDistance dist = new EuclideanDistance();
    OrderedNodesComparator comp = new OrderedNodesComparator(centers.values().toArray(new Vector[] {}), dist);
    massCenters.sort(comp);
    KMeansDistributedClusterer clusterer = new KMeansDistributedClusterer(dist, 3, 100, 1L);
    KMeansModel mdl = clusterer.cluster(points, 4);
    Vector[] resCenters = mdl.centers();
    Arrays.sort(resCenters, comp);
    checkIsInEpsilonNeighbourhood(resCenters, massCenters.toArray(new Vector[] {}), 30.0);
}
Also used : SparseDistributedMatrix(org.apache.ignite.ml.math.impls.matrix.SparseDistributedMatrix) 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) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 15 with Vector

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

the class LUDecompositionTest method getPivot.

/** */
@Test
public void getPivot() throws Exception {
    Vector pivot = new LUDecomposition(testMatrix).getPivot();
    assertEquals("Unexpected pivot size.", rawPivot.length, pivot.size());
    for (int i = 0; i < testU.rowSize(); i++) assertEquals("Unexpected value at " + i, rawPivot[i], (int) pivot.get(i) + 1);
}
Also used : Vector(org.apache.ignite.ml.math.Vector) DenseLocalOnHeapVector(org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector) Test(org.junit.Test)

Aggregations

Vector (org.apache.ignite.ml.math.Vector)44 Test (org.junit.Test)17 DenseLocalOnHeapVector (org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector)13 DenseLocalOffHeapVector (org.apache.ignite.ml.math.impls.vector.DenseLocalOffHeapVector)7 IdentityValueMapper (org.apache.ignite.ml.math.IdentityValueMapper)5 Matrix (org.apache.ignite.ml.math.Matrix)5 UnsupportedOperationException (org.apache.ignite.ml.math.exceptions.UnsupportedOperationException)5 SparseLocalVector (org.apache.ignite.ml.math.impls.vector.SparseLocalVector)5 ExternalizeTest (org.apache.ignite.ml.math.ExternalizeTest)4 RandomVector (org.apache.ignite.ml.math.impls.vector.RandomVector)4 DenseLocalOnHeapMatrix (org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix)3 BiConsumer (java.util.function.BiConsumer)1 Function (java.util.function.Function)1 CardinalityException (org.apache.ignite.ml.math.exceptions.CardinalityException)1 PivotedMatrixView (org.apache.ignite.ml.math.impls.matrix.PivotedMatrixView)1 SparseDistributedMatrix (org.apache.ignite.ml.math.impls.matrix.SparseDistributedMatrix)1 AbstractVector (org.apache.ignite.ml.math.impls.vector.AbstractVector)1 MatrixUtil.likeVector (org.apache.ignite.ml.math.util.MatrixUtil.likeVector)1 GridCommonAbstractTest (org.apache.ignite.testframework.junits.common.GridCommonAbstractTest)1 Assert.assertFalse (org.junit.Assert.assertFalse)1