/
ballseptron.js
59 lines (57 loc) · 1.36 KB
/
ballseptron.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/**
* Ballseptron
*/
export default class Ballseptron {
// A New Perspective on an Old Perceptron Algorithm
// https://www.cs.huji.ac.il/~shais/papers/ShalevSi05.pdf
/**
* @param {number} r Radius
*/
constructor(r) {
this._r = r
this._w = null
this._c = 0
}
/**
* Fit model parameters.
*
* @param {Array<Array<number>>} x Training data
* @param {(1 | -1)[]} y Target values
*/
fit(x, y) {
if (!this._w) {
this._w = Array(x[0].length).fill(0)
}
for (let i = 0; i < x.length; i++) {
const pt = x[i].reduce((s, v, j) => s + v * this._w[j], this._c)
if (pt * y[i] <= 0) {
for (let j = 0; j < x[i].length; j++) {
this._w[j] += y[i] * x[i][j]
}
this._c += y[i]
} else {
const wnorm = Math.sqrt(this._w.reduce((s, v) => s + v ** 2, this._c ** 2))
if ((pt * y[i]) / wnorm < this._r) {
for (let j = 0; j < x[i].length; j++) {
this._w[j] += y[i] * (x[i][j] - (y[i] * this._r * this._w[j]) / wnorm)
}
this._c += y[i] * (1 - (y[i] * this._r * this._c) / wnorm)
}
}
}
}
/**
* Returns predicted values.
*
* @param {Array<Array<number>>} data Sample data
* @returns {(1 | -1)[]} Predicted values
*/
predict(data) {
const p = []
for (let i = 0; i < data.length; i++) {
const m = data[i].reduce((s, v, j) => s + v * this._w[j], this._c)
p.push(m <= 0 ? -1 : 1)
}
return p
}
}