Reformat code
This commit is contained in:
1 parent
51c12cc0ea
commit
60c654936a
31 files changed
+262
-466
No files matched your search
@@ -18,18 +18,16 @@ import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
|
||||
class KNearestNeighbourTest
|
||||
{
|
||||
class KNearestNeighbourTest {
|
||||
private List<Vector> positives;
|
||||
private List<Vector> negatives;
|
||||
private DistanceFunction distanceFunction;
|
||||
|
||||
@BeforeAll
|
||||
void initLearnData()
|
||||
{
|
||||
void initLearnData() {
|
||||
this.positives = new ArrayList<>(List.of(
|
||||
new Vector(8d, 4d),
|
||||
new Vector(8d, 6d),
|
||||
@@ -48,8 +46,7 @@ class KNearestNeighbourTest
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReturnCorrectClassForVectorWithKEquals3()
|
||||
{
|
||||
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);
|
||||
@@ -61,8 +58,7 @@ class KNearestNeighbourTest
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReturnCorrectClassForVectorWithKEquals5()
|
||||
{
|
||||
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);
|
||||
@@ -74,8 +70,7 @@ class KNearestNeighbourTest
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReturnCorrectClassesForAppendicitisData()
|
||||
{
|
||||
public void shouldReturnCorrectClassesForAppendicitisData() {
|
||||
final var trainDataFile = "./resources/app1.data";
|
||||
final var testDataFile = "./resources/app1.test";
|
||||
|
||||
@@ -91,20 +86,16 @@ class KNearestNeighbourTest
|
||||
final var testDataVectors = readFromFile(testDataFile);
|
||||
var failCount = 0;
|
||||
|
||||
for (final var vector : testDataVectors)
|
||||
{
|
||||
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
|
||||
{
|
||||
try {
|
||||
assertEquals(expectedClass, actualClass);
|
||||
}
|
||||
catch (final AssertionFailedError e)
|
||||
{
|
||||
} catch (final AssertionFailedError e) {
|
||||
failCount++;
|
||||
}
|
||||
}
|
||||
@@ -115,8 +106,7 @@ class KNearestNeighbourTest
|
||||
|
||||
@Disabled("Takes long")
|
||||
@Test
|
||||
public void shouldReturnOptimum()
|
||||
{
|
||||
public void shouldReturnOptimum() {
|
||||
final var trainDataFile = "./resources/app1.data";
|
||||
final var testDataFile = "./resources/app1.test";
|
||||
|
||||
@@ -130,16 +120,13 @@ class KNearestNeighbourTest
|
||||
final var kNearestNeighbour = crossValidation.validate(data, data.size());
|
||||
}
|
||||
|
||||
private List<Vector> readFromFile(final String file)
|
||||
{
|
||||
private List<Vector> readFromFile(final String file) {
|
||||
final List<Vector> vectorList = new ArrayList<>();
|
||||
|
||||
try (final var reader = new BufferedReader(new FileReader(file)))
|
||||
{
|
||||
try (final var reader = new BufferedReader(new FileReader(file))) {
|
||||
String line;
|
||||
|
||||
while ((line = reader.readLine()) != null)
|
||||
{
|
||||
while ((line = reader.readLine()) != null) {
|
||||
vectorList.add(new Vector(
|
||||
Arrays.stream(line.split(","))
|
||||
.map(Double::valueOf)
|
||||
@@ -147,23 +134,20 @@ class KNearestNeighbourTest
|
||||
));
|
||||
}
|
||||
|
||||
}
|
||||
catch (final IOException e)
|
||||
{
|
||||
} catch (final IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return vectorList;
|
||||
}
|
||||
|
||||
private Map<DataClass, List<Vector>> splitIntoClasses(final List<Vector> data)
|
||||
{
|
||||
private Map<DataClass, List<Vector>> splitIntoClasses(final List<Vector> data) {
|
||||
var positives = data.stream()
|
||||
.filter(v -> v.get(v.dimension()-1) == 1)
|
||||
.filter(v -> v.get(v.dimension() - 1) == 1)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
var negatives = data.stream()
|
||||
.filter(v -> v.get(v.dimension()-1) == 0)
|
||||
.filter(v -> v.get(v.dimension() - 1) == 0)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
positives = positives.stream()
|
||||
|
||||
Reference in new issue
Block a user