Search in sources :

Example 1 with LinearRegressionQRTrainer

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

the class IgniteOLSMultipleLinearRegressionBenchmark method runLongly.

/**
 * Based on OLSMultipleLinearRegressionTest#testLongly.
 */
private void runLongly() {
    // Y values are first, then independent vars
    // Each row is one observation
    double[][] data = new double[][] { { 60323, 83.0, 234289, 2356, 1590, 107608, 1947 }, { 61122, 88.5, 259426, 2325, 1456, 108632, 1948 }, { 60171, 88.2, 258054, 3682, 1616, 109773, 1949 }, { 61187, 89.5, 284599, 3351, 1650, 110929, 1950 }, { 63221, 96.2, 328975, 2099, 3099, 112075, 1951 }, { 63639, 98.1, 346999, 1932, 3594, 113270, 1952 }, { 64989, 99.0, 365385, 1870, 3547, 115094, 1953 }, { 63761, 100.0, 363112, 3578, 3350, 116219, 1954 }, { 66019, 101.2, 397469, 2904, 3048, 117388, 1955 }, { 67857, 104.6, 419180, 2822, 2857, 118734, 1956 }, { 68169, 108.4, 442769, 2936, 2798, 120445, 1957 }, { 66513, 110.8, 444546, 4681, 2637, 121950, 1958 }, { 68655, 112.6, 482704, 3813, 2552, 123366, 1959 }, { 69564, 114.2, 502601, 3931, 2514, 125368, 1960 }, { 69331, 115.7, 518173, 4806, 2572, 127852, 1961 }, { 70551, 116.9, 554894, 4007, 2827, 130081, 1962 } };
    final int nobs = 16;
    final int nvars = 6;
    LinearRegressionQRTrainer trainer = new LinearRegressionQRTrainer();
    LinearRegressionModel model = trainer.train(new DenseLocalOnHeapMatrix(data));
}
Also used : LinearRegressionModel(org.apache.ignite.ml.regressions.linear.LinearRegressionModel) LinearRegressionQRTrainer(org.apache.ignite.ml.regressions.linear.LinearRegressionQRTrainer) DenseLocalOnHeapMatrix(org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix)

Example 2 with LinearRegressionQRTrainer

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

the class DistributedLinearRegressionWithQRTrainerExample method main.

/**
 * Run example.
 */
public static void main(String[] args) throws InterruptedException {
    System.out.println();
    System.out.println(">>> Linear regression model over sparse distributed matrix API usage example started.");
    // Start ignite grid.
    try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) {
        System.out.println(">>> Ignite grid started.");
        // Create IgniteThread, we must work with SparseDistributedMatrix inside IgniteThread
        // because we create ignite cache internally.
        IgniteThread igniteThread = new IgniteThread(ignite.configuration().getIgniteInstanceName(), SparseDistributedMatrixExample.class.getSimpleName(), () -> {
            // Create SparseDistributedMatrix, new cache will be created automagically.
            System.out.println(">>> Create new SparseDistributedMatrix inside IgniteThread.");
            SparseDistributedMatrix distributedMatrix = new SparseDistributedMatrix(data);
            System.out.println(">>> Create new linear regression trainer object.");
            Trainer<LinearRegressionModel, Matrix> trainer = new LinearRegressionQRTrainer();
            System.out.println(">>> Perform the training to get the model.");
            LinearRegressionModel model = trainer.train(distributedMatrix);
            System.out.println(">>> Linear regression model: " + model);
            System.out.println(">>> ---------------------------------");
            System.out.println(">>> | Prediction\t| Ground Truth\t|");
            System.out.println(">>> ---------------------------------");
            for (double[] observation : data) {
                Vector inputs = new SparseDistributedVector(Arrays.copyOfRange(observation, 1, observation.length));
                double prediction = model.apply(inputs);
                double groundTruth = observation[0];
                System.out.printf(">>> | %.4f\t\t| %.4f\t\t|\n", prediction, groundTruth);
            }
            System.out.println(">>> ---------------------------------");
        });
        igniteThread.start();
        igniteThread.join();
    }
}
Also used : SparseDistributedMatrix(org.apache.ignite.ml.math.impls.matrix.SparseDistributedMatrix) SparseDistributedMatrixExample(org.apache.ignite.examples.ml.math.matrix.SparseDistributedMatrixExample) Matrix(org.apache.ignite.ml.math.Matrix) SparseDistributedMatrix(org.apache.ignite.ml.math.impls.matrix.SparseDistributedMatrix) LinearRegressionModel(org.apache.ignite.ml.regressions.linear.LinearRegressionModel) LinearRegressionQRTrainer(org.apache.ignite.ml.regressions.linear.LinearRegressionQRTrainer) SparseDistributedVector(org.apache.ignite.ml.math.impls.vector.SparseDistributedVector) Ignite(org.apache.ignite.Ignite) IgniteThread(org.apache.ignite.thread.IgniteThread) Vector(org.apache.ignite.ml.math.Vector) SparseDistributedVector(org.apache.ignite.ml.math.impls.vector.SparseDistributedVector)

Aggregations

LinearRegressionModel (org.apache.ignite.ml.regressions.linear.LinearRegressionModel)2 LinearRegressionQRTrainer (org.apache.ignite.ml.regressions.linear.LinearRegressionQRTrainer)2 Ignite (org.apache.ignite.Ignite)1 SparseDistributedMatrixExample (org.apache.ignite.examples.ml.math.matrix.SparseDistributedMatrixExample)1 Matrix (org.apache.ignite.ml.math.Matrix)1 Vector (org.apache.ignite.ml.math.Vector)1 DenseLocalOnHeapMatrix (org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix)1 SparseDistributedMatrix (org.apache.ignite.ml.math.impls.matrix.SparseDistributedMatrix)1 SparseDistributedVector (org.apache.ignite.ml.math.impls.vector.SparseDistributedVector)1 IgniteThread (org.apache.ignite.thread.IgniteThread)1