use of org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector in project ignite by apache.
the class LinearRegressionModelTest method testPredictOnAnObservationWithWrongCardinality.
/**
*/
@Test(expected = CardinalityException.class)
public void testPredictOnAnObservationWithWrongCardinality() {
Vector weights = new DenseLocalOnHeapVector(new double[] { 2.0, 3.0 });
LinearRegressionModel mdl = new LinearRegressionModel(weights, 1.0);
Vector observation = new DenseLocalOnHeapVector(new double[] { 1.0 });
mdl.apply(observation);
}
use of org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector in project ignite by apache.
the class SVMBinaryTrainerTest method testTrainWithTheLinearlySeparableCase.
/**
* Test trainer on classification model y = x.
*/
@Test
public void testTrainWithTheLinearlySeparableCase() {
Map<Integer, double[]> data = new HashMap<>();
ThreadLocalRandom rndX = ThreadLocalRandom.current();
ThreadLocalRandom rndY = ThreadLocalRandom.current();
for (int i = 0; i < AMOUNT_OF_OBSERVATIONS; i++) {
double x = rndX.nextDouble(-1000, 1000);
double y = rndY.nextDouble(-1000, 1000);
double[] vec = new double[AMOUNT_OF_FEATURES + 1];
// assign label.
vec[0] = y - x > 0 ? 1 : -1;
vec[1] = x;
vec[2] = y;
data.put(i, vec);
}
SVMLinearBinaryClassificationTrainer<Integer, double[]> trainer = new SVMLinearBinaryClassificationTrainer<>();
SVMLinearBinaryClassificationModel mdl = trainer.fit(new LocalDatasetBuilder<>(data, 10), (k, v) -> Arrays.copyOfRange(v, 1, v.length), (k, v) -> v[0], AMOUNT_OF_FEATURES);
TestUtils.assertEquals(-1, mdl.apply(new DenseLocalOnHeapVector(new double[] { 100, 10 })), PRECISION);
TestUtils.assertEquals(1, mdl.apply(new DenseLocalOnHeapVector(new double[] { 10, 100 })), PRECISION);
}
use of org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector in project ignite by apache.
the class SVMModelTest method testPredictWithErasedLabels.
/**
*/
@Test
public void testPredictWithErasedLabels() {
Vector weights = new DenseLocalOnHeapVector(new double[] { 1.0, 1.0 });
SVMLinearBinaryClassificationModel mdl = new SVMLinearBinaryClassificationModel(weights, 1.0);
Vector observation = new DenseLocalOnHeapVector(new double[] { 1.0, 1.0 });
TestUtils.assertEquals(1.0, mdl.apply(observation), PRECISION);
observation = new DenseLocalOnHeapVector(new double[] { 3.0, 4.0 });
TestUtils.assertEquals(1.0, mdl.apply(observation), PRECISION);
observation = new DenseLocalOnHeapVector(new double[] { -1.0, -1.0 });
TestUtils.assertEquals(-1.0, mdl.apply(observation), PRECISION);
observation = new DenseLocalOnHeapVector(new double[] { -2.0, 1.0 });
TestUtils.assertEquals(-1.0, mdl.apply(observation), PRECISION);
observation = new DenseLocalOnHeapVector(new double[] { -1.0, -2.0 });
TestUtils.assertEquals(-1.0, mdl.apply(observation), PRECISION);
final SVMLinearBinaryClassificationModel mdlWithNewData = mdl.withIntercept(-2.0).withWeights(new DenseLocalOnHeapVector(new double[] { -2.0, -2.0 }));
System.out.println("The SVM model is " + mdlWithNewData);
observation = new DenseLocalOnHeapVector(new double[] { -1.0, -2.0 });
TestUtils.assertEquals(1.0, mdl.apply(observation), PRECISION);
TestUtils.assertEquals(-2.0, mdl.intercept(), PRECISION);
}
use of org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector in project ignite by apache.
the class SVMModelTest method testPredictWithErasedLabelsAndChangedThreshold.
/**
*/
@Test
public void testPredictWithErasedLabelsAndChangedThreshold() {
Vector weights = new DenseLocalOnHeapVector(new double[] { 1.0, 1.0 });
SVMLinearBinaryClassificationModel mdl = new SVMLinearBinaryClassificationModel(weights, 1.0).withThreshold(5);
Vector observation = new DenseLocalOnHeapVector(new double[] { 1.0, 1.0 });
TestUtils.assertEquals(-1.0, mdl.apply(observation), PRECISION);
observation = new DenseLocalOnHeapVector(new double[] { 3.0, 4.0 });
TestUtils.assertEquals(1.0, mdl.apply(observation), PRECISION);
TestUtils.assertEquals(5, mdl.threshold(), PRECISION);
}
use of org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector in project ignite by apache.
the class SVMModelTest method testPredictWithRawLabels.
/**
*/
@Test
public void testPredictWithRawLabels() {
Vector weights = new DenseLocalOnHeapVector(new double[] { 2.0, 3.0 });
SVMLinearBinaryClassificationModel mdl = new SVMLinearBinaryClassificationModel(weights, 1.0).withRawLabels(true);
Vector observation = new DenseLocalOnHeapVector(new double[] { 1.0, 1.0 });
TestUtils.assertEquals(1.0 + 2.0 * 1.0 + 3.0 * 1.0, mdl.apply(observation), PRECISION);
observation = new DenseLocalOnHeapVector(new double[] { 2.0, 1.0 });
TestUtils.assertEquals(1.0 + 2.0 * 2.0 + 3.0 * 1.0, mdl.apply(observation), PRECISION);
observation = new DenseLocalOnHeapVector(new double[] { 1.0, 2.0 });
TestUtils.assertEquals(1.0 + 2.0 * 1.0 + 3.0 * 2.0, mdl.apply(observation), PRECISION);
observation = new DenseLocalOnHeapVector(new double[] { -2.0, 1.0 });
TestUtils.assertEquals(1.0 - 2.0 * 2.0 + 3.0 * 1.0, mdl.apply(observation), PRECISION);
observation = new DenseLocalOnHeapVector(new double[] { 1.0, -2.0 });
TestUtils.assertEquals(1.0 + 2.0 * 1.0 - 3.0 * 2.0, mdl.apply(observation), PRECISION);
Assert.assertEquals(true, mdl.isKeepingRawLabels());
}
Aggregations