use of org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector 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.vector.DenseLocalOnHeapVector 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.vector.DenseLocalOnHeapVector in project ignite by apache.
the class MatrixImplementationsTest method testTimesVector.
/** */
@Test
public void testTimesVector() {
consumeSampleMatrix((m, desc) -> {
if (ignore(m.getClass()))
return;
double[][] data = fillAndReturn(m);
double[] arr = fillArray(m.columnSize());
Vector times = m.times(new DenseLocalOnHeapVector(arr));
assertEquals("Unexpected vector size for " + desc, times.size(), m.rowSize());
for (int i = 0; i < m.rowSize(); i++) {
double exp = 0.0;
for (int j = 0; j < m.columnSize(); j++) exp += arr[j] * data[i][j];
assertEquals("Unexpected value for " + desc + " at " + i, times.get(i), exp, 0d);
}
testInvalidCardinality(() -> m.times(new DenseLocalOnHeapVector(m.columnSize() + 1)), desc);
});
}
use of org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector in project ignite by apache.
the class DiagonalMatrixTest method testSetGet.
/** */
@Test
public void testSetGet() {
verifyDiagonal(testMatrix);
final int size = MathTestConstants.STORAGE_SIZE;
for (Matrix m : new Matrix[] { new DenseLocalOnHeapMatrix(size + 1, size), new DenseLocalOnHeapMatrix(size, size + 1) }) {
fillMatrix(m);
verifyDiagonal(new DiagonalMatrix(m));
}
final double[] data = new double[size];
for (int i = 0; i < size; i++) data[i] = 1 + i;
final Matrix m = new DiagonalMatrix(new DenseLocalOnHeapVector(data));
assertEquals("Rows in matrix constructed from vector", size, m.rowSize());
assertEquals("Cols in matrix constructed from vector", size, m.columnSize());
for (int i = 0; i < size; i++) assertEquals(UNEXPECTED_VALUE + " at vector index " + i, data[i], m.get(i, i), 0d);
verifyDiagonal(m);
final Matrix m1 = new DiagonalMatrix(data);
assertEquals("Rows in matrix constructed from array", size, m1.rowSize());
assertEquals("Cols in matrix constructed from array", size, m1.columnSize());
for (int i = 0; i < size; i++) assertEquals(UNEXPECTED_VALUE + " at array index " + i, data[i], m1.get(i, i), 0d);
verifyDiagonal(m1);
}
use of org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector 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);
}
Aggregations