Search in sources :

Example 46 with DenseLocalOnHeapMatrix

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

the class SingularValueDecompositionExample method main.

/**
 * Executes example.
 *
 * @param args Command line arguments, none required.
 */
public static void main(String[] args) {
    System.out.println(">>> Singular value decomposition (SVD) example started.");
    // Let's compute a SVD of (l x k) matrix m. This decomposition can be thought as extension of EigenDecomposition to
    // rectangular matrices. The factorization we get is following:
    // m = u * s * v^{*}, where
    // u is a real or complex unitary matrix
    // s is a rectangular diagonal matrix with non-negative real numbers on diagonal (this numbers are singular values of m)
    // v is a real or complex unitary matrix
    // If m is real then u and v are also real.
    // Complex case is not supported for the moment.
    DenseLocalOnHeapMatrix m = new DenseLocalOnHeapMatrix(new double[][] { { 1.0d, 0.0d, 0.0d, 0.0d, 2.0d }, { 0.0d, 0.0d, 3.0d, 0.0d, 0.0d }, { 0.0d, 0.0d, 0.0d, 0.0d, 0.0d }, { 0.0d, 2.0d, 0.0d, 0.0d, 0.0d } });
    System.out.println("\n>>> Matrix m for decomposition: ");
    Tracer.showAscii(m);
    SingularValueDecomposition dec = new SingularValueDecomposition(m);
    System.out.println("\n>>> Made decomposition m = u * s * v^{*}.");
    System.out.println(">>> Matrix u is ");
    Tracer.showAscii(dec.getU());
    System.out.println(">>> Matrix s is ");
    Tracer.showAscii(dec.getS());
    System.out.println(">>> Matrix v is ");
    Tracer.showAscii(dec.getV());
    // This decomposition can in particular help with solving problem of finding x minimizing 2-norm of m x such
    // that 2-norm of x is 1. It appears that it is the right singular vector corresponding to minimal singular
    // value, which is always last.
    System.out.println("\n>>> Vector x minimizing 2-norm of m x such that 2 norm of x is 1: ");
    Tracer.showAscii(dec.getV().viewColumn(dec.getSingularValues().length - 1));
    System.out.println("\n>>> Singular value decomposition (SVD) example completed.");
}
Also used : DenseLocalOnHeapMatrix(org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix) SingularValueDecomposition(org.apache.ignite.ml.math.decompositions.SingularValueDecomposition)

Example 47 with DenseLocalOnHeapMatrix

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

the class IgniteKMeansLocalClustererBenchmark method test.

/**
 * {@inheritDoc}
 */
@Override
public boolean test(Map<Object, Object> ctx) throws Exception {
    final DataChanger.Scale scale = new DataChanger.Scale();
    // IMPL NOTE originally taken from KMeansLocalClustererTest
    KMeansLocalClusterer clusterer = new KMeansLocalClusterer(new EuclideanDistance(), 1, 1L);
    double[] v1 = scale.mutate(new double[] { 1959, 325100 });
    double[] v2 = scale.mutate(new double[] { 1960, 373200 });
    DenseLocalOnHeapMatrix points = new DenseLocalOnHeapMatrix(new double[][] { v1, v2 });
    clusterer.cluster(points, 1);
    return true;
}
Also used : EuclideanDistance(org.apache.ignite.ml.math.distances.EuclideanDistance) DataChanger(org.apache.ignite.yardstick.ml.DataChanger) DenseLocalOnHeapMatrix(org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix) KMeansLocalClusterer(org.apache.ignite.ml.clustering.KMeansLocalClusterer)

Example 48 with DenseLocalOnHeapMatrix

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

the class IgniteSingularValueDecompositionBenchmark method runSingularValueDecomposition.

/**
 * Based on SingularValueDecompositionTest#basicTest.
 */
private void runSingularValueDecomposition() {
    Matrix m = new DenseLocalOnHeapMatrix(new DataChanger.Scale().mutate(new double[][] { { 2.0d, -1.0d, 0.0d }, { -1.0d, 2.0d, -1.0d }, { 0.0d, -1.0d, 2.0d } }));
    SingularValueDecomposition dec = new SingularValueDecomposition(m);
    Matrix s = dec.getS();
    Matrix u = dec.getU();
    Matrix v = dec.getV();
    u.times(s).times(v.transpose());
    dec.destroy();
}
Also used : Matrix(org.apache.ignite.ml.math.Matrix) DenseLocalOnHeapMatrix(org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix) DenseLocalOnHeapMatrix(org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix) SingularValueDecomposition(org.apache.ignite.ml.math.decompositions.SingularValueDecomposition)

Example 49 with DenseLocalOnHeapMatrix

use of org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix 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 50 with DenseLocalOnHeapMatrix

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

the class FuzzyCMeansDistributedClusterer method chooseKCenters.

/**
 * Weight candidates and use K-Means to choose required number of them.
 *
 * @param cacheName Cache name of the point matrix.
 * @param uuid Uuid of the point matrix.
 * @param centers The list of candidates.
 * @param k The estimated number of centers.
 * @return {@code k} centers.
 */
private Vector[] chooseKCenters(String cacheName, UUID uuid, List<Vector> centers, int k) {
    centers = centers.stream().distinct().collect(Collectors.toList());
    ConcurrentHashMap<Integer, Integer> weightsMap = weightCenters(cacheName, uuid, centers);
    List<Double> weights = new ArrayList<>(centers.size());
    for (int i = 0; i < centers.size(); i++) weights.add(i, Double.valueOf(weightsMap.getOrDefault(i, 0)));
    DenseLocalOnHeapMatrix centersMatrix = MatrixUtil.fromList(centers, true);
    KMeansLocalClusterer clusterer = new KMeansLocalClusterer(measure, kMeansMaxIterations, seed);
    return clusterer.cluster(centersMatrix, k, weights).centers();
}
Also used : ArrayList(java.util.ArrayList) DenseLocalOnHeapMatrix(org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix)

Aggregations

DenseLocalOnHeapMatrix (org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix)67 Test (org.junit.Test)33 Matrix (org.apache.ignite.ml.math.Matrix)28 DenseLocalOnHeapVector (org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector)19 Vector (org.apache.ignite.ml.math.Vector)17 EuclideanDistance (org.apache.ignite.ml.math.distances.EuclideanDistance)14 ArrayList (java.util.ArrayList)8 DistanceMeasure (org.apache.ignite.ml.math.distances.DistanceMeasure)6 Random (java.util.Random)5 MathIllegalArgumentException (org.apache.ignite.ml.math.exceptions.MathIllegalArgumentException)5 Arrays (java.util.Arrays)4 Collections (java.util.Collections)4 Comparator (java.util.Comparator)4 MLPArchitecture (org.apache.ignite.ml.nn.architecture.MLPArchitecture)4 Assert.assertEquals (org.junit.Assert.assertEquals)4 Assert.assertTrue (org.junit.Assert.assertTrue)4 Ignite (org.apache.ignite.Ignite)3 KMeansLocalClusterer (org.apache.ignite.ml.clustering.KMeansLocalClusterer)3 BaseFuzzyCMeansClusterer (org.apache.ignite.ml.clustering.BaseFuzzyCMeansClusterer)2 FuzzyCMeansLocalClusterer (org.apache.ignite.ml.clustering.FuzzyCMeansLocalClusterer)2