private static void Step(Dictionary <int, Vector> data, List <Cluster> clusters) { foreach (var cluster in clusters) { cluster.Points.Clear(); } foreach (var point in data) { var distances = new List <Tuple <int, double> >(); for (var c = 0; c < clusters.Count; c++) { distances.Add(Tuple.Create(c, Euclidean.GetDistance(point.Value, clusters[c].Centroid))); } var bestDistance = distances.OrderBy(d => d.Item2).First(); clusters[bestDistance.Item1].Points.Add(point.Value); } foreach (var cluster in clusters) { if (cluster.Points.Count > 0) { cluster.Centroid = CalculateMean(cluster); } } }
private static double SSE(List <Cluster> clusters) { foreach (var cluster in clusters) { foreach (var point in cluster.Points) { point.Distance = Euclidean.GetDistance(cluster.Centroid, point); } } return(clusters.Select(c => c.Points.Select(p => Math.Pow(p.Distance.Value, 2)).Sum()).Average()); }