Add() public method

Adds a single dimension array data to the cluster
public Add ( double data ) : void
data double A 1-dimensional array containing data that will be added to the cluster
return void
Ejemplo n.º 1
0
        public ClusterCollection RandomSeeding(int k, double[][] data)
        {
            int size = data.Length;

            double[][]        seeds         = new double[k][];
            Random            random        = new Random();
            Hashtable         random_table  = new Hashtable();
            Cluster           cluster       = null;
            ClusterCollection init_clusters = new ClusterCollection();

            for (int i = 0; i < k;)
            {
                int r = random.Next(size - 1);
                if (!random_table.ContainsKey(r))
                {
                    random_table.Add(r, 0);
                    seeds[i]    = new double[3];
                    seeds[i][0] = data[r][0]; seeds[i][1] = data[r][1]; seeds[i][2] = data[r][2];
                    cluster     = new Cluster();
                    cluster.Add(seeds[i]);
                    init_clusters.Add(cluster);
                    i++;
                }
            }
            return(init_clusters);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Seperates a dataset into clusters or groups with similar characteristics
        /// </summary>
        /// <param name="clusterCount">The number of clusters or groups to form</param>
        /// <param name="data">An array containing data that will be clustered</param>
        /// <returns>A collection of clusters of data</returns>
        public ClusterCollection ClusterDataSet(int clusterCount, double [][] data)
        {
            int rowCount            = data.Length;
            int stableClustersCount = 0;

            Cluster           cluster  = null;
            ClusterCollection clusters = new ClusterCollection();

            //setup seed clusters
            for (int i = 0; i < clusterCount; i++)
            {
                cluster = new Cluster();
                cluster.Add(data[i]);
                clusters.Add(cluster);
            }

            //DateTime start = DateTime.Now;
            //Console.WriteLine("Start clustering {0} objects into {1} clusters: {2}", rowCount.ToString(), clusterCount.ToString(), start.ToLongTimeString());

            //do actual clustering
            int iterationCount = 0;

            while (stableClustersCount != clusters.Count)
            {
                iterationCount++;
                stableClustersCount = 0;

                //Do actual clustering

                //Console.WriteLine("Start Cluster for ineration {0}: {1}", iterationCount, DateTime.Now.ToLongTimeString());
                ClusterCollection newClusters = this.ClusterDataSet(clusters, data);
                //Console.WriteLine("  End Cluster for ineration {0}: {1}", iterationCount, DateTime.Now.ToLongTimeString());

                for (int clusterIndex = 0; clusterIndex < clusters.Count; clusterIndex++)
                {
                    double[] originalClusterMean = clusters[clusterIndex].ClusterMean;
                    double[] newClusterMean      = newClusters[clusterIndex].ClusterMean;
                    double   distance            = this.EuclideanDistance(newClusterMean, originalClusterMean);
                    if (distance == 0)
                    {
                        stableClustersCount++;
                        //Console.WriteLine("{0} stable clusters out of {1}", stableClustersCount.ToString(), clusterCount.ToString());
                    }
                }
                clusters = newClusters;
            }

            //DateTime end = DateTime.Now;
            //TimeSpan span = end - start;
            //Console.WriteLine("End clustering {0} objects into {1} clusters with {2} iterations: {3}", rowCount.ToString(), clusterCount.ToString(), iterationCount, end.ToLongTimeString());
            //Console.WriteLine("Clustering {0} objects into {1} clusters took {2} seconds", rowCount.ToString(), clusterCount.ToString(), span.TotalSeconds);
            //Console.WriteLine();

            return(clusters);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Seperates a dataset into clusters or groups with similar characteristics
        /// </summary>
        /// <param name="clusterCount">The number of clusters or groups to form</param>
        /// <param name="data">An array containing data that will be clustered</param>
        /// <returns>A collection of clusters of data</returns>
        public ClusterCollection ClusterDataSet(int clusterCount, double [][] data)
        {
            int rowCount = data.Length;
            int stableClustersCount = 0;

            Cluster cluster = null;
            ClusterCollection clusters = new ClusterCollection();

            //setup seed clusters
            for (int i = 0; i < clusterCount; i++)
            {
                    cluster = new Cluster();
                    cluster.Add(data[i]);
                    clusters.Add(cluster);
            }

            DateTime start = DateTime.Now;
            Console.WriteLine("Start clustering {0} objects into {1} clusters: {2}", rowCount.ToString(), clusterCount.ToString(), start.ToLongTimeString());

            //do actual clustering
            int iterationCount = 0;
            while (stableClustersCount != clusters.Count)
            {
                iterationCount++;
                stableClustersCount = 0;

                //Do actual clustering

                //Console.WriteLine("Start Cluster for ineration {0}: {1}", iterationCount, DateTime.Now.ToLongTimeString());
                ClusterCollection newClusters = this.ClusterDataSet(clusters, data);
                //Console.WriteLine("  End Cluster for ineration {0}: {1}", iterationCount, DateTime.Now.ToLongTimeString());

                for (int clusterIndex = 0; clusterIndex < clusters.Count; clusterIndex++)
                {
                    double[] originalClusterMean = clusters[clusterIndex].ClusterMean;
                    double[] newClusterMean = newClusters[clusterIndex].ClusterMean;
                    double distance = this.EuclideanDistance(newClusterMean, originalClusterMean);
                    if (distance ==0)
                    {
                        stableClustersCount++;
                        //Console.WriteLine("{0} stable clusters out of {1}", stableClustersCount.ToString(), clusterCount.ToString());
                    }
                }
                clusters = newClusters;
            }

            DateTime end = DateTime.Now;
            TimeSpan span = end - start;
            Console.WriteLine("End clustering {0} objects into {1} clusters with {2} iterations: {3}", rowCount.ToString(), clusterCount.ToString(), iterationCount, end.ToLongTimeString());
            Console.WriteLine("Clustering {0} objects into {1} clusters took {2} seconds", rowCount.ToString(), clusterCount.ToString(), span.TotalSeconds);
            Console.WriteLine();

            return clusters;
        }
 public ClusterCollection RandomSeeding(int k, double[][] data)
 {
     int size = data.Length;
     double[][] seeds = new double[k][];
     Random random = new Random();
     Hashtable random_table = new Hashtable();
     Cluster cluster = null;
     ClusterCollection init_clusters = new ClusterCollection();
     for (int i = 0; i < k; )
     {
         int r = random.Next(size - 1);
         if(!random_table.ContainsKey(r))
         {
             random_table.Add(r,0);
             seeds[i] = new double[3];
             seeds[i][0]=data[r][0];seeds[i][1]=data[r][1];seeds[i][2]=data[r][2];
             cluster = new Cluster();
             cluster.Add(seeds[i]);
             init_clusters.Add(cluster);
             i++;
         }
     }
     return init_clusters;
 }