JAVA 写一个KNN 算法
时间: 2023-11-10 19:58:54 浏览: 48
以下是一个简单的KNN算法Java代码示例:
```java
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class KNN {
public static void main(String[] args) {
List<DataPoint> dataPoints = new ArrayList<>();
dataPoints.add(new DataPoint(1, 2, "A"));
dataPoints.add(new DataPoint(2, 1, "B"));
dataPoints.add(new DataPoint(3, 4, "C"));
dataPoints.add(new DataPoint(4, 3, "A"));
dataPoints.add(new DataPoint(5, 6, "B"));
dataPoints.add(new DataPoint(6, 5, "C"));
DataPoint testPoint = new DataPoint(3, 2, "");
int k = 3;
String result = classify(testPoint, dataPoints, k);
System.out.println("Test point belongs to class: " + result);
}
public static String classify(DataPoint testPoint, List<DataPoint> dataPoints, int k) {
List<DataPoint> distances = new ArrayList<>();
for (DataPoint dataPoint : dataPoints) {
double distance = Math.sqrt(Math.pow(dataPoint.x - testPoint.x, 2) + Math.pow(dataPoint.y - testPoint.y, 2));
distances.add(new DataPoint(dataPoint.x, dataPoint.y, dataPoint.label, distance));
}
Collections.sort(distances, new Comparator<DataPoint>() {
@Override
public int compare(DataPoint o1, DataPoint o2) {
return Double.compare(o1.distance, o2.distance);
}
});
List<DataPoint> kNearestNeighbors = distances.subList(0, k);
int maxCount = 0;
String maxLabel = "";
for (DataPoint neighbor : kNearestNeighbors) {
int count = 0;
for (DataPoint dp : kNearestNeighbors) {
if (dp.label.equals(neighbor.label)) {
count++;
}
}
if (count > maxCount) {
maxCount = count;
maxLabel = neighbor.label;
}
}
return maxLabel;
}
static class DataPoint {
double x;
double y;
String label;
double distance;
DataPoint(double x, double y, String label) {
this.x = x;
this.y = y;
this.label = label;
}
DataPoint(double x, double y, String label, double distance) {
this.x = x;
this.y = y;
this.label = label;
this.distance = distance;
}
}
}
```
此代码实现了一个简单的二维KNN算法,假设我们有一组数据点,每个数据点都有x和y坐标和一个标签。我们还有一个测试点,我们想要确定它属于哪个标签类别。首先,我们计算测试点与所有数据点之间的距离,然后对距离进行排序。然后我们选择距离最近的前k个点。最后,我们根据这k个点的标签来确定测试点所属的类别,并返回它。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)