Search in sources :

Example 1 with DenseLocalOnHeapVector

use of org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector in project ignite by apache.

the class OLSMultipleLinearRegressionTest method createRegression.

/** */
@Override
protected OLSMultipleLinearRegression createRegression() {
    OLSMultipleLinearRegression regression = new OLSMultipleLinearRegression();
    regression.newSampleData(new DenseLocalOnHeapVector(y), new DenseLocalOnHeapMatrix(x));
    return regression;
}
Also used : DenseLocalOnHeapVector(org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector) DenseLocalOnHeapMatrix(org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix)

Example 2 with DenseLocalOnHeapVector

use of org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector in project ignite by apache.

the class OLSMultipleLinearRegressionTest method testYVariance.

/**
     * test calculateYVariance
     */
@Test
public void testYVariance() {
    // assumes: y = new double[]{11.0, 12.0, 13.0, 14.0, 15.0, 16.0};
    OLSMultipleLinearRegression mdl = new OLSMultipleLinearRegression();
    mdl.newSampleData(new DenseLocalOnHeapVector(y), new DenseLocalOnHeapMatrix(x));
    TestUtils.assertEquals(mdl.calculateYVariance(), 3.5, 0);
}
Also used : DenseLocalOnHeapVector(org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector) DenseLocalOnHeapMatrix(org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix) Test(org.junit.Test)

Example 3 with DenseLocalOnHeapVector

use of org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector 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 4 with DenseLocalOnHeapVector

use of org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector in project ignite by apache.

the class DiagonalMatrixTest method testSetGet.

/** */
@Test
public void testSetGet() {
    verifyDiagonal(testMatrix);
    final int size = MathTestConstants.STORAGE_SIZE;
    for (Matrix m : new Matrix[] { new DenseLocalOnHeapMatrix(size + 1, size), new DenseLocalOnHeapMatrix(size, size + 1) }) {
        fillMatrix(m);
        verifyDiagonal(new DiagonalMatrix(m));
    }
    final double[] data = new double[size];
    for (int i = 0; i < size; i++) data[i] = 1 + i;
    final Matrix m = new DiagonalMatrix(new DenseLocalOnHeapVector(data));
    assertEquals("Rows in matrix constructed from vector", size, m.rowSize());
    assertEquals("Cols in matrix constructed from vector", size, m.columnSize());
    for (int i = 0; i < size; i++) assertEquals(UNEXPECTED_VALUE + " at vector index " + i, data[i], m.get(i, i), 0d);
    verifyDiagonal(m);
    final Matrix m1 = new DiagonalMatrix(data);
    assertEquals("Rows in matrix constructed from array", size, m1.rowSize());
    assertEquals("Cols in matrix constructed from array", size, m1.columnSize());
    for (int i = 0; i < size; i++) assertEquals(UNEXPECTED_VALUE + " at array index " + i, data[i], m1.get(i, i), 0d);
    verifyDiagonal(m1);
}
Also used : Matrix(org.apache.ignite.ml.math.Matrix) DenseLocalOnHeapVector(org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector) ExternalizeTest(org.apache.ignite.ml.math.ExternalizeTest) Test(org.junit.Test)

Example 5 with DenseLocalOnHeapVector

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

Aggregations

DenseLocalOnHeapVector (org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector)16 Test (org.junit.Test)12 Vector (org.apache.ignite.ml.math.Vector)7 DenseLocalOnHeapMatrix (org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix)7 ExternalizeTest (org.apache.ignite.ml.math.ExternalizeTest)4 Matrix (org.apache.ignite.ml.math.Matrix)4 Path (java.nio.file.Path)1 PivotedMatrixView (org.apache.ignite.ml.math.impls.matrix.PivotedMatrixView)1 SparseDistributedMatrix (org.apache.ignite.ml.math.impls.matrix.SparseDistributedMatrix)1 DenseLocalOffHeapVector (org.apache.ignite.ml.math.impls.vector.DenseLocalOffHeapVector)1 RandomVector (org.apache.ignite.ml.math.impls.vector.RandomVector)1 SparseLocalVector (org.apache.ignite.ml.math.impls.vector.SparseLocalVector)1 GridCommonAbstractTest (org.apache.ignite.testframework.junits.common.GridCommonAbstractTest)1