Search in sources :

Example 66 with DenseLocalOnHeapMatrix

use of org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix in project ignite by apache.

the class FuzzyCMeansLocalClusterer method calculateMembership.

/**
 * Calculate membership matrix.
 *
 * @param distances Matrix of distances.
 * @param weights Vector of weights.
 * @
 */
private Matrix calculateMembership(Matrix distances, Vector weights) {
    Matrix newMembership = new DenseLocalOnHeapMatrix(distances.rowSize(), distances.columnSize());
    int numPoints = distances.columnSize();
    int numCenters = distances.rowSize();
    double fuzzyMembershipCoefficient = 2 / (exponentialWeight - 1);
    for (int i = 0; i < numCenters; i++) {
        for (int j = 0; j < numPoints; j++) {
            double invertedFuzzyWeight = 0.0;
            for (int k = 0; k < numCenters; k++) {
                double val = Math.pow(distances.get(i, j) / distances.get(k, j), fuzzyMembershipCoefficient);
                if (Double.isNaN(val))
                    val = 1.0;
                invertedFuzzyWeight += val;
            }
            double weight = 1.0 / invertedFuzzyWeight * weights.getX(j);
            newMembership.setX(i, j, Math.pow(weight, exponentialWeight));
        }
    }
    return newMembership;
}
Also used : Matrix(org.apache.ignite.ml.math.Matrix) DenseLocalOnHeapMatrix(org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix) DenseLocalOnHeapMatrix(org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix)

Example 67 with DenseLocalOnHeapMatrix

use of org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix 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

DenseLocalOnHeapMatrix (org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix)67 Test (org.junit.Test)33 Matrix (org.apache.ignite.ml.math.Matrix)28 DenseLocalOnHeapVector (org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector)19 Vector (org.apache.ignite.ml.math.Vector)17 EuclideanDistance (org.apache.ignite.ml.math.distances.EuclideanDistance)14 ArrayList (java.util.ArrayList)8 DistanceMeasure (org.apache.ignite.ml.math.distances.DistanceMeasure)6 Random (java.util.Random)5 MathIllegalArgumentException (org.apache.ignite.ml.math.exceptions.MathIllegalArgumentException)5 Arrays (java.util.Arrays)4 Collections (java.util.Collections)4 Comparator (java.util.Comparator)4 MLPArchitecture (org.apache.ignite.ml.nn.architecture.MLPArchitecture)4 Assert.assertEquals (org.junit.Assert.assertEquals)4 Assert.assertTrue (org.junit.Assert.assertTrue)4 Ignite (org.apache.ignite.Ignite)3 KMeansLocalClusterer (org.apache.ignite.ml.clustering.KMeansLocalClusterer)3 BaseFuzzyCMeansClusterer (org.apache.ignite.ml.clustering.BaseFuzzyCMeansClusterer)2 FuzzyCMeansLocalClusterer (org.apache.ignite.ml.clustering.FuzzyCMeansLocalClusterer)2