DataSharp/Data/Analysis/FuzzyCMeans.cs
/// <summary>
/// Generate random initial centroids using roulette selection.
/// </summary>
/// <returns>Random initial centroids selected by roulette selection.</returns>
Generate random initial centroids using roulette selection.
Generate random initial centroids using roulette selection .
protected Vector[] GenerateRandomCentroids()
        {
            Vector[] randomCentroids = new Vector[this.K];
            int numSamples = dataMatrix.Length;
            LinkedList<int> centroidIndices = new LinkedList<int>();
            int nextIndex = StdRandom.Uniform(0, numSamples);
            randomCentroids[0] = dataMatrix[nextIndex];
            centroidIndices.AddLast(nextIndex);
            Vector lastCentroid = randomCentroids[0];
            Vector distanceSquares = new Vector(numSamples);
            double[] probabilities = new double[numSamples];
            while (centroidIndices.Count < this.K)
            {
                for (int i = 0; i < numSamples; i++)
                {
                    distanceSquares[i] = DistanceMetric(dataMatrix[i], lastCentroid);
                    distanceSquares[i] = distanceSquares[i] * distanceSquares[i];
                }
                double distanceSquareSum = distanceSquares.SumComponents();
                for (int i = 0; i < numSamples; i++)
                    probabilities[i] = distanceSquares[i] / distanceSquareSum;
                for (; ; )
                {
                    int selectedIndex = MathUtil.RouletteSelection(probabilities);
                    if (centroidIndices.Contains(selectedIndex))
                        continue;
                    else
                    {
                        centroidIndices.AddLast(selectedIndex);
                        lastCentroid = dataMatrix[selectedIndex];
                        break;
                    }
                }
            }
            Vector[] initialCentroids = new Vector[this.K];
            int j = 0;
            foreach (int selectedIndex in centroidIndices)
                initialCentroids[j++] = dataMatrix[selectedIndex];
            return initialCentroids;
        }
protected Vector [ ] GenerateRandomCentroids ( ) { Vector [ ] randomCentroids = new Vector [ this . K ] ; int numSamples = dataMatrix . Length ; LinkedList < int > centroidIndices = new LinkedList < int > ( ) ; int nextIndex = StdRandom . Uniform ( 0 , numSamples ) ; randomCentroids [ 0 ] = dataMatrix [ nextIndex ] ; centroidIndices . AddLast ( nextIndex ) ; Vector lastCentroid = randomCentroids [ 0 ] ; Vector distanceSquares = new Vector ( numSamples ) ; double [ ] probabilities = new double [ numSamples ] ; while ( centroidIndices . Count < this . K ) { for ( int i = 0 ; i < numSamples ; i ++ ) { distanceSquares [ i ] = DistanceMetric ( dataMatrix [ i ] , lastCentroid ) ; distanceSquares [ i ] = distanceSquares [ i ] * distanceSquares [ i ] ; } double distanceSquareSum = distanceSquares . SumComponents ( ) ; for ( int i = 0 ; i < numSamples ; i ++ ) probabilities [ i ] = distanceSquares [ i ] / distanceSquareSum ; for ( ; ; ) { int selectedIndex = MathUtil . RouletteSelection ( probabilities ) ; if ( centroidIndices . Contains ( selectedIndex ) ) continue ; else { centroidIndices . AddLast ( selectedIndex ) ; lastCentroid = dataMatrix [ selectedIndex ] ; break ; } } } Vector [ ] initialCentroids = new Vector [ this . K ] ; int j = 0 ; foreach ( int selectedIndex in centroidIndices ) initialCentroids [ j ++ ] = dataMatrix [ selectedIndex ] ; return initialCentroids ; }
Generate random initial centroids using roulette selection.
Generate random initial centroids using roulette selection .
// Get the squares of distances between each sample and last selected centroid. // Calculate the probability of each sample. // Loop until find next centroid. // Get the index of next potential centroid using roulette selection. // Continue if current selected index has been seleceted before. // Otherwise, store the selected index and break this loop to find next one.
Random initial centroids selected by roulette selection.
Random initial centroids selected by roulette selection .