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

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 + ")");

}

}

}

相关推荐
凡人的AI工具箱7 分钟前
15分钟学 Go 第 60 天 :综合项目展示 - 构建微服务电商平台(完整示例25000字)
开发语言·后端·微服务·架构·golang
chnming198723 分钟前
STL关联式容器之map
开发语言·c++
进击的六角龙25 分钟前
深入浅出:使用Python调用API实现智能天气预报
开发语言·python
檀越剑指大厂25 分钟前
【Python系列】浅析 Python 中的字典更新与应用场景
开发语言·python
湫ccc33 分钟前
Python简介以及解释器安装(保姆级教学)
开发语言·python
程序伍六七36 分钟前
day16
开发语言·c++
wkj00141 分钟前
php操作redis
开发语言·redis·php
极客代码1 小时前
【Python TensorFlow】进阶指南(续篇三)
开发语言·人工智能·python·深度学习·tensorflow
土豆湿1 小时前
拥抱极简主义前端开发:NoCss.js 引领无 CSS 编程潮流
开发语言·javascript·css
界面开发小八哥1 小时前
更高效的Java 23开发,IntelliJ IDEA助力全面升级
java·开发语言·ide·intellij-idea·开发工具