public static bool ChooseKNeighbours(TestVectorAndTrainingVectorsCollection TestAndTrainingPair, int k) { TestAndTrainingPair.TrainingReuters = TestAndTrainingPair.TrainingReuters.OrderBy(h => h.HowFar).ToList(); //sorted list var Kneighbours = TestAndTrainingPair.TrainingReuters.Take(k).ToList(); // take k neighbours return(ChoosePlace(Kneighbours, TestAndTrainingPair.TestReuter));; }
public static bool CalculateManhattanMetricForOneTestSet(Reuter testSet, List <Reuter> TrainingVectors, int k) { double xn = 0; double yn = 0; double underSqrt = 0; TestVectorAndTrainingVectorsCollection result = new TestVectorAndTrainingVectorsCollection(); for (int i = 0; i < TrainingVectors.Count; i++) //wykonujemy petle dla kazdego wzorca treningowego { foreach (var word in testSet.VectorFeatures) //sprawdzamy dla kazdego slowa z wektora testowego czy istnieje takie slowo w wektorze treningowym { if (TrainingVectors.ElementAt(i).VectorFeatures.ContainsKey(word.Key)) { yn = TrainingVectors.ElementAt(i).VectorFeatures[word.Key]; } else { yn = 0; } xn = word.Value; underSqrt += Math.Abs(xn - yn); } TrainingVectors.ElementAt(i).HowFar = underSqrt; result.TestReuter = testSet; underSqrt = 0; } result.TrainingReuters = TrainingVectors; return(KnnAlgorithm.Calculate(result, k)); }
public static bool Calculate(TestVectorAndTrainingVectorsCollection item, int k) { return(ChooseKNeighbours(item, k)); }