|
| 1 | +package com.thealgorithms.machinelearning; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.Collections; |
| 5 | +import java.util.Comparator; |
| 6 | +import java.util.List; |
| 7 | + |
| 8 | +public class KNN { |
| 9 | + public static class DataPoint { |
| 10 | + double[] features; |
| 11 | + String label; |
| 12 | + |
| 13 | + public DataPoint(double[] features, String label) { |
| 14 | + this.features = features; |
| 15 | + this.label = label; |
| 16 | + } |
| 17 | + } |
| 18 | + |
| 19 | + private static class DistancePair { |
| 20 | + double distance; |
| 21 | + String label; |
| 22 | + |
| 23 | + public DistancePair(double distance, String label) { |
| 24 | + this.distance = distance; |
| 25 | + this.label = label; |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + public static double calculateEuclideanDistance(double[] point1, double[] point2) { |
| 30 | + double sum = 0.0; |
| 31 | + for (int i = 0; i < point1.length; i++) { |
| 32 | + sum += Math.pow(point1[i] - point2[i], 2); |
| 33 | + } |
| 34 | + return Math.sqrt(sum); |
| 35 | + } |
| 36 | + |
| 37 | + public static String classify(List<DataPoint> dataset, double[] queryPoint, int k) { |
| 38 | + List<DistancePair> distances = new ArrayList<>(); |
| 39 | + |
| 40 | + for (DataPoint p : dataset) { |
| 41 | + double dist = calculateEuclideanDistance(p.features, queryPoint); |
| 42 | + distances.add(new DistancePair(dist, p.label)); |
| 43 | + } |
| 44 | + |
| 45 | + distances.sort(Comparator.comparingDouble(d -> d.distance)); |
| 46 | + |
| 47 | + List<String> topKLabels = new ArrayList<>(); |
| 48 | + for (int i = 0; i < Math.min(k, distances.size()); i++) { |
| 49 | + topKLabels.add(distances.get(i).label); |
| 50 | + } |
| 51 | + |
| 52 | + String bestLabel = null; |
| 53 | + int maxCount = -1; |
| 54 | + for (String label : topKLabels) { |
| 55 | + int count = Collections.frequency(topKLabels, label); |
| 56 | + if (count > maxCount) { |
| 57 | + maxCount = count; |
| 58 | + bestLabel = label; |
| 59 | + } |
| 60 | + } |
| 61 | + return bestLabel; |
| 62 | + } |
| 63 | + |
| 64 | + public static void main(String[] args) { |
| 65 | + List<DataPoint> trainData = new ArrayList<>(); |
| 66 | + trainData.add(new DataPoint(new double[] {1.0, 2.0}, "ClassA")); |
| 67 | + trainData.add(new DataPoint(new double[] {2.0, 3.0}, "ClassA")); |
| 68 | + trainData.add(new DataPoint(new double[] {7.0, 8.0}, "ClassB")); |
| 69 | + |
| 70 | + double[] target = new double[] {1.5, 2.5}; |
| 71 | + String prediction = classify(trainData, target, 3); |
| 72 | + System.out.println("Predicted Category: " + prediction); |
| 73 | + } |
| 74 | +} |
0 commit comments