Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ for (let i = 0; i < n; i++) {
| task | model |
| ---- | ----- |
| clustering | (Soft / Kernel / Genetic / Weighted) k-means, k-means++, k-medois, k-medians, x-means, G-means, LBG, ISODATA, Fuzzy c-means, Possibilistic c-means, Agglomerative (complete linkage, single linkage, group average, Ward's, centroid, weighted average, median), DIANA, Monothetic, Mutual kNN, Mean shift, DBSCAN, OPTICS, HDBSCAN, DENCLUE, DBCLASD, CLUES, PAM, CLARA, CLARANS, BIRCH, CURE, ROCK, C2P, PLSA, Latent dirichlet allocation, GMM, VBGMM, Affinity propagation, Spectral clustering, Mountain, (Growing) SOM, GTM, (Growing) Neural gas, Growing cell structures, LVQ, ART, SVC, CAST, CHAMELEON, COLL, CLIQUE, PROCLUS, ORCLUS, FINDIT, NMF, Autoencoder |
| classification | (Fisher's) Linear discriminant, Quadratic discriminant, Mixture discriminant, Least squares, (Multiclass / Kernel) Ridge, (Complement / Negation / Universal-set / Selective) Naive Bayes (gaussian), AODE, (Fuzzy / Weighted) k-nearest neighbor, Radius neighbor, Nearest centroid, ENN, ENaN, NNBCA, ADAMENN, DANN, IKNN, Decision tree, Random forest, Extra trees, GBDT, XGBoost, ALMA, (Aggressive) ROMMA, (Bounded) Online gradient descent, (Budgeted online) Passive aggressive, RLS, (Selective-sampling) Second order perceptron, AROW, NAROW, Confidence weighted, CELLIP, IELLIP, Normal herd, Stoptron, (Kernelized) Pegasos, MIRA, Forgetron, Projectron, Projectron++, Banditron, Ballseptron, (Multiclass) BSGD, ILK, SILK, (Multinomial) Logistic regression, (Multinomial) Probit, Ordered logistic, Ordered probit, PRank, SVM, Gaussian process, HMM, CRF, Bayesian Network, LVQ, (Average / Multiclass / Voted / Kernelized / Selective-sampling / Margin / Shifting / Budget / Tighter / Tightest) Perceptron, PAUM, RBP, ADALINE, MADALINE, MLP, LMNN |
| classification | (Fisher's) Linear discriminant, Quadratic discriminant, Mixture discriminant, Least squares, (Multiclass / Kernel) Ridge, (Complement / Negation / Universal-set / Selective) Naive Bayes (gaussian), AODE, (Fuzzy / Weighted) k-nearest neighbor, Radius neighbor, Nearest centroid, ENN, ENaN, NNBCA, ADAMENN, DANN, IKNN, Decision tree, Random forest, Extra trees, GBDT, XGBoost, ALMA, (Aggressive) ROMMA, (Bounded) Online gradient descent, (Budgeted online) Passive aggressive, RLS, (Selective-sampling) Second order perceptron, AROW, NAROW, Confidence weighted, CELLIP, IELLIP, Normal herd, Stoptron, (Kernelized) Pegasos, MIRA, Forgetron, Projectron, Projectron++, Banditron, Ballseptron, (Multiclass) BSGD, ILK, SILK, (Multinomial) Logistic regression, (Multinomial) Probit, Ordered logistic, Ordered probit, PRank, OAP-BPM, SVM, Gaussian process, HMM, CRF, Bayesian Network, LVQ, (Average / Multiclass / Voted / Kernelized / Selective-sampling / Margin / Shifting / Budget / Tighter / Tightest) Perceptron, PAUM, RBP, ADALINE, MADALINE, MLP, LMNN |
| semi-supervised classification | k-nearest neighbor, Radius neighbor, Label propagation, Label spreading, k-means, GMM, S3VM, Ladder network |
| regression | Least squares, Ridge, Lasso, Elastic net, RLS, Bayesian linear, Poisson, Least absolute deviations, Huber, Tukey, Least trimmed squares, Least median squares, Lp norm linear, SMA, Deming, Segmented, LOWESS, LOESS, spline, Naive Bayes, Gaussian process, Principal components, Partial least squares, Projection pursuit, Quantile regression, k-nearest neighbor, Radius neighbor, IDW, Nadaraya Watson, Priestley Chao, Gasser Muller, RBF Network, RVM, Decision tree, Random forest, Extra trees, GBDT, XGBoost, SVR, MLP, GMR, Isotonic, Ramer Douglas Peucker, Theil-Sen, Passing-Bablok, Repeated median |
| interpolation | Nearest neighbor, IDW, (Spherical) Linear, Brahmagupta, Logarithmic, Cosine, (Inverse) Smoothstep, Cubic, (Centripetal) Catmull-Rom, Hermit, Polynomial, Lagrange, Trigonometric, Spline, RBF Network, Akima, Natural neighbor, Delaunay |
Expand Down
1 change: 1 addition & 0 deletions js/model_selector.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ const AIMethods = [
{ value: 'ordered_logistic', title: 'Ordered logistic regression' },
{ value: 'ordered_probit', title: 'Ordered probit regression' },
{ value: 'prank', title: 'PRank' },
{ value: 'oapbpm', title: 'OAP-BPM' },
],
'': [
{ value: 'least_square', title: 'Least squares' },
Expand Down
39 changes: 39 additions & 0 deletions js/view/oapbpm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import OAPBPM from '../../lib/model/oapbpm.js'
import Controller from '../controller.js'

export default function (platform) {
platform.setting.ml.usage =
'Click and add data point. Next, click "Initialize". Finally, click "Fit" button repeatedly.'
platform.setting.ml.reference = {
author: 'R. F. Harrington',
title: 'Online Ranking/Collaborative filtering using the Perceptron Algorithm',
year: 2003,
}
const controller = new Controller(platform)

let model = null
const fitModel = () => {
if (!model) {
return
}

model.fit(
platform.trainInput,
platform.trainOutput.map(v => v[0])
)
const pred = model.predict(platform.testInput(4))
platform.testResult(pred)
}

const n = controller.input.number({ label: ' N ', value: 10, min: 1, max: 100 })
const tau = controller.input.number({ label: ' Tau ', value: 0.5, min: 0, max: 1, step: 0.1 })
const rate = controller.input.number({ label: ' Learning rate ', value: 0.1, min: 0, max: 100, step: 0.1 })
controller
.stepLoopButtons()
.init(() => {
model = new OAPBPM(n.value, tau.value, rate.value)
platform.init()
})
.step(fitModel)
.epoch()
}
126 changes: 126 additions & 0 deletions lib/model/oapbpm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
/**
* Online Aggregate Prank-Bayes Point Machine
*/
export default class OAPBPM {
// Online Ranking/Collaborative filtering using the Perceptron Algorithm
// https://cdn.aaai.org/ICML/2003/ICML03-035.pdf
/**
* @param {number} n Number of PRank models
* @param {number} tau Probability to learn
* @param {number} [rate=0.1] Learning rate
*/
constructor(n, tau, rate = 0.1) {
this._n = n
this._tau = tau
this._wh = null
this._w = []
this._a = rate

this._bh = [0, Infinity]
this._b = Array.from({ length: n }, () => [0, Infinity])
this._min = 1
}

_update(x, y, k) {
const p = this._w[k].reduce((s, v, i) => s + v * x[i], 0)
let r = 0
for (; r < this._b[k].length; r++) {
if (p - this._b[k][r] < 0) break
}
const yh = r + this._min
if (y === yh) return
let t = 0
for (let i = 0; i < this._b[k].length - 1; i++) {
const yt = y <= i + this._min ? -1 : 1
if ((p - this._b[k][i]) * yt <= 0) {
t += yt
this._b[k][i] -= this._a * yt
}
}
for (let m = 0; m < this._w[k].length; m++) {
this._w[k][m] += this._a * t * x[m]
}
}

/**
* Fit model.
*
* @param {Array<Array<number>>} x Training data
* @param {Array<number>} y Target values
*/
fit(x, y) {
if (!this._wh) {
for (let i = 0; i < this._n; i++) {
this._w[i] = Array(x[0].length).fill(0)
}
}

for (let k = 0; k < x.length; k++) {
if (y[k] < this._min) {
for (let j = 0; j < this._b.length; j++) {
this._b[j].splice(0, 0, ...Array(this._min - y[k]).fill(this._b[j][0]))
}
this._min = y[k]
} else if (y[k] >= this._min + this._b[0].length) {
for (let j = 0; j < this._b.length; j++) {
this._b[j].splice(
this._b[j].length - 1,
0,
...Array(this._min + this._b[j].length - y[k] + 1).fill(this._b[j][this._b[j].length - 2])
)
}
}

for (let j = 0; j < this._n; j++) {
const p = this._w[j].reduce((s, v, i) => s + v * x[k][i], 0)
let r = 0
for (; r < this._b[j].length; r++) {
if (p - this._b[j][r] < 0) break
}
const yh = r + this._min
if (Math.random() < this._tau && y[k] !== yh) {
let t = 0
for (let i = 0; i < this._b[j].length - 1; i++) {
const yt = y[k] <= i + this._min ? -1 : 1
if ((p - this._b[j][i]) * yt <= 0) {
t += yt
this._b[j][i] -= this._a * yt
}
}
for (let m = 0; m < this._w[j].length; m++) {
this._w[j][m] += this._a * t * x[k][m]
}
}
}
}
this._wh = Array(this._w[0].length).fill(0)
this._bh = Array(this._b[0].length).fill(0)
for (let j = 0; j < this._n; j++) {
for (let m = 0; m < this._wh.length; m++) {
this._wh[m] += this._w[j][m] / this._n
}
for (let i = 0; i < this._bh.length; i++) {
this._bh[i] += this._b[j][i] / this._n
}
}
}

/**
* Returns predicted values.
*
* @param {Array<Array<number>>} x Sample data
* @returns {Array<number>} Predicted values
*/
predict(x) {
const p = []
for (let k = 0; k < x.length; k++) {
const v = this._wh.reduce((s, v, i) => s + v * x[k][i], 0)
let r = 0
for (; r < this._bh.length; r++) {
if (v - this._bh[r] < 0) break
}
p[k] = r + this._min
}
return p
}
}
51 changes: 51 additions & 0 deletions tests/gui/view/oapbpm.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { getPage } from '../helper/browser'

describe('classification', () => {
/** @type {Awaited<ReturnType<getPage>>} */
let page
beforeEach(async () => {
page = await getPage()
})

afterEach(async () => {
await page?.close()
})

test('initialize', async () => {
const taskSelectBox = await page.waitForSelector('#ml_selector dl:first-child dd:nth-child(5) select')
await taskSelectBox.selectOption('CF')
const modelSelectBox = await page.waitForSelector('#ml_selector .model_selection #mlDisp')
await modelSelectBox.selectOption('oapbpm')
const methodMenu = await page.waitForSelector('#ml_selector #method_menu')
const buttons = await methodMenu.waitForSelector('.buttons')

const n = await buttons.waitForSelector('input:nth-of-type(1)')
await expect((await n.getProperty('value')).jsonValue()).resolves.toBe('10')
const tau = await buttons.waitForSelector('input:nth-of-type(2)')
await expect((await tau.getProperty('value')).jsonValue()).resolves.toBe('0.5')
const rate = await buttons.waitForSelector('input:nth-of-type(3)')
await expect((await rate.getProperty('value')).jsonValue()).resolves.toBe('0.1')
})

test('learn', async () => {
const taskSelectBox = await page.waitForSelector('#ml_selector dl:first-child dd:nth-child(5) select')
await taskSelectBox.selectOption('CF')
const modelSelectBox = await page.waitForSelector('#ml_selector .model_selection #mlDisp')
await modelSelectBox.selectOption('oapbpm')
const methodMenu = await page.waitForSelector('#ml_selector #method_menu')
const buttons = await methodMenu.waitForSelector('.buttons')

const epoch = await buttons.waitForSelector('[name=epoch]')
await expect(epoch.evaluate(el => el.textContent)).resolves.toBe('0')
const methodFooter = await page.waitForSelector('#method_footer', { state: 'attached' })
await expect(methodFooter.evaluate(el => el.textContent)).resolves.toBe('')

const initButton = await buttons.waitForSelector('input[value=Initialize]')
await initButton.evaluate(el => el.click())
const stepButton = await buttons.waitForSelector('input[value=Step]:enabled')
await stepButton.evaluate(el => el.click())

await expect(epoch.evaluate(el => el.textContent)).resolves.toBe('1')
await expect(methodFooter.evaluate(el => el.textContent)).resolves.toMatch(/^Accuracy:[0-9.]+$/)
})
})
24 changes: 24 additions & 0 deletions tests/lib/model/oapbpm.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import Matrix from '../../../lib/util/matrix.js'
import OAPBPM from '../../../lib/model/oapbpm.js'

import { rmse } from '../../../lib/evaluate/regression.js'

describe('ordinal', () => {
test('fit', () => {
const model = new OAPBPM(10, 0.3)
const x = Matrix.concat(
Matrix.concat(Matrix.randn(50, 2, -5, 0.2), Matrix.randn(50, 2, 0, 0.2)),
Matrix.concat(Matrix.randn(50, 2, 5, 0.2), Matrix.randn(50, 2, 10, 0.2))
).toArray()
const t = []
for (let i = 0; i < x.length; i++) {
t[i] = Math.floor(i / 50)
}
for (let i = 0; i < 100; i++) {
model.fit(x, t)
}
const y = model.predict(x)
const err = rmse(y, t)
expect(err).toBeLessThan(0.5)
})
})