use of org.apache.ignite.ml.knn.classification.KNNClassificationModel in project ignite by apache.
the class IrisClassificationExample method main.
/**
* Runs example.
*/
public static void main(String[] args) throws IOException {
try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) {
System.out.println(">>> Ignite grid started.");
IgniteCache<Integer, Vector> dataCache = null;
try {
System.out.println(">>> Fill dataset cache.");
dataCache = new SandboxMLCache(ignite).fillCacheWith(MLSandboxDatasets.IRIS);
KNNClassificationTrainer trainer = ((KNNClassificationTrainer) new KNNClassificationTrainer().withEnvironmentBuilder(LearningEnvironmentBuilder.defaultBuilder().withRNGSeed(0))).withK(3).withDistanceMeasure(new EuclideanDistance()).withWeighted(true);
// This vectorizer works with values in cache of Vector class.
Vectorizer<Integer, Vector, Integer, Double> vectorizer = new DummyVectorizer<Integer>().labeled(// FIRST means "label are stored at first coordinate of vector"
Vectorizer.LabelCoordinate.FIRST);
// Splits dataset to train and test samples with 60/40 proportion.
TrainTestSplit<Integer, Vector> split = new TrainTestDatasetSplitter<Integer, Vector>().split(0.6);
System.out.println(">>> Start traininig.");
KNNClassificationModel mdl = trainer.fit(ignite, dataCache, split.getTrainFilter(), vectorizer);
System.out.println(">>> Perform scoring.");
double accuracy = Evaluator.evaluate(dataCache, split.getTestFilter(), mdl, vectorizer, new Accuracy<>());
System.out.println(">> Model accuracy: " + accuracy);
} finally {
if (dataCache != null)
dataCache.destroy();
}
} finally {
System.out.flush();
}
}
use of org.apache.ignite.ml.knn.classification.KNNClassificationModel in project ignite by apache.
the class BinaryClassificationEvaluatorTest method testEvaluatorWithFilter.
/**
* Test evaluator and trainer on classification model y = x.
*/
@Test
public void testEvaluatorWithFilter() {
Map<Integer, Vector> cacheMock = new HashMap<>();
for (int i = 0; i < twoLinearlySeparableClasses.length; i++) cacheMock.put(i, VectorUtils.of(twoLinearlySeparableClasses[i]));
KNNClassificationTrainer trainer = new KNNClassificationTrainer().withK(3);
TrainTestSplit<Integer, Vector> split = new TrainTestDatasetSplitter<Integer, Vector>(new SHA256UniformMapper<>(new Random(100))).split(0.75);
Vectorizer<Integer, Vector, Integer, Double> vectorizer = new DummyVectorizer<Integer>().labeled(Vectorizer.LabelCoordinate.FIRST);
KNNClassificationModel mdl = trainer.fit(cacheMock, split.getTrainFilter(), parts, vectorizer);
double score = Evaluator.evaluate(cacheMock, split.getTestFilter(), mdl, vectorizer, MetricName.ACCURACY);
assertEquals(0.9769230769230769, score, 1e-12);
}
use of org.apache.ignite.ml.knn.classification.KNNClassificationModel 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.knn.classification.KNNClassificationModel 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.knn.classification.KNNClassificationModel 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