Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@jbeuckm/k-means-js

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@jbeuckm/k-means-js

A basic Javascript implementation of the cluster analysis algorithm.

  • 0.5.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
2
decreased by-33.33%
Maintainers
1
Weekly downloads
 
Created
Source

K-Means Clustering

Build Status

A basic Javascript implementation of the [cluster analysis] 1 algorithm.

Usage

  • Optionally, normalize the data.

The normalizer will scale numerical data between [0,1] and will generate n outputs of either zero or one for discrete data, eg. category.

// Tell the normalizer about the category field.
var params = {
   category: "discrete"
};

// Category is a discrete field with two possible values.
// Value is a linear field with continuous possible values.
var data = [
    {
       category: "a",
       value: 25
    },
    {
       category: "b",
       value: 7.6
    },
    {
       category: "a",
       value: 28
    }
];


var ranges = require('dataset').findRanges(params, data);
var normalized = require('dataset').normalize(data, ranges);
  • Run the algorithm.
// This non-normalized sample data with n=k is a pretty awful example.
var points = [
  [.1, .2, .3],
  [.4, .5, .6],
  [.7, .8, .9]
];

var k = 3;

var means = require('kmeans').algorithm(points, k, console.log);

The call to algorithm() will find the data's range in each dimension, generate k=3 random points, and iterate until the means are static.

  • Find the best K

The method described by Pham, et al. is implemented. The algorithm evaluates K-means repeatedly for different values of K, and returns the best (guess) value for K as well as the set of means found during evaluation.

var pbk = require('phamBestK');

var maxKToTest = 10;
var result = pbk.findBestK(points, maxKToTest);

console.log("this data has "+result.K+" clusters");
console.log("cluster centroids = "+result.means);
  • Denormalize data

Denormalization can be used to show the means discovered:

for (var i= 0, l=result.means.length; i<l; i++) {
    console.log(dataset.denormalizeDatum(result.means[i], ranges));
}

Todo

  • denormalize data
  • provide ability to label data points, dimensions and means
  • build an asynchronous version of the algorithm

Keywords

FAQs

Package last updated on 08 Dec 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc