use of org.apache.ignite.ml.knn.NNClassificationModel in project ignite by apache.
the class ANNClassificationExample method main.
/**
* Run example.
*/
public static void main(String[] args) {
System.out.println();
System.out.println(">>> ANN multi-class classification algorithm over cached dataset usage example started.");
// Start ignite grid.
try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) {
System.out.println(">>> Ignite grid started.");
IgniteCache<Integer, double[]> dataCache = null;
try {
dataCache = getTestCache(ignite);
ANNClassificationTrainer trainer = new ANNClassificationTrainer().withDistance(new ManhattanDistance()).withK(50).withMaxIterations(1000).withEpsilon(1e-2);
long startTrainingTime = System.currentTimeMillis();
NNClassificationModel knnMdl = trainer.fit(ignite, dataCache, new DoubleArrayVectorizer<Integer>().labeled(Vectorizer.LabelCoordinate.FIRST)).withK(5).withDistanceMeasure(new EuclideanDistance()).withWeighted(true);
long endTrainingTime = System.currentTimeMillis();
System.out.println(">>> ---------------------------------");
System.out.println(">>> | Prediction\t| Ground Truth\t|");
System.out.println(">>> ---------------------------------");
int amountOfErrors = 0;
int totalAmount = 0;
long totalPredictionTime = 0L;
try (QueryCursor<Cache.Entry<Integer, double[]>> observations = dataCache.query(new ScanQuery<>())) {
for (Cache.Entry<Integer, double[]> observation : observations) {
double[] val = observation.getValue();
double[] inputs = Arrays.copyOfRange(val, 1, val.length);
double groundTruth = val[0];
long startPredictionTime = System.currentTimeMillis();
double prediction = knnMdl.predict(new DenseVector(inputs));
long endPredictionTime = System.currentTimeMillis();
totalPredictionTime += (endPredictionTime - startPredictionTime);
totalAmount++;
if (!Precision.equals(groundTruth, prediction, Precision.EPSILON))
amountOfErrors++;
System.out.printf(">>> | %.4f\t\t| %.4f\t\t|\n", prediction, groundTruth);
}
System.out.println(">>> ---------------------------------");
System.out.println("Training costs = " + (endTrainingTime - startTrainingTime));
System.out.println("Prediction costs = " + totalPredictionTime);
System.out.println("\n>>> Absolute amount of errors " + amountOfErrors);
System.out.println("\n>>> Accuracy " + (1 - amountOfErrors / (double) totalAmount));
System.out.println(totalAmount);
System.out.println(">>> ANN multi-class classification algorithm over cached dataset usage example completed.");
}
} finally {
dataCache.destroy();
}
} finally {
System.out.flush();
}
}
use of org.apache.ignite.ml.knn.NNClassificationModel in project ignite by apache.
the class LocalModelsTest method importExportANNModelTest.
/**
*/
@Test
public void importExportANNModelTest() throws IOException {
executeModelTest(mdlFilePath -> {
final LabeledVectorSet<LabeledVector> centers = new LabeledVectorSet<>();
NNClassificationModel mdl = new ANNClassificationModel(centers, new ANNClassificationTrainer.CentroidStat()).withK(4).withDistanceMeasure(new ManhattanDistance()).withWeighted(true);
Exporter<KNNModelFormat, String> exporter = new FileExporter<>();
mdl.saveModel(exporter, mdlFilePath);
ANNModelFormat load = (ANNModelFormat) exporter.load(mdlFilePath);
Assert.assertNotNull(load);
NNClassificationModel importedMdl = new ANNClassificationModel(load.getCandidates(), new ANNClassificationTrainer.CentroidStat()).withK(load.getK()).withDistanceMeasure(load.getDistanceMeasure()).withWeighted(true);
Assert.assertEquals("", mdl, importedMdl);
return null;
});
}
Aggregations