Search in sources :

Example 6 with SVMLinearClassificationModel

use of org.apache.ignite.ml.svm.SVMLinearClassificationModel in project ignite by apache.

the class SVMFromSparkExample method main.

/**
 * Run example.
 */
public static void main(String[] args) throws FileNotFoundException {
    System.out.println();
    System.out.println(">>> SVM model loaded from Spark through serialization over partitioned 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, Vector> dataCache = null;
        try {
            dataCache = TitanicUtils.readPassengers(ignite);
            final Vectorizer<Integer, Vector, Integer, Double> vectorizer = new DummyVectorizer<Integer>(0, 5, 6).labeled(1);
            SVMLinearClassificationModel mdl = (SVMLinearClassificationModel) SparkModelParser.parse(SPARK_MDL_PATH, SupportedSparkModels.LINEAR_SVM, env);
            System.out.println(">>> SVM: " + mdl);
            double accuracy = Evaluator.evaluate(dataCache, mdl, vectorizer, new Accuracy<>());
            System.out.println("\n>>> Accuracy " + accuracy);
            System.out.println("\n>>> Test Error " + (1 - accuracy));
        } finally {
            dataCache.destroy();
        }
    }
}
Also used : Ignite(org.apache.ignite.Ignite) SVMLinearClassificationModel(org.apache.ignite.ml.svm.SVMLinearClassificationModel) Vector(org.apache.ignite.ml.math.primitives.vector.Vector)

Example 7 with SVMLinearClassificationModel

use of org.apache.ignite.ml.svm.SVMLinearClassificationModel in project ignite by apache.

the class SparkModelParser method loadLinearSVMModel.

/**
 * Load SVM model.
 *
 * @param pathToMdl Path to model.
 * @param learningEnvironment Learning environment.
 */
private static Model loadLinearSVMModel(String pathToMdl, LearningEnvironment learningEnvironment) {
    Vector coefficients = null;
    double interceptor = 0;
    try (ParquetFileReader r = ParquetFileReader.open(HadoopInputFile.fromPath(new Path(pathToMdl), new Configuration()))) {
        PageReadStore pages;
        final MessageType schema = r.getFooter().getFileMetaData().getSchema();
        final MessageColumnIO colIO = new ColumnIOFactory().getColumnIO(schema);
        while (null != (pages = r.readNextRowGroup())) {
            final long rows = pages.getRowCount();
            final RecordReader recordReader = colIO.getRecordReader(pages, new GroupRecordConverter(schema));
            for (int i = 0; i < rows; i++) {
                final SimpleGroup g = (SimpleGroup) recordReader.read();
                interceptor = readSVMInterceptor(g);
                coefficients = readSVMCoefficients(g);
            }
        }
    } catch (IOException e) {
        String msg = "Error reading parquet file: " + e.getMessage();
        learningEnvironment.logger().log(MLLogger.VerboseLevel.HIGH, msg);
        e.printStackTrace();
    }
    return new SVMLinearClassificationModel(coefficients, interceptor);
}
Also used : Path(org.apache.hadoop.fs.Path) GroupRecordConverter(org.apache.parquet.example.data.simple.convert.GroupRecordConverter) Configuration(org.apache.hadoop.conf.Configuration) ParquetFileReader(org.apache.parquet.hadoop.ParquetFileReader) RecordReader(org.apache.parquet.io.RecordReader) SimpleGroup(org.apache.parquet.example.data.simple.SimpleGroup) IOException(java.io.IOException) MessageColumnIO(org.apache.parquet.io.MessageColumnIO) ColumnIOFactory(org.apache.parquet.io.ColumnIOFactory) PageReadStore(org.apache.parquet.column.page.PageReadStore) SVMLinearClassificationModel(org.apache.ignite.ml.svm.SVMLinearClassificationModel) Vector(org.apache.ignite.ml.math.primitives.vector.Vector) DenseVector(org.apache.ignite.ml.math.primitives.vector.impl.DenseVector) MessageType(org.apache.parquet.schema.MessageType)

Example 8 with SVMLinearClassificationModel

use of org.apache.ignite.ml.svm.SVMLinearClassificationModel in project ignite by apache.

the class LocalModelsTest method importExportSVMBinaryClassificationModelTest.

/**
 */
@Test
public void importExportSVMBinaryClassificationModelTest() throws IOException {
    executeModelTest(mdlFilePath -> {
        SVMLinearClassificationModel mdl = new SVMLinearClassificationModel(new DenseVector(new double[] { 1, 2 }), 3);
        Exporter<SVMLinearClassificationModel, String> exporter = new FileExporter<>();
        mdl.saveModel(exporter, mdlFilePath);
        SVMLinearClassificationModel load = exporter.load(mdlFilePath);
        Assert.assertNotNull(load);
        Assert.assertEquals("", mdl, load);
        return null;
    });
}
Also used : FileExporter(org.apache.ignite.ml.FileExporter) SVMLinearClassificationModel(org.apache.ignite.ml.svm.SVMLinearClassificationModel) DenseVector(org.apache.ignite.ml.math.primitives.vector.impl.DenseVector) Test(org.junit.Test)

Aggregations

SVMLinearClassificationModel (org.apache.ignite.ml.svm.SVMLinearClassificationModel)8 Vector (org.apache.ignite.ml.math.primitives.vector.Vector)7 Ignite (org.apache.ignite.Ignite)5 SandboxMLCache (org.apache.ignite.examples.ml.util.SandboxMLCache)4 SVMLinearClassificationTrainer (org.apache.ignite.ml.svm.SVMLinearClassificationTrainer)4 DenseVector (org.apache.ignite.ml.math.primitives.vector.impl.DenseVector)3 Test (org.junit.Test)2 IOException (java.io.IOException)1 Path (java.nio.file.Path)1 Cache (javax.cache.Cache)1 Configuration (org.apache.hadoop.conf.Configuration)1 Path (org.apache.hadoop.fs.Path)1 IgniteCache (org.apache.ignite.IgniteCache)1 FileExporter (org.apache.ignite.ml.FileExporter)1 KMeansModel (org.apache.ignite.ml.clustering.kmeans.KMeansModel)1 KMeansModelFormat (org.apache.ignite.ml.clustering.kmeans.KMeansModelFormat)1 DummyVectorizer (org.apache.ignite.ml.dataset.feature.extractor.impl.DummyVectorizer)1 ANNClassificationModel (org.apache.ignite.ml.knn.ann.ANNClassificationModel)1 ANNModelFormat (org.apache.ignite.ml.knn.ann.ANNModelFormat)1 EuclideanDistance (org.apache.ignite.ml.math.distances.EuclideanDistance)1