public void PropogateInput(Example example) { List <BestExample> best = new List <BestExample>(); double maxBest = 0.0; foreach (Example e in savedExamples) { double d = distance_sq(e, example); if (best.Count < k) { best.Add(new BestExample(e, d)); if (d > maxBest) { maxBest = d; } } else if (d < maxBest) { // at this point, the Best list contains k elements best.Sort(); maxBest = best[k - 1].distance; best[k - 1] = new BestExample(e, d); } } if (best.Count < 1) { throw new LearnerNotReady(); } // perform a weighted average List <Double> result = new List <Double>(); for (int i = 0; i < best[0].example.Labels.Count; i++) { result.Add(0.0); } double sumWeight = 0.0; foreach (BestExample be in best) { double thisWeight = 1 / (1 + be.distance); for (int i = 0; i < result.Count; i++) { result[i] += be.example.Labels[i] * thisWeight; } sumWeight += thisWeight; } for (int i = 0; i < result.Count; i++) { result[i] /= sumWeight; } example.Predictions = result; }
public void PropogateInput(Example example) { List<BestExample> best = new List<BestExample>(); double maxBest = 0.0; foreach (Example e in savedExamples) { double d = distance_sq(e, example); if (best.Count < k) { best.Add(new BestExample(e, d)); if (d > maxBest) { maxBest = d; } } else if (d < maxBest) { // at this point, the Best list contains k elements best.Sort(); maxBest = best[k - 1].distance; best[k - 1] = new BestExample(e, d); } } if (best.Count < 1) throw new LearnerNotReady(); // perform a weighted average List<Double> result = new List<Double>(); for (int i = 0; i < best[0].example.Labels.Count; i++) { result.Add(0.0); } double sumWeight = 0.0; foreach (BestExample be in best) { double thisWeight = 1 / (1 + be.distance); for (int i = 0; i < result.Count; i++) { result[i] += be.example.Labels[i] * thisWeight; } sumWeight += thisWeight; } for (int i = 0; i < result.Count; i++) { result[i] /= sumWeight; } example.Predictions = result; }