Exemplo n.º 1
0
        //get nearest cluster
        private int GetNearestCluster(GenericVector vector)
        {
            var clusterid = Centroids
                            .OrderBy(v => GenericVector.Distance(vector, v.Value))
                            .Select(v => v.Key)
                            .FirstOrDefault();

            return(clusterid);
        }
Exemplo n.º 2
0
        public static float GetAngle(GenericVector a, GenericVector b)
        {
            var x = DotProduct(a, b) / (a.VectorLength() * b.VectorLength());

            if (x > 1 || x < -1)
            {
                return(0);
            }
            return((float)Math.Acos(x));
        }
Exemplo n.º 3
0
        public static float DotProduct(GenericVector vectorA, GenericVector vectorB)
        {
            if (vectorA.Size != vectorB.Size)
            {
                throw new Exception("GenericVector a size of dotProduct not equal to GenericVector b size");
            }
            var aTimesBpoints = vectorA.Points.Select((t, i) => t * vectorB.Points[i]).ToList();

            return(aTimesBpoints.Sum());
        }
Exemplo n.º 4
0
        public double CalculateSumofSquaredErrors()
        {
            var orderedclusters = Dataset.GroupBy(v => v.Cluster).OrderBy(v => v.Key).ToList();
            var sse             = (from cluster in orderedclusters
                                   let clustercenter = Centroids[cluster.Key]
                                                       from point in cluster
                                                       select Math.Pow(GenericVector.Distance(clustercenter, point), 2)).Sum();

            return(sse);
        }
Exemplo n.º 5
0
        public GenericVector Sum(GenericVector vectorToSum)
        {
            if (Size != vectorToSum.Size)
            {
                throw new Exception("GenericVector size of vectorToSum not equal to instance vector size");
            }

            for (var i = 0; i < Size; i++)
            {
                Points[i] += vectorToSum.Points[i];
            }
            return(this);
        }
Exemplo n.º 6
0
        //STATIC GENERICVECTOR METHODS
        public static GenericVector Sum(GenericVector vectorA, GenericVector vectorB)
        {
            if (vectorA.Size != vectorB.Size)
            {
                throw new Exception("GenericVector size of vectorToSum not equal to instance vector size");
            }

            var summedVector = new GenericVector(vectorA.Size);

            for (var i = 0; i < summedVector.Size; i++)
            {
                summedVector.Points[i] = vectorA.Points[i] + vectorB.Points[i];
            }
            return(summedVector);
        }
Exemplo n.º 7
0
        public static void Main(string[] args)
        {
            var dataDictionary = new Dictionary <int, GenericVector>();

            //read csv file
            var lines = File.ReadAllLines("WineData.csv")
                        .Select(v => v.Split(',').Select(float.Parse).ToList())
                        .ToList();

            //flip data and add to genericvector list
            foreach (var line in lines)
            {
                for (var j = 0; j < line.Count; j++)
                {
                    if (!dataDictionary.ContainsKey(j))
                    {
                        dataDictionary[j] = new GenericVector();
                    }
                    dataDictionary[j].Add(line[j]);
                }
            }

            var kMeanses = new List <KMeans>();

            //init kmeans ad run it.
            for (var i = 0; i < 25; i++)
            {
                var kMeans = new KMeans
                {
                    Iterations = 100,
                    Dataset    = dataDictionary.Values.ToList(),
                    Clusters   = 4
                };
                kMeans.Run();
                kMeanses.Add(kMeans);
            }


            var lowestKmeans = kMeanses.Aggregate(
                (minItem, nextItem) => minItem.Sse < nextItem.Sse ? minItem : nextItem);

            lowestKmeans.PrintClusterInfo();
        }
Exemplo n.º 8
0
 public static bool NotEqual(GenericVector a, GenericVector b)
 {
     return(a.Points.Where((value, index) => value != b.Points[index]).Any());
 }
Exemplo n.º 9
0
        public static double Distance(GenericVector a, GenericVector b)
        {
            var aMinusBpoints = a.Points.Select((t, i) => t - b.Points[i]).ToList();

            return(Math.Sqrt(aMinusBpoints.Sum(item => Math.Pow(item, 2))));
        }
Exemplo n.º 10
0
 private static bool CentroidsChanged(IEnumerable <GenericVector> a, IReadOnlyList <GenericVector> b)
 {
     return(a.Where((item, index) => GenericVector.NotEqual(item, b[index])).Any());
 }