Search in sources :

Example 71 with DenseVector

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

the class EncoderTrainerTest method testFitWithExceptionOnMissedEncodedFeatureIndex.

/**
 * Tests {@code fit()} method.
 */
@Test(expected = org.apache.ignite.ml.math.exceptions.preprocessing.IllegalFeatureTypeException.class)
public void testFitWithExceptionOnMissedEncodedFeatureIndex() {
    Map<Integer, Vector> data = new HashMap<>();
    data.put(1, new DenseVector(new Serializable[] { 1.0, "Monday", "September" }));
    data.put(2, new DenseVector(new Serializable[] { 2.0, "Monday", "August" }));
    data.put(3, new DenseVector(new Serializable[] { 3.0, "Monday", "August" }));
    data.put(4, new DenseVector(new Serializable[] { 4.0, "Friday", "June" }));
    data.put(5, new DenseVector(new Serializable[] { 5.0, "Friday", "June" }));
    data.put(6, new DenseVector(new Serializable[] { 6.0, "Sunday", "August" }));
    final Vectorizer<Integer, Vector, Integer, Double> vectorizer = new DummyVectorizer<Integer>(1, 2).labeled(0);
    DatasetBuilder<Integer, Vector> datasetBuilder = new LocalDatasetBuilder<>(data, parts);
    EncoderTrainer<Integer, Vector> strEncoderTrainer = new EncoderTrainer<Integer, Vector>().withEncoderType(EncoderType.STRING_ENCODER).withEncodedFeature(0);
    EncoderPreprocessor<Integer, Vector> preprocessor = strEncoderTrainer.fit(TestUtils.testEnvBuilder(), datasetBuilder, vectorizer);
    assertArrayEquals(new double[] { 0.0, 2.0 }, preprocessor.apply(7, new DenseVector(new Serializable[] { 7.0, "Monday", "September" })).features().asArray(), 1e-8);
}
Also used : Serializable(java.io.Serializable) HashMap(java.util.HashMap) LocalDatasetBuilder(org.apache.ignite.ml.dataset.impl.local.LocalDatasetBuilder) Vector(org.apache.ignite.ml.math.primitives.vector.Vector) DenseVector(org.apache.ignite.ml.math.primitives.vector.impl.DenseVector) DenseVector(org.apache.ignite.ml.math.primitives.vector.impl.DenseVector) TrainerTest(org.apache.ignite.ml.common.TrainerTest) Test(org.junit.Test)

Example 72 with DenseVector

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

the class EncoderTrainerTest method testFitOnStringCategorialFeatures.

/**
 * Tests {@code fit()} method.
 */
@Test
public void testFitOnStringCategorialFeatures() {
    Map<Integer, Vector> data = new HashMap<>();
    data.put(1, new DenseVector(new Serializable[] { 1.0, "Monday", "September" }));
    data.put(2, new DenseVector(new Serializable[] { 2.0, "Monday", "August" }));
    data.put(3, new DenseVector(new Serializable[] { 3.0, "Monday", "August" }));
    data.put(4, new DenseVector(new Serializable[] { 4.0, "Friday", "June" }));
    data.put(5, new DenseVector(new Serializable[] { 5.0, "Friday", "June" }));
    data.put(6, new DenseVector(new Serializable[] { 6.0, "Sunday", "August" }));
    final Vectorizer<Integer, Vector, Integer, Double> vectorizer = new DummyVectorizer<Integer>(1, 2).labeled(0);
    DatasetBuilder<Integer, Vector> datasetBuilder = new LocalDatasetBuilder<>(data, parts);
    EncoderTrainer<Integer, Vector> strEncoderTrainer = new EncoderTrainer<Integer, Vector>().withEncoderType(EncoderType.STRING_ENCODER).withEncodedFeature(0).withEncodedFeature(1);
    EncoderPreprocessor<Integer, Vector> preprocessor = strEncoderTrainer.fit(TestUtils.testEnvBuilder(), datasetBuilder, vectorizer);
    assertArrayEquals(new double[] { 0.0, 2.0 }, preprocessor.apply(7, new DenseVector(new Serializable[] { 7.0, "Monday", "September" })).features().asArray(), 1e-8);
}
Also used : Serializable(java.io.Serializable) HashMap(java.util.HashMap) LocalDatasetBuilder(org.apache.ignite.ml.dataset.impl.local.LocalDatasetBuilder) Vector(org.apache.ignite.ml.math.primitives.vector.Vector) DenseVector(org.apache.ignite.ml.math.primitives.vector.impl.DenseVector) DenseVector(org.apache.ignite.ml.math.primitives.vector.impl.DenseVector) TrainerTest(org.apache.ignite.ml.common.TrainerTest) Test(org.junit.Test)

Example 73 with DenseVector

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

the class EncoderTrainerTest method testFitOnStringCategorialFeaturesWithFrequencyEncoding.

/**
 * Tests {@code fit()} method.
 */
