Ejemplo n.º 1
0
        public void TestCenter()
        {
            var cluster = new Cluster(3);
            double[] ob1 = { 2.0, 10.0, 100.0 };
            double[] ob2 = { 4.0, 20.0, 200.0 };
            double[] ob3 = { 6.0, 30.0, 300.0 };

            cluster.Observations.Add(new BasicData(ob1));
            cluster.Observations.Add(new BasicData(ob2));
            cluster.Observations.Add(new BasicData(ob3));

            Assert.AreEqual(3, cluster.Observations.Count);

            cluster.CalculateCenter();

            Assert.AreEqual(4.0, cluster.Center[0], 0.00001);
            Assert.AreEqual(20.0, cluster.Center[1], 0.00001);
            Assert.AreEqual(200.0, cluster.Center[2], 0.00001);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Init the observations to random clusters.  The Forgy method randomly chooses k observations from the
        /// data set and uses these as the initial means. 
        /// </summary>
        /// <param name="theObservations">The observations to cluster.</param>
        public void InitForgy(IList<BasicData> theObservations)
        {
            int dimensions = FindDimensions(theObservations);

            _clusters.Clear();

            var usedObservations = new HashSet<int>();

            for (int i = 0; i < _k; i++)
            {
                var cluster = new Cluster(dimensions);
                _clusters.Add(cluster);

                int observationIndex = -1;

                while (observationIndex == -1)
                {
                    observationIndex = _randomGeneration.NextInt(theObservations.Count);
                    if (usedObservations.Contains(observationIndex))
                    {
                        observationIndex = -1;
                    }
                }

                double[] observation = theObservations[observationIndex].Input;
                Array.Copy(observation, 0, cluster.Center, 0, dimensions);
                usedObservations.Add(observationIndex);
            }

            // assign all observations to a cluster
            foreach (BasicData observation in theObservations)
            {
                Cluster cluster = FindNearestCluster(observation.Input);
                cluster.Observations.Add(observation);
            }

            // calculate initial centers
            UpdateStep();
        }
Ejemplo n.º 3
0
 public void TestDimensions()
 {
     var cluster = new Cluster(3);
     Assert.AreEqual(true, cluster.ToString().Length > 0);
     Assert.AreEqual(3, cluster.Dimensions);
 }