///<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);
        }
Exemplo n.º 2
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();
        }