Comparing version 4.0.0 to 4.0.1
@@ -0,1 +1,11 @@ | ||
<a name="4.0.1"></a> | ||
## [4.0.1](https://github.com/mljs/kmeans/compare/v4.0.0...v4.0.1) (2018-05-24) | ||
### Bug Fixes | ||
* keep initial value for empty centroids ([94b0de9](https://github.com/mljs/kmeans/commit/94b0de9)) | ||
<a name="4.0.0"></a> | ||
@@ -2,0 +12,0 @@ # [4.0.0](https://github.com/mljs/kmeans/compare/v3.1.0...v4.0.0) (2018-05-23) |
@@ -57,2 +57,3 @@ 'use strict'; | ||
* @ignore | ||
* @param {Array<Array<number>>} prevCenters - Centroids from the previous iteration | ||
* @param {Array <Array <number>>} data - the [x,y,z,...] points to cluster | ||
@@ -63,6 +64,6 @@ * @param {Array <number>} clusterID - the cluster identifier for each data dot | ||
*/ | ||
function updateCenters(data, clusterID, K) { | ||
function updateCenters(prevCenters, data, clusterID, K) { | ||
const nDim = data[0].length; | ||
// creates empty centers with 0 size | ||
// copy previous centers | ||
var centers = new Array(K); | ||
@@ -89,3 +90,7 @@ var centersLen = new Array(K); | ||
for (var d = 0; d < nDim; d++) { | ||
centers[id][d] /= centersLen[id]; | ||
if (centersLen[id]) { | ||
centers[id][d] /= centersLen[id]; | ||
} else { | ||
centers[id][d] = prevCenters[id][d]; | ||
} | ||
} | ||
@@ -325,3 +330,7 @@ } | ||
for (var j = 0; j < this.centroids.length; j++) { | ||
enrichedCentroids[j].error /= enrichedCentroids[j].size; | ||
if (enrichedCentroids[j].size) { | ||
enrichedCentroids[j].error /= enrichedCentroids[j].size; | ||
} else { | ||
enrichedCentroids[j].error = null; | ||
} | ||
} | ||
@@ -365,3 +374,3 @@ | ||
); | ||
var newCenters = updateCenters(data, clusterID, K); | ||
var newCenters = updateCenters(centers, data, clusterID, K); | ||
var converged = hasConverged( | ||
@@ -414,2 +423,3 @@ newCenters, | ||
* @param {string|Array<Array<number>>} [options.initialization = 'kmeans++'] - K centers in format [x,y,z,...] or a method for initialize the data: | ||
* * You can either specify your custom start centroids, or select one of the following initialization method: | ||
* * `'kmeans++'` will use the kmeans++ method as described by http://ilpubs.stanford.edu:8090/778/1/2006-13.pdf | ||
@@ -416,0 +426,0 @@ * * `'random'` will choose K random different values. |
{ | ||
"name": "ml-kmeans", | ||
"version": "4.0.0", | ||
"version": "4.0.1", | ||
"description": "K-Means clustering", | ||
@@ -5,0 +5,0 @@ "main": "kmeans.js", |
@@ -38,3 +38,3 @@ import euclidean from 'ml-distance-euclidean'; | ||
); | ||
var newCenters = updateCenters(data, clusterID, K); | ||
var newCenters = updateCenters(centers, data, clusterID, K); | ||
var converged = hasConverged( | ||
@@ -87,2 +87,3 @@ newCenters, | ||
* @param {string|Array<Array<number>>} [options.initialization = 'kmeans++'] - K centers in format [x,y,z,...] or a method for initialize the data: | ||
* * You can either specify your custom start centroids, or select one of the following initialization method: | ||
* * `'kmeans++'` will use the kmeans++ method as described by http://ilpubs.stanford.edu:8090/778/1/2006-13.pdf | ||
@@ -89,0 +90,0 @@ * * `'random'` will choose K random different values. |
@@ -60,3 +60,7 @@ import { updateClusterID } from './utils'; | ||
for (var j = 0; j < this.centroids.length; j++) { | ||
enrichedCentroids[j].error /= enrichedCentroids[j].size; | ||
if (enrichedCentroids[j].size) { | ||
enrichedCentroids[j].error /= enrichedCentroids[j].size; | ||
} else { | ||
enrichedCentroids[j].error = null; | ||
} | ||
} | ||
@@ -63,0 +67,0 @@ |
@@ -49,2 +49,3 @@ import nearestVector from 'ml-nearest-vector'; | ||
* @ignore | ||
* @param {Array<Array<number>>} prevCenters - Centroids from the previous iteration | ||
* @param {Array <Array <number>>} data - the [x,y,z,...] points to cluster | ||
@@ -55,6 +56,6 @@ * @param {Array <number>} clusterID - the cluster identifier for each data dot | ||
*/ | ||
export function updateCenters(data, clusterID, K) { | ||
export function updateCenters(prevCenters, data, clusterID, K) { | ||
const nDim = data[0].length; | ||
// creates empty centers with 0 size | ||
// copy previous centers | ||
var centers = new Array(K); | ||
@@ -81,3 +82,7 @@ var centersLen = new Array(K); | ||
for (var d = 0; d < nDim; d++) { | ||
centers[id][d] /= centersLen[id]; | ||
if (centersLen[id]) { | ||
centers[id][d] /= centersLen[id]; | ||
} else { | ||
centers[id][d] = prevCenters[id][d]; | ||
} | ||
} | ||
@@ -84,0 +89,0 @@ } |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
38400
903