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);
}
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);
}
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);
}
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);
}
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);
}
Aggregations