🌐 AI搜索 & 代理 主页
Skip to content

Commit 22aedb2

Browse files
authored
Merge pull request #22 from dcrescim/vs-code
KMeans implementation with Code Restructuring
2 parents c43bfd9 + b84ff16 commit 22aedb2

Some content is hidden

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

51 files changed

+2437
-8
lines changed

package.json

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,14 @@
77
"doc": "docs"
88
},
99
"scripts": {
10-
"test:clean": "cd scikitjs-node && yarn test && cd ../scikitjs-browser && yarn test",
11-
"build": "cd scikitjs-node && yarn build && cd ../scikitjs-browser && yarn build",
10+
"test:clean": "yarn test:node && yarn test:browser",
11+
"test:browser": "cd scikitjs-browser && yarn test",
12+
"test:node": "cd scikitjs-node && yarn test",
13+
"build:browser": "cd scikitjs-browser && yarn build",
14+
"build:node": "cd scikitjs-node && yarn build",
15+
"build": "yarn build:node && yarn build:browser",
1216
"build:docs": "cd docs && yarn build",
13-
"postinstall": "(cd docs && yarn install); (cd scikitjs-browser && yarn install); (cd scikitjs-node && yarn install)"
17+
"postinstall": "(cd docs && yarn install); (cd scikitjs-browser && yarn install); (cd scikitjs-node && yarn install); (cd shared && yarn install)"
1418
},
1519
"repository": {
1620
"type": "git",

scikitjs-browser/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "scikitjs",
3-
"version": "0.0.1",
3+
"version": "0.0.2",
44
"description": "JavaScript package for predictive data analysis and machine learning.",
55
"main": "dist/index.js",
66
"types": "dist/shared/index.d.ts",
@@ -16,7 +16,7 @@
1616
"coveralls": "cat ./coverage/lcov.info | ./node_modules/.bin/coveralls",
1717
"coverage": "nyc report --reporter=text-lcov | coveralls && nyc report --reporter=lcov",
1818
"patch": "npm version patch",
19-
"copy:shared": "cp -r ../shared src/shared",
19+
"copy:shared": "cp -r ../shared/lib src && mv src/lib src/shared",
2020
"delete:shared": "rm -r src/shared",
2121
"test:src": "nyc mocha --require ts-node/register 'src/**/*.test.ts'",
2222
"clean": "rimraf ./dist && rimraf ./src/shared"

scikitjs-node/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "scikitjs-node",
3-
"version": "0.0.1",
3+
"version": "0.0.2",
44
"description": "JavaScript package for predictive data analysis and machine learning.",
55
"main": "dist/shared/index.js",
66
"types": "dist/shared/index.d.ts",
@@ -16,7 +16,7 @@
1616
"coveralls": "cat ./coverage/lcov.info | ./node_modules/.bin/coveralls",
1717
"coverage": "nyc report --reporter=text-lcov | coveralls && nyc report --reporter=lcov",
1818
"patch": "npm version patch",
19-
"copy:shared": "cp -r ../shared src",
19+
"copy:shared": "cp -r ../shared/lib src && mv src/lib src/shared",
2020
"delete:shared": "rm -r src/shared",
2121
"test:src": "nyc mocha --require ts-node/register 'src/**/*.test.ts'",
2222
"clean": "rimraf ./dist && rimraf ./src/shared"

shared/.gitignore

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
yarn-debug.log*
6+
yarn-error.log*
7+
lerna-debug.log*
8+
9+
# Diagnostic reports (https://nodejs.org/api/report.html)
10+
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
11+
12+
# Runtime data
13+
pids
14+
*.pid
15+
*.seed
16+
*.pid.lock
17+
18+
# Directory for instrumented libs generated by jscoverage/JSCover
19+
lib-cov
20+
21+
# Coverage directory used by tools like istanbul
22+
coverage
23+
*.lcov
24+
25+
# nyc test coverage
26+
.nyc_output
27+
28+
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
29+
.grunt
30+
31+
# Bower dependency directory (https://bower.io/)
32+
bower_components
33+
34+
# node-waf configuration
35+
.lock-wscript
36+
37+
# Compiled binary addons (https://nodejs.org/api/addons.html)
38+
build/Release
39+
40+
# Dependency directories
41+
node_modules/
42+
jspm_packages/
43+
44+
# TypeScript v1 declaration files
45+
typings/
46+
47+
# TypeScript cache
48+
*.tsbuildinfo
49+
50+
# Optional npm cache directory
51+
.npm
52+
53+
# Optional eslint cache
54+
.eslintcache
55+
56+
# Microbundle cache
57+
.rpt2_cache/
58+
.rts2_cache_cjs/
59+
.rts2_cache_es/
60+
.rts2_cache_umd/
61+
62+
# Optional REPL history
63+
.node_repl_history
64+
65+
# Output of 'npm pack'
66+
*.tgz
67+
68+
# Yarn Integrity file
69+
.yarn-integrity
70+
71+
# dotenv environment variables file
72+
.env
73+
.env.test
74+
75+
# parcel-bundler cache (https://parceljs.org/)
76+
.cache
77+
78+
# Next.js build output
79+
.next
80+
81+
# Nuxt.js build / generate output
82+
.nuxt
83+
dist
84+
85+
# Gatsby files
86+
.cache/
87+
# Comment in the public line in if your project uses Gatsby and *not* Next.js
88+
# https://nextjs.org/blog/next-9-1#public-directory-support
89+
# public
90+
91+
# vuepress build output
92+
.vuepress/dist
93+
94+
# Serverless directories
95+
.serverless/
96+
97+
# FuseBox cache
98+
.fusebox/
99+
100+
# DynamoDB Local files
101+
.dynamodb/
102+
103+
# TernJS port file
104+
.tern-port
105+
106+
# Any copied src from shared
107+
scikitjs-node/src/shared
108+
scikitjs-browser/src/shared

shared/globals.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import * as tf from '@tensorflow/tfjs-node'
2+
import * as dfd from 'danfojs-node'
3+
4+
export { tf, dfd }

shared/lib/cluster/kmeans.test.ts

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
import * as _ from 'lodash'
2+
import KMeans from './kmeans'
3+
import { assert } from 'chai'
4+
import { tensor2d } from '@tensorflow/tfjs-core'
5+
6+
// TODO: Improve on kmeans cluster testing
7+
describe('clusters:k_means', () => {
8+
const X = tensor2d([
9+
[1, 2],
10+
[1, 4],
11+
[1, 0],
12+
[4, 2],
13+
[4, 4],
14+
[4, 0]
15+
])
16+
17+
const predVector1 = [
18+
[0, 0],
19+
[4, 4]
20+
]
21+
const predVector2 = [
22+
[1298392183, 0],
23+
[0, 1]
24+
]
25+
26+
it('should fit vector1 + k=2 should return centroids of size 2 and clusters of size 2', () => {
27+
const expecterdCluster = {
28+
centroids: [
29+
[2.5, 1],
30+
[2.5, 4]
31+
],
32+
clusters: [
33+
[
34+
[1, 2],
35+
[1, 0],
36+
[4, 2],
37+
[4, 0]
38+
],
39+
[
40+
[1, 4],
41+
[4, 4]
42+
]
43+
],
44+
k: 2
45+
}
46+
const kmean = new KMeans({ nClusters: 2, randomState: 0 })
47+
kmean.fit(X)
48+
assert.deepEqual(
49+
expecterdCluster.centroids,
50+
kmean.clusterCenters.arraySync()
51+
)
52+
})
53+
54+
// it('should fit vector1 + k=3 size 3 and clusters of size 2', () => {
55+
// const expectedCluster = {
56+
// centroids: [
57+
// [2.5, 2],
58+
// [2.5, 4],
59+
// [2.5, 0]
60+
// ],
61+
// clusters: [
62+
// [
63+
// [1, 2],
64+
// [4, 2]
65+
// ],
66+
// [
67+
// [1, 4],
68+
// [4, 4]
69+
// ],
70+
// [
71+
// [1, 0],
72+
// [4, 0]
73+
// ]
74+
// ],
75+
// k: 3
76+
// }
77+
// const kmean = new KMeans({ k: 3 })
78+
// kmean.fit(vector1)
79+
// expect(_.isEqual(expectedCluster, kmean.toJSON())).toBe(true)
80+
// })
81+
82+
// it('should predict [2, 1] from predVector1 prediction', () => {
83+
// const expectedResult = [2, 1]
84+
// const kmean = new KMeans({ k: 3 })
85+
// kmean.fit(vector1)
86+
// const pred = kmean.predict(predVector1)
87+
// expect(_.isEqual(pred, expectedResult)).toBe(true)
88+
// })
89+
90+
// it('should predict [ 0, 0 ] from predVector2 prediction', () => {
91+
// const expectedResult = [0, 0]
92+
// const kmean = new KMeans({ k: 3 })
93+
// kmean.fit(vector1)
94+
// const pred = kmean.predict(predVector2)
95+
// expect(_.isEqual(pred, expectedResult)).toBe(true)
96+
// })
97+
98+
// it('should predict [ 0, 0 ] from predVector2 with X: vector1', () => {
99+
// const expectedResult = [0, 0]
100+
// const kmean = new KMeans({ k: 2 })
101+
// kmean.fit(vector1)
102+
// const pred = kmean.predict(predVector2)
103+
// expect(_.isEqual(expectedResult, pred)).toBe(true)
104+
// })
105+
106+
// it('should predict the same after reloading the model', () => {
107+
// const expectedResult = [0, 0]
108+
// const kmean = new KMeans({ k: 2 })
109+
// kmean.fit(vector1)
110+
// const pred1 = kmean.predict(predVector2)
111+
// expect(_.isEqual(expectedResult, pred1)).toBe(true)
112+
113+
// const checkpoint = kmean.toJSON()
114+
115+
// const kmean2 = new KMeans()
116+
// kmean2.fromJSON(checkpoint)
117+
// const pred2 = kmean.predict(predVector2)
118+
// expect(_.isEqual(expectedResult, pred2)).toBe(true)
119+
// })
120+
121+
// it('should not fit none 2D matrix', () => {
122+
// const kmean = new KMeans({ k: 2 })
123+
124+
// try {
125+
// kmean.fit([1, 2] as any)
126+
// } catch (err) {
127+
// expect(err).toBeInstanceOf(Validation2DMatrixError)
128+
// }
129+
// try {
130+
// kmean.fit(null as any)
131+
// } catch (err) {
132+
// expect(err).toBeInstanceOf(ValidationError)
133+
// }
134+
// // TODO: implement datatype check to the validation method
135+
// // expect(() => kmean.fit([["aa", "bb"]])).toThrow('The matrix is not 2D shaped: [1,2] of [2]');
136+
// })
137+
})

0 commit comments

Comments
 (0)