Search in sources :

Example 71 with Vector

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

the class KNNClassificationTest method testPredictOnIrisDataset.

/**
 */
public void testPredictOnIrisDataset() {
    IgniteUtils.setCurrentIgniteName(ignite.configuration().getIgniteInstanceName());
    LabeledDataset training = loadDatasetFromTxt(KNN_IRIS_TXT, false);
    KNNModel knnMdl = new KNNModel(7, new EuclideanDistance(), KNNStrategy.SIMPLE, training);
    Vector vector = new DenseLocalOnHeapVector(new double[] { 5.15, 3.55, 1.45, 0.25 });
    assertEquals(knnMdl.apply(vector), 1.0);
}
Also used : EuclideanDistance(org.apache.ignite.ml.math.distances.EuclideanDistance) KNNModel(org.apache.ignite.ml.knn.models.KNNModel) DenseLocalOnHeapVector(org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector) LabeledDataset(org.apache.ignite.ml.structures.LabeledDataset) Vector(org.apache.ignite.ml.math.Vector) DenseLocalOnHeapVector(org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector)

Example 72 with Vector

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

the class KNNMultipleLinearRegressionTest method testSimpleRegressionWithOneNeighbour.

/**
 */
public void testSimpleRegressionWithOneNeighbour() {
    y = new double[] { 11.0, 12.0, 13.0, 14.0, 15.0, 16.0 };
    x = new double[6][];
    x[0] = new double[] { 0, 0, 0, 0, 0 };
    x[1] = new double[] { 2.0, 0, 0, 0, 0 };
    x[2] = new double[] { 0, 3.0, 0, 0, 0 };
    x[3] = new double[] { 0, 0, 4.0, 0, 0 };
    x[4] = new double[] { 0, 0, 0, 5.0, 0 };
    x[5] = new double[] { 0, 0, 0, 0, 6.0 };
    IgniteUtils.setCurrentIgniteName(ignite.configuration().getIgniteInstanceName());
    LabeledDataset training = new LabeledDataset(x, y);
    KNNMultipleLinearRegression knnMdl = new KNNMultipleLinearRegression(1, new EuclideanDistance(), KNNStrategy.SIMPLE, training);
    Vector vector = new SparseBlockDistributedVector(new double[] { 0, 0, 0, 5.0, 0.0 });
    System.out.println(knnMdl.apply(vector));
    Assert.assertEquals(15, knnMdl.apply(vector), 1E-12);
}
Also used : EuclideanDistance(org.apache.ignite.ml.math.distances.EuclideanDistance) KNNMultipleLinearRegression(org.apache.ignite.ml.knn.regression.KNNMultipleLinearRegression) SparseBlockDistributedVector(org.apache.ignite.ml.math.impls.vector.SparseBlockDistributedVector) LabeledDataset(org.apache.ignite.ml.structures.LabeledDataset) Vector(org.apache.ignite.ml.math.Vector) SparseBlockDistributedVector(org.apache.ignite.ml.math.impls.vector.SparseBlockDistributedVector) DenseLocalOnHeapVector(org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector)

Example 73 with Vector

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

the class KNNMultipleLinearRegressionTest method testLongly.

/**
 */
public void testLongly() {
    y = new double[] { 60323, 61122, 60171, 61187, 63221, 63639, 64989, 63761, 66019, 68169, 66513, 68655, 69564, 69331, 70551 };
    x = new double[15][];
    x[0] = new double[] { 83.0, 234289, 2356, 1590, 107608, 1947 };
    x[1] = new double[] { 88.5, 259426, 2325, 1456, 108632, 1948 };
    x[2] = new double[] { 88.2, 258054, 3682, 1616, 109773, 1949 };
    x[3] = new double[] { 89.5, 284599, 3351, 1650, 110929, 1950 };
    x[4] = new double[] { 96.2, 328975, 2099, 3099, 112075, 1951 };
    x[5] = new double[] { 98.1, 346999, 1932, 3594, 113270, 1952 };
    x[6] = new double[] { 99.0, 365385, 1870, 3547, 115094, 1953 };
    x[7] = new double[] { 100.0, 363112, 3578, 3350, 116219, 1954 };
    x[8] = new double[] { 101.2, 397469, 2904, 3048, 117388, 1955 };
    x[9] = new double[] { 108.4, 442769, 2936, 2798, 120445, 1957 };
    x[10] = new double[] { 110.8, 444546, 4681, 2637, 121950, 1958 };
    x[11] = new double[] { 112.6, 482704, 3813, 2552, 123366, 1959 };
    x[12] = new double[] { 114.2, 502601, 3931, 2514, 125368, 1960 };
    x[13] = new double[] { 115.7, 518173, 4806, 2572, 127852, 1961 };
    x[14] = new double[] { 116.9, 554894, 4007, 2827, 130081, 1962 };
    IgniteUtils.setCurrentIgniteName(ignite.configuration().getIgniteInstanceName());
    LabeledDataset training = new LabeledDataset(x, y);
    KNNMultipleLinearRegression knnMdl = new KNNMultipleLinearRegression(3, new EuclideanDistance(), KNNStrategy.SIMPLE, training);
    Vector vector = new DenseLocalOnHeapVector(new double[] { 104.6, 419180, 2822, 2857, 118734, 1956 });
    System.out.println(knnMdl.apply(vector));
    Assert.assertEquals(67857, knnMdl.apply(vector), 2000);
}
Also used : EuclideanDistance(org.apache.ignite.ml.math.distances.EuclideanDistance) KNNMultipleLinearRegression(org.apache.ignite.ml.knn.regression.KNNMultipleLinearRegression) DenseLocalOnHeapVector(org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector) LabeledDataset(org.apache.ignite.ml.structures.LabeledDataset) Vector(org.apache.ignite.ml.math.Vector) SparseBlockDistributedVector(org.apache.ignite.ml.math.impls.vector.SparseBlockDistributedVector) DenseLocalOnHeapVector(org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector)

