JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 54
  • Score
    100M100P100Q68284F
  • License MIT

Implementation of the k-mediods clustering algorithm

Package Exports

  • k-medoids

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (k-medoids) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

k-medoids

An implementation of the k-medoids algorithm

Example Usage

Simple Example (Uses Euclidean Distance Function by default)

TypeScript:

import { Cluster, Clusterer } from "k-medoids";

const k = 2;
const myData = [
    [1, 2],
    [1, 3],
    [-1, 2.5],
    [0, 0],
    [510, 203],
    [-100, 120],
];

const clusterer = Clusterer.getInstance(myData, 2);
const clusteredData = clusterer.getClusteredData();
clusteredData

JavaScript:

const kmeds = require("k-medoids");

const k = 2;
const myData = [
    [1, 2],
    [1, 3],
    [-1, 2.5],
    [0, 0],
    [510, 203],
    [-100, 120],
];

const clusterer = kmeds.Clusterer.getInstance(myData, 2);
const clusteredData = clusterer.getClusteredData();
clusteredData

outputs:

[
    [
        [510,203]
    ],
    [
        [1,2],[1,3],[-1,2.5],[0,0],[-100,120]
    ]
]

Using a Custom Distance Function

const myFunkyDistanceFn = (a: number[], b: number[]) => {
    return Math.abs(a[1] - b[1]);
};

const myClusterer = Clusterer.getInstance(myData, 2, myFunkyDistanceFn);
const data = myClusterer.getClusteredData();
data

outputs:

[
    [
        [510,203],
        [-100,120]
    ],
    [
        [1,2],
        [1,3],
        [-1,2.5],
        [0,0]
    ]
]

Clustering custom objects

We can cluster any object type as long as we provide a distance function to give the distance between them.

For example with a set of "widgets" like this:

const myWidgets = [
    {
        Name: "DoHickey",
        Weight: 10,
    },
    {
        Name: "Thingy",
        Weight: 10.5,
    },
    {
        Name: "Whatsit",
        Weight: 9.5,
    },
    {
        Name: "Bohemoth",
        Weight: 120,
    },
    {
        Name: "Goliath",
        Weight: 125,
    },
];

we might consider items to be similar by weight, and thus:

const myWidgetClusterer = Clusterer.getInstance(myWidgets, 2, (a, b) => {
    return Math.abs(a.Weight - b.Weight);
});
const groupedWidgets = myWidgetClusterer.getClusteredData();
groupedWidgets

gives us:

[
    [
        {"Name":"Bohemoth","Weight":120},
        {"Name":"Goliath","Weight":125}
    ],
    [
        {"Name":"DoHickey","Weight":10},
        {"Name":"Thingy","Weight":10.5},
        {"Name":"Whatsit","Weight":9.5}]
    ]
]