Пример #1
0
 private static double getDistanceBeetwen(Iris inputIris, Iris anotherIris)
 {
     return(Math.Sqrt(Math.Pow((inputIris.getLeafLength() - anotherIris.getLeafLength()), 2)
                      + Math.Pow((inputIris.getLeafWidth() - anotherIris.getLeafWidth()), 2)
                      + Math.Pow((inputIris.getPetalLength() - anotherIris.getPetalLength()), 2)
                      + Math.Pow((inputIris.getPetalWidth() - anotherIris.getPetalWidth()), 2)));
 }
Пример #2
0
        static void Main(string[] args)
        {
            Console.WriteLine("Metoda K Najbliższych Sąsiadów");
            Console.WriteLine();

            //Wczytanie pliku
            readFile(setOfIrises, "IrisData.txt");

            //Wprowadzenie współrzędnych nowego punktu
            //W przypadku liczby z ułamkiem dziesiętym należy użyć przecinka zamiast kropki
            Console.WriteLine("Wprowadź dane obiektu");
            List <double> inputCoordinates = new List <double>();

            setNewCoordinates(inputCoordinates);
            Iris inputIris = new Iris(inputCoordinates[0], inputCoordinates[1], inputCoordinates[2], inputCoordinates[3]);

            //Wprowadzenie liczby sąsiadów K
            Console.Write("Parametr K: ");
            parameterK = Convert.ToInt32(Console.ReadLine());

            //Stworzenie kolekcji Dictionary<Iris,double> zawierającej obiekty wraz z odległościami od wprowadzonego obiektu
            foreach (Iris singleIris in setOfIrises)
            {
                distances.Add(singleIris, getDistanceBeetwen(inputIris, singleIris));
            }

            //Wyszukanie najbliższych K sąsiadów
            findNeighbours();

            //Zliczenie typów
            countTypes();

            //Prezentacja wyników
            Console.WriteLine();
            Console.WriteLine("Neighbours:");
            foreach (Iris singleNeighbour in nearestNeighbours)
            {
                singleNeighbour.showIris();
            }
            Console.WriteLine();
            Console.WriteLine("Setosa: " + setosa + "  Versicolor: " + versicolor + "  Virginica: " + virginica);
            Console.WriteLine();
            Console.WriteLine("Obiekt o parametrach:");
            Console.WriteLine("leaf-length = " + inputIris.getLeafLength());
            Console.WriteLine("leaf-width = " + inputIris.getLeafWidth());
            Console.WriteLine("petal-length = " + inputIris.getPetalLength());
            Console.WriteLine("petal-width = " + inputIris.getPetalWidth());
            Console.WriteLine("dla parametru K = " + parameterK);
            Console.WriteLine("jest klasy " + getProperType(setosa, versicolor, virginica));
            Console.WriteLine();
            Console.WriteLine("Wcisnij dowolny klawisz aby zakonczyc dzialanie programu.");
            Console.ReadKey();
        }
Пример #3
0
        private static void readFile(List <Iris> setOfIrises, string filePath)
        {
            try
            {
                using (StreamReader sReader = new StreamReader(filePath))
                {
                    string line = sReader.ReadLine();
                    while (!sReader.EndOfStream)
                    {
                        line = sReader.ReadLine();
                        string[] parts;

                        parts = line.Split(default(string[]), StringSplitOptions.RemoveEmptyEntries);
                        Iris newIris = new Iris(Convert.ToDouble(parts[0]), Convert.ToDouble(parts[1]), Convert.ToDouble(parts[2]), Convert.ToDouble(parts[3]), parts[4]);
                        setOfIrises.Add(newIris);
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("The file could not be read.");
                Console.WriteLine(e.Message);
            }
        }
Пример #4
0
        private static void findNeighbours()
        {
            double minLength    = 0;
            double actualLength = 0;
            bool   firstLoop    = true;
            Iris   neighbour    = new Iris();

            for (int i = 0; i < parameterK; i++)
            {
                foreach (Iris singleIris in setOfIrises)
                {
                    actualLength = distances[singleIris];
                    if (minLength > actualLength || firstLoop == true)
                    {
                        minLength = actualLength;
                        neighbour = singleIris;
                        firstLoop = false;
                    }
                }
                firstLoop = true;
                nearestNeighbours.Add(neighbour);
                setOfIrises.Remove(neighbour);
            }
        }