Fixed the single line if statements.
This commit is contained in:
parent
5a10f94946
commit
5e7396ba64
@ -23,8 +23,12 @@ public class Centroid {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean equals(Object o) {
|
public boolean equals(Object o) {
|
||||||
if (this == o) return true;
|
if (this == o) {
|
||||||
if (o == null || getClass() != o.getClass()) return false;
|
return true;
|
||||||
|
}
|
||||||
|
if (o == null || getClass() != o.getClass()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
Centroid centroid = (Centroid) o;
|
Centroid centroid = (Centroid) o;
|
||||||
return Objects.equals(getCoordinates(), centroid.getCoordinates());
|
return Objects.equals(getCoordinates(), centroid.getCoordinates());
|
||||||
}
|
}
|
||||||
|
@ -9,7 +9,9 @@ public class EuclideanDistance implements Distance {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public double calculate(Map<String, Double> f1, Map<String, Double> f2) {
|
public double calculate(Map<String, Double> f1, Map<String, Double> f2) {
|
||||||
if (f1 == null || f2 == null) throw new IllegalArgumentException("Feature vectors can't be null");
|
if (f1 == null || f2 == null) {
|
||||||
|
throw new IllegalArgumentException("Feature vectors can't be null");
|
||||||
|
}
|
||||||
|
|
||||||
double sum = 0;
|
double sum = 0;
|
||||||
for (String key : f1.keySet()) {
|
for (String key : f1.keySet()) {
|
||||||
|
@ -55,7 +55,9 @@ public class KMeans {
|
|||||||
// if the assignment does not change, then the algorithm terminates
|
// if the assignment does not change, then the algorithm terminates
|
||||||
boolean shouldTerminate = isLastIteration || clusters.equals(lastState);
|
boolean shouldTerminate = isLastIteration || clusters.equals(lastState);
|
||||||
lastState = clusters;
|
lastState = clusters;
|
||||||
if (shouldTerminate) break;
|
if (shouldTerminate) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
// at the end of each iteration we should relocate the centroids
|
// at the end of each iteration we should relocate the centroids
|
||||||
centroids = relocateCentroids(clusters);
|
centroids = relocateCentroids(clusters);
|
||||||
@ -92,7 +94,9 @@ public class KMeans {
|
|||||||
*/
|
*/
|
||||||
private static Centroid average(Centroid centroid, List<Record> records) {
|
private static Centroid average(Centroid centroid, List<Record> records) {
|
||||||
// if this cluster is empty, then we shouldn't move the centroid
|
// if this cluster is empty, then we shouldn't move the centroid
|
||||||
if (records == null || records.isEmpty()) return centroid;
|
if (records == null || records.isEmpty()) {
|
||||||
|
return centroid;
|
||||||
|
}
|
||||||
|
|
||||||
// Since some records don't have all possible attributes, we initialize
|
// Since some records don't have all possible attributes, we initialize
|
||||||
// average coordinates equal to current centroid coordinates
|
// average coordinates equal to current centroid coordinates
|
||||||
@ -213,12 +217,20 @@ public class KMeans {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static void applyPreconditions(List<Record> records, int k, Distance distance, int maxIterations) {
|
private static void applyPreconditions(List<Record> records, int k, Distance distance, int maxIterations) {
|
||||||
if (records == null || records.isEmpty()) throw new IllegalArgumentException("The dataset can't be empty");
|
if (records == null || records.isEmpty()) {
|
||||||
|
throw new IllegalArgumentException("The dataset can't be empty");
|
||||||
|
}
|
||||||
|
|
||||||
if (k <= 1) throw new IllegalArgumentException("It doesn't make sense to have less than or equal to 1 cluster");
|
if (k <= 1) {
|
||||||
|
throw new IllegalArgumentException("It doesn't make sense to have less than or equal to 1 cluster");
|
||||||
|
}
|
||||||
|
|
||||||
if (distance == null) throw new IllegalArgumentException("The distance calculator is required");
|
if (distance == null) {
|
||||||
|
throw new IllegalArgumentException("The distance calculator is required");
|
||||||
|
}
|
||||||
|
|
||||||
if (maxIterations <= 0) throw new IllegalArgumentException("Max iterations should be a positive number");
|
if (maxIterations <= 0) {
|
||||||
|
throw new IllegalArgumentException("Max iterations should be a positive number");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -48,8 +48,12 @@ public class Record {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean equals(Object o) {
|
public boolean equals(Object o) {
|
||||||
if (this == o) return true;
|
if (this == o) {
|
||||||
if (o == null || getClass() != o.getClass()) return false;
|
return true;
|
||||||
|
}
|
||||||
|
if (o == null || getClass() != o.getClass()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
Record record = (Record) o;
|
Record record = (Record) o;
|
||||||
return Objects.equals(getDescription(), record.getDescription()) && Objects.equals(getFeatures(), record.getFeatures());
|
return Objects.equals(getDescription(), record.getDescription()) && Objects.equals(getFeatures(), record.getFeatures());
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user