Perceptron (unfinished)

This commit is contained in:
Niklas Birk
2019-06-24 00:23:19 +02:00
parent 5f9776c4ef
commit c44e53707d
4 changed files with 69 additions and 12 deletions

View File

@ -15,7 +15,7 @@ public class Perceptron
{
if (weight.scalar(x) <= 0)
{
weight = weight.add(x);
weight = weight.add(x.divide(x.euclid()));
}
}
@ -59,7 +59,7 @@ public class Perceptron
for (var x : vectors)
{
actualClass = scalarForThreshholdPerceptron(weight, x) > weight.get(weight.dimension()-1) ? 1 : 0;
actualClass = weight.scalar(x) > 0 ? 1 : 0;
if (actualClass != expectedClass)
{
@ -70,7 +70,7 @@ public class Perceptron
return true;
}
private double scalarForThreshholdPerceptron(Vector a, Vector b)
private double scalarForThresholdPerceptron(Vector a, Vector b)
{
return IntStream.range(0,
a.dimension()-1)

View File

@ -34,8 +34,8 @@ public class Vector
{
return new Vector(IntStream.range(0,
this.dimension())
.mapToObj(i -> this.values.get(i) + b.values.get(i))
.collect(Collectors.toCollection(ArrayList::new))
.mapToObj(i -> this.get(i) + b.get(i))
.collect(Collectors.toList())
);
}
@ -44,8 +44,8 @@ public class Vector
{
return new Vector(IntStream.range(0,
this.dimension())
.mapToObj(i -> this.values.get(i) - b.values.get(i))
.collect(Collectors.toCollection(ArrayList::new))
.mapToObj(i -> this.get(i) - b.get(i))
.collect(Collectors.toList())
);
}
@ -53,10 +53,30 @@ public class Vector
{
return IntStream.range(0,
this.dimension())
.mapToDouble(i -> this.values.get(i) * b.values.get(i))
.mapToDouble(i -> this.get(i) * b.get(i))
.sum();
}
public double euclid()
{
return Math.sqrt(IntStream.range(0,
this.dimension())
.mapToDouble(i -> this.get(i) * this.get(i))
.sum());
}
public Vector divide(double div)
{
var divided = new ArrayList<Double>();
for (int i = 0; i < this.dimension(); i++)
{
divided.add(this.values.get(i) / div);
}
return new Vector(divided);
}
public double get(int index)
{
return this.values.get(index);
@ -88,8 +108,6 @@ public class Vector
@Override
public String toString()
{
return values.toString()
.replace("[", "(")
.replace("]", ")");
return this.values.toString();
}
}