Advertisement
Guest User

Untitled

a guest
Dec 12th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.56 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10.  
  11. namespace k_nearest_neighbors_algorithm
  12. {
  13.     public partial class Form1 : Form
  14.     {
  15.         static List<Tuple<double[], bool>> listOfFruits = new List<Tuple<double[], bool>>();
  16.         static List<double[]> trainingSet = new List<double[]>();
  17.  
  18.         public Form1()
  19.         {
  20.             InitializeComponent();
  21.            
  22.             FillListOfFruits();
  23.             Training();
  24.             Test();
  25.         }
  26.  
  27.         static void FillListOfFruits()
  28.         {
  29.             listOfFruits.Add(new Tuple<double[], bool>(new double[] { 6.6, 35.8, 46.8 }, false));
  30.             listOfFruits.Add(new Tuple<double[], bool>(new double[] { 1.2, 0.1, 8.1 }, true));
  31.             listOfFruits.Add(new Tuple<double[], bool>(new double[] { 14.1, 8.3, 1.2 }, true));
  32.             listOfFruits.Add(new Tuple<double[], bool>(new double[] { 3.4, 30.2, 64.7 }, false));
  33.             listOfFruits.Add(new Tuple<double[], bool>(new double[] { 4.7, 9.3, 84.4 }, false));
  34.             listOfFruits.Add(new Tuple<double[], bool>(new double[] { 1.7, 0, 10.8 }, true));
  35.             listOfFruits.Add(new Tuple<double[], bool>(new double[] { 1.5, 0, 5.3 }, true));
  36.             listOfFruits.Add(new Tuple<double[], bool>(new double[] { 3.1, 67, 2.6 }, false));
  37.         }
  38.  
  39.         static void Training()
  40.         {
  41.             foreach (var fruit in listOfFruits)
  42.             {
  43.                 Fruit f = new Fruit(fruit.Item1[0], fruit.Item1[1], fruit.Item1[2], fruit.Item2);
  44.  
  45.                 trainingSet.Add(f.Method());
  46.             }
  47.         }
  48.  
  49.         static double EuclideanDistance(double[] a, double[] b)
  50.         {
  51.             double res = 0;
  52.             for (int i = 0; i < a.Length; i++)
  53.             {
  54.                 res += Math.Pow(a[i] - b[i], 2);
  55.             }
  56.  
  57.             return Math.Sqrt(res);
  58.         }
  59.  
  60.         static List<double> GetListOfDistances(double[] newFruitParams)
  61.         {
  62.             double distance = 0.0;
  63.  
  64.             List<double> distances = new List<double>();
  65.  
  66.             for (int i = 0; i < trainingSet.Count; i++)
  67.             {
  68.                 distance = EuclideanDistance(newFruitParams, trainingSet[i]);
  69.                 distances.Add(distance);
  70.             }
  71.  
  72.             return distances;
  73.         }
  74.  
  75.         static List<int> GetIndexesOfNearestNeighbors(List<double> distances, int k)
  76.         {
  77.             List<int> indexes = new List<int>();
  78.             for (int i = 0; i < k; i++)
  79.             {
  80.                 int index = distances.IndexOf(distances.Min());
  81.                 indexes.Add(index);
  82.                 // MessageBox.Show(index.ToString());
  83.                 distances[index] = 99999;
  84.             }
  85.  
  86.             return indexes;
  87.         }
  88.  
  89.         static List<bool> GetClassesOfNeighbors(List<int> indexesOfNearestNeighbors)
  90.         {
  91.             List<bool> classificators = new List<bool>();
  92.             foreach (var ind in indexesOfNearestNeighbors)
  93.             {
  94.                 //MessageBox.Show(listOfFruits[ind].Item2.ToString());
  95.                 classificators.Add(listOfFruits[ind].Item2);
  96.             }
  97.  
  98.             return classificators;
  99.         }
  100.  
  101.         static bool Classify(List<bool> classificators)
  102.         {
  103.             int health = 0;
  104.             int notHealth = 0;
  105.  
  106.             foreach(var c in classificators)
  107.             {
  108.                 if (c)
  109.                 {
  110.                     health++;
  111.                 }
  112.                 else
  113.                 {
  114.                     notHealth++;
  115.                 }
  116.             }
  117.             if (health > notHealth)
  118.                 return true;
  119.             return false;
  120.         }
  121.  
  122.         static void Test()
  123.         {
  124.             Fruit newFruit = new Fruit(1.1, 30, 42.6); // создаем экземпляр нового фрукта
  125.             double[] newFruitParams = newFruit.Method(); // переводим его БЖУ в массив
  126.  
  127.             List<double> distances = GetListOfDistances(newFruitParams);
  128.  
  129.             int k = 3; // количество соседей
  130.  
  131.             List<int> indexesOfNearestNeighbors = GetIndexesOfNearestNeighbors(distances, k); // индексы ближайших соседей
  132.  
  133.             List<bool> classificators = GetClassesOfNeighbors(indexesOfNearestNeighbors);
  134.  
  135.             bool isHealthy = Classify(classificators);
  136.             MessageBox.Show(isHealthy.ToString());
  137.         }
  138.     }
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement