Search in sources :

Example 1 with SparseDistributedMatrix

use of org.apache.ignite.ml.math.impls.matrix.SparseDistributedMatrix in project ignite by apache.

the class KMeansDistributedClustererTest method testClusterizationOnDatasetWithObviousStructure.

/** */
@Test
public void testClusterizationOnDatasetWithObviousStructure() throws IOException {
    IgniteUtils.setCurrentIgniteName(ignite.configuration().getIgniteInstanceName());
    int ptsCnt = 10000;
    int squareSideLen = 10000;
    Random rnd = new Random(123456L);
    // Let centers be in the vertices of square.
    Map<Integer, Vector> centers = new HashMap<>();
    centers.put(100, new DenseLocalOnHeapVector(new double[] { 0.0, 0.0 }));
    centers.put(900, new DenseLocalOnHeapVector(new double[] { squareSideLen, 0.0 }));
    centers.put(3000, new DenseLocalOnHeapVector(new double[] { 0.0, squareSideLen }));
    centers.put(6000, new DenseLocalOnHeapVector(new double[] { squareSideLen, squareSideLen }));
    int centersCnt = centers.size();
    SparseDistributedMatrix points = new SparseDistributedMatrix(ptsCnt, 2, StorageConstants.ROW_STORAGE_MODE, StorageConstants.RANDOM_ACCESS_MODE);
    List<Integer> permutation = IntStream.range(0, ptsCnt).boxed().collect(Collectors.toList());
    Collections.shuffle(permutation, rnd);
    Vector[] mc = new Vector[centersCnt];
    Arrays.fill(mc, VectorUtils.zeroes(2));
    int centIndex = 0;
    int totalCount = 0;
    List<Vector> massCenters = new ArrayList<>();
    for (Integer count : centers.keySet()) {
        for (int i = 0; i < count; i++) {
            DenseLocalOnHeapVector pnt = (DenseLocalOnHeapVector) new DenseLocalOnHeapVector(2).assign(centers.get(count));
            // pertrubate point on random value.
            pnt.map(val -> val + rnd.nextDouble() * squareSideLen / 100);
            mc[centIndex] = mc[centIndex].plus(pnt);
            points.assignRow(permutation.get(totalCount), pnt);
            totalCount++;
        }
        massCenters.add(mc[centIndex].times(1 / (double) count));
        centIndex++;
    }
    EuclideanDistance dist = new EuclideanDistance();
    OrderedNodesComparator comp = new OrderedNodesComparator(centers.values().toArray(new Vector[] {}), dist);
    massCenters.sort(comp);
    KMeansDistributedClusterer clusterer = new KMeansDistributedClusterer(dist, 3, 100, 1L);
    KMeansModel mdl = clusterer.cluster(points, 4);
    Vector[] resCenters = mdl.centers();
    Arrays.sort(resCenters, comp);
    checkIsInEpsilonNeighbourhood(resCenters, massCenters.toArray(new Vector[] {}), 30.0);
}
Also used : SparseDistributedMatrix(org.apache.ignite.ml.math.impls.matrix.SparseDistributedMatrix) DenseLocalOnHeapVector(org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector) Vector(org.apache.ignite.ml.math.Vector) DenseLocalOnHeapVector(org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 2 with SparseDistributedMatrix

use of org.apache.ignite.ml.math.impls.matrix.SparseDistributedMatrix in project ignite by apache.

the class KMeansDistributedClustererTest method testPerformClusterAnalysisDegenerate.

/** */
@Test
public void testPerformClusterAnalysisDegenerate() {
    IgniteUtils.setCurrentIgniteName(ignite.configuration().getIgniteInstanceName());
    KMeansDistributedClusterer clusterer = new KMeansDistributedClusterer(new EuclideanDistance(), 1, 1, 1L);
    double[] v1 = new double[] { 1959, 325100 };
    double[] v2 = new double[] { 1960, 373200 };
    SparseDistributedMatrix points = new SparseDistributedMatrix(2, 2, StorageConstants.ROW_STORAGE_MODE, StorageConstants.RANDOM_ACCESS_MODE);
    points.setRow(0, v1);
    points.setRow(1, v2);
    KMeansModel mdl = clusterer.cluster(points, 1);
    Assert.assertEquals(1, mdl.centers().length);
    Assert.assertEquals(2, mdl.centers()[0].size());
}
Also used : SparseDistributedMatrix(org.apache.ignite.ml.math.impls.matrix.SparseDistributedMatrix) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 3 with SparseDistributedMatrix

use of org.apache.ignite.ml.math.impls.matrix.SparseDistributedMatrix in project ignite by apache.

the class DistributedRegressionExample method main.

/** Run example. */
public static void main(String[] args) throws InterruptedException {
    System.out.println();
    System.out.println(">>> Linear regression 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(), () -> {
            double[] data = { 8, 78, 284, 9.100000381, 109, 9.300000191, 68, 433, 8.699999809, 144, 7.5, 70, 739, 7.199999809, 113, 8.899999619, 96, 1792, 8.899999619, 97, 10.19999981, 74, 477, 8.300000191, 206, 8.300000191, 111, 362, 10.89999962, 124, 8.800000191, 77, 671, 10, 152, 8.800000191, 168, 636, 9.100000381, 162, 10.69999981, 82, 329, 8.699999809, 150, 11.69999981, 89, 634, 7.599999905, 134, 8.5, 149, 631, 10.80000019, 292, 8.300000191, 60, 257, 9.5, 108, 8.199999809, 96, 284, 8.800000191, 111, 7.900000095, 83, 603, 9.5, 182, 10.30000019, 130, 686, 8.699999809, 129, 7.400000095, 145, 345, 11.19999981, 158, 9.600000381, 112, 1357, 9.699999809, 186, 9.300000191, 131, 544, 9.600000381, 177, 10.60000038, 80, 205, 9.100000381, 127, 9.699999809, 130, 1264, 9.199999809, 179, 11.60000038, 140, 688, 8.300000191, 80, 8.100000381, 154, 354, 8.399999619, 103, 9.800000191, 118, 1632, 9.399999619, 101, 7.400000095, 94, 348, 9.800000191, 117, 9.399999619, 119, 370, 10.39999962, 88, 11.19999981, 153, 648, 9.899999619, 78, 9.100000381, 116, 366, 9.199999809, 102, 10.5, 97, 540, 10.30000019, 95, 11.89999962, 176, 680, 8.899999619, 80, 8.399999619, 75, 345, 9.600000381, 92, 5, 134, 525, 10.30000019, 126, 9.800000191, 161, 870, 10.39999962, 108, 9.800000191, 111, 669, 9.699999809, 77, 10.80000019, 114, 452, 9.600000381, 60, 10.10000038, 142, 430, 10.69999981, 71, 10.89999962, 238, 822, 10.30000019, 86, 9.199999809, 78, 190, 10.69999981, 93, 8.300000191, 196, 867, 9.600000381, 106, 7.300000191, 125, 969, 10.5, 162, 9.399999619, 82, 499, 7.699999809, 95, 9.399999619, 125, 925, 10.19999981, 91, 9.800000191, 129, 353, 9.899999619, 52, 3.599999905, 84, 288, 8.399999619, 110, 8.399999619, 183, 718, 10.39999962, 69, 10.80000019, 119, 540, 9.199999809, 57, 10.10000038, 180, 668, 13, 106, 9, 82, 347, 8.800000191, 40, 10, 71, 345, 9.199999809, 50, 11.30000019, 118, 463, 7.800000191, 35, 11.30000019, 121, 728, 8.199999809, 86, 12.80000019, 68, 383, 7.400000095, 57, 10, 112, 316, 10.39999962, 57, 6.699999809, 109, 388, 8.899999619, 94 };
            final int nobs = 53;
            final int nvars = 4;
            System.out.println(">>> Create new SparseDistributedMatrix inside IgniteThread.");
            // Create SparseDistributedMatrix, new cache will be created automagically.
            SparseDistributedMatrix distributedMatrix = new SparseDistributedMatrix(0, 0, StorageConstants.ROW_STORAGE_MODE, StorageConstants.RANDOM_ACCESS_MODE);
            System.out.println(">>> Create new linear regression object");
            OLSMultipleLinearRegression regression = new OLSMultipleLinearRegression();
            regression.newSampleData(data, nobs, nvars, distributedMatrix);
            System.out.println();
            System.out.println(">>> Estimates the regression parameters b:");
            System.out.println(Arrays.toString(regression.estimateRegressionParameters()));
            System.out.println(">>> Estimates the residuals, ie u = y - X*b:");
            System.out.println(Arrays.toString(regression.estimateResiduals()));
            System.out.println(">>> Standard errors of the regression parameters:");
            System.out.println(Arrays.toString(regression.estimateRegressionParametersStandardErrors()));
            System.out.println(">>> Estimates the variance of the regression parameters, ie Var(b):");
            Tracer.showAscii(regression.estimateRegressionParametersVariance());
            System.out.println(">>> Estimates the standard error of the regression:");
            System.out.println(regression.estimateRegressionStandardError());
            System.out.println(">>> R-Squared statistic:");
            System.out.println(regression.calculateRSquared());
            System.out.println(">>> Adjusted R-squared statistic:");
            System.out.println(regression.calculateAdjustedRSquared());
            System.out.println(">>> Returns the variance of the regressand, ie Var(y):");
            System.out.println(regression.estimateErrorVariance());
        });
        igniteThread.start();
        igniteThread.join();
    }
}
Also used : SparseDistributedMatrix(org.apache.ignite.ml.math.impls.matrix.SparseDistributedMatrix) SparseDistributedMatrixExample(org.apache.ignite.examples.ml.math.matrix.SparseDistributedMatrixExample) OLSMultipleLinearRegression(org.apache.ignite.ml.regressions.OLSMultipleLinearRegression) Ignite(org.apache.ignite.Ignite) IgniteThread(org.apache.ignite.thread.IgniteThread)

