Ejemplo n.º 1
0
        /// <summary>
        /// Re-assign sample to its nearest cluster center.
        /// </summary>
        /// <param name="sample">Sample to be clustered.</param>
        /// <param name="centroids">Centroids to be assigned.</param>
        /// <returns>The inner cluster distance.</returns>
        private float Assign(Sample sample, IList<Sample> centroids)
        {
            var smallestDistance = float.MaxValue;
            var lastCentroid = sample.Centroid;

            foreach (var centroid in centroids)
            {
                var distance = _sampleStrategy.Diff(sample, centroid);

                if (distance < smallestDistance)
                {
                    smallestDistance = distance;
                    sample.Centroid = centroid;
                }
            }

            return smallestDistance;
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Find the furthest sample to reference.
        /// </summary>
        /// <param name="reference">Reference sample.</param>
        /// <param name="samples">Sample list.</param>
        /// <returns>The furthest sample to reference.</returns>
        private Sample FindFurthest(Sample reference, IList<Sample> samples)
        {
            float largestDistance = 0;
            Sample ret = null;

            foreach (var sample in samples)
            {
                var distance = _sampleStrategy.Diff(sample, reference);

                if (distance > largestDistance)
                {
                    largestDistance = distance;
                    ret = sample;
                }
            }

            return ret;
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Look for the similar centroids by representative centroids.
        /// </summary>
        /// <param name="sample">Sample.</param>
        /// <returns>Similar centroids.</returns>
        private IList<Sample> SearchCentroids(Sample sample)
        {
            List<Sample> nearCentroids = new List<Sample>();
            foreach (Sample clusterCentroid in _representativeCentroids.OrderBy(s => _sampleStrategy.Diff(s, sample)))
            {
                nearCentroids.AddRange(_representativeCentroidsMap[clusterCentroid]);
                if (nearCentroids.Count >= _centroids.Count * 0.1)
                {
                    break;
                }
            }

            return nearCentroids;
        }