Example #1
0
        private static double CalculateDistanceToCentroid(KMeansEntity entity, KMeansEntity centroid)
        {
            var squaredDistance = 0d;

            for (var i = 0; i < centroid.Arguments.Count; i++)
            {
                squaredDistance += Math.Pow(centroid.Arguments[i] - entity.Arguments[i], 2);
            }

            var distance = Math.Sqrt(squaredDistance);

            return(distance);
        }
Example #2
0
        private void InitRandom(List <KMeansEntity> data)
        {
            for (var i = 0; i < clusterCount; i++)
            {
                var randomlySelectedPoint = data[rnd.Next(0, data.Count)];
                var centroid = new KMeansEntity
                {
                    Label     = $"centroid_{i}",
                    ClusterId = i
                };
                foreach (var argument in randomlySelectedPoint.Arguments)
                {
                    centroid.Arguments.Add(argument);
                }

                centroids.Add(centroid);
            }

            UpdateClustering(data);
        }