Advertisement
anananananana

iada

Jun 21st, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.53 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace Classification
  8. {
  9.     public class Knn
  10.     {
  11.         private int NeighborhoodSize { get; set; }
  12.         private bool NormalizationOn { get; set; }
  13.  
  14.         private List<Func<double, double>> Normalization = new List<Func<double, double>>();
  15.         private List<ClassifiedVector> TrainingSet;
  16.  
  17.         public Knn(int neighborhoodSize, bool normalizationOn)
  18.         {
  19.             NeighborhoodSize = neighborhoodSize;
  20.             NormalizationOn = normalizationOn;
  21.         }
  22.  
  23.         public void FeedWithTrainingSet(IEnumerable<ClassifiedVector> trainingSet)
  24.         {
  25.             int dimensions = trainingSet.First().Vector.DimensionCount;
  26.             Normalization.Clear();
  27.             if (NormalizationOn)
  28.             {
  29.                 for (int i = 0; i < dimensions; ++i)
  30.                 {
  31.                     double avg = trainingSet.Average(p => p.Vector[i]);
  32.                     double dev = trainingSet.Select(p => p.Vector[i]).StandardDeviation();
  33.                     Normalization.Add(x => (x - avg) / dev);
  34.                 }
  35.                 TrainingSet = trainingSet.Select(x => new ClassifiedVector()
  36.                 {
  37.                     Vector = Normalize(x.Vector),
  38.                     Classification = x.Classification,
  39.                 }).ToList();
  40.             }
  41.             else
  42.             {
  43.                 for(int i = 0; i < dimensions; ++i)
  44.                 {
  45.                     Normalization.Add(x => x);
  46.                 }
  47.                 TrainingSet = trainingSet.ToList(); // copy
  48.                 ;
  49.             }
  50.         }
  51.  
  52.         public int Classify(Vector arg)
  53.         {
  54.             Vector v = Normalize(arg);
  55.             return TrainingSet
  56.                 .OrderBy(t => (t.Vector - v).LenghtSquared)
  57.                 .Take(NeighborhoodSize)
  58.                 .GroupBy(t => t.Classification)
  59.                 .OrderByDescending(g => g.Count())
  60.                 .ThenBy(g => g.Min(t => (t.Vector - v).LenghtSquared))
  61.                 .First().First().Classification;
  62.         }
  63.  
  64.         private Vector Normalize(Vector arg)
  65.         {
  66.             List<double> normalizedValues = new List<double>();
  67.             for (int i = 0; i < arg.DimensionCount; ++i)
  68.             {
  69.                 normalizedValues.Add(Normalization[i](arg[i]));
  70.             }
  71.             return new Vector(normalizedValues);
  72.         }
  73.  
  74.         public override string ToString()
  75.         {
  76.             return $"k={NeighborhoodSize}, normalized={NormalizationOn}";
  77.         }
  78.     }
  79.  
  80.   public struct ClassifiedVector
  81.     {
  82.         public Vector Vector;
  83.         public int Classification;
  84.     }
  85.  
  86.     public static class Extension
  87.     {
  88.         public static double StandardDeviation(this IEnumerable<double> arg)
  89.         {
  90.             double avg = arg.Average();
  91.             double sum = arg.Sum(x => (x - avg) * (x - avg));
  92.             return Math.Sqrt(sum / arg.Count());
  93.         }
  94.     }
  95.  
  96. public class Vector
  97.     {
  98.         public int DimensionCount
  99.         {
  100.             get { return _data.Count; }
  101.         }
  102.  
  103.         public double this[int idx]
  104.         {
  105.             get { return _data[idx]; }
  106.         }
  107.  
  108.         private List<double> _data;
  109.  
  110.         public Vector(IEnumerable<double> values)
  111.         {
  112.             _data = values.ToList();
  113.         }
  114.  
  115.         public Vector(double x, double y)
  116.         {
  117.             _data = new List<double>() { x, y };
  118.         }
  119.  
  120.         public double LenghtSquared
  121.         {
  122.             get { return _data.Sum(x => x * x); }
  123.         }
  124.  
  125.         public double Lenght
  126.         {
  127.             get { return Math.Sqrt(LenghtSquared); }
  128.         }
  129.  
  130.         public static Vector operator +(Vector left, Vector right)
  131.         {
  132.             return new Vector(left._data.Zip(right._data, (a, b) => a + b));
  133.         }
  134.  
  135.         public static Vector operator -(Vector left, Vector right)
  136.         {
  137.             return new Vector(left._data.Zip(right._data, (a, b) => a - b));
  138.         }
  139.  
  140.         public static Vector operator *(Vector left, double right)
  141.         {
  142.             return new Vector(left._data.Select(x => x * right));
  143.         }
  144.  
  145.         public static Vector operator *(double left, Vector right)
  146.         {
  147.             return right * left;
  148.         }
  149.  
  150.         public static Vector operator /(Vector left, double right)
  151.         {
  152.             return new Vector(left._data.Select(x => x / right));
  153.         }
  154.     }
  155.  
  156. }
  157.  
  158. //------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement