use of org.apache.ignite.ml.math.Vector in project ignite by apache.
the class MatrixImplementationsTest method testLikeVector.
/** */
@Test
public void testLikeVector() {
consumeSampleMatrix((m, desc) -> {
if (likeVectorTypesMap().containsKey(m.getClass())) {
Vector likeVector = m.likeVector(m.columnSize());
assertNotNull(likeVector);
assertEquals("Unexpected value for " + desc, likeVector.size(), m.columnSize());
return;
}
boolean expECaught = false;
try {
m.likeVector(1);
} catch (UnsupportedOperationException uoe) {
expECaught = true;
}
assertTrue("Expected exception was not caught for " + desc, expECaught);
});
}
use of org.apache.ignite.ml.math.Vector 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.Vector in project ignite by apache.
the class CacheVectorTest method testMax.
/** */
public void testMax() {
IgniteUtils.setCurrentIgniteName(ignite.configuration().getIgniteInstanceName());
IdentityValueMapper valMapper = new IdentityValueMapper();
CacheVector<Integer, Double> cacheVector = new CacheVector<>(size, getCache(), keyMapper, valMapper);
Vector testVec = new DenseLocalOnHeapVector(IntStream.range(0, size).asDoubleStream().toArray());
cacheVector.assign(testVec);
assertEquals("Unexpected value.", cacheVector.maxValue(), testVec.get(size - 1), 0d);
}
use of org.apache.ignite.ml.math.Vector 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);
}
use of org.apache.ignite.ml.math.Vector in project ignite by apache.
the class LUDecompositionTest method getPivot.
/** */
@Test
public void getPivot() throws Exception {
Vector pivot = new LUDecomposition(testMatrix).getPivot();
assertEquals("Unexpected pivot size.", rawPivot.length, pivot.size());
for (int i = 0; i < testU.rowSize(); i++) assertEquals("Unexpected value at " + i, rawPivot[i], (int) pivot.get(i) + 1);
}
Aggregations