Skip to content

Commit ee5cbfd

Browse files
authored
Move arguments related to learning from the predict function (#818)
1 parent a369a2e commit ee5cbfd

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

98 files changed

+1475
-678
lines changed

js/view/dann.js

+8-12
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,22 @@
11
import DiscriminantAdaptiveNearestNeighbor from '../../lib/model/dann.js'
2+
import Controller from '../controller.js'
23

3-
var dispDANN = function (elm, platform) {
4+
export default function (platform) {
5+
platform.setting.ml.usage = 'Click and add data point. Then, click "Calculate".'
46
platform.setting.ml.reference = {
57
author: 'T. Hastie, R. Tibshirani',
68
title: 'Discriminant Adaptive Nearest Neighbor Classification',
79
year: 1996,
810
}
11+
const controller = new Controller(platform)
912
const calc = () => {
1013
const ty = platform.trainOutput.map(v => v[0])
11-
const iter = +elm.select('[name=iter]').property('value')
12-
const model = new DiscriminantAdaptiveNearestNeighbor()
14+
const model = new DiscriminantAdaptiveNearestNeighbor(undefined, iteration.value)
1315
model.fit(platform.trainInput, ty)
14-
const categories = model.predict(platform.testInput(10), iter)
16+
const categories = model.predict(platform.testInput(10))
1517
platform.testResult(categories)
1618
}
1719

18-
elm.append('span').text(' iteration ')
19-
elm.append('input').attr('type', 'number').attr('name', 'iter').attr('min', 0).attr('max', 100).attr('value', 1)
20-
elm.append('input').attr('type', 'button').attr('value', 'Calculate').on('click', calc)
21-
}
22-
23-
export default function (platform) {
24-
platform.setting.ml.usage = 'Click and add data point. Then, click "Calculate".'
25-
dispDANN(platform.setting.ml.configElement, platform)
20+
const iteration = controller.input.number({ label: ' iteration ', min: 0, max: 100, value: 1 })
21+
controller.input.button('Calculate').on('click', calc)
2622
}

js/view/diffusion_map.js

+10-23
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,18 @@
11
import DiffusionMap from '../../lib/model/diffusion_map.js'
2+
import Controller from '../controller.js'
23

3-
var dispDM = function (elm, platform) {
4+
export default function (platform) {
5+
platform.setting.ml.usage = 'Click and add data point. Next, click "Fit" button.'
46
platform.setting.ml.reference = {
57
author: 'J. de la Porte, B. M. Herbst, W. Hereman, S. J. van der Walt',
68
title: 'An Introduction to Diffusion Maps',
79
year: 2008,
810
}
9-
elm.append('span')
10-
.text('t')
11-
.append('input')
12-
.attr('type', 'number')
13-
.attr('name', 't')
14-
.attr('value', 2)
15-
.attr('min', 1)
16-
.attr('max', 100)
17-
elm.append('input')
18-
.attr('type', 'button')
19-
.attr('value', 'Fit')
20-
.on('click', () => {
21-
const t = +elm.select('[name=t]').property('value')
22-
const dim = platform.dimension
23-
const y = new DiffusionMap(t).predict(platform.trainInput, dim)
24-
platform.trainResult = y
25-
})
26-
}
27-
28-
export default function (platform) {
29-
platform.setting.ml.usage = 'Click and add data point. Next, click "Fit" button.'
30-
dispDM(platform.setting.ml.configElement, platform)
11+
const controller = new Controller(platform)
12+
const t = controller.input.number({ label: 't', min: 1, max: 100, value: 2 })
13+
controller.input.button('Fit').on('click', () => {
14+
const dim = platform.dimension
15+
const y = new DiffusionMap(t.value, dim).predict(platform.trainInput)
16+
platform.trainResult = y
17+
})
3118
}

js/view/exponential_average.js

+13-32
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,39 @@
11
import { ExponentialMovingAverage, ModifiedMovingAverage } from '../../lib/model/exponential_average.js'
2+
import Controller from '../controller.js'
23

3-
var dispMovingAverage = function (elm, platform) {
4+
export default function (platform) {
5+
platform.setting.ml.usage = 'Click and add data point. Click "Calculate" to update.'
46
platform.setting.ml.reference = {
57
title: 'Moving average (Wikipedia)',
68
url: 'https://en.wikipedia.org/wiki/Moving_average',
79
}
10+
const controller = new Controller(platform)
811
const fitModel = () => {
9-
const method = elm.select('[name=method]').property('value')
10-
const k = +elm.select('[name=k]').property('value')
1112
let model
12-
switch (method) {
13+
switch (method.value) {
1314
case 'exponential':
14-
model = new ExponentialMovingAverage()
15+
model = new ExponentialMovingAverage(k.value)
1516
break
1617
case 'modified':
17-
model = new ModifiedMovingAverage()
18+
model = new ModifiedMovingAverage(k.value)
1819
break
1920
}
2021
const tx = platform.trainInput
2122
const pred = []
2223
for (let i = 0; i < tx.length; pred[i++] = []);
2324
for (let d = 0; d < tx[0].length; d++) {
2425
const xd = tx.map(v => v[d])
25-
const p = model.predict(xd, k)
26+
const p = model.predict(xd)
2627
for (let i = 0; i < pred.length; i++) {
2728
pred[i][d] = p[i]
2829
}
2930
}
3031
platform.trainResult = pred
3132
}
3233

33-
elm.append('select')
34-
.attr('name', 'method')
35-
.on('change', () => {
36-
fitModel()
37-
})
38-
.selectAll('option')
39-
.data(['exponential', 'modified'])
40-
.enter()
41-
.append('option')
42-
.attr('value', d => d)
43-
.text(d => d)
44-
elm.append('span').text('k')
45-
elm.append('input')
46-
.attr('type', 'number')
47-
.attr('name', 'k')
48-
.attr('min', 1)
49-
.attr('max', 100)
50-
.attr('value', 5)
51-
.on('change', fitModel)
52-
elm.append('input').attr('type', 'button').attr('value', 'Calculate').on('click', fitModel)
53-
}
54-
55-
export default function (platform) {
56-
platform.setting.ml.usage = 'Click and add data point. Click "Calculate" to update.'
57-
dispMovingAverage(platform.setting.ml.configElement, platform)
34+
const method = controller.select(['exponential', 'modified']).on('change', () => {
35+
fitModel()
36+
})
37+
const k = controller.input.number({ label: 'k', min: 1, max: 100, value: 5 }).on('change', fitModel)
38+
controller.input.button('Calculate').on('click', fitModel)
5839
}

js/view/fastmap.js

+7-10
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,17 @@
11
import FastMap from '../../lib/model/fastmap.js'
2+
import Controller from '../controller.js'
23

3-
var dispFastMap = function (elm, platform) {
4+
export default function (platform) {
5+
platform.setting.ml.usage = 'Click and add data point. Next, click "Fit" button.'
46
platform.setting.ml.reference = {
57
author: 'C. Faloutsos, KI. Lin',
68
title: 'FastMap: A fast algorithm for indexing, data-mining and visualization of traditional and multimedia datasets',
79
year: 1995,
810
}
9-
const fitModel = () => {
11+
const controller = new Controller(platform)
12+
controller.input.button('Fit').on('click', () => {
1013
const dim = platform.dimension
11-
const pred = new FastMap().predict(platform.trainInput, dim)
14+
const pred = new FastMap(dim).predict(platform.trainInput)
1215
platform.trainResult = pred
13-
}
14-
elm.append('input').attr('type', 'button').attr('value', 'Fit').on('click', fitModel)
15-
}
16-
17-
export default function (platform) {
18-
platform.setting.ml.usage = 'Click and add data point. Next, click "Fit" button.'
19-
dispFastMap(platform.setting.ml.configElement, platform)
16+
})
2017
}

js/view/hlle.js

+7-19
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,15 @@
11
import HLLE from '../../lib/model/hlle.js'
2+
import Controller from '../controller.js'
23

3-
var dispHLLE = function (elm, platform) {
4+
export default function (platform) {
5+
platform.setting.ml.usage = 'Click and add data point. Next, click "Fit" button.'
6+
const controller = new Controller(platform)
47
const fitModel = () => {
5-
const neighbor = +elm.select('[name=neighbor_size]').property('value')
68
const dim = platform.dimension
7-
const y = new HLLE(neighbor).predict(platform.trainInput, dim)
9+
const y = new HLLE(neighbor.value, dim).predict(platform.trainInput)
810
platform.trainResult = y
911
}
1012

11-
elm.append('span')
12-
.text('Select neighbor #')
13-
.append('input')
14-
.attr('type', 'number')
15-
.attr('name', 'neighbor_size')
16-
.attr('value', 20)
17-
.attr('min', 1)
18-
elm.append('input')
19-
.attr('type', 'button')
20-
.attr('value', 'Fit')
21-
.on('click', () => fitModel())
22-
}
23-
24-
export default function (platform) {
25-
platform.setting.ml.usage = 'Click and add data point. Next, click "Fit" button.'
26-
dispHLLE(platform.setting.ml.configElement, platform)
13+
const neighbor = controller.input.number({ label: 'Select neighbor #', min: 1, value: 20 })
14+
controller.input.button('Fit').on('click', () => fitModel())
2715
}

js/view/ica.js

+9-14
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,14 @@
11
import ICA from '../../lib/model/ica.js'
2-
3-
var dispICA = function (elm, platform) {
4-
elm.append('input')
5-
.attr('type', 'button')
6-
.attr('value', 'Fit')
7-
.on('click', () => {
8-
const dim = platform.dimension
9-
const model = new ICA()
10-
model.fit(platform.trainInput)
11-
const y = model.predict(platform.trainInput, dim)
12-
platform.trainResult = y
13-
})
14-
}
2+
import Controller from '../controller.js'
153

164
export default function (platform) {
175
platform.setting.ml.usage = 'Click and add data point. Next, click "Fit" button.'
18-
dispICA(platform.setting.ml.configElement, platform)
6+
const controller = new Controller(platform)
7+
controller.input.button('Fit').on('click', () => {
8+
const dim = platform.dimension
9+
const model = new ICA(dim)
10+
model.fit(platform.trainInput)
11+
const y = model.predict(platform.trainInput)
12+
platform.trainResult = y
13+
})
1914
}

js/view/incremental_pca.js

+8-12
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,19 @@
11
import IncrementalPCA from '../../lib/model/incremental_pca.js'
2+
import Controller from '../controller.js'
23

3-
var dispIPCA = function (elm, platform) {
4+
export default function (platform) {
5+
platform.setting.ml.usage = 'Click and add data point. Next, click "Fit" button.'
46
platform.setting.ml.reference = {
57
author: 'T. Oyama, S. G. Karungaru, S. Tsuge, Y. Mitsukura, M. Fukumi',
68
title: 'Fast Incremental Algorithm of Simple Principal Component Analysis',
79
year: 2009,
810
}
9-
const fitModel = () => {
11+
const controller = new Controller(platform)
12+
controller.input.button('Fit').on('click', () => {
1013
const dim = platform.dimension
11-
const model = new IncrementalPCA()
14+
const model = new IncrementalPCA(undefined, dim)
1215
model.fit(platform.trainInput)
13-
const y = model.predict(platform.trainInput, dim)
16+
const y = model.predict(platform.trainInput)
1417
platform.trainResult = y
15-
}
16-
17-
elm.append('input').attr('type', 'button').attr('value', 'Fit').on('click', fitModel)
18-
}
19-
20-
export default function (platform) {
21-
platform.setting.ml.usage = 'Click and add data point. Next, click "Fit" button.'
22-
dispIPCA(platform.setting.ml.configElement, platform)
18+
})
2319
}

js/view/isomap.js

+8-20
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,19 @@
11
import Isomap from '../../lib/model/isomap.js'
2+
import Controller from '../controller.js'
23

3-
var dispIsomap = function (elm, platform) {
4+
export default function (platform) {
5+
platform.setting.ml.usage = 'Click and add data point. Next, click "Fit" button.'
46
platform.setting.ml.reference = {
57
title: 'Isomap (Wikipedia)',
68
url: 'https://en.wikipedia.org/wiki/Isomap',
79
}
8-
const fitModel = cb => {
9-
const neighbors = +elm.select('[name=neighbors]').property('value')
10+
const controller = new Controller(platform)
11+
const fitModel = () => {
1012
const dim = platform.dimension
11-
const y = new Isomap(neighbors).predict(platform.trainInput, dim)
13+
const y = new Isomap(neighbors.value, dim).predict(platform.trainInput)
1214
platform.trainResult = y
1315
}
1416

15-
elm.append('span').text(' neighbors = ')
16-
elm.append('input')
17-
.attr('type', 'number')
18-
.attr('name', 'neighbors')
19-
.attr('value', 10)
20-
.attr('min', 0)
21-
.attr('max', 10000)
22-
elm.append('input')
23-
.attr('type', 'button')
24-
.attr('value', 'Fit')
25-
.on('click', () => fitModel())
26-
}
27-
28-
export default function (platform) {
29-
platform.setting.ml.usage = 'Click and add data point. Next, click "Fit" button.'
30-
dispIsomap(platform.setting.ml.configElement, platform)
17+
const neighbors = controller.input.number({ label: ' neighbors = ', min: 0, max: 10000, value: 10 })
18+
controller.input.button('Fit').on('click', () => fitModel())
3119
}

js/view/laplacian_eigenmaps.js

+12-36
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,23 @@
11
import LaplacianEigenmaps from '../../lib/model/laplacian_eigenmaps.js'
22
import Controller from '../controller.js'
33

4-
var dispLE = function (elm, platform) {
4+
export default function (platform) {
5+
platform.setting.ml.usage = 'Click and add data point. Next, click "Fit" button.'
56
const controller = new Controller(platform)
67

7-
const method = controller
8-
.select({
9-
values: ['rbf', 'knn'],
10-
name: 'method',
11-
})
12-
.on('change', () => {
13-
const value = method.value
14-
paramSpan.selectAll('*').style('display', 'none')
15-
paramSpan.selectAll(`.${value}`).style('display', 'inline')
16-
})
17-
const paramSpan = elm.append('span')
18-
paramSpan.append('span').classed('rbf', true).text('s =')
19-
paramSpan
20-
.append('input')
21-
.attr('type', 'number')
22-
.attr('name', 'sigma')
23-
.classed('rbf', true)
24-
.attr('min', 0.01)
25-
.attr('max', 100)
26-
.attr('step', 0.01)
27-
.property('value', 1)
28-
const k = controller.input.number({
29-
label: 'k =',
30-
name: 'k_nearest',
31-
min: 1,
32-
max: 100,
33-
value: 10,
8+
const method = controller.select({ values: ['rbf', 'knn'], name: 'method' }).on('change', () => {
9+
const value = method.value
10+
paramSpan.selectAll('*').style('display', 'none')
11+
paramSpan.selectAll(`.${value}`).style('display', 'inline')
3412
})
13+
const paramSpan = controller.span()
14+
paramSpan.element.classList.add('rbf')
15+
const sigma = paramSpan.input.number({ label: 's =', min: 0.01, max: 100, step: 0.01, value: 1 })
16+
const k = controller.input.number({ label: 'k =', name: 'k_nearest', min: 1, max: 100, value: 10 })
3517
controller.input.button('Fit').on('click', () => {
36-
const sigma = +paramSpan.select('[name=sigma]').property('value')
3718
const dim = platform.dimension
38-
const model = new LaplacianEigenmaps(method.value, k.value, sigma)
39-
const pred = model.predict(platform.trainInput, dim)
19+
const model = new LaplacianEigenmaps(dim, method.value, k.value, sigma.value)
20+
const pred = model.predict(platform.trainInput)
4021
platform.trainResult = pred
4122
})
4223
}
43-
44-
export default function (platform) {
45-
platform.setting.ml.usage = 'Click and add data point. Next, click "Fit" button.'
46-
dispLE(platform.setting.ml.configElement, platform)
47-
}

0 commit comments

Comments
 (0)