Search in sources :

Example 96 with Vector

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

the class ParametricVectorGeneratorTest method testGet.

/**
 */
@Test
public void testGet() {
    Vector vec = new ParametricVectorGenerator(() -> 2., t -> t, t -> 2 * t, t -> 3 * t, t -> 100.).get();
    assertEquals(4, vec.size());
    assertArrayEquals(new double[] { 2., 4., 6., 100. }, vec.asArray(), 1e-7);
}
Also used : Vector(org.apache.ignite.ml.math.primitives.vector.Vector) Assert.assertArrayEquals(org.junit.Assert.assertArrayEquals) Test(org.junit.Test) Assert.assertEquals(org.junit.Assert.assertEquals) Vector(org.apache.ignite.ml.math.primitives.vector.Vector) Test(org.junit.Test)

Example 97 with Vector

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

the class VectorGeneratorTest method duplicateRandomFeatures.

/**
 */
@Test
public void duplicateRandomFeatures() {
    VectorGenerator g1 = VectorGeneratorPrimitives.constant(VectorUtils.of(1., 2., 3., 4.)).duplicateRandomFeatures(2, 1L);
    double[] exp = { 1., 2., 3., 4., 3., 1. };
    Vector v1 = g1.get();
    Vector v2 = g1.get();
    assertArrayEquals(exp, v1.asArray(), 1e-7);
    try {
        assertArrayEquals(v1.asArray(), v2.asArray(), 1e-7);
    } catch (ArrayComparisonFailure e) {
    // this is valid situation - duplicator should get different features
    }
}
Also used : ArrayComparisonFailure(org.junit.internal.ArrayComparisonFailure) Vector(org.apache.ignite.ml.math.primitives.vector.Vector) Test(org.junit.Test)

Example 98 with Vector

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

the class VectorGeneratorTest method rotate.

/**
 */
@Test
public void rotate() {
    double[] angles = { 0., Math.PI / 2, -Math.PI / 2, Math.PI, 2 * Math.PI, Math.PI / 4 };
    Vector[] exp = new Vector[] { VectorUtils.of(1., 0., 100.), VectorUtils.of(0., -1., 100.), VectorUtils.of(0., 1., 100.), VectorUtils.of(-1., 0., 100.), VectorUtils.of(1., 0., 100.), VectorUtils.of(0.707, -0.707, 100.) };
    for (int i = 0; i < angles.length; i++) {
        Vector res = VectorGeneratorPrimitives.constant(VectorUtils.of(1., 0., 100.)).rotate(angles[i]).get();
        assertArrayEquals(exp[i].asArray(), res.asArray(), 1e-3);
    }
}
Also used : Vector(org.apache.ignite.ml.math.primitives.vector.Vector) Test(org.junit.Test)

Example 99 with Vector

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

the class SVMBinaryTrainerTest method testUpdate.

/**
 */
@Test
public void testUpdate() {
    Map<Integer, double[]> cacheMock = new HashMap<>();
    for (int i = 0; i < twoLinearlySeparableClasses.length; i++) cacheMock.put(i, twoLinearlySeparableClasses[i]);
    SVMLinearClassificationTrainer trainer = new SVMLinearClassificationTrainer().withAmountOfIterations(1000).withSeed(1234L);
    Vectorizer<Integer, double[], Integer, Double> vectorizer = new DoubleArrayVectorizer<Integer>().labeled(Vectorizer.LabelCoordinate.FIRST);
    SVMLinearClassificationModel originalMdl = trainer.fit(cacheMock, parts, vectorizer);
    SVMLinearClassificationModel updatedOnSameDS = trainer.update(originalMdl, cacheMock, parts, vectorizer);
    SVMLinearClassificationModel updatedOnEmptyDS = trainer.update(originalMdl, new HashMap<>(), parts, vectorizer);
    Vector v = VectorUtils.of(100, 10);
    TestUtils.assertEquals(originalMdl.predict(v), updatedOnSameDS.predict(v), PRECISION);
    TestUtils.assertEquals(originalMdl.predict(v), updatedOnEmptyDS.predict(v), PRECISION);
}
Also used : HashMap(java.util.HashMap) Vector(org.apache.ignite.ml.math.primitives.vector.Vector) TrainerTest(org.apache.ignite.ml.common.TrainerTest) Test(org.junit.Test)

Example 100 with Vector

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

the class VectorGeneratorsFamilyTest method testGet.

/**
 */
@Test
public void testGet() {
    VectorGeneratorsFamily family = new VectorGeneratorsFamily.Builder().add(() -> VectorUtils.of(0.)).add(() -> VectorUtils.of(1.)).add(() -> VectorUtils.of(2.)).build(0L);
    Set<Double> validValues = DoubleStream.of(0., 1., 2.).boxed().collect(Collectors.toSet());
    for (int i = 0; i < 100; i++) {
        Vector vector = family.get();
        assertTrue(validValues.contains(vector.get(0)));
    }
}
Also used : Vector(org.apache.ignite.ml.math.primitives.vector.Vector) Test(org.junit.Test)

Aggregations

Vector (org.apache.ignite.ml.math.primitives.vector.Vector)265 DenseVector (org.apache.ignite.ml.math.primitives.vector.impl.DenseVector)95 Test (org.junit.Test)94 Ignite (org.apache.ignite.Ignite)78 LabeledVector (org.apache.ignite.ml.structures.LabeledVector)49 HashMap (java.util.HashMap)39 SandboxMLCache (org.apache.ignite.examples.ml.util.SandboxMLCache)38 DummyVectorizer (org.apache.ignite.ml.dataset.feature.extractor.impl.DummyVectorizer)26 FileNotFoundException (java.io.FileNotFoundException)22 TrainerTest (org.apache.ignite.ml.common.TrainerTest)22 DecisionTreeClassificationTrainer (org.apache.ignite.ml.tree.DecisionTreeClassificationTrainer)21 DecisionTreeModel (org.apache.ignite.ml.tree.DecisionTreeModel)21 Serializable (java.io.Serializable)19 IgniteCache (org.apache.ignite.IgniteCache)18 EncoderTrainer (org.apache.ignite.ml.preprocessing.encoding.EncoderTrainer)16 Cache (javax.cache.Cache)15 DoubleArrayVectorizer (org.apache.ignite.ml.dataset.feature.extractor.impl.DoubleArrayVectorizer)15 EuclideanDistance (org.apache.ignite.ml.math.distances.EuclideanDistance)14 ArrayList (java.util.ArrayList)12 ModelsComposition (org.apache.ignite.ml.composition.ModelsComposition)12