Search in sources :

Example 11 with LinearRegressionModel

use of org.apache.ignite.ml.regressions.linear.LinearRegressionModel in project ignite by apache.

the class LocalModelsTest method importExportLinearRegressionModelTest.

/**
 */
@Test
public void importExportLinearRegressionModelTest() throws IOException {
    executeModelTest(mdlFilePath -> {
        LinearRegressionModel model = new LinearRegressionModel(new DenseLocalOnHeapVector(new double[] { 1, 2 }), 3);
        Exporter<LinearRegressionModel, String> exporter = new FileExporter<>();
        model.saveModel(exporter, mdlFilePath);
        LinearRegressionModel load = exporter.load(mdlFilePath);
        Assert.assertNotNull(load);
        Assert.assertEquals("", model, load);
        return null;
    });
}
Also used : LinearRegressionModel(org.apache.ignite.ml.regressions.linear.LinearRegressionModel) DenseLocalOnHeapVector(org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector) Test(org.junit.Test)

Example 12 with LinearRegressionModel

use of org.apache.ignite.ml.regressions.linear.LinearRegressionModel in project ignite by apache.

the class BostonHousePricesPredictionExample method toString.

/**
 * Prepare pretty string for model.
 *
 * @param mdl Model.
 * @return String representation of model.
 */
private static String toString(LinearRegressionModel mdl) {
    BiFunction<Integer, Double, String> formatter = (idx, val) -> String.format("%.2f*f%d", val, idx);
    Vector weights = mdl.weights();
    StringBuilder sb = new StringBuilder(formatter.apply(0, weights.get(0)));
    for (int fid = 1; fid < weights.size(); fid++) {
        double w = weights.get(fid);
        sb.append(" ").append(w > 0 ? "+" : "-").append(" ").append(formatter.apply(fid, Math.abs(w)));
    }
    double intercept = mdl.intercept();
    sb.append(" ").append(intercept > 0 ? "+" : "-").append(" ").append(String.format("%.2f", Math.abs(intercept)));
    return sb.toString();
}
Also used : Evaluator(org.apache.ignite.ml.selection.scoring.evaluator.Evaluator) BiFunction(java.util.function.BiFunction) Vector(org.apache.ignite.ml.math.primitives.vector.Vector) LinearRegressionModel(org.apache.ignite.ml.regressions.linear.LinearRegressionModel) IOException(java.io.IOException) Ignite(org.apache.ignite.Ignite) DatasetTrainer(org.apache.ignite.ml.trainers.DatasetTrainer) IgniteCache(org.apache.ignite.IgniteCache) DummyVectorizer(org.apache.ignite.ml.dataset.feature.extractor.impl.DummyVectorizer) Ignition(org.apache.ignite.Ignition) MLSandboxDatasets(org.apache.ignite.examples.ml.util.MLSandboxDatasets) LinearRegressionLSQRTrainer(org.apache.ignite.ml.regressions.linear.LinearRegressionLSQRTrainer) TrainTestDatasetSplitter(org.apache.ignite.ml.selection.split.TrainTestDatasetSplitter) SandboxMLCache(org.apache.ignite.examples.ml.util.SandboxMLCache) MetricName(org.apache.ignite.ml.selection.scoring.metric.MetricName) TrainTestSplit(org.apache.ignite.ml.selection.split.TrainTestSplit) LearningEnvironmentBuilder(org.apache.ignite.ml.environment.LearningEnvironmentBuilder) Vectorizer(org.apache.ignite.ml.dataset.feature.extractor.Vectorizer) Vector(org.apache.ignite.ml.math.primitives.vector.Vector)

Example 13 with LinearRegressionModel

use of org.apache.ignite.ml.regressions.linear.LinearRegressionModel in project ignite by apache.

