Esempio n. 1
0
        public string Classify(T NewNods)
        {
            List <KeyValuePair <T, float> > TrainedResultDistancePair = new List <KeyValuePair <T, float> >();

            foreach (T Center in TrainedResult)
            {
                float DistanceGet = Center.GetEuclideanDistance(NewNods);
                TrainedResultDistancePair.Add(new KeyValuePair <T, float>(Center, DistanceGet));
            }

            var ShortestGroup    = TrainedResultDistancePair.OrderBy(x => x.Value).Select(x => x.Key);
            var ShortestGroupTag = ShortestGroup.First().Tag;

            OnClassify?.Invoke(NewNods, ShortestGroup, ShortestGroupTag);
            NewNods.Tag = ShortestGroupTag;

            return(ShortestGroupTag);
        }
Esempio n. 2
0
        public KnnClassifyResult Classify(int k, T NewNode)
        {
            List <KeyValuePair <T, float> > NodeDistance = new List <KeyValuePair <T, float> >();

            foreach (var ClassifiedNode in ClassifiedNodes)
            {
                var distance = ClassifiedNode.GetEuclideanDistance(NewNode);

                if (Threshold != null && distance < Threshold)
                {
                    NodeDistance.Add(new KeyValuePair <T, float>(ClassifiedNode, distance));
                }
                else
                {
                    NodeDistance.Add(new KeyValuePair <T, float>(ClassifiedNode, distance));
                }
            }

            var ClosestKPoints = NodeDistance.OrderBy(x => x.Value).Take(k);

            var MostElementsGroup = ClosestKPoints
                                    .GroupBy(node => node.Key.Tag)
                                    .OrderByDescending(group => group.Count())
                                    .First();

            float  confidence = ((float)MostElementsGroup.Count()) / ((float)k);
            string MostTag    = MostElementsGroup.Key;

            NewNode.Tag = MostTag;

            OnClassify?.Invoke(NewNode, ClosestKPoints.Select(x => x.Key), MostTag, ClassifiedNodes);

            return(new KnnClassifyResult()
            {
                Tag = MostTag, Confidence = confidence
            });
        }