Example 4 with SparseDistributedMatrix

use of org.apache.ignite.ml.math.impls.matrix.SparseDistributedMatrix in project ignite by apache.

the class SparseDistributedMatrixExample method main.

/**
 * Executes example.
 *
 * @param args Command line arguments, none required.
 */
public static void main(String[] args) throws InterruptedException {
    System.out.println();
    System.out.println(">>> 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(), () -> {
            double[][] testValues = { { 1.0, 0.0, 0.0 }, { 0.0, 1.0, 0.0 }, { 0.0, 0.0, 1.0 } };
            System.out.println(">>> Create new SparseDistributedMatrix inside IgniteThread.");
            // Create SparseDistributedMatrix, new cache will be created automagically.
            SparseDistributedMatrix distributedMatrix = new SparseDistributedMatrix(testValues.length, testValues[0].length, StorageConstants.ROW_STORAGE_MODE, StorageConstants.RANDOM_ACCESS_MODE);
            distributedMatrix.assign(testValues);
            System.out.println("Sum of all matrix elements is " + distributedMatrix.sum());
            System.out.println(">>> Destroy SparseDistributedMatrix after using.");
            // Destroy internal cache.
            distributedMatrix.destroy();
        });
        igniteThread.start();
        igniteThread.join();
    }
}
Also used : SparseDistributedMatrix(org.apache.ignite.ml.math.impls.matrix.SparseDistributedMatrix) Ignite(org.apache.ignite.Ignite) IgniteThread(org.apache.ignite.thread.IgniteThread)