the class BostonHousePricesPredictionExample 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.BOSTON_HOUSE_PRICES);
            DatasetTrainer<LinearRegressionModel, Double> trainer = new LinearRegressionLSQRTrainer().withEnvironmentBuilder(LearningEnvironmentBuilder.defaultBuilder().withRNGSeed(0));
            // 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 80/20 proportion.
            TrainTestSplit<Integer, Vector> split = new TrainTestDatasetSplitter<Integer, Vector>().split(0.8);
            System.out.println(">>> Start training.");
            LinearRegressionModel mdl = trainer.fit(ignite, dataCache, split.getTrainFilter(), vectorizer);
            System.out.println(">>> Perform scoring.");
            double score = Evaluator.evaluate(dataCache, split.getTestFilter(), mdl, vectorizer, MetricName.R2);
            System.out.println(">>> Model: " + toString(mdl));
            System.out.println(">>> R^2 score: " + score);
        } finally {
            if (dataCache != null)
                dataCache.destroy();
        }
    } finally {
        System.out.flush();
    }
}
Also used : SandboxMLCache(org.apache.ignite.examples.ml.util.SandboxMLCache) LinearRegressionModel(org.apache.ignite.ml.regressions.linear.LinearRegressionModel) LinearRegressionLSQRTrainer(org.apache.ignite.ml.regressions.linear.LinearRegressionLSQRTrainer) Ignite(org.apache.ignite.Ignite) Vector(org.apache.ignite.ml.math.primitives.vector.Vector)

Example 14 with LinearRegressionModel

use of org.apache.ignite.ml.regressions.linear.LinearRegressionModel in project ignite by apache.

the class TrainTestDatasetSplitterExample method main.

/**
 * Run example.
 */
public static void main(String[] args) throws IOException {
    System.out.println();
    System.out.println(">>> Linear regression model over cache based 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 = new SandboxMLCache(ignite).fillCacheWith(MLSandboxDatasets.MORTALITY_DATA);
            System.out.println(">>> Create new linear regression trainer object.");
            LinearRegressionLSQRTrainer trainer = new LinearRegressionLSQRTrainer();
            System.out.println(">>> Create new training dataset splitter object.");
            TrainTestSplit<Integer, Vector> split = new TrainTestDatasetSplitter<Integer, Vector>().split(0.75);
            System.out.println(">>> Perform the training to get the model.");
            Vectorizer<Integer, Vector, Integer, Double> vectorizer = new DummyVectorizer<Integer>().labeled(Vectorizer.LabelCoordinate.FIRST);
            LinearRegressionModel mdl = trainer.fit(ignite, dataCache, split.getTrainFilter(), vectorizer);
            System.out.println(">>> Linear regression model: " + mdl);
            System.out.println(">>> ---------------------------------");
            System.out.println(">>> | Prediction\t| Ground Truth\t|");
            System.out.println(">>> ---------------------------------");
            ScanQuery<Integer, Vector> qry = new ScanQuery<>();
            qry.setFilter(split.getTestFilter());
            try (QueryCursor<Cache.Entry<Integer, Vector>> observations = dataCache.query(qry)) {
                for (Cache.Entry<Integer, Vector> observation : observations) {
                    Vector val = observation.getValue();
                    Vector inputs = val.copyOfRange(1, val.size());
                    double groundTruth = val.get(0);
                    double prediction = mdl.predict(inputs);
                    System.out.printf(">>> | %.4f\t\t| %.4f\t\t|\n", prediction, groundTruth);
                }
            }
            System.out.println(">>> ---------------------------------");
            System.out.println(">>> Linear regression model over cache based dataset usage example completed.");
        } finally {
            if (dataCache != null)
                dataCache.destroy();
        }
    } finally {
        System.out.flush();
    }
}
Also used : SandboxMLCache(org.apache.ignite.examples.ml.util.SandboxMLCache) LinearRegressionModel(org.apache.ignite.ml.regressions.linear.LinearRegressionModel) ScanQuery(org.apache.ignite.cache.query.ScanQuery) LinearRegressionLSQRTrainer(org.apache.ignite.ml.regressions.linear.LinearRegressionLSQRTrainer) Ignite(org.apache.ignite.Ignite) Vector(org.apache.ignite.ml.math.primitives.vector.Vector) IgniteCache(org.apache.ignite.IgniteCache) SandboxMLCache(org.apache.ignite.examples.ml.util.SandboxMLCache) Cache(javax.cache.Cache)

