Skip to content

Commit 604ea17

Browse files
style(machinelearning): apply clang-format
1 parent ded88c0 commit 604ea17

2 files changed

Lines changed: 61 additions & 172 deletions

File tree

src/main/java/com/thealgorithms/machinelearning/KMeans.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,7 @@ private static int nearestCentroid(double[] point, double[][] centroids) {
6363
* @return cluster assignment for each point
6464
* @throws IllegalArgumentException if the input is invalid
6565
*/
66-
public static int[] cluster(
67-
double[][] points,
68-
double[][] initialCentroids,
69-
int maxIterations,
70-
double tolerance) {
66+
public static int[] cluster(double[][] points, double[][] initialCentroids, int maxIterations, double tolerance) {
7167

7268
if (points == null || initialCentroids == null) {
7369
throw new IllegalArgumentException("Input arrays cannot be null.");

src/test/java/com/thealgorithms/machinelearning/KMeansTest.java

Lines changed: 60 additions & 167 deletions
Original file line numberDiff line numberDiff line change
@@ -2,245 +2,138 @@
22

33
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
44
import static org.junit.jupiter.api.Assertions.assertThrows;
5+
56
import org.junit.jupiter.api.Test;
67

78
class KMeansTest {
89

910
@Test
1011
void testSimpleClustering() {
1112

12-
double[][] points = {
13-
{1.0, 1.0},
14-
{1.2, 1.1},
15-
{8.0, 8.0},
16-
{8.2, 8.1}
17-
};
13+
double[][] points = {{1.0, 1.0}, {1.2, 1.1}, {8.0, 8.0}, {8.2, 8.1}};
1814

19-
double[][] centroids = {
20-
{1.0, 1.0},
21-
{8.0, 8.0}
22-
};
15+
double[][] centroids = {{1.0, 1.0}, {8.0, 8.0}};
2316

2417
int[] expected = {0, 0, 1, 1};
2518

26-
assertArrayEquals(
27-
expected,
28-
KMeans.cluster(points, centroids, 100, 0.0001)
29-
);
19+
assertArrayEquals(expected, KMeans.cluster(points, centroids, 100, 0.0001));
3020
}
3121

3222
@Test
3323
void testNullCentroids() {
34-
double[][] points = {
35-
{1.0, 1.0}
36-
};
37-
38-
assertThrows(
39-
IllegalArgumentException.class,
40-
() -> KMeans.cluster(points, null, 100, 0.0001)
41-
);
24+
double[][] points = {{1.0, 1.0}};
4225

26+
assertThrows(IllegalArgumentException.class, () -> KMeans.cluster(points, null, 100, 0.0001));
4327
}
4428

4529
@Test
4630
void testEmptyDataset() {
4731
double[][] points = {};
48-
double[][] centroids = {
49-
{1.0, 1.0}
50-
};
51-
52-
assertThrows(
53-
IllegalArgumentException.class,
54-
() -> KMeans.cluster(points, centroids, 100, 0.0001)
55-
);
32+
double[][] centroids = {{1.0, 1.0}};
33+
34+
assertThrows(IllegalArgumentException.class, () -> KMeans.cluster(points, centroids, 100, 0.0001));
5635
}
5736

5837
@Test
5938
void testEmptyPoints() {
6039
double[][] points = {};
61-
double[][] centroids = {
62-
{1.0, 1.0}
63-
};
64-
65-
assertThrows(
66-
IllegalArgumentException.class,
67-
() -> KMeans.cluster(points, centroids, 100, 0.0001)
68-
);
40+
double[][] centroids = {{1.0, 1.0}};
41+
42+
assertThrows(IllegalArgumentException.class, () -> KMeans.cluster(points, centroids, 100, 0.0001));
6943
}
7044

7145
@Test
7246
void testNoCentroids() {
73-
double[][] points = {
74-
{1.0, 1.0}
75-
};
47+
double[][] points = {{1.0, 1.0}};
7648

7749
double[][] centroids = {};
7850

79-
assertThrows(
80-
IllegalArgumentException.class,
81-
() -> KMeans.cluster(points, centroids, 100, 0.0001)
82-
);
51+
assertThrows(IllegalArgumentException.class, () -> KMeans.cluster(points, centroids, 100, 0.0001));
8352
}
8453

8554
@Test
8655
void testNonPositiveMaxIterations() {
87-
double[][] points = {
88-
{1.0, 1.0}
89-
};
90-
91-
double[][] centroids = {
92-
{1.0, 1.0}
93-
};
94-
95-
assertThrows(
96-
IllegalArgumentException.class,
97-
() -> KMeans.cluster(points, centroids, 0, 0.0001)
98-
);
56+
double[][] points = {{1.0, 1.0}};
57+
58+
double[][] centroids = {{1.0, 1.0}};
59+
60+
assertThrows(IllegalArgumentException.class, () -> KMeans.cluster(points, centroids, 0, 0.0001));
9961
}
10062

10163
@Test
10264
void testNegativeTolerance() {
103-
double[][] points = {
104-
{1.0, 1.0}
105-
};
106-
double[][] centroids = {
107-
{1.0, 1.0}
108-
};
109-
110-
assertThrows(
111-
IllegalArgumentException.class,
112-
() -> KMeans.cluster(points, centroids, 100, -1.0)
113-
);
65+
double[][] points = {{1.0, 1.0}};
66+
double[][] centroids = {{1.0, 1.0}};
67+
68+
assertThrows(IllegalArgumentException.class, () -> KMeans.cluster(points, centroids, 100, -1.0));
11469
}
11570

11671
@Test
11772
void testTooManyCentroids() {
118-
double[][] points = {
119-
{1.0, 1.0}
120-
};
121-
122-
double[][] centroids = {
123-
{1.0, 1.0},
124-
{2.0, 2.0}
125-
};
126-
127-
assertThrows(
128-
IllegalArgumentException.class,
129-
() -> KMeans.cluster(points, centroids, 100, 0.0001)
130-
);
73+
double[][] points = {{1.0, 1.0}};
74+
75+
double[][] centroids = {{1.0, 1.0}, {2.0, 2.0}};
76+
77+
assertThrows(IllegalArgumentException.class, () -> KMeans.cluster(points, centroids, 100, 0.0001));
13178
}
13279

13380
@Test
13481
void testDimensionMismatchInPoints() {
135-
double[][] points = {
136-
{1.0, 1.0},
137-
{2.0}
138-
};
139-
140-
double[][] centroids = {
141-
{1.0, 1.0}
142-
};
143-
144-
assertThrows(
145-
IllegalArgumentException.class,
146-
() -> KMeans.cluster(points, centroids, 100, 0.0001)
147-
);
82+
double[][] points = {{1.0, 1.0}, {2.0}};
83+
84+
double[][] centroids = {{1.0, 1.0}};
85+
86+
assertThrows(IllegalArgumentException.class, () -> KMeans.cluster(points, centroids, 100, 0.0001));
14887
}
14988

15089
@Test
15190
void testDimensionMismatchInCentroids() {
152-
double[][] points = {
153-
{1.0, 1.0}
154-
};
155-
156-
double[][] centroids = {
157-
{1.0}
158-
};
159-
160-
assertThrows(
161-
IllegalArgumentException.class,
162-
() -> KMeans.cluster(points, centroids, 100, 0.0001)
163-
);
91+
double[][] points = {{1.0, 1.0}};
92+
93+
double[][] centroids = {{1.0}};
94+
95+
assertThrows(IllegalArgumentException.class, () -> KMeans.cluster(points, centroids, 100, 0.0001));
16496
}
16597

16698
@Test
16799
void testZeroDimensionPoints() {
168-
double[][] points = {
169-
{}
170-
};
171-
172-
double[][] centroids = {
173-
{}
174-
};
175-
176-
assertThrows(
177-
IllegalArgumentException.class,
178-
() -> KMeans.cluster(points, centroids, 100, 0.0001)
179-
);
100+
double[][] points = {{}};
101+
102+
double[][] centroids = {{}};
103+
104+
assertThrows(IllegalArgumentException.class, () -> KMeans.cluster(points, centroids, 100, 0.0001));
180105
}
181106

182107
@Test
183108
void testSingleCluster() {
184-
double[][] points = {
185-
{1.0, 1.0},
186-
{2.0, 2.0},
187-
{3.0, 3.0}
188-
};
189-
190-
double[][] centroids = {
191-
{2.0, 2.0}
192-
};
193-
194-
int[] expected = {
195-
0, 0, 0
196-
};
197-
198-
assertArrayEquals(
199-
expected,
200-
KMeans.cluster(points, centroids, 100, 0.0001)
201-
);
109+
double[][] points = {{1.0, 1.0}, {2.0, 2.0}, {3.0, 3.0}};
110+
111+
double[][] centroids = {{2.0, 2.0}};
112+
113+
int[] expected = {0, 0, 0};
114+
115+
assertArrayEquals(expected, KMeans.cluster(points, centroids, 100, 0.0001));
202116
}
203117

204118
@Test
205119
void testEmptyClusterHandling() {
206-
double[][] points = {
207-
{0.0, 0.0},
208-
{0.1, 0.1},
209-
{10.0, 10.0}
210-
};
120+
double[][] points = {{0.0, 0.0}, {0.1, 0.1}, {10.0, 10.0}};
211121

212-
double[][] centroids = {
213-
{0.0, 0.0},
214-
{100.0, 100.0}
215-
};
122+
double[][] centroids = {{0.0, 0.0}, {100.0, 100.0}};
216123

217124
int[] result = KMeans.cluster(points, centroids, 100, 0.0001);
218125

219-
assertArrayEquals(
220-
new int[]{0, 0, 0},
221-
result
222-
);
126+
assertArrayEquals(new int[] {0, 0, 0}, result);
223127
}
224128

225129
@Test
226130
void testImmediateConvergence() {
227-
double[][] points = {
228-
{1.0, 1.0},
229-
{9.0, 9.0}
230-
};
231-
232-
double[][] centroids = {
233-
{1.0, 1.0},
234-
{9.0, 9.0}
235-
};
236-
237-
int[] expected = {
238-
0, 1
239-
};
240-
241-
assertArrayEquals(
242-
expected,
243-
KMeans.cluster(points, centroids, 100, 0.000001)
244-
);
131+
double[][] points = {{1.0, 1.0}, {9.0, 9.0}};
132+
133+
double[][] centroids = {{1.0, 1.0}, {9.0, 9.0}};
134+
135+
int[] expected = {0, 1};
136+
137+
assertArrayEquals(expected, KMeans.cluster(points, centroids, 100, 0.000001));
245138
}
246139
}

0 commit comments

Comments
 (0)