示例#1
0
        //Přesunout MeansPointy do správných clusterů
        private bool UpdateClusterMembership()
        {
            MinIndex min = new MinIndex();

            bool changed = false;

            double[] distances = new double[_numberOfClusters];

            double[] sse_count = new double[_numberOfClusters];

            double result = 0;

            for (int i = 0; i < _normalizedDataToCluster.Count; ++i)
            {
                for (int k = 0; k < _numberOfClusters; ++k)
                {
                    distances[k] = ElucDist.Get(_normalizedDataToCluster[i], _clusters[k]);
                }

                int newClusterId = min.Get(distances);

                if (newClusterId != _normalizedDataToCluster[i].Cluster)
                {
                    changed = true;
                    _normalizedDataToCluster[i].Cluster = _rawDataToCluster[i].Cluster = newClusterId;

                    if (print)
                    {
                        Console.WriteLine("Width: " + _rawDataToCluster[i].Width + ", Lenght: " +
                                          _rawDataToCluster[i].Length + " ----> Cluster " + newClusterId);
                    }
                }
                //SSE pro cluster
                sse_count[newClusterId] = sse_count[newClusterId] + distances.Min();
            }

            //SSE celkové
            for (int o = 0; o < sse_count.Length; o++)
            {
                result = result + sse_count[o];
            }
            sse = result;

            if (changed == false)
            {
                return(false);
            }
            if (IsClusterEmpty(_normalizedDataToCluster))
            {
                return(false);
            }
            return(true);
        }
        ///<summary>
        ///Generate elucidan distance for the whole list of points.
        ///</summary>
        public double[,] GenerateElucid(List <Iris> source)
        {
            ElucidanDistance elucid = new ElucidanDistance();

            double[,] x = new double[source.Count, source.Count];

            for (int i = 0; i < source.Count(); i++)
            {
                for (int z = 0; z < source.Count(); z++)
                {
                    x[i, z] = elucid.Get(source[i], source[z]);
                }
            }
            return(x);
        }
示例#3
0
        public void Classify(int neighborsNumber)
        {
            Console.WriteLine();
            ElucidanDistance ElucDist = new ElucidanDistance();

            this.K = neighborsNumber;
            double correct = 0, testN = 0;

            //Vytvořím si 2D pole pro uložení vzdáleností
            double[][] distances = new double[trainingSetValues.Count][];
            for (int i = 0; i < trainingSetValues.Count; i++)
            {
                distances[i] = new double[2];
            }

            // pro všechny prvky v tetsovací sadě
            for (var test = 0; test < this.testSetValues.Count; test++)
            {
                //paralelně počitám euclid z testovacího prvku na všechny prvky v training sadě
                Parallel.For(0, trainingSetValues.Count, index =>
                {
                    var dist            = ElucDist.Get(this.testSetValues[test], this.trainingSetValues[index]);
                    distances[index][0] = dist;
                    distances[index][1] = index;
                }
                             );

                Console.WriteLine("--VALUE {0} closest {1} neighbors--", test, this.K);

                // seřadit vzdalenosti ke všem sousedum v training sadě a vzít první K
                var sortedDistances = distances.AsParallel().OrderBy(t => t[0]).Take(this.K);

                string realClass = testSetClasses[test];
                foreach (var d in sortedDistances)
                {
                    string predictedClass = trainingSetClasses[(int)d[1]];
                    Console.Write("PREDICTED: {0}", predictedClass);

                    if (string.Equals(realClass, predictedClass) == true)
                    {
                        Console.Write(" - TRUE");
                        correct++;
                    }

                    testN++;

                    if (string.Equals(realClass, predictedClass) != true)
                    {
                        Console.Write(" - WRONG!");
                    }

                    Console.WriteLine();
                }
                Console.WriteLine();
            }

            Console.WriteLine("TOTAL: {0}", testN);
            Console.WriteLine("CORRECT: {0}", correct);
            Console.WriteLine("{0}%", (correct / testN) * 100);

            Console.ReadLine();
            Console.Clear();

            Console.WriteLine();
        }