Beispiel #1
0
        /// <summary>
        /// Builds the hierarchical clustering.
        /// </summary>
        /// <param name="indexNewCluster">The index new cluster.</param>
        /// <param name="k">The k.</param>
        /// <param name="isWithIndex">if set to <c>true</c> [is with index].</param>
        private void BuildHierarchicalClustering(int indexNewCluster, int k, bool isWithIndex = false)
        {
            ClusterPair closestClusterPair = _dissimilarityMatrix.GetClosestClusterPair(); // gets the clusterpair with minimal distance

            // creates new cluster by merging clusters from closestClusterPair
            Cluster newCluster = new Cluster();

            newCluster.AddSubCluster(closestClusterPair.Cluster1);
            newCluster.AddSubCluster(closestClusterPair.Cluster2);
            newCluster.Id = indexNewCluster;
            newCluster.SetCentroid();

            // removes cluster pair from _clusters
            _clusters.RemoveClusterPair(closestClusterPair);
            _UpdateDissimilarityMatrix(newCluster);
            // add new cluster to _clusters
            _clusters.AddCluster(newCluster);

            if (isWithIndex)                                // checks is executed for calculating CH index
            {
                _chValue.Add(GetCHIndex());                 // adds index to array of CH values
                _chIndex.Add(_clusters.ClustersList.Count); // adds number of clusters for current CH value
            }

            // exit point of algorithm (Where _clusters count is equal to k)
            if (_clusters.Count > k)
            {
                BuildHierarchicalClustering(indexNewCluster + 1, k, isWithIndex);
            }
        }
Beispiel #2
0
        /// <summary>
        /// Returns the cluster pair distance.
        /// </summary>
        /// <param name="clusterPair">The cluster pair.</param>
        /// <returns></returns>
        public double ReturnClusterPairDistance(ClusterPair clusterPair)
        {
            double clusterPairDistance;

            clusterPairDistance = _distanceMatrix.ContainsKey(clusterPair) ? _distanceMatrix[clusterPair] : _distanceMatrix[new ClusterPair(clusterPair.Cluster2, clusterPair.Cluster1)];
            return(clusterPairDistance);
        }
Beispiel #3
0
        /// <summary>
        /// Builds the dissimilarity matrix.
        /// </summary>
        private void BuildDissimilarityMatrix()
        {
            _dissimilarityMatrix = new DissimilarityMatrix();

            for (int i = 0; i < _clusters.Count - 1; i++)
            {
                for (int j = i + 1; j < _clusters.Count; j++)
                {
                    var clusterPair = new ClusterPair(_clusters.GetCluster(i), _clusters.GetCluster(j));

                    var distanceBetweenTwoClusters = ClusterDistance.ComputeDistance(clusterPair.Cluster1, clusterPair.Cluster2, _distanceMetric);
                    _dissimilarityMatrix.AddClusterPairAndDistance(clusterPair, distanceBetweenTwoClusters); // adds distance to matrix
                }
            }
        }
Beispiel #4
0
        /// <summary>
        /// Gets the closest cluster pair.
        /// </summary>
        /// <returns></returns>
        public ClusterPair GetClosestClusterPair()
        {
            double      minDistance        = double.MaxValue;
            ClusterPair closestClusterPair = null;

            foreach (var item in _distanceMatrix)
            {
                if (item.Value < minDistance)
                {
                    minDistance        = item.Value;
                    closestClusterPair = item.Key;
                }
            }

            return(closestClusterPair);
        }
Beispiel #5
0
 /// <summary>
 /// Removes the cluster pair.
 /// </summary>
 /// <param name="clusterPair">The cluster pair.</param>
 public void RemoveClusterPair(ClusterPair clusterPair)
 {
     _distanceMatrix.Remove(_distanceMatrix.ContainsKey(clusterPair)
         ? clusterPair
         : new ClusterPair(clusterPair.Cluster2, clusterPair.Cluster1));
 }
Beispiel #6
0
 /// <summary>
 /// Adds the cluster pair and distance.
 /// </summary>
 /// <param name="clusterPair">The cluster pair.</param>
 /// <param name="distance">The distance.</param>
 public void AddClusterPairAndDistance(ClusterPair clusterPair, double distance)
 {
     _distanceMatrix.Add(clusterPair, distance);
 }
Beispiel #7
0
 /// <summary>
 /// Removes the cluster pair.
 /// </summary>
 /// <param name="clusterPair">The cluster pair.</param>
 public void RemoveClusterPair(ClusterPair clusterPair)
 {
     RemoveCluster(clusterPair.Cluster1);
     RemoveCluster(clusterPair.Cluster2);
 }