-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKMeans.java
More file actions
176 lines (138 loc) · 4.74 KB
/
Copy pathKMeans.java
File metadata and controls
176 lines (138 loc) · 4.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import java.util.ArrayList;
import java.util.List;
public class KMeans {
private List<Point> centroids;
public static void main(String[] args) {
// Generate random points
List<Point> points = generateRandomPoints(100);
// Set the number of clusters
int k = 3;
// Create an instance of KMeans
KMeans kMeans = new KMeans();
// Initialize centroids randomly
kMeans.initializeCentroids(k);
// Run K-means algorithm
kMeans.runKMeans(points);
// Print the final centroids and their respective clusters
kMeans.printCentroids();
kMeans.printClusters();
}
public void initializeCentroids(int k) {
centroids = generateRandomPoints(k);
}
public void runKMeans(List<Point> points) {
boolean centroidsChanged;
do {
centroidsChanged = false;
// Clear clusters
for (Point centroid : centroids) {
centroid.getCluster().clear();
}
// Assign points to the nearest centroid
for (Point point : points) {
Point nearestCentroid = findNearestCentroid(point);
nearestCentroid.getCluster().add(point);
// Check if the point changed its cluster
if (!nearestCentroid.equals(point.getCentroid())) {
point.setCentroid(nearestCentroid);
centroidsChanged = true;
}
}
// Recalculate centroids
for (Point centroid : centroids) {
centroid.updateCentroid();
}
} while (centroidsChanged);
}
public Point findNearestCentroid(Point point) {
Point nearestCentroid = null;
double minDistance = Double.MAX_VALUE;
for (Point centroid : centroids) {
double distance = calculateDistance(point, centroid);
if (distance < minDistance) {
minDistance = distance;
nearestCentroid = centroid;
}
}
return nearestCentroid;
}
public double calculateDistance(Point point1, Point point2) {
double xDiff = point1.getX() - point2.getX();
double yDiff = point1.getY() - point2.getY();
return Math.sqrt(xDiff * xDiff + yDiff * yDiff);
}
public void printCentroids() {
System.out.println("Final Centroids:");
for (Point centroid : centroids) {
System.out.println(centroid);
}
System.out.println();
}
public void printClusters() {
System.out.println("Final Clusters:");
for (Point centroid : centroids) {
System.out.println("Centroid: " + centroid);
for (Point point : centroid.getCluster()) {
System.out.println(point);
}
System.out.println();
}
}
public static List<Point> generateRandomPoints(int numPoints) {
List<Point> points = new ArrayList<>();
for (int i = 1; i <= numPoints; i++) {
double x = Math.random() * 100; // Adjust the range as needed
double y = Math.random() * 100; // Adjust the range as needed
points.add(new Point(i, x, y));
}
return points;
}
public static class Point {
private int id;
private double x;
private double y;
private Point centroid;
private List<Point> cluster;
public Point(int id, double x, double y) {
this.id = id;
this.x = x;
this.y = y;
this.cluster = new ArrayList<>();
}
public int getId() {
return id;
}
public double getX() {
return x;
}
public double getY() {
return y;
}
public Point getCentroid() {
return centroid;
}
public void setCentroid(Point centroid) {
this.centroid = centroid;
}
public List<Point> getCluster() {
return cluster;
}
public void updateCentroid() {
double sumX = 0;
double sumY = 0;
for (Point point : cluster) {
sumX += point.getX();
sumY += point.getY();
}
int clusterSize = cluster.size();
if (clusterSize > 0) {
x = sumX / clusterSize;
y = sumY / clusterSize;
}
}
@Override
public String toString() {
return id + ", " + x + ", " + y;
}
}
}