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;
}
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);
}
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.");
}
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.");
}
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.");
}
Aggregations