Example 15 with LinearRegressionModel

use of org.apache.ignite.ml.regressions.linear.LinearRegressionModel in project ignite by apache.

the class LinearRegressionLSQRTrainerExample method main.

/**
 * Run example.
 */
public static void main(String[] args) throws IOException {
    System.out.println();
    System.out.println(">>> Linear regression model over cache based 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 = new SandboxMLCache(ignite).fillCacheWith(MLSandboxDatasets.MORTALITY_DATA);
            System.out.println(">>> Create new linear regression trainer object.");
            LinearRegressionLSQRTrainer trainer = new LinearRegressionLSQRTrainer();
            System.out.println(">>> Perform the training to get the model.");
            // This object is used to extract features and vectors from upstream entities which are
            // essentially tuples of the form (key, value) (in our case (Integer, Vector)).
            // Key part of tuple in our example is ignored.
            // Label is extracted from 0th entry of the value (which is a Vector)
            // and features are all remaining vector part. Alternatively we could use
            // DatasetTrainer#fit(Ignite, IgniteCache, IgniteBiFunction, IgniteBiFunction) method call
            // where there is a separate lambda for extracting label from (key, value) and a separate labmda for
            // extracting features.
            LinearRegressionModel mdl = trainer.fit(ignite, dataCache, new DummyVectorizer<Integer>().labeled(Vectorizer.LabelCoordinate.FIRST));
            double rmse = Evaluator.evaluate(dataCache, mdl, new DummyVectorizer<Integer>().labeled(Vectorizer.LabelCoordinate.FIRST), MetricName.RMSE);
            System.out.println("\n>>> Rmse = " + rmse);
            System.out.println(">>> Linear regression model over cache based dataset usage example completed.");
        } finally {
            if (dataCache != null)
                dataCache.destroy();
        }
    } finally {
        System.out.flush();
    }
}
Also used : LinearRegressionLSQRTrainer(org.apache.ignite.ml.regressions.linear.LinearRegressionLSQRTrainer) SandboxMLCache(org.apache.ignite.examples.ml.util.SandboxMLCache) LinearRegressionModel(org.apache.ignite.ml.regressions.linear.LinearRegressionModel) DummyVectorizer(org.apache.ignite.ml.dataset.feature.extractor.impl.DummyVectorizer) Ignite(org.apache.ignite.Ignite) Vector(org.apache.ignite.ml.math.primitives.vector.Vector)

Aggregations

LinearRegressionModel (org.apache.ignite.ml.regressions.linear.LinearRegressionModel)19 Ignite (org.apache.ignite.Ignite)13 Vector (org.apache.ignite.ml.math.primitives.vector.Vector)12 LinearRegressionLSQRTrainer (org.apache.ignite.ml.regressions.linear.LinearRegressionLSQRTrainer)11 SandboxMLCache (org.apache.ignite.examples.ml.util.SandboxMLCache)8 IgniteCache (org.apache.ignite.IgniteCache)6 Cache (javax.cache.Cache)5 SparseDistributedMatrixExample (org.apache.ignite.examples.ml.math.matrix.SparseDistributedMatrixExample)4 DummyVectorizer (org.apache.ignite.ml.dataset.feature.extractor.impl.DummyVectorizer)4 Test (org.junit.Test)4 DenseLocalOnHeapVector (org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector)3 IgniteThread (org.apache.ignite.thread.IgniteThread)3 IOException (java.io.IOException)2 TrainerTest (org.apache.ignite.ml.common.TrainerTest)2 DoubleArrayVectorizer (org.apache.ignite.ml.dataset.feature.extractor.impl.DoubleArrayVectorizer)2 Matrix (org.apache.ignite.ml.math.Matrix)2 Vector (org.apache.ignite.ml.math.Vector)2 SparseDistributedMatrix (org.apache.ignite.ml.math.impls.matrix.SparseDistributedMatrix)2 SparseDistributedVector (org.apache.ignite.ml.math.impls.vector.SparseDistributedVector)2 VectorUtils (org.apache.ignite.ml.math.primitives.vector.VectorUtils)2