@Test
public void testFitOnStringCategorialFeaturesWithFrequencyEncoding() {
    Map<Integer, Vector> data = new HashMap<>();
    data.put(1, new DenseVector(new Serializable[] { "Monday", "September" }));
    data.put(2, new DenseVector(new Serializable[] { "Monday", "August" }));
    data.put(3, new DenseVector(new Serializable[] { "Monday", "August" }));
    data.put(4, new DenseVector(new Serializable[] { "Friday", "June" }));
    data.put(5, new DenseVector(new Serializable[] { "Friday", "June" }));
    data.put(6, new DenseVector(new Serializable[] { "Sunday", "August" }));
    final Vectorizer<Integer, Vector, Integer, Double> vectorizer = new DummyVectorizer<>(0, 1);
    DatasetBuilder<Integer, Vector> datasetBuilder = new LocalDatasetBuilder<>(data, parts);
    EncoderTrainer<Integer, Vector> strEncoderTrainer = new EncoderTrainer<Integer, Vector>().withEncoderType(EncoderType.FREQUENCY_ENCODER).withEncodedFeature(0).withEncodedFeature(1);
    EncoderPreprocessor<Integer, Vector> preprocessor = strEncoderTrainer.fit(TestUtils.testEnvBuilder(), datasetBuilder, vectorizer);
    assertArrayEquals(new double[] { 0.5, 0.166 }, preprocessor.apply(7, new DenseVector(new Serializable[] { "Monday", "September" })).features().asArray(), 0.1);
    assertArrayEquals(new double[] { 0.33, 0.5 }, preprocessor.apply(7, new DenseVector(new Serializable[] { "Friday", "August" })).features().asArray(), 0.1);
    assertArrayEquals(new double[] { 0.166, 0.33 }, preprocessor.apply(7, new DenseVector(new Serializable[] { "Sunday", "June" })).features().asArray(), 0.1);
}
Also used : Serializable(java.io.Serializable) HashMap(java.util.HashMap) DummyVectorizer(org.apache.ignite.ml.dataset.feature.extractor.impl.DummyVectorizer) LocalDatasetBuilder(org.apache.ignite.ml.dataset.impl.local.LocalDatasetBuilder) Vector(org.apache.ignite.ml.math.primitives.vector.Vector) DenseVector(org.apache.ignite.ml.math.primitives.vector.impl.DenseVector) DenseVector(org.apache.ignite.ml.math.primitives.vector.impl.DenseVector) TrainerTest(org.apache.ignite.ml.common.TrainerTest) Test(org.junit.Test)

Example 74 with DenseVector

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

the class LossFunctionsTest method testL1.

/**
 */
@Test
public void testL1() {
    IgniteDifferentiableVectorToDoubleFunction f = LossFunctions.L1.apply(new DenseVector(new double[] { 2.0, 1.0 }));
    assertNotNull(f);
    test(new double[] { 1.0, 3.0 }, f);
}
Also used : IgniteDifferentiableVectorToDoubleFunction(org.apache.ignite.ml.math.functions.IgniteDifferentiableVectorToDoubleFunction) DenseVector(org.apache.ignite.ml.math.primitives.vector.impl.DenseVector) Test(org.junit.Test)

Example 75 with DenseVector

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

the class LossFunctionsTest method testLOG.

/**
 */
@Test
public void testLOG() {
    IgniteDifferentiableVectorToDoubleFunction f = LossFunctions.LOG.apply(new DenseVector(new double[] { 2.0, 1.0 }));
    assertNotNull(f);
    test(new double[] { 1.0, 3.0 }, f);
}
Also used : IgniteDifferentiableVectorToDoubleFunction(org.apache.ignite.ml.math.functions.IgniteDifferentiableVectorToDoubleFunction) DenseVector(org.apache.ignite.ml.math.primitives.vector.impl.DenseVector) Test(org.junit.Test)

Aggregations

DenseVector (org.apache.ignite.ml.math.primitives.vector.impl.DenseVector)101 Vector (org.apache.ignite.ml.math.primitives.vector.Vector)59 Test (org.junit.Test)59 Serializable (java.io.Serializable)16 SparseVector (org.apache.ignite.ml.math.primitives.vector.impl.SparseVector)14 HashMap (java.util.HashMap)13 DenseMatrix (org.apache.ignite.ml.math.primitives.matrix.impl.DenseMatrix)13 DummyVectorizer (org.apache.ignite.ml.dataset.feature.extractor.impl.DummyVectorizer)10 LabeledVector (org.apache.ignite.ml.structures.LabeledVector)10 RendezvousAffinityFunction (org.apache.ignite.cache.affinity.rendezvous.RendezvousAffinityFunction)9 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)9 HashSet (java.util.HashSet)7 TrainerTest (org.apache.ignite.ml.common.TrainerTest)7 KMeansModel (org.apache.ignite.ml.clustering.kmeans.KMeansModel)5 LocalDatasetBuilder (org.apache.ignite.ml.dataset.impl.local.LocalDatasetBuilder)5 EuclideanDistance (org.apache.ignite.ml.math.distances.EuclideanDistance)5 IgniteDifferentiableVectorToDoubleFunction (org.apache.ignite.ml.math.functions.IgniteDifferentiableVectorToDoubleFunction)5 MLPArchitecture (org.apache.ignite.ml.nn.architecture.MLPArchitecture)5 OneHotEncoderPreprocessor (org.apache.ignite.ml.preprocessing.encoding.onehotencoder.OneHotEncoderPreprocessor)4 Random (java.util.Random)3