use of org.apache.ignite.ml.math.primitives.vector.impl.DenseVector in project ignite by apache.
the class VectorNormTest method getDistanceSquaredTest.
/**
*/
@Test
public void getDistanceSquaredTest() {
consumeSampleVectors((v, desc) -> {
// IMPL NOTE this initialises vector
new VectorImplementationsTest.ElementsChecker(v, desc);
final int size = v.size();
final Vector vOnHeap = new DenseVector(size);
invertValues(v, vOnHeap);
for (int idx = 0; idx < size; idx++) {
final double exp = v.get(idx);
final int idxMirror = size - 1 - idx;
assertTrue("On heap vector difference at " + desc + ", idx " + idx, exp - vOnHeap.get(idxMirror) == 0);
}
// IMPL NOTE this won't mutate vOnHeap
final double exp = vOnHeap.minus(v).getLengthSquared();
final VectorImplementationsTest.Metric metric = new VectorImplementationsTest.Metric(exp, v.getDistanceSquared(vOnHeap));
assertTrue("On heap vector not close enough at " + desc + ", " + metric, metric.closeEnough());
});
}
use of org.apache.ignite.ml.math.primitives.vector.impl.DenseVector in project ignite by apache.
the class EncoderTrainerTest method testFitWithUnknownStringValueInTheGivenData.
/**
* Tests {@code fit()} method.
*/
@Test
public void testFitWithUnknownStringValueInTheGivenData() {
Map<Integer, Vector> data = new HashMap<>();
data.put(1, VectorUtils.of(3.0, 0.0));
data.put(2, VectorUtils.of(3.0, 12.0));
data.put(3, VectorUtils.of(3.0, 12.0));
data.put(4, VectorUtils.of(2.0, 45.0));
data.put(5, VectorUtils.of(2.0, 45.0));
data.put(6, VectorUtils.of(14.0, 12.0));
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.STRING_ENCODER).withEncodedFeature(0).withEncodedFeature(1);
EncoderPreprocessor<Integer, Vector> preprocessor = strEncoderTrainer.fit(TestUtils.testEnvBuilder(), datasetBuilder, vectorizer);
try {
preprocessor.apply(7, new DenseVector(new Serializable[] { "Monday", "September" })).features().asArray();
fail("UnknownCategorialFeatureValue");
} catch (UnknownCategorialValueException e) {
return;
}
fail("UnknownCategorialFeatureValue");
}
use of org.apache.ignite.ml.math.primitives.vector.impl.DenseVector in project ignite by apache.
the class EncoderTrainerTest method testFitOnStringCategorialFeaturesWithReversedOrder.
/**
* Tests {@code fit()} method.
*/
@Test
public void testFitOnStringCategorialFeaturesWithReversedOrder() {
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.STRING_ENCODER).withEncoderIndexingStrategy(EncoderSortingStrategy.FREQUENCY_ASC).withEncodedFeature(0).withEncodedFeature(1);
EncoderPreprocessor<Integer, Vector> preprocessor = strEncoderTrainer.fit(TestUtils.testEnvBuilder(), datasetBuilder, vectorizer);
assertArrayEquals(new double[] { 2.0, 0.0 }, preprocessor.apply(7, new DenseVector(new Serializable[] { "Monday", "September" })).features().asArray(), 1e-8);
}
use of org.apache.ignite.ml.math.primitives.vector.impl.DenseVector in project ignite by apache.
the class LossFunctionsTest method testMSE.
/**
*/
@Test
public void testMSE() {
IgniteDifferentiableVectorToDoubleFunction f = LossFunctions.MSE.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 testHINGE.
/**
*/
@Test
public void testHINGE() {
IgniteDifferentiableVectorToDoubleFunction f = LossFunctions.HINGE.apply(new DenseVector(new double[] { 2.0, 1.0 }));
assertNotNull(f);
test(new double[] { 1.0, 3.0 }, f);
}
Aggregations