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 @@ -121,7 +121,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, BRIDGE, 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 |
| 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, k-harmonic 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, BRIDGE, 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, 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 |
Expand Down
1 change: 1 addition & 0 deletions js/model_selector.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ const AIMethods = [
{ value: 'lbg', title: 'Linde-Buzo-Gray' },
{ value: 'pam', title: 'PAM / CLARA' },
{ value: 'clarans', title: 'CLARANS' },
{ value: 'kharmonic', title: 'k-Harmonic Means' },
{ value: 'som', title: 'Self-organizing map' },
{ value: 'neural_gas', title: 'Neural Gas' },
{ value: 'growing_som', title: 'Growing SOM' },
Expand Down
40 changes: 40 additions & 0 deletions js/view/kharmonic.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import KHarmonicMeans from '../../lib/model/kharmonic.js'
import Controller from '../controller.js'

export default function (platform) {
platform.setting.ml.usage = 'Click and add data point. Then, click "Fit" button.'
platform.setting.ml.reference = {
author: 'B. Zhang, M. Hsu, U. Dayal',
title: 'K-Harmonic Means - A Data Clustering Algorithm',
year: 1999,
}
const controller = new Controller(platform)
let model = null

const fitModel = cb => {
if (!model) {
model = new KHarmonicMeans(k.value)
model.init(platform.trainInput)
}
model.fit()
const pred = model.predict(platform.trainInput)
platform.trainResult = pred.map(v => v + 1)
platform.centroids(
model.centroids,
model.centroids.map((c, i) => i + 1),
{
line: true,
}
)
cb && cb()
}

const k = controller.input.number({ label: ' k ', min: 1, max: 1000, value: 3 }).on('change', fitModel)
controller
.stepLoopButtons()
.init(() => {
model = null
})
.step(fitModel)
.epoch()
}
98 changes: 98 additions & 0 deletions lib/model/kharmonic.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/**
* K-Harmonic Means
*/
export default class KHarmonicMeans {
// K-Harmonic Means - A Data Clustering Algorithm
// https://www.hpl.hp.com/techreports/1999/HPL-1999-124.pdf
/**
* @param {number} k Number of clusters
*/
constructor(k) {
this._k = k
this._d = (a, b) => Math.sqrt(a.reduce((s, v, i) => s + (v - b[i]) ** 2, 0))
}

/**
* Centroids
*
* @type {Array<Array<number>>}
*/
get centroids() {
return this._m
}

/**
* Initialize this model.
*
* @param {Array<Array<number>>} datas Training data
*/
init(datas) {
this._x = datas
const n = this._x.length
const idx = []
for (let i = 0; i < this._k; i++) {
idx.push(Math.floor(Math.random() * (n - i)))
}
for (let i = idx.length - 1; i >= 0; i--) {
for (let j = idx.length - 1; j > i; j--) {
if (idx[i] <= idx[j]) {
idx[j]++
}
}
}
this._m = idx.map(v => this._x[v])
}

/**
* Fit model.
*/
fit() {
const n = this._x.length
const q = []
for (let i = 0; i < n; i++) {
const d = this._m.map(m => this._d(this._x[i], m))
const dmin = d.reduce((m, v, k) => (v < m[0] ? [v, k] : m), [Infinity, -1])

q[i] = []
for (let k = 0; k < this._k; k++) {
const normd = k === dmin[1] ? 1 : dmin[0] / d[k]
q[i][k] = (normd ** 3 * dmin[0]) / (1 + (k === dmin[1] ? 0 : normd ** 2)) ** 2
}
}

for (let k = 0; k < this._k; k++) {
let s = 0
const m = Array(this._m[k].length).fill(0)
for (let i = 0; i < n; i++) {
for (let j = 0; j < this._x[i].length; j++) {
m[j] += q[i][k] * this._x[i][j]
}
s += q[i][k]
}
this._m[k] = m.map(v => v / s)
}
}

/**
* Returns predicted categories.
*
* @param {Array<Array<number>>} datas Sample data
* @returns {number[]} Predicted values
*/
predict(datas) {
const p = []
for (let i = 0; i < datas.length; i++) {
let min_d = Infinity
let min_k = -1
for (let k = 0; k < this._m.length; k++) {
const d = this._d(datas[i], this._m[k])
if (d < min_d) {
min_d = d
min_k = k
}
}
p[i] = min_k
}
return p
}
}
44 changes: 44 additions & 0 deletions tests/gui/view/kharmonic.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { getPage } from '../helper/browser'

describe('clustering', () => {
/** @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('CT')
const modelSelectBox = await page.waitForSelector('#ml_selector .model_selection #mlDisp')
await modelSelectBox.selectOption('kharmonic')
const methodMenu = await page.waitForSelector('#ml_selector #method_menu')
const buttons = await methodMenu.waitForSelector('.buttons')

const k = await buttons.waitForSelector('input:nth-of-type(1)')
await expect((await k.getProperty('value')).jsonValue()).resolves.toBe('3')
})

test('learn', async () => {
const taskSelectBox = await page.waitForSelector('#ml_selector dl:first-child dd:nth-child(5) select')
await taskSelectBox.selectOption('CT')
const modelSelectBox = await page.waitForSelector('#ml_selector .model_selection #mlDisp')
await modelSelectBox.selectOption('kharmonic')
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 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')
})
})
28 changes: 28 additions & 0 deletions tests/lib/model/kharmonic.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import Matrix from '../../../lib/util/matrix.js'
import KHarmonicMeans from '../../../lib/model/kharmonic.js'

import { randIndex } from '../../../lib/evaluate/clustering.js'

test('clustering', () => {
const model = new KHarmonicMeans(3)
const n = 20
const x = Matrix.concat(
Matrix.concat(Matrix.randn(n, 2, 0, 0.1), Matrix.randn(n, 2, 5, 0.1)),
Matrix.randn(n, 2, [0, 5], 0.1)
).toArray()

model.init(x)
expect(model.centroids).toHaveLength(3)
for (let i = 0; i < 10; i++) {
model.fit()
}
const y = model.predict(x)
expect(y).toHaveLength(x.length)

const t = []
for (let i = 0; i < x.length; i++) {
t[i] = Math.floor(i / n)
}
const ri = randIndex(y, t)
expect(ri).toBeGreaterThan(0.9)
})