use of org.apache.ignite.ml.math.exceptions.ConvergenceException in project ignite by apache.
the class FuzzyCMeansDistributedClusterer method cluster.
/**
* {@inheritDoc}
*/
@Override
public FuzzyCMeansModel cluster(SparseDistributedMatrix points, int k) throws MathIllegalArgumentException, ConvergenceException {
GridArgumentCheck.notNull(points, "points");
if (k < 2)
throw new MathIllegalArgumentException("The number of clusters is less than 2");
Vector[] centers = initializeCenters(points, k);
MembershipsAndSums membershipsAndSums = null;
int iteration = 0;
boolean finished = false;
while (!finished && iteration < cMeansMaxIterations) {
MembershipsAndSums newMembershipsAndSums = calculateMembership(points, centers);
Vector[] newCenters = calculateNewCenters(points, newMembershipsAndSums, k);
if (stopCond == StopCondition.STABLE_CENTERS)
finished = isFinished(centers, newCenters);
else
finished = isFinished(membershipsAndSums, newMembershipsAndSums);
centers = newCenters;
membershipsAndSums = newMembershipsAndSums;
iteration++;
}
if (iteration == cMeansMaxIterations)
throw new ConvergenceException("Fuzzy C-Means algorithm has not converged after " + Integer.toString(iteration) + " iterations");
return new FuzzyCMeansModel(centers, measure);
}
use of org.apache.ignite.ml.math.exceptions.ConvergenceException in project ignite by apache.
the class FuzzyCMeansLocalClusterer method cluster.
/**
* {@inheritDoc}
*/
@Override
public FuzzyCMeansModel cluster(DenseLocalOnHeapMatrix points, int k, List<Double> weights) throws MathIllegalArgumentException, ConvergenceException {
GridArgumentCheck.notNull(points, "points");
GridArgumentCheck.notNull(weights, "weights");
if (points.rowSize() != weights.size())
throw new MathIllegalArgumentException("The number of points and the number of weights are not equal");
if (k < 2)
throw new MathIllegalArgumentException("The number of clusters is less than 2");
Matrix centers = new DenseLocalOnHeapMatrix(k, points.columnSize());
Matrix distances = new DenseLocalOnHeapMatrix(k, points.rowSize());
Matrix membership = new DenseLocalOnHeapMatrix(k, points.rowSize());
Vector weightsVector = new DenseLocalOnHeapVector(weights.size());
for (int i = 0; i < weights.size(); i++) weightsVector.setX(i, weights.get(i));
initializeCenters(centers, points, k, weightsVector);
int iteration = 0;
boolean finished = false;
while (iteration < maxIterations && !finished) {
calculateDistances(distances, points, centers);
Matrix newMembership = calculateMembership(distances, weightsVector);
Matrix newCenters = calculateNewCenters(points, newMembership);
if (this.stopCond == StopCondition.STABLE_CENTERS)
finished = areCentersStable(centers, newCenters);
else
finished = areMembershipStable(membership, newMembership);
centers = newCenters;
membership = newMembership;
iteration++;
}
if (iteration == maxIterations)
throw new ConvergenceException("Fuzzy C-Means algorithm has not converged after " + Integer.toString(iteration) + " iterations");
Vector[] centersArr = new Vector[k];
for (int i = 0; i < k; i++) centersArr[i] = centers.getRow(i);
return new FuzzyCMeansModel(centersArr, measure);
}
Aggregations