Skip to content

Commit f0ce829

Browse files
committed
Fix checkstyle issues
1 parent 3127891 commit f0ce829

2 files changed

Lines changed: 90 additions & 82 deletions

File tree

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

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,17 @@
99
/**
1010
* K-Nearest Neighbors (KNN) classifier.
1111
*
12-
* <p>K-Nearest Neighbors is a supervised machine learning algorithm that
12+
* <p>
13+
* K-Nearest Neighbors is a supervised machine learning algorithm that
1314
* classifies a sample based on the majority class among its {@code k}
1415
* nearest training samples using the Euclidean distance metric.
1516
*
16-
* <p>The classifier stores the training dataset during the fitting phase and
17+
* <p>
18+
* The classifier stores the training dataset during the fitting phase and
1719
* predicts class labels for new samples without building an explicit model.
1820
*
1921
* @see <a href="https://en.wikipedia.org/wiki/K-nearest_neighbors_algorithm">
20-
* K-Nearest Neighbors</a>
22+
* K-Nearest Neighbors</a>
2123
*/
2224
public final class KNearestNeighbors {
2325
private final int k;
@@ -38,7 +40,8 @@ public KNearestNeighbors(int k) {
3840
}
3941

4042
/**
41-
* Represents a neighboring training sample and its distance from the test sample.
43+
* Represents a neighboring training sample and its distance from the test
44+
* sample.
4245
*/
4346
private static final class Neighbor {
4447

@@ -59,11 +62,12 @@ private static final class Neighbor {
5962
/**
6063
* Fits the classifier using the provided training dataset.
6164
*
62-
* <p>The training feature vectors and their corresponding class labels are
65+
* <p>
66+
* The training feature vectors and their corresponding class labels are
6367
* stored for use during prediction.
6468
*
6569
* @param features the training feature vectors
66-
* @param labels the corresponding class labels
70+
* @param labels the corresponding class labels
6771
*/
6872
public void fit(double[][] features, int[] labels) {
6973

@@ -106,7 +110,7 @@ public void fit(double[][] features, int[] labels) {
106110
/**
107111
* Computes the Euclidean distance between two feature vectors.
108112
*
109-
* @param first the first feature vector
113+
* @param first the first feature vector
110114
* @param second the second feature vector
111115
* @return the Euclidean distance between the two vectors
112116
*/
@@ -124,7 +128,8 @@ private static double euclideanDistance(double[] first, double[] second) {
124128
/**
125129
* Predicts the class label for a single sample.
126130
*
127-
* <p>The prediction is made by finding the {@code k} nearest neighbors
131+
* <p>
132+
* The prediction is made by finding the {@code k} nearest neighbors
128133
* among the training samples and selecting the class with the highest
129134
* number of votes. In the event of a tie, the smaller class label is
130135
* returned.
@@ -137,7 +142,7 @@ public int predict(double[] testPoint) {
137142
throw new IllegalStateException("Classifier has not been fitted.");
138143
}
139144

140-
if(testPoint == null) {
145+
if (testPoint == null) {
141146
throw new IllegalArgumentException("Sample cannot be null.");
142147
}
143148

@@ -151,19 +156,18 @@ public int predict(double[] testPoint) {
151156
double distance = euclideanDistance(trainingFeatures[i], testPoint);
152157
neighbors.add(new Neighbor(distance, trainingLabels[i]));
153158
}
154-
155159

156160
neighbors.sort(Comparator.comparingDouble(neighbor -> neighbor.distance));
157161

158-
Map<Integer, Integer> votes = new HashMap<>();
162+
Map<Integer, Integer> votes = new HashMap<>();
159163

160164
if (k > trainingFeatures.length) {
161165
throw new IllegalArgumentException("k cannot be greater than the number of training samples.");
162166
}
163167

164-
for (int i = 0; i < k; i++) {
165-
int label = neighbors.get(i).label;
166-
votes.merge(label, 1, Integer::sum);
168+
for (int i = 0; i < k; i++) {
169+
int label = neighbors.get(i).label;
170+
votes.merge(label, 1, Integer::sum);
167171
}
168172

169173
int predictedLabel = -1;
@@ -182,7 +186,6 @@ public int predict(double[] testPoint) {
182186
return predictedLabel;
183187
}
184188

185-
186189
/**
187190
* Predicts class labels for multiple samples.
188191
*

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

Lines changed: 72 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -6,73 +6,73 @@
66

77
import org.junit.jupiter.api.Test;
88

9-
public class KNearestNeighborsTest {
10-
9+
class KNearestNeighborsTest {
10+
1111
@Test
1212
void predictsCorrectClassOnSeparableDataset() {
1313
double[][] features = {
14-
{0, 0},
15-
{1, 1},
16-
{8, 8},
17-
{9, 9},
14+
{ 0, 0 },
15+
{ 1, 1 },
16+
{ 8, 8 },
17+
{ 9, 9 },
1818
};
1919

20-
int[] labels = {0, 0, 1, 1};
20+
int[] labels = { 0, 0, 1, 1 };
2121

2222
KNearestNeighbors knn = new KNearestNeighbors(3);
2323
knn.fit(features, labels);
2424

25-
assertEquals(0, knn.predict(new double[] {1.5, 1.5}));
26-
assertEquals(1, knn.predict(new double[] {8.5, 8.5}));
25+
assertEquals(0, knn.predict(new double[] { 1.5, 1.5 }));
26+
assertEquals(1, knn.predict(new double[] { 8.5, 8.5 }));
2727
}
2828

2929
@Test
3030
void predictsBatchClassesOnSeparableDataset() {
3131
double[][] features = {
32-
{0, 0},
33-
{1, 1},
34-
{8, 8},
35-
{9, 9},
32+
{ 0, 0 },
33+
{ 1, 1 },
34+
{ 8, 8 },
35+
{ 9, 9 },
3636
};
3737

38-
int[] labels = {0, 0, 1, 1};
38+
int[] labels = { 0, 0, 1, 1 };
3939

4040
KNearestNeighbors knn = new KNearestNeighbors(3);
4141
knn.fit(features, labels);
4242

43-
double[][] samples = {{1.5, 1.5}, {8.5, 8.5}};
43+
double[][] samples = { { 1.5, 1.5 }, { 8.5, 8.5 } };
4444
int[] predictions = knn.predict(samples);
4545

46-
assertArrayEquals(new int[] {0, 1}, predictions);
46+
assertArrayEquals(new int[] { 0, 1 }, predictions);
4747
}
4848

4949
@Test
50-
void throwsExceptionWhenKIsNotPositive() {
50+
void throwsExceptionWhenKIsNotPositive() {
5151
assertThrows(IllegalArgumentException.class, () -> new KNearestNeighbors(-4));
5252
assertThrows(IllegalArgumentException.class, () -> new KNearestNeighbors(0));
5353
}
5454

5555
@Test
5656
void throwsExceptionWhenKIsGreaterThanNumberOfTrainingSamples() {
5757
double[][] features = {
58-
{0, 0},
59-
{1, 1},
60-
{8, 8},
61-
{9, 9},
58+
{ 0, 0 },
59+
{ 1, 1 },
60+
{ 8, 8 },
61+
{ 9, 9 },
6262
};
6363

64-
int[] labels = {0, 0, 1, 1};
64+
int[] labels = { 0, 0, 1, 1 };
6565
KNearestNeighbors knn = new KNearestNeighbors(7);
6666
knn.fit(features, labels);
6767

68-
assertThrows(IllegalArgumentException.class, () -> knn.predict(new double[] {1.5, 1.5}));
68+
assertThrows(IllegalArgumentException.class, () -> knn.predict(new double[] { 1.5, 1.5 }));
6969
}
7070

7171
@Test
7272
void nullFeaturesArrayThrowsIllegalArgumentException() {
7373
KNearestNeighbors knn = new KNearestNeighbors(1);
74-
75-
assertThrows(IllegalArgumentException.class, () -> knn.fit(null, new int[] {0, 1}));
74+
75+
assertThrows(IllegalArgumentException.class, () -> knn.fit(null, new int[] { 0, 1 }));
7676
}
7777

7878
@Test
@@ -85,62 +85,66 @@ void emptyFeaturesArrayThrowsIllegalArgumentException() {
8585
@Test
8686
void nullLabelsArrayThrowsIllegalArgumentException() {
8787
KNearestNeighbors knn = new KNearestNeighbors(1);
88-
89-
assertThrows(IllegalArgumentException.class, () -> knn.fit(new double[][] {{1, 1}, {2, 2}}, null));
88+
89+
assertThrows(IllegalArgumentException.class, () -> knn.fit(new double[][] { { 1, 1 }, { 2, 2 } }, null));
9090
}
9191

9292
@Test
9393
void emptyLabelsArrayThrowsIllegalArgumentException() {
9494
KNearestNeighbors knn = new KNearestNeighbors(1);
95-
96-
assertThrows(IllegalArgumentException.class, () -> knn.fit(new double[][] {{1, 1}, {2, 2}}, new int[] {}));
95+
96+
assertThrows(IllegalArgumentException.class,
97+
() -> knn.fit(new double[][] { { 1, 1 }, { 2, 2 } }, new int[] {}));
9798
}
9899

99100
@Test
100101
void mismatchedFeatureAndLabelLengthsThrowsIllegalArgumentException() {
101102
KNearestNeighbors knn = new KNearestNeighbors(3);
102-
103-
assertThrows(IllegalArgumentException.class, () -> knn.fit(new double[][] {{0, 0}, {1, 1}, {8, 8}, {9, 9}}, new int[] {0, 0, 1}));
103+
104+
assertThrows(IllegalArgumentException.class,
105+
() -> knn.fit(new double[][] { { 0, 0 }, { 1, 1 }, { 8, 8 }, { 9, 9 } }, new int[] { 0, 0, 1 }));
104106
}
105107

106108
@Test
107109
void emptyFeatureVectorThrowsIllegalArgumentException() {
108110
KNearestNeighbors knn = new KNearestNeighbors(1);
109111

110-
assertThrows(IllegalArgumentException.class,() -> knn.fit(new double[][] {{}}, new int[] {0}));
112+
assertThrows(IllegalArgumentException.class, () -> knn.fit(new double[][] { {} }, new int[] { 0 }));
111113
}
112114

113115
@Test
114116
void nullFeatureSampleThrowsIllegalArgumentException() {
115117
KNearestNeighbors knn = new KNearestNeighbors(2);
116-
117-
assertThrows(IllegalArgumentException.class, () -> knn.fit(new double[][] {{0, 0}, null, {8, 8}}, new int[] {0, 0, 1}));
118+
119+
assertThrows(IllegalArgumentException.class,
120+
() -> knn.fit(new double[][] { { 0, 0 }, null, { 8, 8 } }, new int[] { 0, 0, 1 }));
118121
}
119122

120123
@Test
121124
void mismatchedDimensionThrowsIllegalArgumentException() {
122125
KNearestNeighbors knn = new KNearestNeighbors(2);
123-
124-
assertThrows(IllegalArgumentException.class, () -> knn.fit(new double[][] {{0, 0}, {1, 1, 2}, {8, 8}}, new int[] {0, 0, 1}));
126+
127+
assertThrows(IllegalArgumentException.class,
128+
() -> knn.fit(new double[][] { { 0, 0 }, { 1, 1, 2 }, { 8, 8 } }, new int[] { 0, 0, 1 }));
125129
}
126130

127131
@Test
128132
void predictBeforeFitThrowsIllegalStateException() {
129133
KNearestNeighbors knn = new KNearestNeighbors(3);
130134

131-
assertThrows(IllegalStateException.class, () -> knn.predict(new double[] {1, 1}));
135+
assertThrows(IllegalStateException.class, () -> knn.predict(new double[] { 1, 1 }));
132136
}
133137

134138
@Test
135139
void nullTestPointThrowsIllegalArgumentException() {
136140
double[][] features = {
137-
{0, 0},
138-
{1, 1},
139-
{8, 8},
140-
{9, 9},
141+
{ 0, 0 },
142+
{ 1, 1 },
143+
{ 8, 8 },
144+
{ 9, 9 },
141145
};
142146

143-
int[] labels = {0, 0, 1, 1};
147+
int[] labels = { 0, 0, 1, 1 };
144148

145149
KNearestNeighbors knn = new KNearestNeighbors(3);
146150
knn.fit(features, labels);
@@ -152,69 +156,70 @@ void nullTestPointThrowsIllegalArgumentException() {
152156
@Test
153157
void mismatchedTestPointLengthThrowsIllegalArgumentException() {
154158
double[][] features = {
155-
{0, 0},
156-
{1, 1},
157-
{8, 8},
158-
{9, 9},
159+
{ 0, 0 },
160+
{ 1, 1 },
161+
{ 8, 8 },
162+
{ 9, 9 },
159163
};
160164

161-
int[] labels = {0, 0, 1, 1};
165+
int[] labels = { 0, 0, 1, 1 };
162166

163167
KNearestNeighbors knn = new KNearestNeighbors(3);
164168
knn.fit(features, labels);
165169

166-
assertThrows(IllegalArgumentException.class, () -> knn.predict(new double[] {1.5, 1.5, 1.5}));
170+
assertThrows(IllegalArgumentException.class, () -> knn.predict(new double[] { 1.5, 1.5, 1.5 }));
167171
}
168172

169173
@Test
170174
void tieBreakReturnsSmallerLabel() {
171175
double[][] features = {
172-
{0, 0},
173-
{0, 2},
174-
{2, 0},
175-
{2, 2}
176+
{ 0, 0 },
177+
{ 0, 2 },
178+
{ 2, 0 },
179+
{ 2, 2 }
176180
};
177181

178-
int[] labels = {0, 1, 0, 1};
182+
int[] labels = { 0, 1, 0, 1 };
179183

180184
KNearestNeighbors knn = new KNearestNeighbors(4);
181185
knn.fit(features, labels);
182186

183-
assertEquals(0, knn.predict(new double[] {1, 1}));
187+
assertEquals(0, knn.predict(new double[] { 1, 1 }));
184188
}
185189

186190
@Test
187191
void nullBatchSampleThrowsIllegalArgumentException() {
188192
double[][] features = {
189-
{0, 0},
190-
{1, 1},
191-
{8, 8},
192-
{9, 9},
193+
{ 0, 0 },
194+
{ 1, 1 },
195+
{ 8, 8 },
196+
{ 9, 9 },
193197
};
194198

195-
int[] labels = {0, 0, 1, 1};
199+
int[] labels = { 0, 0, 1, 1 };
196200

197201
KNearestNeighbors knn = new KNearestNeighbors(3);
198202
knn.fit(features, labels);
199203

200-
assertThrows(IllegalArgumentException.class, () -> knn.predict(new double[][] {{1.5, 1.5}, null}));
204+
assertThrows(IllegalArgumentException.class, () -> knn.predict(new double[][] { { 1.5, 1.5 }, null }));
201205
}
202206

203207
@Test
204208
void invalidBatchSampleThrowsIllegalArgumentException() {
205209
double[][] features = {
206-
{0, 0},
207-
{1, 1},
208-
{8, 8},
209-
{9, 9},
210+
{ 0, 0 },
211+
{ 1, 1 },
212+
{ 8, 8 },
213+
{ 9, 9 },
210214
};
211215

212-
int[] labels = {0, 0, 1, 1};
216+
int[] labels = { 0, 0, 1, 1 };
213217

214218
KNearestNeighbors knn = new KNearestNeighbors(3);
215219
knn.fit(features, labels);
216220

217-
assertThrows(IllegalArgumentException.class, () -> knn.predict(new double[][] {{1.5, 1.5}, {8.5, 8.5, 8.5}}));
221+
assertThrows(IllegalArgumentException.class,
222+
() -> knn.predict(new double[][] { { 1.5, 1.5 }, { 8.5, 8.5, 8.5 } }));
218223
}
219224

220-
}
225+
}

0 commit comments

Comments
 (0)