-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
rename files and separate into class files
- Loading branch information
1 parent
c61ce1c
commit 82a0272
Showing
7 changed files
with
114 additions
and
111 deletions.
There are no files selected for viewing
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
module.exports = require('./lib/kmeans'); | ||
module.exports = require('./lib/KMeansEngine'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
const _ = require('underscore'); | ||
const Vector = require('./Vector'); | ||
|
||
class Cluster { | ||
constructor(centroid) { | ||
this.centroid = centroid; | ||
|
||
this.init(); | ||
} | ||
|
||
init() { | ||
this.hasMoved = false; | ||
this.vectorIds = []; | ||
} | ||
|
||
addVectorId(id) { | ||
this.vectorIds.push(id); | ||
} | ||
|
||
calculateCentroids(allVectors) { | ||
const newCentroid = new Vector(); | ||
|
||
this.vectorIds.map(id => allVectors[id]).forEach((vector) => { | ||
newCentroid.add(vector); | ||
}); | ||
|
||
newCentroid.divide(this.vectorIds.length); | ||
|
||
if (!_.isEqual(this.centroid.getObject(), newCentroid.getObject())) { | ||
this.centroid = newCentroid; | ||
this.hasMoved = true; | ||
} | ||
} | ||
} | ||
|
||
module.exports = Cluster; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
class Vector { | ||
constructor(vector) { | ||
this.vector = vector || {}; | ||
} | ||
|
||
clone() { | ||
return new Vector(Object.assign({}, this.getObject())); | ||
} | ||
|
||
getKeys() { | ||
return Object.keys(this.vector); | ||
} | ||
|
||
getObject() { | ||
return this.vector; | ||
} | ||
|
||
getEuclideanDistance(vector) { | ||
const tmpVector = this.clone().subtract(vector); | ||
const tmp = tmpVector.getObject(); | ||
let d = 0; | ||
|
||
tmpVector.getKeys().forEach((k) => { | ||
// only need to consider the overlapping keys | ||
d += tmp[k] * tmp[k]; | ||
}); | ||
|
||
return Math.sqrt(d); | ||
} | ||
|
||
add(vector) { | ||
const tmp = vector.getObject(); | ||
|
||
vector.getKeys().forEach((k) => { | ||
if (this.vector[k] === undefined) { this.vector[k] = 0; } | ||
|
||
this.vector[k] += tmp[k]; | ||
}); | ||
|
||
return this; | ||
} | ||
|
||
subtract(vector) { | ||
const tmp = vector.getObject(); | ||
|
||
vector.getKeys().forEach((k) => { | ||
if (this.vector[k] === undefined) { this.vector[k] = 0; } | ||
|
||
this.vector[k] -= tmp[k]; | ||
}); | ||
|
||
return this; | ||
} | ||
|
||
multiply(c) { | ||
this.getKeys().forEach((k) => { | ||
this.vector[k] *= c; | ||
}); | ||
|
||
return this; | ||
} | ||
|
||
divide(c) { | ||
this.getKeys().forEach((k) => { | ||
this.vector[k] /= c; | ||
}); | ||
|
||
return this; | ||
} | ||
} | ||
|
||
module.exports = Vector; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters