Search in sources :

Example 1 with ConvergenceException

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);
}
Also used : MathIllegalArgumentException(org.apache.ignite.ml.math.exceptions.MathIllegalArgumentException) ConvergenceException(org.apache.ignite.ml.math.exceptions.ConvergenceException) Vector(org.apache.ignite.ml.math.Vector) DenseLocalOnHeapVector(org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector)

Example 2 with ConvergenceException

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);
}
Also used : Matrix(org.apache.ignite.ml.math.Matrix) DenseLocalOnHeapMatrix(org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix) MathIllegalArgumentException(org.apache.ignite.ml.math.exceptions.MathIllegalArgumentException) ConvergenceException(org.apache.ignite.ml.math.exceptions.ConvergenceException) DenseLocalOnHeapVector(org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector) DenseLocalOnHeapMatrix(org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix) Vector(org.apache.ignite.ml.math.Vector) DenseLocalOnHeapVector(org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector)

Aggregations

Vector (org.apache.ignite.ml.math.Vector)2 ConvergenceException (org.apache.ignite.ml.math.exceptions.ConvergenceException)2 MathIllegalArgumentException (org.apache.ignite.ml.math.exceptions.MathIllegalArgumentException)2 DenseLocalOnHeapVector (org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector)2 Matrix (org.apache.ignite.ml.math.Matrix)1 DenseLocalOnHeapMatrix (org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix)1