Exemple #1
0
        /// <summary>
        /// Generates cluster set using selected datapoints
        /// </summary>
        /// <param name="isChosen">Is datapoint is chosen</param>
        /// <returns></returns>
        public ClusterSet GetClusterSet(bool[] isChosen)
        {
            var clusterSet = new ClusterSet();

            // generates datapoint
            for (var i = 0; i < Rows.Count; i++)
            {
                var dataPoint = new DataPoint {
                    Id = i
                };
                // gets all points of datapoint
                for (var j = StringHeadings.Length; j < FieldsCount; j++)
                {
                    dataPoint.Add(double.Parse(Rows[i][j]));
                }

                // new cluster
                var cluster = new Cluster {
                    Id = i
                };
                cluster.AddDataPoint(dataPoint);
                cluster.SetCentroid();

                // add to set
                clusterSet.AddCluster(cluster);
            }

            return(clusterSet);
        }
Exemple #2
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);
            }
        }