Search in sources :

Example 1 with FuzzyCMeansLocalClusterer

use of org.apache.ignite.ml.clustering.FuzzyCMeansLocalClusterer in project ignite by apache.

the class IgniteFuzzyCMeansLocalClustererBenchmark method test.

/**
 * {@inheritDoc}
 */
@Override
public boolean test(Map<Object, Object> ctx) throws Exception {
    // IMPL NOTE originally taken from FuzzyLocalCMeansExample.
    // Distance measure that computes distance between two points.
    DistanceMeasure distanceMeasure = new EuclideanDistance();
    // "Fuzziness" - specific constant that is used in membership calculation (1.0+-eps ~ K-Means).
    double exponentialWeight = 2.0;
    // Condition that indicated when algorithm must stop.
    // In this example algorithm stops if memberships have changed insignificantly.
    BaseFuzzyCMeansClusterer.StopCondition stopCond = BaseFuzzyCMeansClusterer.StopCondition.STABLE_MEMBERSHIPS;
    // Maximum difference between new and old membership values with which algorithm will continue to work.
    double maxDelta = 0.01;
    // The maximum number of FCM iterations.
    int maxIterations = 50;
    // Create new local clusterer with parameters described above.
    FuzzyCMeansLocalClusterer clusterer = new FuzzyCMeansLocalClusterer(distanceMeasure, exponentialWeight, stopCond, maxDelta, maxIterations, null);
    // Create sample data.
    double[][] points = shuffle((int) (DataChanger.next()));
    // Create the matrix that contains sample points.
    DenseLocalOnHeapMatrix pntMatrix = new DenseLocalOnHeapMatrix(points);
    // Call clusterization method with some number of centers.
    // It returns model that can predict results for new points.
    int numCenters = 4;
    FuzzyCMeansModel mdl = clusterer.cluster(pntMatrix, numCenters);
    // Get centers of clusters that is computed by Fuzzy C-Means algorithm.
    mdl.centers();
    return true;
}
Also used : EuclideanDistance(org.apache.ignite.ml.math.distances.EuclideanDistance) FuzzyCMeansModel(org.apache.ignite.ml.clustering.FuzzyCMeansModel) FuzzyCMeansLocalClusterer(org.apache.ignite.ml.clustering.FuzzyCMeansLocalClusterer) BaseFuzzyCMeansClusterer(org.apache.ignite.ml.clustering.BaseFuzzyCMeansClusterer) DenseLocalOnHeapMatrix(org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix) DistanceMeasure(org.apache.ignite.ml.math.distances.DistanceMeasure)

Example 2 with FuzzyCMeansLocalClusterer

use of org.apache.ignite.ml.clustering.FuzzyCMeansLocalClusterer in project ignite by apache.

the class FuzzyCMeansLocalExample method main.

/**
 * Executes example.
 *
 * @param args Command line arguments, none required.
 */
public static void main(String[] args) {
    System.out.println(">>> Local Fuzzy C-Means usage example started.");
    // Distance measure that computes distance between two points.
    DistanceMeasure distanceMeasure = new EuclideanDistance();
    // "Fuzziness" - specific constant that is used in membership calculation (1.0+-eps ~ K-Means).
    double exponentialWeight = 2.0;
    // Condition that indicated when algorithm must stop.
    // In this example algorithm stops if memberships have changed insignificantly.
    BaseFuzzyCMeansClusterer.StopCondition stopCond = BaseFuzzyCMeansClusterer.StopCondition.STABLE_MEMBERSHIPS;
    // Maximum difference between new and old membership values with which algorithm will continue to work.
    double maxDelta = 0.01;
    // The maximum number of FCM iterations.
    int maxIterations = 50;
    // Value that is used to initialize random numbers generator. You can choose it randomly.
    Long seed = null;
    // Create new distributed clusterer with parameters described above.
    System.out.println(">>> Create new Local Fuzzy C-Means clusterer.");
    FuzzyCMeansLocalClusterer clusterer = new FuzzyCMeansLocalClusterer(distanceMeasure, exponentialWeight, stopCond, maxDelta, maxIterations, seed);
    // Create sample data.
    double[][] points = new double[][] { { -10, -10 }, { -9, -11 }, { -10, -9 }, { -11, -9 }, { 10, 10 }, { 9, 11 }, { 10, 9 }, { 11, 9 }, { -10, 10 }, { -9, 11 }, { -10, 9 }, { -11, 9 }, { 10, -10 }, { 9, -11 }, { 10, -9 }, { 11, -9 } };
    // Initialize matrix of data points. Each row contains one point.
    System.out.println(">>> Create the matrix that contains sample points.");
    // Store points into matrix.
    DenseLocalOnHeapMatrix pntMatrix = new DenseLocalOnHeapMatrix(points);
    // Call clusterization method with some number of centers.
    // It returns model that can predict results for new points.
    System.out.println(">>> Perform clusterization.");
    int numCenters = 4;
    FuzzyCMeansModel mdl = clusterer.cluster(pntMatrix, numCenters);
    // You can also get centers of clusters that is computed by Fuzzy C-Means algorithm.
    Vector[] centers = mdl.centers();
    String res = ">>> Results:\n" + ">>> 1st center: " + centers[0].get(0) + " " + centers[0].get(1) + "\n" + ">>> 2nd center: " + centers[1].get(0) + " " + centers[1].get(1) + "\n" + ">>> 3rd center: " + centers[2].get(0) + " " + centers[2].get(1) + "\n" + ">>> 4th center: " + centers[3].get(0) + " " + centers[3].get(1) + "\n";
    System.out.println(res);
}
Also used : DenseLocalOnHeapMatrix(org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix) DistanceMeasure(org.apache.ignite.ml.math.distances.DistanceMeasure) EuclideanDistance(org.apache.ignite.ml.math.distances.EuclideanDistance) FuzzyCMeansModel(org.apache.ignite.ml.clustering.FuzzyCMeansModel) FuzzyCMeansLocalClusterer(org.apache.ignite.ml.clustering.FuzzyCMeansLocalClusterer) BaseFuzzyCMeansClusterer(org.apache.ignite.ml.clustering.BaseFuzzyCMeansClusterer) Vector(org.apache.ignite.ml.math.Vector)

Aggregations

BaseFuzzyCMeansClusterer (org.apache.ignite.ml.clustering.BaseFuzzyCMeansClusterer)2 FuzzyCMeansLocalClusterer (org.apache.ignite.ml.clustering.FuzzyCMeansLocalClusterer)2 FuzzyCMeansModel (org.apache.ignite.ml.clustering.FuzzyCMeansModel)2 DistanceMeasure (org.apache.ignite.ml.math.distances.DistanceMeasure)2 EuclideanDistance (org.apache.ignite.ml.math.distances.EuclideanDistance)2 DenseLocalOnHeapMatrix (org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix)2 Vector (org.apache.ignite.ml.math.Vector)1