
Subscribe to this site 

Perceptron
// Perceptron
// see https://towardsdatascience.com/perceptronlearningalgorithmd5db0deab975
def weights = [0, 0, 0] // b, xCoeff, yCoeff such that classifier will become b + xCoeff * x + yCoeff * y == 0
def data = [
[1, 0, 0, false], // bias (start with 1), x, y, classification value
[1, 1, 0, true], // here: training the "or" function. Expected: [1, 1, 1]
[1, 1, 1, true],
[1, 0, 1, true]
]
def weightedSum = { sample >
def sum = 0
weights.size().times { i > sum += weights[i]*sample[i] }
return sum
}
def classifiesCorrectly = { sample > weightedSum(sample) >= 0 == sample.last() }
def sampleThatDoesNotFit = { _ > data.find { sample > ! classifiesCorrectly(sample) } }
def checkAndAdapt = { sample >
def weight = weightedSum(sample)
println "sample: $sample weight: $weight"
if (weight < 0 && sample.last() ) {
weights.size().times { i > weights[i] += sample[i]}
}
if (weight >= 0 && !sample.last() ) {
weights.size().times { i > weights[i] = sample[i]}
}
println "adapted weights $weights"
}
while(todo = sampleThatDoesNotFit() ) {
checkAndAdapt(todo)
}
println weights