use of org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix in project ignite by apache.
the class FuzzyCMeansLocalClustererTest method checkCentersLocationOnSphere.
/**
* Test FCM on points located on the circle.
*/
@Test
public void checkCentersLocationOnSphere() {
FuzzyCMeansLocalClusterer clusterer = new FuzzyCMeansLocalClusterer(new EuclideanDistance(), 2, BaseFuzzyCMeansClusterer.StopCondition.STABLE_CENTERS, 0.01, 100, null);
int numOfPoints = 650;
double radius = 100.0;
double[][] points = new double[numOfPoints][2];
for (int i = 0; i < numOfPoints; i++) {
points[i][0] = Math.cos(Math.PI * 2 * i / numOfPoints) * radius;
points[i][1] = Math.sin(Math.PI * 2 * i / numOfPoints) * radius;
}
DenseLocalOnHeapMatrix pntMatrix = new DenseLocalOnHeapMatrix(points);
int k = 10;
FuzzyCMeansModel mdl = clusterer.cluster(pntMatrix, k);
Vector sum = mdl.centers()[0];
for (int i = 1; i < k; i++) sum = sum.plus(mdl.centers()[i]);
assertEquals(0, sum.kNorm(1), 1);
}
use of org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix in project ignite by apache.
the class FuzzyCMeansLocalClustererTest method testDifferentAmountsOfPointsAndWeights.
/**
* Test FCM on different numbers of points and weights.
*/
@Test(expected = MathIllegalArgumentException.class)
public void testDifferentAmountsOfPointsAndWeights() {
FuzzyCMeansLocalClusterer clusterer = new FuzzyCMeansLocalClusterer(new EuclideanDistance(), 2, BaseFuzzyCMeansClusterer.StopCondition.STABLE_CENTERS, 0.01, 10, null);
double[][] points = new double[][] { { 1 }, { 2 }, { 3 }, { 4 } };
ArrayList<Double> weights = new ArrayList<>();
Collections.addAll(weights, 1.0, 34.0, 2.5, 5.0, 0.5);
clusterer.cluster(new DenseLocalOnHeapMatrix(points), 2, weights);
}
use of org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix in project ignite by apache.
the class FuzzyCMeansLocalClustererTest method test2DLineClustering.
/**
* Test FCM on points that forms the line located on the plane.
*/
@Test
public void test2DLineClustering() {
FuzzyCMeansLocalClusterer clusterer = new FuzzyCMeansLocalClusterer(new EuclideanDistance(), 2, BaseFuzzyCMeansClusterer.StopCondition.STABLE_CENTERS, 0.01, 50, null);
double[][] points = new double[][] { { 1, 2 }, { 3, 6 }, { 5, 10 } };
DenseLocalOnHeapMatrix pntMatrix = new DenseLocalOnHeapMatrix(points);
int k = 2;
FuzzyCMeansModel mdl = clusterer.cluster(pntMatrix, k);
Vector[] centers = mdl.centers();
Arrays.sort(centers, Comparator.comparing(vector -> vector.getX(0)));
Vector[] exp = { new DenseLocalOnHeapVector(new double[] { 1.5, 3 }), new DenseLocalOnHeapVector(new double[] { 4.5, 9 }) };
for (int i = 0; i < k; i++) {
Vector center = centers[i];
for (int j = 0; j < 2; j++) assertEquals(exp[i].getX(j), center.getX(j), 0.5);
}
}
use of org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix in project ignite by apache.
the class FuzzyCMeansLocalClustererTest method differentWeightsOneDimension.
/**
* Test FCM on points that have different weights.
*/
@Test
public void differentWeightsOneDimension() {
FuzzyCMeansLocalClusterer clusterer = new FuzzyCMeansLocalClusterer(new EuclideanDistance(), 2, BaseFuzzyCMeansClusterer.StopCondition.STABLE_CENTERS, 0.01, 10, null);
double[][] points = new double[][] { { 1 }, { 2 }, { 3 }, { 4 }, { 5 }, { 6 } };
DenseLocalOnHeapMatrix pntMatrix = new DenseLocalOnHeapMatrix(points);
ArrayList<Double> weights = new ArrayList<>();
Collections.addAll(weights, 3.0, 2.0, 1.0, 1.0, 1.0, 1.0);
Vector[] centers1 = clusterer.cluster(pntMatrix, 2).centers();
Vector[] centers2 = clusterer.cluster(pntMatrix, 2, weights).centers();
Arrays.sort(centers1, Comparator.comparing(vector -> vector.getX(0)));
Arrays.sort(centers2, Comparator.comparing(vector -> vector.getX(0)));
assertTrue(centers1[0].get(0) - centers2[0].get(0) > 0.5);
}
use of org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix in project ignite by apache.
the class LUDecompositionTest method solveMtx.
/**
*/
@Test
public void solveMtx() throws Exception {
Matrix sol = new LUDecomposition(new PivotedMatrixView(testMatrix)).solve(new DenseLocalOnHeapMatrix(testMatrix.rowSize(), testMatrix.rowSize()));
assertEquals("Wrong solution matrix row size.", testMatrix.rowSize(), sol.rowSize());
assertEquals("Wrong solution matrix column size.", testMatrix.rowSize(), sol.columnSize());
for (int row = 0; row < sol.rowSize(); row++) for (int col = 0; col < sol.columnSize(); col++) assertEquals("Unexpected P value at (" + row + "," + col + ").", 0d, sol.getX(row, col), 0.0000001d);
}
Aggregations