Search in sources :

Example 6 with DenseLocalOnHeapMatrix

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

the class OLSMultipleLinearRegressionTest method createRegression.

/** */
@Override
protected OLSMultipleLinearRegression createRegression() {
    OLSMultipleLinearRegression regression = new OLSMultipleLinearRegression();
    regression.newSampleData(new DenseLocalOnHeapVector(y), new DenseLocalOnHeapMatrix(x));
    return regression;
}
Also used : DenseLocalOnHeapVector(org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector) DenseLocalOnHeapMatrix(org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix)

Example 7 with DenseLocalOnHeapMatrix

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

the class OLSMultipleLinearRegressionTest method testYVariance.

/**
     * test calculateYVariance
     */
@Test
public void testYVariance() {
    // assumes: y = new double[]{11.0, 12.0, 13.0, 14.0, 15.0, 16.0};
    OLSMultipleLinearRegression mdl = new OLSMultipleLinearRegression();
    mdl.newSampleData(new DenseLocalOnHeapVector(y), new DenseLocalOnHeapMatrix(x));
    TestUtils.assertEquals(mdl.calculateYVariance(), 3.5, 0);
}
Also used : DenseLocalOnHeapVector(org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector) DenseLocalOnHeapMatrix(org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix) Test(org.junit.Test)

Example 8 with DenseLocalOnHeapMatrix

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

the class KMeansLocalClustererExample method main.

/**
 * Executes example.
 *
 * @param args Command line arguments, none required.
 */
public static void main(String[] args) {
    // IMPL NOTE based on KMeansDistributedClustererTestSingleNode#testClusterizationOnDatasetWithObviousStructure
    System.out.println(">>> K-means local clusterer example started.");
    int ptsCnt = 10000;
    DenseLocalOnHeapMatrix points = new DenseLocalOnHeapMatrix(ptsCnt, 2);
    DatasetWithObviousStructure dataset = new DatasetWithObviousStructure(10000);
    List<Vector> massCenters = dataset.generate(points);
    EuclideanDistance dist = new EuclideanDistance();
    OrderedNodesComparator comp = new OrderedNodesComparator(dataset.centers().values().toArray(new Vector[] {}), dist);
    massCenters.sort(comp);
    KMeansLocalClusterer clusterer = new KMeansLocalClusterer(dist, 100, 1L);
    KMeansModel mdl = clusterer.cluster(points, 4);
    Vector[] resCenters = mdl.centers();
    Arrays.sort(resCenters, comp);
    System.out.println("Mass centers:");
    massCenters.forEach(Tracer::showAscii);
    System.out.println("Cluster centers:");
    Arrays.asList(resCenters).forEach(Tracer::showAscii);
    System.out.println("\n>>> K-means local clusterer example completed.");
}
Also used : EuclideanDistance(org.apache.ignite.ml.math.distances.EuclideanDistance) KMeansModel(org.apache.ignite.ml.clustering.KMeansModel) Tracer(org.apache.ignite.ml.math.Tracer) DenseLocalOnHeapMatrix(org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix) Vector(org.apache.ignite.ml.math.Vector) KMeansLocalClusterer(org.apache.ignite.ml.clustering.KMeansLocalClusterer)

Example 9 with DenseLocalOnHeapMatrix

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

the class LUDecompositionExample method main.

/**
 * Executes example.
 *
 * @param args Command line arguments, none required.
 */
public static void main(String[] args) {
    System.out.println(">>> LU decomposition example started.");
    // Let's compute a LU decomposition for some (n x n) matrix m:
    // m = p l u, where
    // p is an (n x n) is a row-permutation matrix
    // l is a (n x n) lower triangular matrix
    // u is a (n x n) upper triangular matrix
    DenseLocalOnHeapMatrix m = new DenseLocalOnHeapMatrix(new double[][] { { 1.0d, 1.0d, -1.0d }, { 1.0d, -2.0d, 3.0d }, { 2.0d, 3.0d, 1.0d } });
    System.out.println("\n>>> Matrix m for decomposition: ");
    Tracer.showAscii(m);
    // This decomposition is useful when dealing with systems of linear equations.
    // (see https://en.wikipedia.org/wiki/LU_decomposition)
    // suppose we want to solve system
    // m x = b for various bs. Then after we computed LU decomposition, we can feed various bs
    // as a matrix of the form
    // (b1, b2, ..., bm)
    // to the method LUDecomposition::solve which returns solutions in the form
    // (sol1, sol2, ..., solm)
    LUDecomposition dec = new LUDecomposition(m);
    System.out.println("\n>>> Made decomposition.");
    System.out.println(">>> Matrix getL is ");
    Tracer.showAscii(dec.getL());
    System.out.println(">>> Matrix getU is ");
    Tracer.showAscii(dec.getU());
    System.out.println(">>> Matrix getP is ");
    Tracer.showAscii(dec.getP());
    Matrix bs = new DenseLocalOnHeapMatrix(new double[][] { { 4.0, -6.0, 7.0 }, { 1.0, 1.0, 1.0 } });
    System.out.println("\n>>> Matrix to solve: ");
    Tracer.showAscii(bs);
    Matrix sol = dec.solve(bs.transpose());
    System.out.println("\n>>> List of solutions: ");
    for (int i = 0; i < sol.columnSize(); i++) Tracer.showAscii(sol.viewColumn(i));
    System.out.println("\n>>> LU decomposition example completed.");
}
Also used : Matrix(org.apache.ignite.ml.math.Matrix) DenseLocalOnHeapMatrix(org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix) LUDecomposition(org.apache.ignite.ml.math.decompositions.LUDecomposition) DenseLocalOnHeapMatrix(org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix)

Example 10 with DenseLocalOnHeapMatrix

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

the class EigenDecompositionExample method main.

/**
 * Executes example.
 *
 * @param args Command line arguments, none required.
 */
public static void main(String[] args) {
    System.out.println(">>> Eigen decomposition example started.");
    // Let's compute EigenDecomposition for some square (n x n) matrix m with real eigenvalues:
    // m = v d v^{-1}, where d is diagonal matrix having eigenvalues of m on diagonal
    // and v is matrix where i-th column is eigenvector for i-th eigenvalue (i from 0 to n - 1)
    DenseLocalOnHeapMatrix m = new DenseLocalOnHeapMatrix(new double[][] { { 1.0d, 0.0d, 0.0d, 0.0d }, { 0.0d, 1.0d, 0.0d, 0.0d }, { 0.0d, 0.0d, 2.0d, 0.0d }, { 1.0d, 1.0d, 0.0d, 2.0d } });
    System.out.println("\n>>> Matrix m for decomposition: ");
    Tracer.showAscii(m);
    EigenDecomposition dec = new EigenDecomposition(m);
    System.out.println("\n>>> Made decomposition.");
    System.out.println(">>> Matrix getV is ");
    Tracer.showAscii(dec.getV());
    System.out.println(">>> Matrix getD is ");
    Tracer.showAscii(dec.getD());
    // From this decomposition we, for example, can easily compute determinant of matrix m
    // det (m) = det (v d v^{-1}) =
    // det(v) det (d) det(v^{-1}) =
    // det(v) det(v)^{-1} det(d) =
    // det (d) =
    // product of diagonal elements of d =
    // product of eigenvalues
    double det = dec.getRealEigenValues().foldMap(Functions.MULT, Functions.IDENTITY, 1.0);
    System.out.println("\n>>> Determinant is " + det);
    System.out.println("\n>>> Eigen decomposition example completed.");
}
Also used : EigenDecomposition(org.apache.ignite.ml.math.decompositions.EigenDecomposition) 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