/
label_propagation.js
104 lines (95 loc) · 2.49 KB
/
label_propagation.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import Matrix from '../util/matrix.js'
/**
* Label propagation
*/
export default class LabelPropagation {
// https://satomacoto.blogspot.com/2012/07/python.html
// https://qiita.com/MasafumiTsuyuki/items/910b85fb14f7f6bf8853
// http://yamaguchiyuto.hatenablog.com/entry/2016/09/22/014202
// https://github.com/scikit-learn/scikit-learn/blob/15a949460/sklearn/semi_supervised/_label_propagation.py
/**
* @param {'rbf' | 'knn'} [method] Method name
* @param {number} [sigma] Sigma of normal distribution
* @param {number} [k] Number of neighborhoods
*/
constructor(method = 'rbf', sigma = 0.1, k = Infinity) {
this._k = k
this._sigma = sigma
this._affinity = method
}
_affinity_matrix(x) {
const n = x.rows
const distances = Matrix.zeros(n, n)
for (let i = 0; i < n; i++) {
for (let j = i + 1; j < n; j++) {
let d = Matrix.sub(x.row(i), x.row(j)).norm()
distances.set(i, j, d)
distances.set(j, i, d)
}
}
const con = Matrix.zeros(n, n)
if (this._k >= n) {
con.fill(1)
} else if (this._k > 0) {
for (let i = 0; i < n; i++) {
const di = distances.row(i).value.map((v, i) => [v, i])
di.sort((a, b) => a[0] - b[0])
for (let j = 1; j < Math.min(this._k + 1, di.length); j++) {
con.set(i, di[j][1], 1)
}
}
con.add(con.t)
con.div(2)
}
if (this._affinity === 'rbf') {
return Matrix.map(distances, (v, i) => (con.at(i) > 0 ? Math.exp(-(v ** 2) / this._sigma ** 2) : 0))
} else if (this._affinity === 'knn') {
return Matrix.map(con, v => (v > 0 ? 1 : 0))
}
}
/**
* Initialize model.
*
* @param {Array<Array<number>>} x Training data
* @param {(* | null)[]} y Target values
*/
init(x, y) {
x = Matrix.fromArray(x)
const n = x.rows
this._y = y
const classes = new Set()
for (let i = 0; i < n; i++) {
if (this._y[i] != null) {
classes.add(this._y[i])
}
}
this._classes = [...classes]
this._w = this._affinity_matrix(x)
this._probs = Matrix.zeros(n, this._classes.length)
for (let i = 0; i < n; i++) {
if (this._y[i] != null) {
this._probs.set(i, this._classes.indexOf(this._y[i]), 1)
}
}
}
/**
* Fit model.
*/
fit() {
const newProb = this._w.dot(this._probs)
newProb.div(newProb.sum(1))
for (let i = 0; i < this._y.length; i++) {
if (this._y[i] == null) {
this._probs.set(i, 0, newProb.row(i))
}
}
}
/**
* Returns predicted categories.
*
* @returns {*[]} Predicted values
*/
predict() {
return this._probs.argmax(1).value.map(v => this._classes[v])
}
}