DataSharp/Data/Analysis/FuzzyCMeans.cs
/// <summary>
/// Find clusters and centroids using FCM algorithm.
/// </summary>
/// <exception cref="ArgumentNullException">If <param name="DistanceMetric" /> is null.</exception>
Find clusters and centroids using FCM algorithm.
Find clusters and centroids using FCM algorithm .
public void Cluster(double epsilon = 1e-12)
        {
            if (DistanceMetric == null)
                throw new ArgumentNullException("DistanceMetric");
            int numSamples = dataMatrix.Length;
            double[,] memberOf = new double[this.K, numSamples];
            centroids = GenerateRandomCentroids();
            double[,] distanceMatrix = new double[this.K, numSamples];
            bool clusterChanged = true;
            while (clusterChanged)
            {
                clusterChanged = false;
                for (int i = 0; i < this.K; i++)
                {
                    for (int j = 0; j < numSamples; j++)
                        distanceMatrix[i, j] = DistanceMetric(centroids[i], dataMatrix[j]);
                }
                for (int i = 0; i < this.K; i++)
                {
                    for (int j = 0; j < numSamples; j++)
                    {
                        double denominator = 0;
                        for (int k = 0; k < this.K; k++)
                        {
                            if (distanceMatrix[k, j] < 1e-12)
                                distanceMatrix[k, j] = 1e-12;
                            double quotientOfDistance = distanceMatrix[i, j] / distanceMatrix[k, j];
                            denominator += Math.Pow(quotientOfDistance, 2 / (this.ProbabilityDegree - 1));
                        }
                        double updatedMemberOfIJ = 1 / denominator;
                        if (Math.Abs(memberOf[i, j] - updatedMemberOfIJ) > epsilon)
                            clusterChanged = true;
                        memberOf[i, j] = updatedMemberOfIJ;
                    }
                }
                for (int i = 0; i < this.K; i++)
                {
                    centroids[i].Clear();
                    double denominator = 0;
                    for (int j = 0; j < numSamples; j++)
                    {
                        double memberOfFactor = Math.Pow(memberOf[i, j], this.ProbabilityDegree);
                        centroids[i] += memberOfFactor * dataMatrix[j];
                        denominator += memberOfFactor;
                    }
                    centroids[i] /= denominator;
                }
            }
            for (int j = 0; j < numSamples; j++)
            {
                int indexOfMax = 0;
                for (int i = 1; i < this.K; i++)
                {
                    if (memberOf[i, j] > memberOf[indexOfMax, j])
                        indexOfMax = i;
                }
                centroidIndices[j] = indexOfMax;
            }
        }
public void Cluster ( double epsilon = 1e-12 ) { if ( DistanceMetric == null ) throw new ArgumentNullException ( " " ) ; int numSamples = dataMatrix . Length ; double [ , ] memberOf = new double [ this . K , numSamples ] ; centroids = GenerateRandomCentroids ( ) ; double [ , ] distanceMatrix = new double [ this . K , numSamples ] ; bool clusterChanged = true ; while ( clusterChanged ) { clusterChanged = false ; for ( int i = 0 ; i < this . K ; i ++ ) { for ( int j = 0 ; j < numSamples ; j ++ ) distanceMatrix [ i , j ] = DistanceMetric ( centroids [ i ] , dataMatrix [ j ] ) ; } for ( int i = 0 ; i < this . K ; i ++ ) { for ( int j = 0 ; j < numSamples ; j ++ ) { double denominator = 0 ; for ( int k = 0 ; k < this . K ; k ++ ) { if ( distanceMatrix [ k , j ] < 1e-12 ) distanceMatrix [ k , j ] = 1e-12 ; double quotientOfDistance = distanceMatrix [ i , j ] / distanceMatrix [ k , j ] ; denominator += Math . Pow ( quotientOfDistance , 2 / ( this . ProbabilityDegree - 1 ) ) ; } double updatedMemberOfIJ = 1 / denominator ; if ( Math . Abs ( memberOf [ i , j ] - updatedMemberOfIJ ) > epsilon ) clusterChanged = true ; memberOf [ i , j ] = updatedMemberOfIJ ; } } for ( int i = 0 ; i < this . K ; i ++ ) { centroids [ i ] . Clear ( ) ; double denominator = 0 ; for ( int j = 0 ; j < numSamples ; j ++ ) { double memberOfFactor = Math . Pow ( memberOf [ i , j ] , this . ProbabilityDegree ) ; centroids [ i ] += memberOfFactor * dataMatrix [ j ] ; denominator += memberOfFactor ; } centroids [ i ] /= denominator ; } } for ( int j = 0 ; j < numSamples ; j ++ ) { int indexOfMax = 0 ; for ( int i = 1 ; i < this . K ; i ++ ) { if ( memberOf [ i , j ] > memberOf [ indexOfMax , j ] ) indexOfMax = i ; } centroidIndices [ j ] = indexOfMax ; } }
Find clusters and centroids using FCM algorithm.
Find clusters and centroids using FCM algorithm .
// Check if there is a distance metric. // Get the number of samples. // A double array stores the cluster infomation of each sample. // memberOf[i, j]  = the probability of j-th sample belonging to cluster i. // Get initial centroids. // distanceMatrix[i, j] = distance between i-th centroid and j-th sample. // A boolean flag that inidicates if clusters changed. // Loop unitl converge. // Clear the flag. // Update distance matrix. // Update member infomation. // Calculate u[i, j] (i.e. memberOf[i, j]) using  formula. // Update centroids. // Store the indices of each sample.
If  is null.
If is null .
epsilon
double