use of org.apache.ignite.ml.math.Vector in project ignite by apache.
the class ColumnDecisionTreeTrainerBenchmark method tstF1.
/**
* Test decision tree regression.
* To run this test rename this method so it starts from 'test'.
*/
public void tstF1() {
IgniteUtils.setCurrentIgniteName(ignite.configuration().getIgniteInstanceName());
int ptsCnt = 10000;
Map<Integer, double[]> ranges = new HashMap<>();
ranges.put(0, new double[] { -100.0, 100.0 });
ranges.put(1, new double[] { -100.0, 100.0 });
ranges.put(2, new double[] { -100.0, 100.0 });
int featCnt = 100;
double[] defRng = { -1.0, 1.0 };
Vector[] trainVectors = vecsFromRanges(ranges, featCnt, defRng, new Random(123L), ptsCnt, f1);
SparseDistributedMatrix m = new SparseDistributedMatrix(ptsCnt, featCnt + 1, StorageConstants.COLUMN_STORAGE_MODE, StorageConstants.RANDOM_ACCESS_MODE);
SparseDistributedMatrixStorage sto = (SparseDistributedMatrixStorage) m.getStorage();
loadVectorsIntoSparseDistributedMatrixCache(sto.cache().getName(), sto.getUUID(), Arrays.stream(trainVectors).iterator(), featCnt + 1);
IgniteFunction<DoubleStream, Double> regCalc = s -> s.average().orElse(0.0);
ColumnDecisionTreeTrainer<VarianceSplitCalculator.VarianceData> trainer = new ColumnDecisionTreeTrainer<>(10, ContinuousSplitCalculators.VARIANCE, RegionCalculators.VARIANCE, regCalc, ignite);
X.println("Training started.");
long before = System.currentTimeMillis();
DecisionTreeModel mdl = trainer.train(new MatrixColumnDecisionTreeTrainerInput(m, new HashMap<>()));
X.println("Training finished in: " + (System.currentTimeMillis() - before) + " ms.");
Vector[] testVectors = vecsFromRanges(ranges, featCnt, defRng, new Random(123L), 20, f1);
IgniteTriFunction<Model<Vector, Double>, Stream<IgniteBiTuple<Vector, Double>>, Function<Double, Double>, Double> mse = Estimators.MSE();
Double accuracy = mse.apply(mdl, Arrays.stream(testVectors).map(v -> new IgniteBiTuple<>(v.viewPart(0, featCnt), v.getX(featCnt))), Function.identity());
X.println("MSE: " + accuracy);
}
use of org.apache.ignite.ml.math.Vector in project ignite by apache.
the class VectorBenchmarkTest method foldMapMix.
/**
*/
private void foldMapMix(int size, Function<Integer, Vector> constructor) {
final Vector v1 = constructor.apply(size), v2 = constructor.apply(size);
for (int idx = 0; idx < size; idx++) {
v1.set(idx, idx);
v2.set(idx, size - idx);
}
assertNotNull(v1.map((val) -> (val + 1)));
assertNotNull(v1.map(v2, (one, other) -> one + other / 2.0));
assertNotNull(v1.map((val, val1) -> (val + val1), 2.0));
assertNotNull(v1.foldMap((sum, val) -> (val + sum), (val) -> val, 0.0));
assertNotNull(v1.foldMap(v2, (sum, val) -> (val + sum), (val1, val2) -> val1 + val2, 0.0));
}
use of org.apache.ignite.ml.math.Vector in project ignite by apache.
the class VectorBenchmarkTest method basicMix.
/**
*/
private void basicMix(int size, Function<Integer, Vector> constructor) {
final Vector v1 = constructor.apply(size), v2 = constructor.apply(size);
for (int idx = 0; idx < size; idx++) {
v1.set(idx, idx);
v2.set(idx, size - idx);
}
assertNotNull(v1.sum());
assertNotNull(v1.copy());
assertFalse(v1.getLengthSquared() < 0);
assertNotNull(v1.normalize());
assertNotNull(v1.logNormalize());
assertFalse(v1.getDistanceSquared(v2) < 0);
assertNotNull(v1.divide(2));
assertNotNull(v1.minus(v2));
assertNotNull(v1.plus(v2));
assertNotNull(v1.dot(v2));
assertNotNull(v1.assign(v2));
// IMPL NOTE this would better be last test for it sets all values the same
assertNotNull(v1.assign(1));
}
use of org.apache.ignite.ml.math.Vector in project ignite by apache.
the class CholeskyDecompositionTest method basicTest.
/**
*/
private void basicTest(Matrix m) {
// This decomposition is useful when dealing with systems of linear equations of the form
// m x = b where m is a Hermitian matrix.
// For such systems Cholesky decomposition provides
// more effective method of solving compared to LU decomposition.
// Suppose we want to solve system
// m x = b for various bs. Then after we computed Cholesky decomposition, we can feed various bs
// as a matrix of the form
// (b1, b2, ..., bm)
// to the method Cholesky::solve which returns solutions in the form
// (sol1, sol2, ..., solm)
CholeskyDecomposition dec = new CholeskyDecomposition(m);
assertEquals("Unexpected value for decomposition determinant.", 4d, dec.getDeterminant(), 0d);
Matrix l = dec.getL();
Matrix lt = dec.getLT();
assertNotNull("Matrix l is expected to be not null.", l);
assertNotNull("Matrix lt is expected to be not null.", lt);
for (int row = 0; row < l.rowSize(); row++) for (int col = 0; col < l.columnSize(); col++) assertEquals("Unexpected value transposed matrix at (" + row + "," + col + ").", l.get(row, col), lt.get(col, row), 0d);
Matrix bs = new DenseLocalOnHeapMatrix(new double[][] { { 4.0, -6.0, 7.0 }, { 1.0, 1.0, 1.0 } }).transpose();
Matrix sol = dec.solve(bs);
assertNotNull("Solution matrix is expected to be not null.", sol);
assertEquals("Solution rows are not as expected.", bs.rowSize(), sol.rowSize());
assertEquals("Solution columns are not as expected.", bs.columnSize(), sol.columnSize());
for (int i = 0; i < sol.columnSize(); i++) assertNotNull("Solution matrix column is expected to be not null at index " + i, sol.viewColumn(i));
Vector b = new DenseLocalOnHeapVector(new double[] { 4.0, -6.0, 7.0 });
Vector solVec = dec.solve(b);
for (int idx = 0; idx < b.size(); idx++) assertEquals("Unexpected value solution vector at " + idx, b.get(idx), solVec.get(idx), 0d);
dec.destroy();
}
use of org.apache.ignite.ml.math.Vector in project ignite by apache.
the class EigenDecompositionTest method assertDiagonalConsistsOfEigenvalues.
/**
*/
private void assertDiagonalConsistsOfEigenvalues(DenseLocalOnHeapMatrix m, Matrix d, Matrix v) {
int n = m.columnSize();
for (int i = 0; i < n; i++) {
Vector eigenVector = v.viewColumn(i);
double eigenVal = d.getX(i, i);
assertVectorsAreEqual(m.times(eigenVector), eigenVector.times(eigenVal));
}
}
Aggregations