private const uint NUM_CLUSTERS = 3; // for seeds and iris data-sets #endregion #region Private & Protected Methods private static void EvaluateClustering( ISet <DataPoint> dataPoints, ILinkageCriterion <DataPoint> linkage, string linkageName, uint numClusters) { var clusteringAlg = new AgglomerativeClusteringAlgorithm <DataPoint>(linkage); var clustering = clusteringAlg.GetClustering(dataPoints); // gets cluster set according to predefined number of clusters var clusterSet = clustering.First(cs => cs.Count == numClusters); // gets classes for each data-point (first character of the ID in the dataset) var pointClasses = dataPoints.ToDictionary(dataPoint => dataPoint, dataPoint => dataPoint.ID[0]); Console.WriteLine("============================================="); Console.WriteLine($"Evaluating {linkageName} clustering using Euclidean distance..."); // evaluates the clustering according to different criteria var evaluations = new Dictionary <string, double> { { "Purity", new Purity <DataPoint, char>().Evaluate(clusterSet, pointClasses) }, { "NMI", new NormalizedMutualInformation <DataPoint, char>().Evaluate(clusterSet, pointClasses) }, { "Accuracy", new RandIndex <DataPoint, char>().Evaluate(clusterSet, pointClasses) }, { "Precision", new Precision <DataPoint, char>().Evaluate(clusterSet, pointClasses) }, { "Recall", new Recall <DataPoint, char>().Evaluate(clusterSet, pointClasses) }, { "F1Measure", new FMeasure <DataPoint, char>(1).Evaluate(clusterSet, pointClasses) }, { "F2Measure", new FMeasure <DataPoint, char>(2).Evaluate(clusterSet, pointClasses) }, { "F05Measure", new FMeasure <DataPoint, char>(0.5).Evaluate(clusterSet, pointClasses) } }; foreach (var evaluation in evaluations) { Console.WriteLine($" - {evaluation.Key}: {evaluation.Value:0.000}"); } }
private static void PrintClusters(ISet <DataPoint> instances, ILinkageCriterion <DataPoint> linkage, string name) { var perfMeasure = new PerformanceMeasure(); perfMeasure.Start(); var clusteringAlg = new AgglomerativeClusteringAlgorithm <DataPoint>(linkage); var clustering = clusteringAlg.GetClustering(instances); perfMeasure.Stop(); Console.WriteLine("_____________________________________________"); Console.WriteLine(name); Console.WriteLine(perfMeasure); foreach (var clusterSet in clustering) { Console.WriteLine($"Clusters at distance: {clusterSet.Dissimilarity:0.00} ({clusterSet.Count})"); foreach (var cluster in clusterSet) { Console.WriteLine($" - {cluster}"); } } clustering.SaveD3DendrogramFile(Path.GetFullPath($"{name}.json"), formatting: Formatting.Indented); }
/// <summary> /// Creates a new instance of <see cref="AgglomerativeClusteringAlgorithm{TInstance}" /> with the given set of /// instances and linkage /// criterion. /// </summary> /// <param name="linkageCriterion">The criterion used to measure dissimilarities within and between clusters.</param> public AgglomerativeClusteringAlgorithm(ILinkageCriterion <TInstance> linkageCriterion) { this.LinkageCriterion = linkageCriterion; }