Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace k_nearest_neighbors_algorithm
- {
- public partial class Form1 : Form
- {
- static List<Tuple<double[], bool>> listOfFruits = new List<Tuple<double[], bool>>();
- static List<double[]> trainingSet = new List<double[]>();
- public Form1()
- {
- InitializeComponent();
- FillListOfFruits();
- Training();
- Test();
- }
- static void FillListOfFruits()
- {
- listOfFruits.Add(new Tuple<double[], bool>(new double[] { 6.6, 35.8, 46.8 }, false));
- listOfFruits.Add(new Tuple<double[], bool>(new double[] { 1.2, 0.1, 8.1 }, true));
- listOfFruits.Add(new Tuple<double[], bool>(new double[] { 14.1, 8.3, 1.2 }, true));
- listOfFruits.Add(new Tuple<double[], bool>(new double[] { 3.4, 30.2, 64.7 }, false));
- listOfFruits.Add(new Tuple<double[], bool>(new double[] { 4.7, 9.3, 84.4 }, false));
- listOfFruits.Add(new Tuple<double[], bool>(new double[] { 1.7, 0, 10.8 }, true));
- listOfFruits.Add(new Tuple<double[], bool>(new double[] { 1.5, 0, 5.3 }, true));
- listOfFruits.Add(new Tuple<double[], bool>(new double[] { 3.1, 67, 2.6 }, false));
- }
- static void Training()
- {
- foreach (var fruit in listOfFruits)
- {
- Fruit f = new Fruit(fruit.Item1[0], fruit.Item1[1], fruit.Item1[2], fruit.Item2);
- trainingSet.Add(f.Method());
- }
- }
- static double EuclideanDistance(double[] a, double[] b)
- {
- double res = 0;
- for (int i = 0; i < a.Length; i++)
- {
- res += Math.Pow(a[i] - b[i], 2);
- }
- return Math.Sqrt(res);
- }
- static List<double> GetListOfDistances(double[] newFruitParams)
- {
- double distance = 0.0;
- List<double> distances = new List<double>();
- for (int i = 0; i < trainingSet.Count; i++)
- {
- distance = EuclideanDistance(newFruitParams, trainingSet[i]);
- distances.Add(distance);
- }
- return distances;
- }
- static List<int> GetIndexesOfNearestNeighbors(List<double> distances, int k)
- {
- List<int> indexes = new List<int>();
- for (int i = 0; i < k; i++)
- {
- int index = distances.IndexOf(distances.Min());
- indexes.Add(index);
- // MessageBox.Show(index.ToString());
- distances[index] = 99999;
- }
- return indexes;
- }
- static List<bool> GetClassesOfNeighbors(List<int> indexesOfNearestNeighbors)
- {
- List<bool> classificators = new List<bool>();
- foreach (var ind in indexesOfNearestNeighbors)
- {
- //MessageBox.Show(listOfFruits[ind].Item2.ToString());
- classificators.Add(listOfFruits[ind].Item2);
- }
- return classificators;
- }
- static bool Classify(List<bool> classificators)
- {
- int health = 0;
- int notHealth = 0;
- foreach(var c in classificators)
- {
- if (c)
- {
- health++;
- }
- else
- {
- notHealth++;
- }
- }
- if (health > notHealth)
- return true;
- return false;
- }
- static void Test()
- {
- Fruit newFruit = new Fruit(1.1, 30, 42.6); // создаем экземпляр нового фрукта
- double[] newFruitParams = newFruit.Method(); // переводим его БЖУ в массив
- List<double> distances = GetListOfDistances(newFruitParams);
- int k = 3; // количество соседей
- List<int> indexesOfNearestNeighbors = GetIndexesOfNearestNeighbors(distances, k); // индексы ближайших соседей
- List<bool> classificators = GetClassesOfNeighbors(indexesOfNearestNeighbors);
- bool isHealthy = Classify(classificators);
- MessageBox.Show(isHealthy.ToString());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement