Make variables final where possible
This commit is contained in:
1 parent
e1c93881af
commit
51c12cc0ea
15 files changed
+148
-146
No files matched your search
@@ -50,12 +50,12 @@ class KNearestNeighbourTest
|
||||
@Test
|
||||
public void shouldReturnCorrectClassForVectorWithKEquals3()
|
||||
{
|
||||
var kNearestNeighbour = new KNearestNeighbour(this.distanceFunction, 3);
|
||||
final var kNearestNeighbour = new KNearestNeighbour(this.distanceFunction, 3);
|
||||
kNearestNeighbour.learn(this.positives, this.negatives);
|
||||
var vector = new Vector(8, 3.5);
|
||||
final var vector = new Vector(8, 3.5);
|
||||
|
||||
var actualClass = kNearestNeighbour.classify(vector);
|
||||
var expectedClass = DataClass.NEGATIVE;
|
||||
final var actualClass = kNearestNeighbour.classify(vector);
|
||||
final var expectedClass = DataClass.NEGATIVE;
|
||||
|
||||
assertEquals(expectedClass, actualClass);
|
||||
}
|
||||
@@ -63,12 +63,12 @@ class KNearestNeighbourTest
|
||||
@Test
|
||||
public void shouldReturnCorrectClassForVectorWithKEquals5()
|
||||
{
|
||||
var kNearestNeighbour = new KNearestNeighbour(this.distanceFunction, 5);
|
||||
final var kNearestNeighbour = new KNearestNeighbour(this.distanceFunction, 5);
|
||||
kNearestNeighbour.learn(this.positives, this.negatives);
|
||||
var vector = new Vector(8, 3.5);
|
||||
final var vector = new Vector(8, 3.5);
|
||||
|
||||
var actualClass = kNearestNeighbour.classify(vector);
|
||||
var expectedClass = DataClass.POSITIVE;
|
||||
final var actualClass = kNearestNeighbour.classify(vector);
|
||||
final var expectedClass = DataClass.POSITIVE;
|
||||
|
||||
assertEquals(expectedClass, actualClass);
|
||||
}
|
||||
@@ -76,34 +76,34 @@ class KNearestNeighbourTest
|
||||
@Test
|
||||
public void shouldReturnCorrectClassesForAppendicitisData()
|
||||
{
|
||||
var trainDataFile = "./resources/app1.data";
|
||||
var testDataFile = "./resources/app1.test";
|
||||
final var trainDataFile = "./resources/app1.data";
|
||||
final var testDataFile = "./resources/app1.test";
|
||||
|
||||
var trainDataVectors = readFromFile(trainDataFile);
|
||||
final var trainDataVectors = readFromFile(trainDataFile);
|
||||
|
||||
var dataClasses = splitIntoClasses(trainDataVectors);
|
||||
var negatives = dataClasses.get(DataClass.NEGATIVE);
|
||||
var positives = dataClasses.get(DataClass.POSITIVE);
|
||||
final var dataClasses = splitIntoClasses(trainDataVectors);
|
||||
final var negatives = dataClasses.get(DataClass.NEGATIVE);
|
||||
final var positives = dataClasses.get(DataClass.POSITIVE);
|
||||
|
||||
var kNearestNeighbour = new KNearestNeighbour(Vector::distance);
|
||||
final var kNearestNeighbour = new KNearestNeighbour(Vector::distance);
|
||||
kNearestNeighbour.learn(positives, negatives);
|
||||
|
||||
var testDataVectors = readFromFile(testDataFile);
|
||||
final var testDataVectors = readFromFile(testDataFile);
|
||||
var failCount = 0;
|
||||
|
||||
for (var vector : testDataVectors)
|
||||
for (final var vector : testDataVectors)
|
||||
{
|
||||
var expectedClass = DataClass.valueOf(Double.valueOf(vector.get(vector.dimension() - 1)).intValue());
|
||||
final var expectedClass = DataClass.valueOf(Double.valueOf(vector.get(vector.dimension() - 1)).intValue());
|
||||
|
||||
var testVector = vector.decreasedDimension();
|
||||
final var testVector = vector.decreasedDimension();
|
||||
|
||||
var actualClass = kNearestNeighbour.classify(testVector.normalized());
|
||||
final var actualClass = kNearestNeighbour.classify(testVector.normalized());
|
||||
|
||||
try
|
||||
{
|
||||
assertEquals(expectedClass, actualClass);
|
||||
}
|
||||
catch (AssertionFailedError e)
|
||||
catch (final AssertionFailedError e)
|
||||
{
|
||||
failCount++;
|
||||
}
|
||||
@@ -111,31 +111,30 @@ class KNearestNeighbourTest
|
||||
|
||||
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()
|
||||
{
|
||||
var trainDataFile = "./resources/app1.data";
|
||||
var testDataFile = "./resources/app1.test";
|
||||
final var trainDataFile = "./resources/app1.data";
|
||||
final var testDataFile = "./resources/app1.test";
|
||||
|
||||
var trainDataVectors = readFromFile(trainDataFile);
|
||||
var testDataVectors = readFromFile(testDataFile);
|
||||
var data = Stream.concat(trainDataVectors.stream(), testDataVectors.stream())
|
||||
final var trainDataVectors = readFromFile(trainDataFile);
|
||||
final var testDataVectors = readFromFile(testDataFile);
|
||||
final var data = Stream.concat(trainDataVectors.stream(), testDataVectors.stream())
|
||||
.collect(Collectors.toList());
|
||||
|
||||
var crossValidation = new CrossValidation(1, 100);
|
||||
final var crossValidation = new CrossValidation(1, 100);
|
||||
|
||||
var kNearestNeighbour = crossValidation.validate(data, data.size());
|
||||
final var kNearestNeighbour = crossValidation.validate(data, data.size());
|
||||
}
|
||||
|
||||
private List<Vector> readFromFile(String file)
|
||||
private List<Vector> readFromFile(final String file)
|
||||
{
|
||||
List<Vector> vectorList = new ArrayList<>();
|
||||
final List<Vector> vectorList = new ArrayList<>();
|
||||
|
||||
try (var reader = new BufferedReader(new FileReader(file)))
|
||||
try (final var reader = new BufferedReader(new FileReader(file)))
|
||||
{
|
||||
String line;
|
||||
|
||||
@@ -149,7 +148,7 @@ class KNearestNeighbourTest
|
||||
}
|
||||
|
||||
}
|
||||
catch (IOException e)
|
||||
catch (final IOException e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
@@ -157,7 +156,7 @@ class KNearestNeighbourTest
|
||||
return vectorList;
|
||||
}
|
||||
|
||||
private Map<DataClass, List<Vector>> splitIntoClasses(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)
|
||||
@@ -170,12 +169,12 @@ class KNearestNeighbourTest
|
||||
positives = positives.stream()
|
||||
.map(Vector::decreasedDimension)
|
||||
.map(Vector::normalized)
|
||||
.collect(Collectors.toList());
|
||||
.toList();
|
||||
|
||||
negatives = negatives.stream()
|
||||
.map(Vector::decreasedDimension)
|
||||
.map(Vector::normalized)
|
||||
.collect(Collectors.toList());
|
||||
.toList();
|
||||
|
||||
return Map.ofEntries(Map.entry(DataClass.NEGATIVE, negatives), Map.entry(DataClass.POSITIVE, positives));
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user