【无标题】哈哈哈哈哈哈哈哈哈

import java.util.ArrayList;

import java.util.List;

import java.util.Random;

class Point {

double x;

double y;

public Point(double x, double y) {

this.x = x;

this.y = y;

}

}

public class KMeansClustering {

private int k;

private List<Point> points;

private List<Point> centroids;

public KMeansClustering(int k, List<Point> points) {

this.k = k;

this.points = points;

centroids = new ArrayList<>();

}

public void run() {

// 初始化质心

initializeCentroids();

boolean converged = false;

while (!converged) {

List<List<Point>> clusters = new ArrayList<>();

for (int i = 0; i < k; i++) {

clusters.add(new ArrayList<>());

}

// 分配点到最近的质心

for (Point point : points) {

int clusterIndex = findClosestCentroid(point);

clusters.get(clusterIndex).add(point);

}

List<Point> newCentroids = new ArrayList<>();

for (List<Point> cluster : clusters) {

if (!cluster.isEmpty()) {

double sumX = 0;

double sumY = 0;

for (Point p : cluster) {

sumX += p.x;

sumY += p.y;

}

newCentroids.add(new Point(sumX / cluster.size(), sumY / cluster.size()));

} else {

// 如果一个簇为空,随机选择一个点作为新质心

Random random = new Random();

int randomIndex = random.nextInt(points.size());

newCentroids.add(points.get(randomIndex));

}

}

converged = true;

for (int i = 0; i < k; i++) {

if (!centroids.get(i).equals(newCentroids.get(i))) {

converged = false;

break;

}

}

centroids = newCentroids;

}

}

private int findClosestCentroid(Point point) {

double minDistance = Double.MAX_VALUE;

int closestCentroidIndex = 0;

for (int i = 0; i < k; i++) {

double distance = distance(point, centroids.get(i));

if (distance < minDistance) {

minDistance = distance;

closestCentroidIndex = i;

}

}

return closestCentroidIndex;

}

private double distance(Point p1, Point p2) {

return Math.sqrt(Math.pow(p1.x - p2.x, 2) + Math.pow(p1.y - p2.y, 2));

}

private void initializeCentroids() {

Random random = new Random();

for (int i = 0; i < k; i++) {

int randomIndex = random.nextInt(points.size());

centroids.add(points.get(randomIndex));

}

}

public List<Point> getCentroids() {

return centroids;

}

public static void main(String[] args) {

List<Point> points = new ArrayList<>();

points.add(new Point(1, 1));

points.add(new Point(1.5, 2));

points.add(new Point(3, 4));

points.add(new Point(5, 7));

points.add(new Point(3.5, 5));

points.add(new Point(4.5, 5));

points.add(new Point(3.5, 4.5));

int k = 3;

KMeansClustering kMeans = new KMeansClustering(k, points);

kMeans.run();

List<Point> centroids = kMeans.getCentroids();

for (int i = 0; i < centroids.size(); i++) {

System.out.println("Centroid " + (i + 1) + ": (" + centroids.get(i).x + ", " + centroids.get(i).y + ")");

}

}

}

相关推荐
小_太_阳7 分钟前
Scala_【2】变量和数据类型
开发语言·后端·scala·intellij-idea
直裾10 分钟前
scala借阅图书保存记录(三)
开发语言·后端·scala
唐 城31 分钟前
curl 放弃对 Hyper Rust HTTP 后端的支持
开发语言·http·rust
码银2 小时前
【python】银行客户流失预测预处理部分,独热编码·标签编码·数据离散化处理·数据筛选·数据分割
开发语言·python
从善若水2 小时前
【2024】Merry Christmas!一起用Rust绘制一颗圣诞树吧
开发语言·后端·rust
2401_858286113 小时前
115.【C语言】数据结构之排序(希尔排序)
c语言·开发语言·数据结构·算法·排序算法
Jelena技术达人3 小时前
Java爬虫获取1688关键字 item_search接口返回值详细解析
java·开发语言·爬虫
数据小爬虫@3 小时前
Java爬虫:速卖通(AliExpress)商品评论获取指南
java·开发语言
waterme1onY3 小时前
Spring AOP 中记录日志
java·开发语言·笔记·后端
2401_879103684 小时前
24.12.25 AOP
java·开发语言·笔记