下面是使用Java的jdk工具编程实现K-means算法的示例代码:
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class KMeans {
private int k;
private int maxIterations;
private List<Point> points;
private List<Cluster> clusters;
public KMeans(int k, int maxIterations, List<Point> points) {
this.k = k;
this.maxIterations = maxIterations;
this.points = points;
this.clusters = new ArrayList<>();
}
public void run() {
// Initialize clusters with random centroids
Random random = new Random();
for (int i = 0; i < k; i++) {
Point centroid = points.get(random.nextInt(points.size()));
Cluster cluster = new Cluster(i, centroid);
clusters.add(cluster);
}
// Run iterations
int iteration = 0;
boolean isConverged = false;
while (iteration < maxIterations && !isConverged) {
// Assign points to the nearest cluster
for (Point point : points) {
double minDistance = Double.MAX_VALUE;
Cluster nearestCluster = null;
for (Cluster cluster : clusters) {
double distance = cluster.getCentroid().distanceTo(point);
if (distance < minDistance) {
minDistance = distance;
nearestCluster = cluster;
}
}
nearestCluster.addPoint(point);
}
// Update cluster centroids
isConverged = true;
for (Cluster cluster : clusters) {
Point oldCentroid = cluster.getCentroid();
Point newCentroid = cluster.calculateCentroid();
if (!newCentroid.equals(oldCentroid)) {
cluster.setCentroid(newCentroid);
isConverged = false;
}
cluster.clearPoints();
}
iteration++;
}
// Print final results
for (Cluster cluster : clusters) {
System.out.println("Cluster " + cluster.getId() + ":");
System.out.println("Centroid: " + cluster.getCentroid());
System.out.println("Points: " + cluster.getPoints());
System.out.println();
}
}
public static void main(String[] args) {
List<Point> points = new ArrayList<>();
points.add(new Point(1.0, 2.0));
points.add(new Point(2.0, 3.0));
points.add(new Point(3.0, 4.0));
points.add(new Point(10.0, 11.0));
points.add(new Point(11.0, 12.0));
points.add(new Point(12.0, 13.0));
KMeans kMeans = new KMeans(2, 10, points);
kMeans.run();
}
}
class Point {
private double x;
private double y;
public Point(double x, double y) {
this.x = x;
this.y = y;
}
public double getX() {
return x;
}
public double getY() {
return y;
}
public double distanceTo(Point other) {
double dx = x - other.getX();
double dy = y - other.getY();
return Math.sqrt(dx * dx + dy * dy);
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null || getClass() != obj.getClass()) {
return false;
}
Point other = (Point) obj;
return x == other.x && y == other.y;
}
@Override
public int hashCode() {
return Objects.hash(x, y);
}
@Override
public String toString() {
return "(" + x + ", " + y + ")";
}
}
class Cluster {
private int id;
private Point centroid;
private List<Point> points;
public Cluster(int id, Point centroid) {
this.id = id;
this.centroid = centroid;
this.points = new ArrayList<>();
}
public int getId() {
return id;
}
public Point getCentroid() {
return centroid;
}
public void setCentroid(Point centroid) {
this.centroid = centroid;
}
public List<Point> getPoints() {
return points;
}
public void addPoint(Point point) {
points.add(point);
}
public void clearPoints() {
points.clear();
}
public Point calculateCentroid() {
double sumX = 0.0;
double sumY = 0.0;
for (Point point : points) {
sumX += point.getX();
sumY += point.getY();
}
int numPoints = points.size();
double centroidX = sumX / numPoints;
double centroidY = sumY / numPoints;
return new Point(centroidX, centroidY);
}
}
这个示例代码实现了K-means算法,包括点(Point)、簇(Cluster)和K-means主类(KMeans)。在main方法中创建了一些点(Point),然后使用K-means算法进行聚类操作,并打印最终的结果。