示例#1
0
        /// <summary>
        /// Perform the example.
        /// </summary>
        public void Run()
        {
            // read the iris data from the resources
            Assembly assembly = Assembly.GetExecutingAssembly();
            var res = assembly.GetManifestResourceStream("AIFH_Vol1.Resources.iris.csv");

            // did we fail to read the resouce
            if (res == null)
            {
                Console.WriteLine("Can't read iris data from embedded resources.");
                return;
            }

            // load the data
            var istream = new StreamReader(res);
            DataSet ds = DataSet.Load(istream);
            istream.Close();

            // perform the cluster
            IList<BasicData> observations = ds.ExtractUnsupervisedLabeled(4);
            KMeansClustering kmeans = new KMeansClustering(3);
            kmeans.InitForgy(observations);
            int iterations = kmeans.Iteration(1000);
            Console.WriteLine("Finished after " + iterations + " iterations.");

            for (int i = 0; i < kmeans.K; i++)
            {
                Cluster cluster = kmeans.Clusters[i];
                Console.WriteLine("* * * Cluster #" + i);
                foreach (BasicData d in cluster.Observations)
                {
                    Console.WriteLine(d.ToString());
                }
            }
        }
示例#2
0
        public void TestClusterForgy()
        {
            var kmeans = new KMeansClustering(4) {RandomGeneration = new BasicGenerateRandom(22)};
            kmeans.InitForgy(GetDataSet());
            int iterations = kmeans.Iteration(1000);
            Assert.AreEqual(2, iterations);

            Cluster cluster1 = kmeans.Clusters[0];
            Cluster cluster2 = kmeans.Clusters[1];
            Cluster cluster3 = kmeans.Clusters[2];
            Cluster cluster4 = kmeans.Clusters[3];

            Assert.AreEqual(3, cluster1.Observations.Count);
            Assert.AreEqual(3, cluster2.Observations.Count);
            Assert.AreEqual(3, cluster3.Observations.Count);
            Assert.AreEqual(3, cluster4.Observations.Count);
        }
示例#3
0
 public void TestMaxClusters()
 {
     var kmeans = new KMeansClustering(12);
     kmeans.RandomGeneration = new BasicGenerateRandom(22);
     kmeans.InitRandom(GetDataSet());
     int iterations = kmeans.Iteration(1000);
     Assert.AreEqual(1, iterations);
 }
示例#4
0
 public void TestEarlyIteration()
 {
     var kmeans = new KMeansClustering(3);
     kmeans.Iteration();
 }