Reformat code
This commit is contained in:
1 parent
51c12cc0ea
commit
60c654936a
31 files changed
+262
-466
No files matched your search
@@ -2,14 +2,12 @@ package machine_learning;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
class VectorTest
|
||||
{
|
||||
class VectorTest {
|
||||
|
||||
@Test
|
||||
void shouldInitializeZeroVector()
|
||||
{
|
||||
void shouldInitializeZeroVector() {
|
||||
final var v = new Vector(3);
|
||||
|
||||
final var expected = new Vector(0d, 0d, 0d);
|
||||
@@ -19,8 +17,7 @@ class VectorTest
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldReturnCorrectVectorWhenAdding()
|
||||
{
|
||||
void shouldReturnCorrectVectorWhenAdding() {
|
||||
final var v1 = new Vector(1d, 2d);
|
||||
final var v2 = new Vector(3d, 4d);
|
||||
|
||||
@@ -31,8 +28,7 @@ class VectorTest
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldReturnCorrectVectorWhenSubtracting()
|
||||
{
|
||||
void shouldReturnCorrectVectorWhenSubtracting() {
|
||||
final var v1 = new Vector(1d, 2d);
|
||||
final var v2 = new Vector(3d, 4d);
|
||||
|
||||
@@ -43,8 +39,7 @@ class VectorTest
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldReturnCorrectVectorWhenScalarMultiplying()
|
||||
{
|
||||
void shouldReturnCorrectVectorWhenScalarMultiplying() {
|
||||
final var v1 = new Vector(1d, 2d);
|
||||
final var v2 = new Vector(3d, 4d);
|
||||
|
||||
@@ -55,8 +50,7 @@ class VectorTest
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldReturnCorrectVectorWhenEuclid()
|
||||
{
|
||||
void shouldReturnCorrectVectorWhenEuclid() {
|
||||
final var v1 = new Vector(1d, 2d);
|
||||
|
||||
final var actual = v1.euclid();
|
||||
@@ -66,8 +60,7 @@ class VectorTest
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldReturnCorrectDistance()
|
||||
{
|
||||
void shouldReturnCorrectDistance() {
|
||||
final var v1 = new Vector(1d, 2d);
|
||||
final var v2 = new Vector(3d, 4d);
|
||||
|
||||
@@ -78,8 +71,7 @@ class VectorTest
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldReturnCorrectVectorWhenDividing()
|
||||
{
|
||||
void shouldReturnCorrectVectorWhenDividing() {
|
||||
final var v1 = new Vector(1d, 2d);
|
||||
final var div = 2d;
|
||||
|
||||
@@ -90,12 +82,11 @@ class VectorTest
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldDecreaseDimensionCorrect()
|
||||
{
|
||||
void shouldDecreaseDimensionCorrect() {
|
||||
final var v = new Vector(1d, 2d, 3d, 4d);
|
||||
|
||||
|
||||
final var decreasedDimensionVector = v.decreasedDimension();
|
||||
|
||||
|
||||
final var actual = decreasedDimensionVector.dimension();
|
||||
final var expected = 3;
|
||||
|
||||
@@ -103,8 +94,7 @@ class VectorTest
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldNormalizeCorrect()
|
||||
{
|
||||
void shouldNormalizeCorrect() {
|
||||
final var v = new Vector(4d, 4d, 4d, 4d);
|
||||
|
||||
final var actual = v.normalized();
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -2,21 +2,22 @@ package machine_learning.perceptron;
|
||||
|
||||
import machine_learning.DataClass;
|
||||
import machine_learning.Vector;
|
||||
import org.junit.jupiter.api.*;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.TestInstance;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
|
||||
class PerceptronTest
|
||||
{
|
||||
class PerceptronTest {
|
||||
List<Vector> positives;
|
||||
List<Vector> negatives;
|
||||
Perceptron perceptron;
|
||||
|
||||
@BeforeAll
|
||||
void initLearnData()
|
||||
{
|
||||
void initLearnData() {
|
||||
final double biasUnit = 1d;
|
||||
this.positives = new ArrayList<>(List.of(
|
||||
new Vector(8d, 4d, biasUnit),
|
||||
@@ -37,8 +38,7 @@ class PerceptronTest
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldClassifyVectorCorrectAsNegative()
|
||||
{
|
||||
void shouldClassifyVectorCorrectAsNegative() {
|
||||
final var vector = new Vector(0d, 0d, 1d);
|
||||
|
||||
final var actualClass = this.perceptron.classify(vector);
|
||||
@@ -48,8 +48,7 @@ class PerceptronTest
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldClassifyVectorCorrectAsPositive()
|
||||
{
|
||||
void shouldClassifyVectorCorrectAsPositive() {
|
||||
final var vector = new Vector(9d, 3d, 1d);
|
||||
|
||||
final var actualClass = this.perceptron.classify(vector);
|
||||
|
||||
Reference in new issue
Block a user