Beispiel #1
0
 private int GetNearestCluster(GenericVector v)
 {
     return(_centroids
            .OrderBy(centroid => GenericVector.Distance(centroid.Value, v))
            .Select(centroid => centroid.Key)
            .First());
 }
Beispiel #2
0
        public void TestGenericVector()
        {
            var a = new GenericVector(4, 4);
            var b = new GenericVector(6, 6);

            Assert.Equal(GenericVector.Distance(a, b), Math.Sqrt(8.0));

            var c = GenericVector.Sum(a, new GenericVector(6, 6));
            var d = GenericVector.Sum(b, new GenericVector(4, 4));

            Assert.Equal(GenericVector.Distance(c, d), 0);
        }
Beispiel #3
0
        private List <ClusterPoint> RegionQuery(GenericVector point)
        {
            var neighbours = new List <ClusterPoint>();

            foreach (var clusterPoint in _dataSet)
            {
                if (GenericVector.Distance(point, clusterPoint.Vector) <= _radius)
                {
                    neighbours.Add(clusterPoint);
                }
            }

            return(neighbours);
        }
        private List <ClusterPoint> GetNeighbours(GenericVector point)
        {
            var neighbours = new List <ClusterPoint>();

            foreach (var cluster in _trainingData)
            {
                foreach (var trainingPoint in cluster.Value)
                {
                    Console.WriteLine($"Distance {GenericVector.Distance(point, trainingPoint)}");
                    if (GenericVector.Distance(point, trainingPoint) < _radius)
                    {
                        neighbours.Add(new ClusterPoint(trainingPoint, cluster.Key));
                    }
                }
            }
            Console.WriteLine($"Neighbours: {neighbours.Count}");
            return(neighbours);
        }
Beispiel #5
0
        public override int ClassifyPoint(GenericVector point)
        {
            var nearestPoints = new PriorityQue <ClusterPoint>();

            foreach (var cluster in _trainingData)
            {
                foreach (var clusterPoint in cluster.Value)
                {
                    var distanceToClusterPoint = GenericVector.Distance(point, clusterPoint);

                    if (nearestPoints.Count < _k)
                    {
                        nearestPoints.Insert(distanceToClusterPoint, new ClusterPoint(clusterPoint, cluster.Key));
                    }
                    else if (distanceToClusterPoint < nearestPoints.Peek().Priority)
                    {
                        nearestPoints.Pop();
                        nearestPoints.Insert(distanceToClusterPoint, new ClusterPoint(clusterPoint, cluster.Key));
                    }
                }
            }

            return(GetBiggestCluster(nearestPoints));
        }