Example 74 with Vector

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

the class SingleElementVectorViewConstructorTest method basicTest.

/**
 */
private void basicTest(int size, int idx) {
    final Double expVal = (double) (size - idx);
    Vector orig = helper.newSample(size, idx, expVal);
    SingleElementVectorView svv = new SingleElementVectorView(orig, idx);
    assertEquals("Size differs from expected", size, svv.size());
    assertTrue("Expect value " + expVal + " at index " + idx + " for size " + size, expVal.equals(svv.get(idx)));
    final double delta = 1.0;
    svv.set(idx, expVal - delta);
    assertTrue("Expect value " + expVal + " at index " + idx + " for size " + size, expVal.equals(orig.get(idx) + delta));
    final Double zero = 0.0;
    for (int i = 0; i < size; i++) {
        if (i == idx)
            continue;
        assertTrue("Expect zero at index " + i + " for size " + size, zero.equals(svv.get(i)));
        boolean eCaught = false;
        try {
            svv.set(i, 1.0);
        } catch (UnsupportedOperationException uoe) {
            eCaught = true;
        }
        assertTrue("Expect " + UnsupportedOperationException.class.getSimpleName() + " at index " + i + " for size " + size, eCaught);
    }
}
Also used : Vector(org.apache.ignite.ml.math.Vector) UnsupportedOperationException(org.apache.ignite.ml.math.exceptions.UnsupportedOperationException)

Example 75 with Vector

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

the class SparseDistributedVectorTest method testCopy.

/**
 */
public void testCopy() {
    IgniteUtils.setCurrentIgniteName(ignite.configuration().getIgniteInstanceName());
    sparseDistributedVector = new SparseDistributedVector(size, StorageConstants.RANDOM_ACCESS_MODE);
    Vector cp = sparseDistributedVector.copy();
    assertNotNull(cp);
    for (int i = 0; i < size; i++) assertEquals(UNEXPECTED_VAL, cp.get(i), sparseDistributedVector.get(i), PRECISION);
}
Also used : Vector(org.apache.ignite.ml.math.Vector)

Aggregations

Vector (org.apache.ignite.ml.math.Vector)116 DenseLocalOnHeapVector (org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector)62 Test (org.junit.Test)29 EuclideanDistance (org.apache.ignite.ml.math.distances.EuclideanDistance)20 DenseLocalOnHeapMatrix (org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix)20 Matrix (org.apache.ignite.ml.math.Matrix)19 SparseDistributedMatrix (org.apache.ignite.ml.math.impls.matrix.SparseDistributedMatrix)17 Random (java.util.Random)12 ArrayList (java.util.ArrayList)11 DistanceMeasure (org.apache.ignite.ml.math.distances.DistanceMeasure)10 Arrays (java.util.Arrays)9 Ignite (org.apache.ignite.Ignite)9 SparseDistributedMatrixStorage (org.apache.ignite.ml.math.impls.storage.matrix.SparseDistributedMatrixStorage)9 LabeledDataset (org.apache.ignite.ml.structures.LabeledDataset)9 UUID (java.util.UUID)8 Collections (java.util.Collections)7 List (java.util.List)7 MathIllegalArgumentException (org.apache.ignite.ml.math.exceptions.MathIllegalArgumentException)7 DenseLocalOffHeapVector (org.apache.ignite.ml.math.impls.vector.DenseLocalOffHeapVector)7 HashMap (java.util.HashMap)6