Example 5 with SparseDistributedMatrix

use of org.apache.ignite.ml.math.impls.matrix.SparseDistributedMatrix in project ignite by apache.

the class DistributedLinearRegressionWithSGDTrainerExample 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 LinearRegressionSGDTrainer(100_000, 1e-12);
            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) SparseDistributedMatrix(org.apache.ignite.ml.math.impls.matrix.SparseDistributedMatrix) Matrix(org.apache.ignite.ml.math.Matrix) LinearRegressionModel(org.apache.ignite.ml.regressions.linear.LinearRegressionModel) LinearRegressionSGDTrainer(org.apache.ignite.ml.regressions.linear.LinearRegressionSGDTrainer) SparseDistributedVector(org.apache.ignite.ml.math.impls.vector.SparseDistributedVector) Ignite(org.apache.ignite.Ignite) IgniteThread(org.apache.ignite.thread.IgniteThread) SparseDistributedVector(org.apache.ignite.ml.math.impls.vector.SparseDistributedVector) Vector(org.apache.ignite.ml.math.Vector)

Aggregations

SparseDistributedMatrix (org.apache.ignite.ml.math.impls.matrix.SparseDistributedMatrix)29 Vector (org.apache.ignite.ml.math.Vector)18 DenseLocalOnHeapVector (org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector)14 Random (java.util.Random)11 EuclideanDistance (org.apache.ignite.ml.math.distances.EuclideanDistance)9 Ignite (org.apache.ignite.Ignite)8 IgniteThread (org.apache.ignite.thread.IgniteThread)8 HashMap (java.util.HashMap)7 List (java.util.List)7 Map (java.util.Map)7 Collectors (java.util.stream.Collectors)7 StorageConstants (org.apache.ignite.ml.math.StorageConstants)7 UUID (java.util.UUID)6 DistanceMeasure (org.apache.ignite.ml.math.distances.DistanceMeasure)6 IgniteFunction (org.apache.ignite.ml.math.functions.IgniteFunction)6 SparseDistributedMatrixStorage (org.apache.ignite.ml.math.impls.storage.matrix.SparseDistributedMatrixStorage)6 Collections (java.util.Collections)5 LinkedList (java.util.LinkedList)5 DoubleStream (java.util.stream.DoubleStream)5 IgniteUtils (org.apache.ignite.internal.util.IgniteUtils)5