Added classify method for perceptron
This commit is contained in:
parent
05dba5bae5
commit
757eb4dd2b
@ -1,6 +1,7 @@
|
|||||||
package machine_learning.perceptron;
|
package machine_learning.perceptron;
|
||||||
|
|
||||||
import machine_learning.Vector;
|
import machine_learning.Vector;
|
||||||
|
import machine_learning.nearest_neighbour.DataClass;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@ -42,6 +43,11 @@ public class Perceptron
|
|||||||
System.out.println("-----------------------------------------------------------------");
|
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)
|
private Vector getInitializationVector(List<Vector> positives, List<Vector> negatives)
|
||||||
{
|
{
|
||||||
var a = new Vector(positives.get(0).dimension());
|
var a = new Vector(positives.get(0).dimension());
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
package machine_learning.perceptron;
|
package machine_learning.perceptron;
|
||||||
|
|
||||||
import machine_learning.Vector;
|
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.BeforeAll;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.junit.jupiter.api.TestInstance;
|
import org.junit.jupiter.api.TestInstance;
|
||||||
@ -8,11 +10,14 @@ import org.junit.jupiter.api.TestInstance;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
|
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
|
||||||
class PerceptronTest
|
class PerceptronTest
|
||||||
{
|
{
|
||||||
List<Vector> positives;
|
List<Vector> positives;
|
||||||
List<Vector> negatives;
|
List<Vector> negatives;
|
||||||
|
Perceptron perceptron;
|
||||||
|
|
||||||
@BeforeAll
|
@BeforeAll
|
||||||
void initLearnData()
|
void initLearnData()
|
||||||
@ -31,11 +36,30 @@ class PerceptronTest
|
|||||||
new Vector(8d, 2d, biasUnit),
|
new Vector(8d, 2d, biasUnit),
|
||||||
new Vector(9d, 0d, biasUnit))
|
new Vector(9d, 0d, biasUnit))
|
||||||
);
|
);
|
||||||
|
|
||||||
|
this.perceptron = new Perceptron();
|
||||||
|
this.perceptron.learn(this.positives, this.negatives);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@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