Added classify method for perceptron
This commit is contained in:
@ -1,6 +1,8 @@
|
||||
package machine_learning.perceptron;
|
||||
|
||||
import machine_learning.Vector;
|
||||
import machine_learning.nearest_neighbour.DataClass;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.TestInstance;
|
||||
@ -8,11 +10,14 @@ import org.junit.jupiter.api.TestInstance;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
|
||||
class PerceptronTest
|
||||
{
|
||||
List<Vector> positives;
|
||||
List<Vector> negatives;
|
||||
Perceptron perceptron;
|
||||
|
||||
@BeforeAll
|
||||
void initLearnData()
|
||||
@ -31,11 +36,30 @@ class PerceptronTest
|
||||
new Vector(8d, 2d, biasUnit),
|
||||
new Vector(9d, 0d, biasUnit))
|
||||
);
|
||||
|
||||
this.perceptron = new Perceptron();
|
||||
this.perceptron.learn(this.positives, this.negatives);
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldClassifyCorrect()
|
||||
void shouldClassifyVectorCorrectAsNegative()
|
||||
{
|
||||
new Perceptron().learn(this.positives, this.negatives);
|
||||
var vector = new Vector(0d, 0d, 1d);
|
||||
|
||||
var actualClass = this.perceptron.classify(vector);
|
||||
var expectedClass = DataClass.NEGATIVE;
|
||||
|
||||
assertEquals(expectedClass, actualClass);
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldClassifyVectorCorrectAsPositive()
|
||||
{
|
||||
var vector = new Vector(9d, 3d, 1d);
|
||||
|
||||
var actualClass = this.perceptron.classify(vector);
|
||||
var expectedClass = DataClass.POSITIVE;
|
||||
|
||||
assertEquals(expectedClass, actualClass);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user