use of org.apache.ignite.ml.dataset.feature.extractor.impl.DoubleArrayVectorizer in project ignite by apache.
the class KNNRegressionTest method testUpdate.
/**
*/
@Test
public void testUpdate() {
Map<Integer, double[]> data = new HashMap<>();
data.put(0, new double[] { 11.0, 0, 0, 0, 0, 0 });
data.put(1, new double[] { 12.0, 2.0, 0, 0, 0, 0 });
data.put(2, new double[] { 13.0, 0, 3.0, 0, 0, 0 });
data.put(3, new double[] { 14.0, 0, 0, 4.0, 0, 0 });
data.put(4, new double[] { 15.0, 0, 0, 0, 5.0, 0 });
data.put(5, new double[] { 16.0, 0, 0, 0, 0, 6.0 });
KNNRegressionTrainer trainer = new KNNRegressionTrainer().withK(1).withDistanceMeasure(new EuclideanDistance()).withWeighted(false);
KNNRegressionModel originalMdlOnEmptyDataset = trainer.fit(new HashMap<>(), parts, new DoubleArrayVectorizer<Integer>().labeled(Vectorizer.LabelCoordinate.FIRST));
KNNRegressionModel updatedOnDataset = trainer.update(originalMdlOnEmptyDataset, data, parts, new DoubleArrayVectorizer<Integer>().labeled(Vectorizer.LabelCoordinate.FIRST));
Vector vector = VectorUtils.of(0.0, 0.0, 0.0, 5.0, 0.0);
assertNull(originalMdlOnEmptyDataset.predict(vector));
assertEquals(Double.valueOf(15.0), updatedOnDataset.predict(vector));
}
use of org.apache.ignite.ml.dataset.feature.extractor.impl.DoubleArrayVectorizer in project ignite by apache.
the class KNNRegressionTest method testSimpleRegressionWithOneNeighbour.
/**
*/
@Test
public void testSimpleRegressionWithOneNeighbour() {
Map<Integer, double[]> data = new HashMap<>();
data.put(0, new double[] { 11.0, 0.0, 0.0, 0.0, 0.0, 0.0 });
data.put(1, new double[] { 12.0, 2.0, 0.0, 0.0, 0.0, 0.0 });
data.put(2, new double[] { 13.0, 0.0, 3.0, 0.0, 0.0, 0.0 });
data.put(3, new double[] { 14.0, 0.0, 0.0, 4.0, 0.0, 0.0 });
data.put(4, new double[] { 15.0, 0.0, 0.0, 0.0, 5.0, 0.0 });
data.put(5, new double[] { 16.0, 0.0, 0.0, 0.0, 0.0, 6.0 });
KNNRegressionTrainer trainer = new KNNRegressionTrainer().withK(1).withDistanceMeasure(new EuclideanDistance()).withWeighted(false);
KNNRegressionModel knnMdl = trainer.fit(new LocalDatasetBuilder<>(data, parts), new DoubleArrayVectorizer<Integer>().labeled(Vectorizer.LabelCoordinate.FIRST));
assertEquals(15, knnMdl.predict(VectorUtils.of(0.0, 0.0, 0.0, 5.0, 0.0)), 1E-12);
}
use of org.apache.ignite.ml.dataset.feature.extractor.impl.DoubleArrayVectorizer in project ignite by apache.
the class KNNClassificationTest method testBinaryClassificationFarPointsWithWeightedStrategy.
/**
*/
@Test
public void testBinaryClassificationFarPointsWithWeightedStrategy() {
Map<Integer, double[]> data = new HashMap<>();
data.put(0, new double[] { 10.0, 10.0, 1.0 });
data.put(1, new double[] { 10.0, 20.0, 1.0 });
data.put(2, new double[] { -1.0, -1.0, 1.0 });
data.put(3, new double[] { -2.0, -2.0, 2.0 });
data.put(4, new double[] { -1.0, -2.0, 2.0 });
data.put(5, new double[] { -2.0, -1.0, 2.0 });
KNNClassificationTrainer trainer = new KNNClassificationTrainer().withK(3).withDistanceMeasure(new EuclideanDistance()).withWeighted(true);
KNNClassificationModel knnMdl = trainer.fit(data, parts, new DoubleArrayVectorizer<Integer>().labeled(Vectorizer.LabelCoordinate.LAST));
assertEquals(1.0, knnMdl.predict(VectorUtils.of(-1.01, -1.01)), 0);
}
use of org.apache.ignite.ml.dataset.feature.extractor.impl.DoubleArrayVectorizer in project ignite by apache.
the class KNNClassificationTest method testBinaryClassificationWithSmallestK.
/**
*/
@Test
public void testBinaryClassificationWithSmallestK() {
Map<Integer, double[]> data = new HashMap<>();
data.put(0, new double[] { 1.0, 1.0, 1.0 });
data.put(1, new double[] { 1.0, 2.0, 1.0 });
data.put(2, new double[] { 2.0, 1.0, 1.0 });
data.put(3, new double[] { -1.0, -1.0, 2.0 });
data.put(4, new double[] { -1.0, -2.0, 2.0 });
data.put(5, new double[] { -2.0, -1.0, 2.0 });
KNNClassificationTrainer trainer = new KNNClassificationTrainer().withK(1).withDistanceMeasure(new EuclideanDistance()).withWeighted(false);
KNNClassificationModel knnMdl = trainer.fit(data, parts, new DoubleArrayVectorizer<Integer>().labeled(Vectorizer.LabelCoordinate.LAST));
assertEquals(1.0, knnMdl.predict(VectorUtils.of(2.0, 2.0)), 0);
assertEquals(2.0, knnMdl.predict(VectorUtils.of(-2.0, -2.0)), 0);
}
use of org.apache.ignite.ml.dataset.feature.extractor.impl.DoubleArrayVectorizer in project ignite by apache.
the class KNNClassificationTest method testUpdate.
/**
*/
@Test
public void testUpdate() {
Map<Integer, double[]> data = new HashMap<>();
data.put(0, new double[] { 10.0, 10.0, 1.0 });
data.put(1, new double[] { 10.0, 20.0, 1.0 });
data.put(2, new double[] { -1, -1, 1.0 });
data.put(3, new double[] { -2, -2, 2.0 });
data.put(4, new double[] { -1.0, -2.0, 2.0 });
data.put(5, new double[] { -2.0, -1.0, 2.0 });
KNNClassificationTrainer trainer = new KNNClassificationTrainer().withK(3).withDistanceMeasure(new EuclideanDistance()).withWeighted(false);
KNNClassificationModel originalMdlOnEmptyDataset = trainer.fit(new HashMap<>(), parts, new DoubleArrayVectorizer<Integer>().labeled(Vectorizer.LabelCoordinate.LAST));
KNNClassificationModel updatedOnData = trainer.update(originalMdlOnEmptyDataset, data, parts, new DoubleArrayVectorizer<Integer>().labeled(Vectorizer.LabelCoordinate.LAST));
Vector vector = VectorUtils.of(-1.01, -1.01);
assertNull(originalMdlOnEmptyDataset.predict(vector));
assertEquals(Double.valueOf(2.0), updatedOnData.predict(vector));
}
Aggregations