Minor changes in names and extracted method, etc.

This commit is contained in:
2021-11-30 22:27:45 +01:00
parent d0adbf0acb
commit 24c9e0b247
4 changed files with 26 additions and 22 deletions

View File

@ -22,8 +22,9 @@ import static org.junit.jupiter.api.Assertions.*;
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class KNearestNeighbourTest
{
List<Vector> positives;
List<Vector> negatives;
private List<Vector> positives;
private List<Vector> negatives;
private DistanceFunction distanceFunction;
@BeforeAll
void initLearnData()
@ -41,12 +42,14 @@ class KNearestNeighbourTest
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()
{
var kNearestNeighbour = new KNearestNeighbour((a ,b) -> Math.abs(a.get(0) - b.get(0)) + Math.abs(a.get(1) - b.get(1)), 3);
var kNearestNeighbour = new KNearestNeighbour(this.distanceFunction, 3);
kNearestNeighbour.learn(this.positives, this.negatives);
var vector = new Vector(8, 3.5);
@ -59,7 +62,7 @@ class KNearestNeighbourTest
@Test
public void shouldReturnCorrectClassForVectorWithKEquals5()
{
var kNearestNeighbour = new KNearestNeighbour((a ,b) -> Math.abs(a.get(0) - b.get(0)) + Math.abs(a.get(1) - b.get(1)), 5);
var kNearestNeighbour = new KNearestNeighbour(this.distanceFunction, 5);
kNearestNeighbour.learn(this.positives, this.negatives);
var vector = new Vector(8, 3.5);