Files
ki_rwu_java/test/machine_learning/nearest_neighbour/KNearestNeighbourTest.java
T
2026-11-12 13:52:04 +01:00

165 lines
5.7 KiB
Java
Executable File

package machine_learning.nearest_neighbour;
import machine_learning.DataClass;
import machine_learning.Vector;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.opentest4j.AssertionFailedError;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import static org.junit.jupiter.api.Assertions.assertEquals;
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class KNearestNeighbourTest {
private List<Vector> positives;
private List<Vector> negatives;
private DistanceFunction distanceFunction;
@BeforeAll
void initLearnData() {
this.positives = new ArrayList<>(List.of(
new Vector(8d, 4d),
new Vector(8d, 6d),
new Vector(9d, 2d),
new Vector(9d, 5d))
);
this.negatives = new ArrayList<>(List.of(
new Vector(6d, 1d),
new Vector(7d, 3d),
new Vector(8d, 2d),
new Vector(9d, 0d))
);
this.distanceFunction = (a, b) -> Math.abs(a.get(0) - b.get(0)) + Math.abs(a.get(1) - b.get(1));
}
@Test
public void shouldReturnCorrectClassForVectorWithKEquals3() {
final var kNearestNeighbour = new KNearestNeighbour(this.distanceFunction, 3);
kNearestNeighbour.learn(this.positives, this.negatives);
final var vector = new Vector(8, 3.5);
final var actualClass = kNearestNeighbour.classify(vector);
final var expectedClass = DataClass.NEGATIVE;
assertEquals(expectedClass, actualClass);
}
@Test
public void shouldReturnCorrectClassForVectorWithKEquals5() {
final var kNearestNeighbour = new KNearestNeighbour(this.distanceFunction, 5);
kNearestNeighbour.learn(this.positives, this.negatives);
final var vector = new Vector(8, 3.5);
final var actualClass = kNearestNeighbour.classify(vector);
final var expectedClass = DataClass.POSITIVE;
assertEquals(expectedClass, actualClass);
}
@Test
public void shouldReturnCorrectClassesForAppendicitisData() {
final var trainDataFile = "./resources/app1.data";
final var testDataFile = "./resources/app1.test";
final var trainDataVectors = readFromFile(trainDataFile);
final var dataClasses = splitIntoClasses(trainDataVectors);
final var negatives = dataClasses.get(DataClass.NEGATIVE);
final var positives = dataClasses.get(DataClass.POSITIVE);
final var kNearestNeighbour = new KNearestNeighbour(Vector::distance);
kNearestNeighbour.learn(positives, negatives);
final var testDataVectors = readFromFile(testDataFile);
var failCount = 0;
for (final var vector : testDataVectors) {
final var expectedClass = DataClass.valueOf(Double.valueOf(vector.get(vector.dimension() - 1)).intValue());
final var testVector = vector.decreasedDimension();
final var actualClass = kNearestNeighbour.classify(testVector.normalized());
try {
assertEquals(expectedClass, actualClass);
} catch (final AssertionFailedError e) {
failCount++;
}
}
System.out.println(failCount + " of " + testDataVectors.size() + " are not correct classified.");
System.out.println("Fail rate of " + Math.round(100d * failCount / testDataVectors.size()) + " %");
}
@Disabled("Takes long")
@Test
public void shouldReturnOptimum() {
final var trainDataFile = "./resources/app1.data";
final var testDataFile = "./resources/app1.test";
final var trainDataVectors = readFromFile(trainDataFile);
final var testDataVectors = readFromFile(testDataFile);
final var data = Stream.concat(trainDataVectors.stream(), testDataVectors.stream())
.collect(Collectors.toList());
final var crossValidation = new CrossValidation(1, 100);
final var kNearestNeighbour = crossValidation.validate(data, data.size());
}
private List<Vector> readFromFile(final String file) {
final List<Vector> vectorList = new ArrayList<>();
try (final var reader = new BufferedReader(new FileReader(file))) {
String line;
while ((line = reader.readLine()) != null) {
vectorList.add(new Vector(
Arrays.stream(line.split(","))
.map(Double::valueOf)
.collect(Collectors.toList())
));
}
} catch (final IOException e) {
e.printStackTrace();
}
return vectorList;
}
private Map<DataClass, List<Vector>> splitIntoClasses(final List<Vector> data) {
var positives = data.stream()
.filter(v -> v.get(v.dimension() - 1) == 1)
.collect(Collectors.toList());
var negatives = data.stream()
.filter(v -> v.get(v.dimension() - 1) == 0)
.collect(Collectors.toList());
positives = positives.stream()
.map(Vector::decreasedDimension)
.map(Vector::normalized)
.toList();
negatives = negatives.stream()
.map(Vector::decreasedDimension)
.map(Vector::normalized)
.toList();
return Map.ofEntries(Map.entry(DataClass.NEGATIVE, negatives), Map.entry(DataClass.POSITIVE, positives));
}
}