use of org.apache.ignite.ml.svm.SVMLinearMultiClassClassificationModel in project ignite by apache.
the class SVMMultiClassClassificationExample method main.
/**
* Run example.
*/
public static void main(String[] args) throws InterruptedException {
System.out.println();
System.out.println(">>> SVM Multi-class classification model 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.");
IgniteThread igniteThread = new IgniteThread(ignite.configuration().getIgniteInstanceName(), SVMMultiClassClassificationExample.class.getSimpleName(), () -> {
IgniteCache<Integer, double[]> dataCache = getTestCache(ignite);
SVMLinearMultiClassClassificationTrainer<Integer, double[]> trainer = new SVMLinearMultiClassClassificationTrainer<>();
SVMLinearMultiClassClassificationModel mdl = trainer.fit(new CacheBasedDatasetBuilder<>(ignite, dataCache), (k, v) -> Arrays.copyOfRange(v, 1, v.length), (k, v) -> v[0], 5);
System.out.println(">>> SVM Multi-class model");
System.out.println(mdl.toString());
NormalizationTrainer<Integer, double[]> normalizationTrainer = new NormalizationTrainer<>();
NormalizationPreprocessor<Integer, double[]> preprocessor = normalizationTrainer.fit(new CacheBasedDatasetBuilder<>(ignite, dataCache), (k, v) -> Arrays.copyOfRange(v, 1, v.length), 5);
SVMLinearMultiClassClassificationModel mdlWithNormalization = trainer.fit(new CacheBasedDatasetBuilder<>(ignite, dataCache), preprocessor, (k, v) -> v[0], 5);
System.out.println(">>> SVM Multi-class model with normalization");
System.out.println(mdlWithNormalization.toString());
System.out.println(">>> ----------------------------------------------------------------");
System.out.println(">>> | Prediction\t| Prediction with Normalization\t| Ground Truth\t|");
System.out.println(">>> ----------------------------------------------------------------");
int amountOfErrors = 0;
int amountOfErrorsWithNormalization = 0;
int totalAmount = 0;
// Build confusion matrix. See https://en.wikipedia.org/wiki/Confusion_matrix
int[][] confusionMtx = { { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 } };
int[][] confusionMtxWithNormalization = { { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 } };
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];
double prediction = mdl.apply(new DenseLocalOnHeapVector(inputs));
double predictionWithNormalization = mdlWithNormalization.apply(new DenseLocalOnHeapVector(inputs));
totalAmount++;
// Collect data for model
if (groundTruth != prediction)
amountOfErrors++;
int idx1 = (int) prediction == 1 ? 0 : ((int) prediction == 3 ? 1 : 2);
int idx2 = (int) groundTruth == 1 ? 0 : ((int) groundTruth == 3 ? 1 : 2);
confusionMtx[idx1][idx2]++;
// Collect data for model with normalization
if (groundTruth != predictionWithNormalization)
amountOfErrorsWithNormalization++;
idx1 = (int) predictionWithNormalization == 1 ? 0 : ((int) predictionWithNormalization == 3 ? 1 : 2);
idx2 = (int) groundTruth == 1 ? 0 : ((int) groundTruth == 3 ? 1 : 2);
confusionMtxWithNormalization[idx1][idx2]++;
System.out.printf(">>> | %.4f\t\t| %.4f\t\t\t\t\t\t| %.4f\t\t|\n", prediction, predictionWithNormalization, groundTruth);
}
System.out.println(">>> ----------------------------------------------------------------");
System.out.println("\n>>> -----------------SVM model-------------");
System.out.println("\n>>> Absolute amount of errors " + amountOfErrors);
System.out.println("\n>>> Accuracy " + (1 - amountOfErrors / (double) totalAmount));
System.out.println("\n>>> Confusion matrix is " + Arrays.deepToString(confusionMtx));
System.out.println("\n>>> -----------------SVM model with Normalization-------------");
System.out.println("\n>>> Absolute amount of errors " + amountOfErrorsWithNormalization);
System.out.println("\n>>> Accuracy " + (1 - amountOfErrorsWithNormalization / (double) totalAmount));
System.out.println("\n>>> Confusion matrix is " + Arrays.deepToString(confusionMtxWithNormalization));
}
});
igniteThread.start();
igniteThread.join();
}
}
use of org.apache.ignite.ml.svm.SVMLinearMultiClassClassificationModel in project ignite by apache.
the class LocalModelsTest method importExportSVMMulticlassClassificationModelTest.
/**
*/
@Test
public void importExportSVMMulticlassClassificationModelTest() throws IOException {
executeModelTest(mdlFilePath -> {
SVMLinearBinaryClassificationModel binaryMdl1 = new SVMLinearBinaryClassificationModel(new DenseLocalOnHeapVector(new double[] { 1, 2 }), 3);
SVMLinearBinaryClassificationModel binaryMdl2 = new SVMLinearBinaryClassificationModel(new DenseLocalOnHeapVector(new double[] { 2, 3 }), 4);
SVMLinearBinaryClassificationModel binaryMdl3 = new SVMLinearBinaryClassificationModel(new DenseLocalOnHeapVector(new double[] { 3, 4 }), 5);
SVMLinearMultiClassClassificationModel mdl = new SVMLinearMultiClassClassificationModel();
mdl.add(1, binaryMdl1);
mdl.add(2, binaryMdl2);
mdl.add(3, binaryMdl3);
Exporter<SVMLinearMultiClassClassificationModel, String> exporter = new FileExporter<>();
mdl.saveModel(exporter, mdlFilePath);
SVMLinearMultiClassClassificationModel load = exporter.load(mdlFilePath);
Assert.assertNotNull(load);
Assert.assertEquals("", mdl, load);
return null;
});
}
Aggregations