/// <summary> /// Gets the centroids for each cluster. /// </summary> /// <returns>The centroids and their class labels.</returns> public List <ClusterCentroid> GetCentroids() { return(Clusters .ClassLabels() .Select(label => new ClusterCentroid { ClusterLabel = label, Centroid = UnsignedPoint.Centroid(Clusters.PointsInClass(label)), Count = Clusters.PointsInClass(label).Count }).ToList()); }
/// <summary> /// Searches for the point in the first cluster that is closest to a corresponding point in the second cluster /// and returns an approximate result. /// /// This finds the centroid C1 of the first cluster, then the point P2 in the second cluster closest to centroid C1, then the /// point P1 in the first cluster closest to P2. /// /// NOTE: If the two clusters overlap or are shaped irregularly, this is likely to return a poor result. /// If the clusters are spherical, the results are likely to be very good. /// </summary> /// <param name="color1">Indicates the first cluster to be searched.</param> /// <param name="color2">Indicates the second cluster to be searched.</param> /// <returns>An approximate result, inclusing one point from each cluster and the square of the distance between them.</returns> public ClosestPair FindPairByCentroids(TLabel color1, TLabel color2) { var points1 = Clusters.PointsInClass(color1); var points2 = Clusters.PointsInClass(color2); var c1 = UnsignedPoint.Centroid(points1); var p2 = points2 .OrderBy(p => c1.Measure(p)) .First() ; var closest = points1.Select(p1 => new ClosestPair(color1, p1, color2, p2, p1.Measure(p2))).OrderBy(cp => cp.SquareDistance).First(); return(closest.Swap(color1)); }
public ClusterRadius(IList <UnsignedPoint> points) { Centroid = UnsignedPoint.Centroid(points); var radiusSum = 0.0; MaximumRadius = 0; foreach (var point in points) { var distance = Centroid.Distance(point); MaximumRadius = Math.Max(MaximumRadius, distance); radiusSum += distance; } if (points.Count > 0) { MeanRadius = radiusSum / points.Count; } }