コード例 #1
0
        public string ClassifyArticle(PreprocessedArticle article)
        {
            KnnArticle knnArticle = new KnnArticle {
                Label = article.Label, FeaturesVector = FeaturesVectorService.GetFeaturesVector(article)
            };
            List <(string, double)> neighbors = new List <(string, double)>();

            foreach (KnnArticle classifiedArticle in ClassifiedArticles)
            {
                neighbors.Add((classifiedArticle.PredictedLabel, Metric.CalculateDistance(classifiedArticle.FeaturesVector, knnArticle.FeaturesVector)));
            }

            var orderedNeighbors       = neighbors.OrderBy(t => t.Item2).Take(K).ToList();
            var distinctNeighborsCount = orderedNeighbors.GroupBy(t => t.Item1).Select(g => (g.Key, g.Count())).OrderByDescending(e => e.Item2).ToList();
            var neighborsWithSameCount =
                distinctNeighborsCount.Where(t => t.Item2 == distinctNeighborsCount[0].Item2).ToList();

            if (neighborsWithSameCount.Count != 1)
            {
                var orderedNeighborsWithSameCount =
                    orderedNeighbors.Where(c => neighborsWithSameCount.Select(d => d.Item1).Contains(c.Item1));
                var distinctNeighborsSum = orderedNeighborsWithSameCount.GroupBy(t => t.Item1).Select(g => (g.Key, g.Sum(t => t.Item2))).OrderBy(e => e.Item2).ToList();
                knnArticle.PredictedLabel = distinctNeighborsSum[0].Item1;
                ClassifiedArticles.Add(knnArticle);
                return(distinctNeighborsSum[0].Item1);
            }

            knnArticle.PredictedLabel = distinctNeighborsCount[0].Item1;
            ClassifiedArticles.Add(knnArticle);
            return(distinctNeighborsCount[0].Item1);
        }
コード例 #2
0
 public void InitKnn(List <PreprocessedArticle> articles)
 {
     coldStartNumber    = articles.Count;
     ClassifiedArticles = articles.Select(t => new KnnArticle()
     {
         Label = t.Label, PredictedLabel = t.Label, FeaturesVector = FeaturesVectorService.GetFeaturesVector(t)
     }).ToList();
 }