使用java随机生成十个点,并对这十个点进行kmeans聚类,聚3类
时间: 2024-05-14 17:16:40 浏览: 10
好的,您想了解Java如何生成十个随机点并对其进行kmeans聚类,聚3类。以下是Java代码示例:
```java
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class KMeansExample {
//定义数据点对象
public static class DataPoint {
double x;
double y;
int cluster;
public DataPoint(double x, double y) {
this.x = x;
this.y = y;
this.cluster = 0; //将cluster初始化为0
}
//getter和setter方法
}
public static void main(String[] args) {
int numPoints = 10; //生成10个数据点
int k = 3; //聚3类
List<DataPoint> allPoints = generateRandomData(numPoints); //生成随机数据点集合
//将数据点划分为k个簇
List<DataPoint>[] clusters = kmeansClustering(allPoints, k);
//打印每个簇中的数据点
for(int i = 0; i < k; i++) {
System.out.println("Cluster " + i + ": ");
for(int j = 0; j < clusters[i].size(); j++) {
System.out.println(clusters[i].get(j).x + "," + clusters[i].get(j).y);
}
System.out.println();
}
}
//生成随机数据点的方法
public static List<DataPoint> generateRandomData(int numPoints) {
List<DataPoint> dataPoints = new ArrayList<>();
Random random = new Random();
for(int i = 0; i < numPoints; i++) {
double x = random.nextInt(100) + 1; //随机生成1-100的x坐标
double y = random.nextInt(100) + 1; //随机生成1-100的y坐标
dataPoints.add(new DataPoint(x, y));
}
return dataPoints;
}
//实现kmeans聚类的方法
public static List<DataPoint>[] kmeansClustering(List<DataPoint> allPoints, int k) {
List<DataPoint>[] clusters = new List[k]; //定义k个簇
List<DataPoint> centroids = getInitialCentroids(allPoints, k); //获取随机k个质心
while(true) {
//将所有数据点分配到最近的中心点所在的簇中
for(int i = 0; i < allPoints.size(); i++) {
DataPoint currentPoint = allPoints.get(i);
int closestCentroid = getClosestCentroid(currentPoint, centroids);
currentPoint.cluster = closestCentroid;
if(clusters[closestCentroid] == null) {
clusters[closestCentroid] = new ArrayList<>();
}
clusters[closestCentroid].add(currentPoint);
}
//更新每个簇的质心
boolean converged = true;
for(int i = 0; i < k; i++) {
double sumX = 0.0;
double sumY = 0.0;
int clusterSize = clusters[i].size();
for(int j = 0; j < clusterSize; j++) {
DataPoint dataPoint = clusters[i].get(j);
sumX += dataPoint.x;
sumY += dataPoint.y;
}
if(clusterSize > 0) {
double newX = sumX / clusterSize;
double newY = sumY / clusterSize;
if(centroids.get(i).x != newX || centroids.get(i).y != newY) {
converged = false;
centroids.get(i).x = newX;
centroids.get(i).y = newY;
}
}
}
//如果质心没有变化,则跳出循环
if(converged) {
break;
}
//清空簇
for(int i = 0; i < k; i++) {
clusters[i] = new ArrayList<>();
}
}
return clusters;
}
//获取随机k个质心的方法
public static List<DataPoint> getInitialCentroids(List<DataPoint> allPoints, int k) {
List<DataPoint> centroids = new ArrayList<>();
Random random = new Random();
for(int i = 0; i < k; i++) {
int randomIndex = random.nextInt(allPoints.size());
centroids.add(new DataPoint(allPoints.get(randomIndex).x, allPoints.get(randomIndex).y));
}
return centroids;
}
//获取当前数据点距离哪个质心最近的方法
public static int getClosestCentroid(DataPoint currentPoint, List<DataPoint> centroids) {
int closestCentroid = 0;
double closestDistance = Double.MAX_VALUE;
for(int i = 0; i < centroids.size(); i++) {
DataPoint centroid = centroids.get(i);
double distance = Math.sqrt(Math.pow((currentPoint.x - centroid.x), 2) + Math.pow((currentPoint.y - centroid.y), 2));
if(distance < closestDistance) {
closestDistance = distance;
closestCentroid = i;
}
}
return closestCentroid;
}
}
```
希望对您有所帮助!
相关推荐
![](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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)