w-cluster 1.0.9 → 1.0.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.jsdoc +1 -1
- package/LICENSE +1 -1
- package/README.md +53 -42
- package/dist/w-cluster.umd.js +2 -2
- package/dist/w-cluster.umd.js.map +1 -1
- package/dist/w-cluster.wk.umd.js +1 -1
- package/docs/WCluster.mjs.html +66 -56
- package/docs/examples/ex-PCA.html +1 -1
- package/docs/examples/{ex-cluster-webworker-k-medoids.html → ex-cluster-webworker.html} +12 -4
- package/docs/examples/{ex-cluster-k-means.html → ex-cluster.html} +11 -3
- package/docs/global.html +11 -6
- package/docs/index.html +1 -1
- package/examples/{ex-cluster-webworker-k-medoids.html → ex-cluster-webworker.html} +11 -3
- package/examples/{ex-cluster-k-means.html → ex-cluster.html} +10 -2
- package/g-PCA-nodeworker.mjs +22 -5
- package/g-PCA.mjs +22 -5
- package/g-cluster-nodeworker.mjs +20 -28
- package/g-cluster.mjs +20 -29
- package/package.json +3 -8
- package/src/WCluster.mjs +65 -55
- package/src/WClusterCore.mjs +14 -9
- package/src/WClusterMat.mjs +4 -3
- package/src/WPCAMat.mjs +33 -11
- package/test/PCA.test.mjs +4 -4
- package/test/cluster.test.mjs +54 -2
- package/docs/examples/ex-cluster-k-medoids.html +0 -138
- package/examples/ex-cluster-k-medoids.html +0 -138
- package/temp-bzmWVlF38ndGv0ip7atIxG1RSDXI39mk-nw.js +0 -1
package/src/WCluster.mjs
CHANGED
|
@@ -11,33 +11,6 @@ import WClusterCore from './WClusterCore.mjs'
|
|
|
11
11
|
* @param {Number} [opt.nCompNIPALS=2] 輸入指定降維的維度整數,不能超過數據的維度(各列有效元素數量),預設2
|
|
12
12
|
* @return {Promise} 回傳Promise,resolve回傳指定維數數值的陣列,reject回傳錯誤訊息
|
|
13
13
|
* @example
|
|
14
|
-
*
|
|
15
|
-
* async function testPCA() {
|
|
16
|
-
*
|
|
17
|
-
* let mat = [
|
|
18
|
-
* [40, 50, 60],
|
|
19
|
-
* [50, 70, 60],
|
|
20
|
-
* [80, 70, 90],
|
|
21
|
-
* [50, 60, 80]
|
|
22
|
-
* ]
|
|
23
|
-
* console.log('mat', mat)
|
|
24
|
-
* // => mat [ [ 40, 50, 60 ], [ 50, 70, 60 ], [ 80, 70, 90 ], [ 50, 60, 80 ] ]
|
|
25
|
-
*
|
|
26
|
-
* let resMat = await WCluster.PCA(mat, { nCompNIPALS: 2 })
|
|
27
|
-
* console.log(resMat)
|
|
28
|
-
* // => [
|
|
29
|
-
* // [ 36.76572825085965, 50.81184119955175, 61.992939427698765 ],
|
|
30
|
-
* // [ 50.520478881706346, 69.86935352609807, 59.679283942385844 ],
|
|
31
|
-
* // [ 77.39502404588043, 70.65388036857387, 91.60517102143689 ],
|
|
32
|
-
* // [ 55.318768821553576, 58.66492490577632, 76.7226056084785 ]
|
|
33
|
-
* // ]
|
|
34
|
-
*
|
|
35
|
-
* }
|
|
36
|
-
* testPCA()
|
|
37
|
-
* .catch((err) => {
|
|
38
|
-
* console.log(err)
|
|
39
|
-
* })
|
|
40
|
-
*
|
|
41
14
|
*/
|
|
42
15
|
async function PCA(data, opt = {}) {
|
|
43
16
|
return WPCAMat(data, opt)
|
|
@@ -53,6 +26,7 @@ async function PCA(data, opt = {}) {
|
|
|
53
26
|
* @param {Number} [opt.nCompNIPALS=2] 輸入指定降維的維度整數,不能超過數據的維度(各列有效元素數量),預設2
|
|
54
27
|
* @param {String} [opt.mode='k-medoids'] 輸入分群方法字串,可為'k-means'、'k-medoids',k-means受初始隨機群中心影響較大,預設'k-medoids'
|
|
55
28
|
* @return {Promise} 回傳Promise,resolve回傳分群後結果物件,keys代表分群有使用到的欄位,ginds代表分群後的指標陣列,gltdt代表分群後的物件陣列,gmat代表分群後且經過PCA處理的數據陣列,其內因是降維後的數據,可例如取最左2欄的數據代表x,y來繪製分群後二維分佈圖,reject回傳錯誤訊息
|
|
29
|
+
* @example
|
|
56
30
|
*/
|
|
57
31
|
async function cluster(data, opt = {}) {
|
|
58
32
|
try {
|
|
@@ -70,6 +44,50 @@ async function cluster(data, opt = {}) {
|
|
|
70
44
|
* @return {Object} 回傳物件,其內有PCA與cluster函式,PCA為主成份分析,cluster為分群分析
|
|
71
45
|
* @example
|
|
72
46
|
*
|
|
47
|
+
* async function testPCA() {
|
|
48
|
+
*
|
|
49
|
+
* let mat = [
|
|
50
|
+
* [40, 50, 60],
|
|
51
|
+
* [50, 70, 60],
|
|
52
|
+
* [80, 70, 90],
|
|
53
|
+
* [50, 60, 80]
|
|
54
|
+
* ]
|
|
55
|
+
* console.log('mat', mat)
|
|
56
|
+
* // => mat [ [ 40, 50, 60 ], [ 50, 70, 60 ], [ 80, 70, 90 ], [ 50, 60, 80 ] ]
|
|
57
|
+
*
|
|
58
|
+
* let resMat = await WCluster.PCA(mat, { nCompNIPALS: 2 })
|
|
59
|
+
* console.log(resMat)
|
|
60
|
+
* // => [
|
|
61
|
+
* // [ -22.27637140016484, -4.7649452046492735 ],
|
|
62
|
+
* // [ -9.12778006150881, 12.303870466192656 ],
|
|
63
|
+
* // [ 31.31672176507748, 0.2183960452169238 ],
|
|
64
|
+
* // [ 0.08742969659617117, -7.757321306760306 ]
|
|
65
|
+
* // ]
|
|
66
|
+
*
|
|
67
|
+
* let mat2 = [
|
|
68
|
+
* [1040, 50, 60],
|
|
69
|
+
* [1050, 70, 60],
|
|
70
|
+
* [1080, 70, 90],
|
|
71
|
+
* [1050, 60, 80]
|
|
72
|
+
* ]
|
|
73
|
+
* console.log('mat2', mat2)
|
|
74
|
+
* // => mat [ [ 1040, 50, 60 ], [ 1050, 70, 60 ], [ 1080, 70, 90 ], [ 1050, 60, 80 ] ]
|
|
75
|
+
*
|
|
76
|
+
* let resMat2 = await WCluster.PCA(mat2, { nCompNIPALS: 2 })
|
|
77
|
+
* console.log(resMat2)
|
|
78
|
+
* // => [
|
|
79
|
+
* // [ -22.27637140016484, -4.7649452046492735 ],
|
|
80
|
+
* // [ -9.12778006150881, 12.303870466192656 ],
|
|
81
|
+
* // [ 31.31672176507748, 0.2183960452169238 ],
|
|
82
|
+
* // [ 0.08742969659617117, -7.757321306760306 ]
|
|
83
|
+
* // ]
|
|
84
|
+
*
|
|
85
|
+
* }
|
|
86
|
+
* testPCA()
|
|
87
|
+
* .catch((err) => {
|
|
88
|
+
* console.log(err)
|
|
89
|
+
* })
|
|
90
|
+
*
|
|
73
91
|
* async function testCluster() {
|
|
74
92
|
* let mode = 'k-medoids'
|
|
75
93
|
*
|
|
@@ -90,24 +108,24 @@ async function cluster(data, opt = {}) {
|
|
|
90
108
|
* // [ 2 ],
|
|
91
109
|
* // [ 0, 1, 3 ]
|
|
92
110
|
* // ],
|
|
93
|
-
* // "
|
|
111
|
+
* // "gmat": [
|
|
94
112
|
* // [
|
|
95
|
-
* // [
|
|
113
|
+
* // [ 31.31672176507748, 0.2183960452169238 ]
|
|
96
114
|
* // ],
|
|
97
115
|
* // [
|
|
98
|
-
* // [
|
|
99
|
-
* // [
|
|
100
|
-
* // [
|
|
116
|
+
* // [ -22.27637140016484, -4.7649452046492735 ],
|
|
117
|
+
* // [ -9.12778006150881, 12.303870466192656 ],
|
|
118
|
+
* // [ 0.08742969659617117, -7.757321306760306 ]
|
|
101
119
|
* // ]
|
|
102
120
|
* // ],
|
|
103
|
-
* // "
|
|
121
|
+
* // "gltdt": [
|
|
104
122
|
* // [
|
|
105
|
-
* // [
|
|
123
|
+
* // [ 80, 70, 90 ]
|
|
106
124
|
* // ],
|
|
107
125
|
* // [
|
|
108
|
-
* // [
|
|
109
|
-
* // [ 50
|
|
110
|
-
* // [
|
|
126
|
+
* // [ 40, 50, 60 ],
|
|
127
|
+
* // [ 50, 70, 60 ],
|
|
128
|
+
* // [ 50, 60, 80 ]
|
|
111
129
|
* // ]
|
|
112
130
|
* // ]
|
|
113
131
|
* // }
|
|
@@ -134,32 +152,24 @@ async function cluster(data, opt = {}) {
|
|
|
134
152
|
* // [ 2 ],
|
|
135
153
|
* // [ 0, 1, 3 ]
|
|
136
154
|
* // ],
|
|
137
|
-
* // "
|
|
155
|
+
* // "gmat": [
|
|
138
156
|
* // [
|
|
139
|
-
* //
|
|
140
|
-
* // "name": "Paul", "a": 80, "b": 70, "c": 90
|
|
141
|
-
* // }
|
|
157
|
+
* // [ 31.31672176507748, 0.2183960452169238 ]
|
|
142
158
|
* // ],
|
|
143
159
|
* // [
|
|
144
|
-
* //
|
|
145
|
-
* //
|
|
146
|
-
* //
|
|
147
|
-
* // {
|
|
148
|
-
* // "name": "Buckley", "a": 50, "b": 70, "c": 60
|
|
149
|
-
* // },
|
|
150
|
-
* // {
|
|
151
|
-
* // "name": "Fawcett", "a": 50, "b": 60, "c": 80
|
|
152
|
-
* // }
|
|
160
|
+
* // [ -22.27637140016484, -4.7649452046492735 ],
|
|
161
|
+
* // [ -9.12778006150881, 12.303870466192656 ],
|
|
162
|
+
* // [ 0.08742969659617117, -7.757321306760306 ]
|
|
153
163
|
* // ]
|
|
154
164
|
* // ],
|
|
155
|
-
* // "
|
|
165
|
+
* // "gltdt": [
|
|
156
166
|
* // [
|
|
157
|
-
* //
|
|
167
|
+
* // { "name": "Paul", "a": 80, "b": 70, "c": 90 }
|
|
158
168
|
* // ],
|
|
159
169
|
* // [
|
|
160
|
-
* //
|
|
161
|
-
* //
|
|
162
|
-
* //
|
|
170
|
+
* // { "name": "Cameron", "a": 40, "b": 50, "c": 60 },
|
|
171
|
+
* // { "name": "Buckley", "a": 50, "b": 70, "c": 60 },
|
|
172
|
+
* // { "name": "Fawcett", "a": 50, "b": 60, "c": 80 }
|
|
163
173
|
* // ]
|
|
164
174
|
* // ]
|
|
165
175
|
* // }
|
package/src/WClusterCore.mjs
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import get from 'lodash/get'
|
|
2
2
|
import each from 'lodash/each'
|
|
3
3
|
import size from 'lodash/size'
|
|
4
|
+
// import take from 'lodash/take'
|
|
4
5
|
import map from 'lodash/map'
|
|
5
6
|
import values from 'lodash/values'
|
|
6
7
|
import dtpick from 'wsemi/src/dtpick.mjs'
|
|
@@ -59,18 +60,15 @@ function WClusterCore(data, opt = {}) {
|
|
|
59
60
|
if (!isnum(kNumber)) {
|
|
60
61
|
kNumber = 2
|
|
61
62
|
}
|
|
62
|
-
|
|
63
|
-
kNumber = cint(kNumber)
|
|
64
|
-
}
|
|
63
|
+
kNumber = cint(kNumber)
|
|
65
64
|
|
|
66
65
|
//nCompNIPALS
|
|
67
66
|
let nCompNIPALS = get(opt, 'nCompNIPALS')
|
|
68
67
|
if (!isnum(nCompNIPALS)) {
|
|
69
68
|
nCompNIPALS = 2
|
|
70
69
|
}
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
}
|
|
70
|
+
nCompNIPALS = cint(nCompNIPALS)
|
|
71
|
+
// console.log('nCompNIPALS', nCompNIPALS)
|
|
74
72
|
|
|
75
73
|
//mode, 由WDataCluster檢核與給予預設值
|
|
76
74
|
let mode = get(opt, 'mode')
|
|
@@ -117,6 +115,13 @@ function WClusterCore(data, opt = {}) {
|
|
|
117
115
|
//WPCAMat
|
|
118
116
|
let pcad = WPCAMat(mat, { nCompNIPALS })
|
|
119
117
|
|
|
118
|
+
// //reduce
|
|
119
|
+
// pcad = map(pcad, (v) => {
|
|
120
|
+
// v = take(v, nCompNIPALS)
|
|
121
|
+
// return v
|
|
122
|
+
// })
|
|
123
|
+
// // console.log('pcad', pcad)
|
|
124
|
+
|
|
120
125
|
//WClusterMat
|
|
121
126
|
let ginds = WClusterMat(pcad, { kNumber, mode })
|
|
122
127
|
|
|
@@ -136,9 +141,9 @@ function WClusterCore(data, opt = {}) {
|
|
|
136
141
|
|
|
137
142
|
return {
|
|
138
143
|
keys: ks,
|
|
139
|
-
ginds,
|
|
140
|
-
|
|
141
|
-
|
|
144
|
+
ginds, //各群所屬樣本指標
|
|
145
|
+
gmat, //各群所屬主成份分析轉換後樣本數據
|
|
146
|
+
gltdt, //各群所屬原始樣本數據
|
|
142
147
|
}
|
|
143
148
|
}
|
|
144
149
|
|
package/src/WClusterMat.mjs
CHANGED
|
@@ -43,11 +43,12 @@ function kMedoids(data, kNumber) {
|
|
|
43
43
|
|
|
44
44
|
function kMeans(data, kNumber) {
|
|
45
45
|
|
|
46
|
-
|
|
46
|
+
//kmeans
|
|
47
47
|
let r = kmeans(data, kNumber)
|
|
48
|
-
//k
|
|
48
|
+
//k: [ 0, 1, 2, 3 ]
|
|
49
49
|
//clusters: [ 0, 0, 1, 0 ]
|
|
50
50
|
|
|
51
|
+
//gs
|
|
51
52
|
let gs = {}
|
|
52
53
|
each(r.clusters, (v, k) => {
|
|
53
54
|
if (!gs[v]) {
|
|
@@ -93,7 +94,7 @@ function WClusterMat(data, opt = {}) {
|
|
|
93
94
|
rs = kMeans(data, kNumber)
|
|
94
95
|
}
|
|
95
96
|
else {
|
|
96
|
-
throw new Error(
|
|
97
|
+
throw new Error(`invalid mode[${mode}]`)
|
|
97
98
|
}
|
|
98
99
|
|
|
99
100
|
return rs
|
package/src/WPCAMat.mjs
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
|
|
2
2
|
import get from 'lodash/get'
|
|
3
3
|
import size from 'lodash/size'
|
|
4
|
-
import isNumber from 'lodash/isNumber'
|
|
5
|
-
import isBoolean from 'lodash/isBoolean'
|
|
6
4
|
import isearr from 'wsemi/src/isearr.mjs'
|
|
5
|
+
import isbol from 'wsemi/src/isbol.mjs'
|
|
6
|
+
import isnum from 'wsemi/src/isnum.mjs'
|
|
7
|
+
import cint from 'wsemi/src/cint.mjs'
|
|
7
8
|
import { PCA } from 'ml-pca'
|
|
8
9
|
|
|
9
10
|
|
|
@@ -16,15 +17,16 @@ function WPCAMat(data, opt = {}) {
|
|
|
16
17
|
|
|
17
18
|
//scale
|
|
18
19
|
let scale = get(opt, 'scale')
|
|
19
|
-
if (!
|
|
20
|
-
scale =
|
|
20
|
+
if (!isbol(scale)) {
|
|
21
|
+
scale = false //true: divide by the standard deviation
|
|
21
22
|
}
|
|
22
23
|
|
|
23
24
|
//nCompNIPALS
|
|
24
25
|
let nCompNIPALS = get(opt, 'nCompNIPALS')
|
|
25
|
-
if (!
|
|
26
|
+
if (!isnum(nCompNIPALS)) {
|
|
26
27
|
nCompNIPALS = 2 //with NIPALS. 指定PCA降維后的特徵數, 也就是降維后的維度
|
|
27
28
|
}
|
|
29
|
+
nCompNIPALS = cint(nCompNIPALS)
|
|
28
30
|
|
|
29
31
|
//n
|
|
30
32
|
let n = size(get(data, 0, []))
|
|
@@ -36,21 +38,41 @@ function WPCAMat(data, opt = {}) {
|
|
|
36
38
|
|
|
37
39
|
//check
|
|
38
40
|
if (nCompNIPALS > n) {
|
|
39
|
-
throw new Error(
|
|
41
|
+
throw new Error(`nCompNIPALS[${nCompNIPALS}] is more than the dimension[${n}] of data`)
|
|
40
42
|
}
|
|
41
43
|
|
|
42
44
|
//pca
|
|
43
45
|
let method = 'NIPALS' //SVD NIPALS
|
|
44
46
|
let pca = new PCA(data, { method, scale, nCompNIPALS })
|
|
47
|
+
// console.log('pca', pca)
|
|
45
48
|
|
|
46
|
-
//
|
|
47
|
-
let
|
|
49
|
+
// //getEigenvalues
|
|
50
|
+
// let eiva = pca.getEigenvalues()
|
|
51
|
+
// // console.log('eiva', eiva)
|
|
52
|
+
|
|
53
|
+
// //getEigenvectors
|
|
54
|
+
// let eivt = pca.getEigenvectors()
|
|
55
|
+
// // console.log('eivt', eivt)
|
|
56
|
+
|
|
57
|
+
// //getCumulativeVariance
|
|
58
|
+
// let cmv = pca.getCumulativeVariance()
|
|
59
|
+
// // console.log('cmv', cmv)
|
|
48
60
|
|
|
49
|
-
//
|
|
50
|
-
let
|
|
61
|
+
//predict
|
|
62
|
+
let tf = pca.predict(data)
|
|
63
|
+
// console.log('tf', tf)
|
|
51
64
|
|
|
52
65
|
//res
|
|
53
|
-
let res =
|
|
66
|
+
let res = tf.toJSON()
|
|
67
|
+
// console.log('res', res)
|
|
68
|
+
|
|
69
|
+
// //invert
|
|
70
|
+
// let itf = pca.invert(tf)
|
|
71
|
+
// console.log('itf', itf)
|
|
72
|
+
|
|
73
|
+
// //res
|
|
74
|
+
// let res = itf.toJSON()
|
|
75
|
+
// // console.log('res', res)
|
|
54
76
|
|
|
55
77
|
return res
|
|
56
78
|
}
|
package/test/PCA.test.mjs
CHANGED
|
@@ -11,10 +11,10 @@ describe(`PCA`, function() {
|
|
|
11
11
|
[50, 60, 80]
|
|
12
12
|
]
|
|
13
13
|
let clmat = [
|
|
14
|
-
[
|
|
15
|
-
[
|
|
16
|
-
[
|
|
17
|
-
[
|
|
14
|
+
[-22.27637140016484, -4.7649452046492735],
|
|
15
|
+
[-9.12778006150881, 12.303870466192656],
|
|
16
|
+
[31.31672176507748, 0.2183960452169238],
|
|
17
|
+
[0.08742969659617117, -7.757321306760306]
|
|
18
18
|
]
|
|
19
19
|
|
|
20
20
|
it(`should return '${JSON.stringify(clmat)}' when input ${JSON.stringify(mat)}, { nCompNIPALS: 2 }`, async function() {
|
package/test/cluster.test.mjs
CHANGED
|
@@ -12,7 +12,33 @@ describe(`cluster`, function() {
|
|
|
12
12
|
[80, 70, 90],
|
|
13
13
|
[50, 60, 80]
|
|
14
14
|
]
|
|
15
|
-
let clmat = {
|
|
15
|
+
let clmat = {
|
|
16
|
+
'keys': null,
|
|
17
|
+
'ginds': [
|
|
18
|
+
[2],
|
|
19
|
+
[0, 1, 3]
|
|
20
|
+
],
|
|
21
|
+
'gmat': [
|
|
22
|
+
[
|
|
23
|
+
[31.31672176507748, 0.2183960452169238]
|
|
24
|
+
],
|
|
25
|
+
[
|
|
26
|
+
[-22.27637140016484, -4.7649452046492735],
|
|
27
|
+
[-9.12778006150881, 12.303870466192656],
|
|
28
|
+
[0.08742969659617117, -7.757321306760306]
|
|
29
|
+
]
|
|
30
|
+
],
|
|
31
|
+
'gltdt': [
|
|
32
|
+
[
|
|
33
|
+
[80, 70, 90]
|
|
34
|
+
],
|
|
35
|
+
[
|
|
36
|
+
[40, 50, 60],
|
|
37
|
+
[50, 70, 60],
|
|
38
|
+
[50, 60, 80]
|
|
39
|
+
]
|
|
40
|
+
]
|
|
41
|
+
}
|
|
16
42
|
|
|
17
43
|
it(`should return '${JSON.stringify(clmat)}' when input ${JSON.stringify(mat)}, { mode, kNumber: 2, nCompNIPALS: 2 }`, async function() {
|
|
18
44
|
let r = await WCluster.cluster(mat, { mode, kNumber: 2, nCompNIPALS: 2 })
|
|
@@ -25,7 +51,33 @@ describe(`cluster`, function() {
|
|
|
25
51
|
{ name: 'Paul', a: 80, b: 70, c: 90 },
|
|
26
52
|
{ name: 'Fawcett', a: 50, b: 60, c: 80 },
|
|
27
53
|
]
|
|
28
|
-
let clltdt = {
|
|
54
|
+
let clltdt = {
|
|
55
|
+
'keys': ['a', 'b', 'c'],
|
|
56
|
+
'ginds': [
|
|
57
|
+
[2],
|
|
58
|
+
[0, 1, 3]
|
|
59
|
+
],
|
|
60
|
+
'gmat': [
|
|
61
|
+
[
|
|
62
|
+
[31.31672176507748, 0.2183960452169238]
|
|
63
|
+
],
|
|
64
|
+
[
|
|
65
|
+
[-22.27637140016484, -4.7649452046492735],
|
|
66
|
+
[-9.12778006150881, 12.303870466192656],
|
|
67
|
+
[0.08742969659617117, -7.757321306760306]
|
|
68
|
+
]
|
|
69
|
+
],
|
|
70
|
+
'gltdt': [
|
|
71
|
+
[
|
|
72
|
+
{ 'name': 'Paul', 'a': 80, 'b': 70, 'c': 90 }
|
|
73
|
+
],
|
|
74
|
+
[
|
|
75
|
+
{ 'name': 'Cameron', 'a': 40, 'b': 50, 'c': 60 },
|
|
76
|
+
{ 'name': 'Buckley', 'a': 50, 'b': 70, 'c': 60 },
|
|
77
|
+
{ 'name': 'Fawcett', 'a': 50, 'b': 60, 'c': 80 }
|
|
78
|
+
]
|
|
79
|
+
]
|
|
80
|
+
}
|
|
29
81
|
|
|
30
82
|
it(`should return '${JSON.stringify(clltdt)}' when input ${JSON.stringify(ltdt)}, { mode, kNumber: 2, nCompNIPALS: 2 }`, async function() {
|
|
31
83
|
let r = await WCluster.cluster(ltdt, { mode, kNumber: 2, nCompNIPALS: 2 })
|
|
@@ -1,138 +0,0 @@
|
|
|
1
|
-
<!DOCTYPE html>
|
|
2
|
-
<html xmlns="http://www.w3.org/1999/xhtml" lang="zh-tw">
|
|
3
|
-
|
|
4
|
-
<head>
|
|
5
|
-
<meta http-equiv="content-type" content="text/html charset=UTF-8" />
|
|
6
|
-
<title>w-cluster cluster for k-medoids</title>
|
|
7
|
-
|
|
8
|
-
<!-- @babel/polyfill已廢棄 -->
|
|
9
|
-
<script nomodule src="https://cdn.jsdelivr.net/npm/@babel/polyfill@7.12.1/dist/polyfill.min.js"></script>
|
|
10
|
-
|
|
11
|
-
<script src="https://cdn.jsdelivr.net/npm/wsemi/dist/wsemi.umd.js"></script>
|
|
12
|
-
|
|
13
|
-
<script src="https://code.highcharts.com/highcharts.js"></script>
|
|
14
|
-
|
|
15
|
-
<script src="https://cdn.jsdelivr.net/npm/w-cluster@1.0.9/dist/w-cluster.umd.js"></script>
|
|
16
|
-
|
|
17
|
-
<script>
|
|
18
|
-
let dataIris=[{"class":"setosa","x1":5.1,"x2":3.5,"x3":1.4,"x4":0.2},{"class":"setosa","x1":4.9,"x2":3,"x3":1.4,"x4":0.2},{"class":"setosa","x1":4.7,"x2":3.2,"x3":1.3,"x4":0.2},{"class":"setosa","x1":4.6,"x2":3.1,"x3":1.5,"x4":0.2},{"class":"setosa","x1":5,"x2":3.6,"x3":1.4,"x4":0.2},{"class":"setosa","x1":5.4,"x2":3.9,"x3":1.7,"x4":0.4},{"class":"setosa","x1":4.6,"x2":3.4,"x3":1.4,"x4":0.3},{"class":"setosa","x1":5,"x2":3.4,"x3":1.5,"x4":0.2},{"class":"setosa","x1":4.4,"x2":2.9,"x3":1.4,"x4":0.2},{"class":"setosa","x1":4.9,"x2":3.1,"x3":1.5,"x4":0.1},{"class":"setosa","x1":5.4,"x2":3.7,"x3":1.5,"x4":0.2},{"class":"setosa","x1":4.8,"x2":3.4,"x3":1.6,"x4":0.2},{"class":"setosa","x1":4.8,"x2":3,"x3":1.4,"x4":0.1},{"class":"setosa","x1":4.3,"x2":3,"x3":1.1,"x4":0.1},{"class":"setosa","x1":5.8,"x2":4,"x3":1.2,"x4":0.2},{"class":"setosa","x1":5.7,"x2":4.4,"x3":1.5,"x4":0.4},{"class":"setosa","x1":5.4,"x2":3.9,"x3":1.3,"x4":0.4},{"class":"setosa","x1":5.1,"x2":3.5,"x3":1.4,"x4":0.3},{"class":"setosa","x1":5.7,"x2":3.8,"x3":1.7,"x4":0.3},{"class":"setosa","x1":5.1,"x2":3.8,"x3":1.5,"x4":0.3},{"class":"setosa","x1":5.4,"x2":3.4,"x3":1.7,"x4":0.2},{"class":"setosa","x1":5.1,"x2":3.7,"x3":1.5,"x4":0.4},{"class":"setosa","x1":4.6,"x2":3.6,"x3":1,"x4":0.2},{"class":"setosa","x1":5.1,"x2":3.3,"x3":1.7,"x4":0.5},{"class":"setosa","x1":4.8,"x2":3.4,"x3":1.9,"x4":0.2},{"class":"setosa","x1":5,"x2":3,"x3":1.6,"x4":0.2},{"class":"setosa","x1":5,"x2":3.4,"x3":1.6,"x4":0.4},{"class":"setosa","x1":5.2,"x2":3.5,"x3":1.5,"x4":0.2},{"class":"setosa","x1":5.2,"x2":3.4,"x3":1.4,"x4":0.2},{"class":"setosa","x1":4.7,"x2":3.2,"x3":1.6,"x4":0.2},{"class":"setosa","x1":4.8,"x2":3.1,"x3":1.6,"x4":0.2},{"class":"setosa","x1":5.4,"x2":3.4,"x3":1.5,"x4":0.4},{"class":"setosa","x1":5.2,"x2":4.1,"x3":1.5,"x4":0.1},{"class":"setosa","x1":5.5,"x2":4.2,"x3":1.4,"x4":0.2},{"class":"setosa","x1":4.9,"x2":3.1,"x3":1.5,"x4":0.2},{"class":"setosa","x1":5,"x2":3.2,"x3":1.2,"x4":0.2},{"class":"setosa","x1":5.5,"x2":3.5,"x3":1.3,"x4":0.2},{"class":"setosa","x1":4.9,"x2":3.6,"x3":1.4,"x4":0.1},{"class":"setosa","x1":4.4,"x2":3,"x3":1.3,"x4":0.2},{"class":"setosa","x1":5.1,"x2":3.4,"x3":1.5,"x4":0.2},{"class":"setosa","x1":5,"x2":3.5,"x3":1.3,"x4":0.3},{"class":"setosa","x1":4.5,"x2":2.3,"x3":1.3,"x4":0.3},{"class":"setosa","x1":4.4,"x2":3.2,"x3":1.3,"x4":0.2},{"class":"setosa","x1":5,"x2":3.5,"x3":1.6,"x4":0.6},{"class":"setosa","x1":5.1,"x2":3.8,"x3":1.9,"x4":0.4},{"class":"setosa","x1":4.8,"x2":3,"x3":1.4,"x4":0.3},{"class":"setosa","x1":5.1,"x2":3.8,"x3":1.6,"x4":0.2},{"class":"setosa","x1":4.6,"x2":3.2,"x3":1.4,"x4":0.2},{"class":"setosa","x1":5.3,"x2":3.7,"x3":1.5,"x4":0.2},{"class":"setosa","x1":5,"x2":3.3,"x3":1.4,"x4":0.2},{"class":"versicolor","x1":7,"x2":3.2,"x3":4.7,"x4":1.4},{"class":"versicolor","x1":6.4,"x2":3.2,"x3":4.5,"x4":1.5},{"class":"versicolor","x1":6.9,"x2":3.1,"x3":4.9,"x4":1.5},{"class":"versicolor","x1":5.5,"x2":2.3,"x3":4,"x4":1.3},{"class":"versicolor","x1":6.5,"x2":2.8,"x3":4.6,"x4":1.5},{"class":"versicolor","x1":5.7,"x2":2.8,"x3":4.5,"x4":1.3},{"class":"versicolor","x1":6.3,"x2":3.3,"x3":4.7,"x4":1.6},{"class":"versicolor","x1":4.9,"x2":2.4,"x3":3.3,"x4":1},{"class":"versicolor","x1":6.6,"x2":2.9,"x3":4.6,"x4":1.3},{"class":"versicolor","x1":5.2,"x2":2.7,"x3":3.9,"x4":1.4},{"class":"versicolor","x1":5,"x2":2,"x3":3.5,"x4":1},{"class":"versicolor","x1":5.9,"x2":3,"x3":4.2,"x4":1.5},{"class":"versicolor","x1":6,"x2":2.2,"x3":4,"x4":1},{"class":"versicolor","x1":6.1,"x2":2.9,"x3":4.7,"x4":1.4},{"class":"versicolor","x1":5.6,"x2":2.9,"x3":3.6,"x4":1.3},{"class":"versicolor","x1":6.7,"x2":3.1,"x3":4.4,"x4":1.4},{"class":"versicolor","x1":5.6,"x2":3,"x3":4.5,"x4":1.5},{"class":"versicolor","x1":5.8,"x2":2.7,"x3":4.1,"x4":1},{"class":"versicolor","x1":6.2,"x2":2.2,"x3":4.5,"x4":1.5},{"class":"versicolor","x1":5.6,"x2":2.5,"x3":3.9,"x4":1.1},{"class":"versicolor","x1":5.9,"x2":3.2,"x3":4.8,"x4":1.8},{"class":"versicolor","x1":6.1,"x2":2.8,"x3":4,"x4":1.3},{"class":"versicolor","x1":6.3,"x2":2.5,"x3":4.9,"x4":1.5},{"class":"versicolor","x1":6.1,"x2":2.8,"x3":4.7,"x4":1.2},{"class":"versicolor","x1":6.4,"x2":2.9,"x3":4.3,"x4":1.3},{"class":"versicolor","x1":6.6,"x2":3,"x3":4.4,"x4":1.4},{"class":"versicolor","x1":6.8,"x2":2.8,"x3":4.8,"x4":1.4},{"class":"versicolor","x1":6.7,"x2":3,"x3":5,"x4":1.7},{"class":"versicolor","x1":6,"x2":2.9,"x3":4.5,"x4":1.5},{"class":"versicolor","x1":5.7,"x2":2.6,"x3":3.5,"x4":1},{"class":"versicolor","x1":5.5,"x2":2.4,"x3":3.8,"x4":1.1},{"class":"versicolor","x1":5.5,"x2":2.4,"x3":3.7,"x4":1},{"class":"versicolor","x1":5.8,"x2":2.7,"x3":3.9,"x4":1.2},{"class":"versicolor","x1":6,"x2":2.7,"x3":5.1,"x4":1.6},{"class":"versicolor","x1":5.4,"x2":3,"x3":4.5,"x4":1.5},{"class":"versicolor","x1":6,"x2":3.4,"x3":4.5,"x4":1.6},{"class":"versicolor","x1":6.7,"x2":3.1,"x3":4.7,"x4":1.5},{"class":"versicolor","x1":6.3,"x2":2.3,"x3":4.4,"x4":1.3},{"class":"versicolor","x1":5.6,"x2":3,"x3":4.1,"x4":1.3},{"class":"versicolor","x1":5.5,"x2":2.5,"x3":4,"x4":1.3},{"class":"versicolor","x1":5.5,"x2":2.6,"x3":4.4,"x4":1.2},{"class":"versicolor","x1":6.1,"x2":3,"x3":4.6,"x4":1.4},{"class":"versicolor","x1":5.8,"x2":2.6,"x3":4,"x4":1.2},{"class":"versicolor","x1":5,"x2":2.3,"x3":3.3,"x4":1},{"class":"versicolor","x1":5.6,"x2":2.7,"x3":4.2,"x4":1.3},{"class":"versicolor","x1":5.7,"x2":3,"x3":4.2,"x4":1.2},{"class":"versicolor","x1":5.7,"x2":2.9,"x3":4.2,"x4":1.3},{"class":"versicolor","x1":6.2,"x2":2.9,"x3":4.3,"x4":1.3},{"class":"versicolor","x1":5.1,"x2":2.5,"x3":3,"x4":1.1},{"class":"versicolor","x1":5.7,"x2":2.8,"x3":4.1,"x4":1.3},{"class":"virginica","x1":6.3,"x2":3.3,"x3":6,"x4":2.5},{"class":"virginica","x1":5.8,"x2":2.7,"x3":5.1,"x4":1.9},{"class":"virginica","x1":7.1,"x2":3,"x3":5.9,"x4":2.1},{"class":"virginica","x1":6.3,"x2":2.9,"x3":5.6,"x4":1.8},{"class":"virginica","x1":6.5,"x2":3,"x3":5.8,"x4":2.2},{"class":"virginica","x1":7.6,"x2":3,"x3":6.6,"x4":2.1},{"class":"virginica","x1":4.9,"x2":2.5,"x3":4.5,"x4":1.7},{"class":"virginica","x1":7.3,"x2":2.9,"x3":6.3,"x4":1.8},{"class":"virginica","x1":6.7,"x2":2.5,"x3":5.8,"x4":1.8},{"class":"virginica","x1":7.2,"x2":3.6,"x3":6.1,"x4":2.5},{"class":"virginica","x1":6.5,"x2":3.2,"x3":5.1,"x4":2},{"class":"virginica","x1":6.4,"x2":2.7,"x3":5.3,"x4":1.9},{"class":"virginica","x1":6.8,"x2":3,"x3":5.5,"x4":2.1},{"class":"virginica","x1":5.7,"x2":2.5,"x3":5,"x4":2},{"class":"virginica","x1":5.8,"x2":2.8,"x3":5.1,"x4":2.4},{"class":"virginica","x1":6.4,"x2":3.2,"x3":5.3,"x4":2.3},{"class":"virginica","x1":6.5,"x2":3,"x3":5.5,"x4":1.8},{"class":"virginica","x1":7.7,"x2":3.8,"x3":6.7,"x4":2.2},{"class":"virginica","x1":7.7,"x2":2.6,"x3":6.9,"x4":2.3},{"class":"virginica","x1":6,"x2":2.2,"x3":5,"x4":1.5},{"class":"virginica","x1":6.9,"x2":3.2,"x3":5.7,"x4":2.3},{"class":"virginica","x1":5.6,"x2":2.8,"x3":4.9,"x4":2},{"class":"virginica","x1":7.7,"x2":2.8,"x3":6.7,"x4":2},{"class":"virginica","x1":6.3,"x2":2.7,"x3":4.9,"x4":1.8},{"class":"virginica","x1":6.7,"x2":3.3,"x3":5.7,"x4":2.1},{"class":"virginica","x1":7.2,"x2":3.2,"x3":6,"x4":1.8},{"class":"virginica","x1":6.2,"x2":2.8,"x3":4.8,"x4":1.8},{"class":"virginica","x1":6.1,"x2":3,"x3":4.9,"x4":1.8},{"class":"virginica","x1":6.4,"x2":2.8,"x3":5.6,"x4":2.1},{"class":"virginica","x1":7.2,"x2":3,"x3":5.8,"x4":1.6},{"class":"virginica","x1":7.4,"x2":2.8,"x3":6.1,"x4":1.9},{"class":"virginica","x1":7.9,"x2":3.8,"x3":6.4,"x4":2},{"class":"virginica","x1":6.4,"x2":2.8,"x3":5.6,"x4":2.2},{"class":"virginica","x1":6.3,"x2":2.8,"x3":5.1,"x4":1.5},{"class":"virginica","x1":6.1,"x2":2.6,"x3":5.6,"x4":1.4},{"class":"virginica","x1":7.7,"x2":3,"x3":6.1,"x4":2.3},{"class":"virginica","x1":6.3,"x2":3.4,"x3":5.6,"x4":2.4},{"class":"virginica","x1":6.4,"x2":3.1,"x3":5.5,"x4":1.8},{"class":"virginica","x1":6,"x2":3,"x3":4.8,"x4":1.8},{"class":"virginica","x1":6.9,"x2":3.1,"x3":5.4,"x4":2.1},{"class":"virginica","x1":6.7,"x2":3.1,"x3":5.6,"x4":2.4},{"class":"virginica","x1":6.9,"x2":3.1,"x3":5.1,"x4":2.3},{"class":"virginica","x1":5.8,"x2":2.7,"x3":5.1,"x4":1.9},{"class":"virginica","x1":6.8,"x2":3.2,"x3":5.9,"x4":2.3},{"class":"virginica","x1":6.7,"x2":3.3,"x3":5.7,"x4":2.5},{"class":"virginica","x1":6.7,"x2":3,"x3":5.2,"x4":2.3},{"class":"virginica","x1":6.3,"x2":2.5,"x3":5,"x4":1.9},{"class":"virginica","x1":6.5,"x2":3,"x3":5.2,"x4":2},{"class":"virginica","x1":6.2,"x2":3.4,"x3":5.4,"x4":2.3},{"class":"virginica","x1":5.9,"x2":3,"x3":5.1,"x4":1.8}]
|
|
19
|
-
</script>
|
|
20
|
-
|
|
21
|
-
</head>
|
|
22
|
-
|
|
23
|
-
<body>
|
|
24
|
-
|
|
25
|
-
<div style="display:flex;">
|
|
26
|
-
<div style="margin-right:5px;">kNumber:</div>
|
|
27
|
-
<select id="kNumber" style="width:50px; margin-right:15px;" onchange="cls()">
|
|
28
|
-
<option value="2">2</option>
|
|
29
|
-
<option value="3" selected>3</option>
|
|
30
|
-
<option value="4">4</option>
|
|
31
|
-
<option value="5">5</option>
|
|
32
|
-
<option value="6">6</option>
|
|
33
|
-
<option value="7">7</option>
|
|
34
|
-
<option value="8">8</option>
|
|
35
|
-
<option value="9">9</option>
|
|
36
|
-
<option value="10">10</option>
|
|
37
|
-
</select>
|
|
38
|
-
<div style="margin-right:5px;">nCompNIPALS:</div>
|
|
39
|
-
<select id="nCompNIPALS" style="width:50px; margin-right:15px;" onchange="cls()">
|
|
40
|
-
<option value="2" selected>2</option>
|
|
41
|
-
<option value="3">3</option>
|
|
42
|
-
<option value="4">4</option>
|
|
43
|
-
</select>
|
|
44
|
-
<div id="message"></div>
|
|
45
|
-
</div>
|
|
46
|
-
|
|
47
|
-
<div id="container" style="width:1000px; height:600px;"></div>
|
|
48
|
-
|
|
49
|
-
<script>
|
|
50
|
-
|
|
51
|
-
let WCluster = window['w-cluster']
|
|
52
|
-
console.log(WCluster)
|
|
53
|
-
|
|
54
|
-
function cls(){
|
|
55
|
-
|
|
56
|
-
let kNumber = wsemi.cint(document.querySelector('#kNumber option:checked').value)
|
|
57
|
-
console.log('kNumber',kNumber)
|
|
58
|
-
|
|
59
|
-
let nCompNIPALS = wsemi.cint(document.querySelector('#nCompNIPALS option:checked').value)
|
|
60
|
-
console.log('nCompNIPALS',nCompNIPALS)
|
|
61
|
-
|
|
62
|
-
document.querySelector('#message').innerHTML = 'analysing...'
|
|
63
|
-
|
|
64
|
-
wsemi.delay(100)
|
|
65
|
-
.then(function (){
|
|
66
|
-
|
|
67
|
-
WCluster.cluster(dataIris, { kNumber: kNumber, nCompNIPALS: nCompNIPALS, mode: 'k-medoids' })
|
|
68
|
-
.then(function(res){
|
|
69
|
-
console.log('res',res)
|
|
70
|
-
|
|
71
|
-
let ds=[]
|
|
72
|
-
for(let g=0;g<res.gmat.length;g++){
|
|
73
|
-
let vs=[]
|
|
74
|
-
for(let j=0;j<res.gmat[g].length;j++){
|
|
75
|
-
vs.push({
|
|
76
|
-
x:res.gmat[g][j][0],
|
|
77
|
-
y:res.gmat[g][j][1],
|
|
78
|
-
label:res.gltdt[g][j].class,
|
|
79
|
-
})
|
|
80
|
-
}
|
|
81
|
-
ds.push({
|
|
82
|
-
name:'group'+(g+1),
|
|
83
|
-
data:vs,
|
|
84
|
-
dataLabels: {
|
|
85
|
-
enabled: true,
|
|
86
|
-
formatter: function() {
|
|
87
|
-
return this.point.label
|
|
88
|
-
}
|
|
89
|
-
},
|
|
90
|
-
})
|
|
91
|
-
|
|
92
|
-
}
|
|
93
|
-
console.log('ds',ds)
|
|
94
|
-
|
|
95
|
-
Highcharts.chart('container', {
|
|
96
|
-
chart: {
|
|
97
|
-
animation: false,
|
|
98
|
-
type: 'scatter',
|
|
99
|
-
},
|
|
100
|
-
plotOptions: {
|
|
101
|
-
scatter: {
|
|
102
|
-
animation: false
|
|
103
|
-
},
|
|
104
|
-
},
|
|
105
|
-
title: {
|
|
106
|
-
text: ''
|
|
107
|
-
},
|
|
108
|
-
xAxis: {
|
|
109
|
-
title: {
|
|
110
|
-
text: ''
|
|
111
|
-
},
|
|
112
|
-
},
|
|
113
|
-
yAxis: {
|
|
114
|
-
title: {
|
|
115
|
-
text: ''
|
|
116
|
-
}
|
|
117
|
-
},
|
|
118
|
-
series: ds,
|
|
119
|
-
})
|
|
120
|
-
|
|
121
|
-
})
|
|
122
|
-
.catch(function(err) {
|
|
123
|
-
console.log(err)
|
|
124
|
-
})
|
|
125
|
-
.finally(function() {
|
|
126
|
-
document.querySelector('#message').innerHTML = 'done'
|
|
127
|
-
})
|
|
128
|
-
|
|
129
|
-
})
|
|
130
|
-
|
|
131
|
-
}
|
|
132
|
-
cls()
|
|
133
|
-
|
|
134
|
-
</script>
|
|
135
|
-
|
|
136
|
-
</body>
|
|
137
|
-
|
|
138
|
-
</html>
|