Added classify method for perceptron
This commit is contained in:
parent
05dba5bae5
commit
757eb4dd2b
@ -1,6 +1,7 @@
|
||||
package machine_learning.perceptron;
|
||||
|
||||
import machine_learning.Vector;
|
||||
import machine_learning.nearest_neighbour.DataClass;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@ -42,6 +43,11 @@ public class Perceptron
|
||||
System.out.println("-----------------------------------------------------------------");
|
||||
}
|
||||
|
||||
public DataClass classify(Vector vector)
|
||||
{
|
||||
return this.weight.scalar(vector) > 0 ? DataClass.POSITIVE : DataClass.NEGATIVE;
|
||||
}
|
||||
|
||||
private Vector getInitializationVector(List<Vector> positives, List<Vector> negatives)
|
||||
{
|
||||
var a = new Vector(positives.get(0).dimension());
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user