Skip to content

Instantly share code, notes, and snippets.

View rpgove's full-sized avatar

Robert Gove rpgove

View GitHub Profile
@rpgove
rpgove / README.md
Last active April 15, 2024 10:12
Using the elbow method to determine the optimal number of clusters for k-means clustering

K-means is a simple unsupervised machine learning algorithm that groups a dataset into a user-specified number (k) of clusters. The algorithm is somewhat naive--it clusters the data into k clusters, even if k is not the right number of clusters to use. Therefore, when using k-means clustering, users need some way to determine whether they are using the right number of clusters.

One method to validate the number of clusters is the elbow method. The idea of the elbow method is to run k-means clustering on the dataset for a range of values of k (say, k from 1 to 10 in the examples above), and for each value of k calculate the sum of squared errors (SSE). Like this:

var sse = {};
for (var k = 1; k <= maxK; ++k) {
    sse[k] = 0;
    clusters = kmeans(dataset, k);
    clusters.forEach(function(cluster) {

mean = clusterMean(cluster);

@rpgove
rpgove / .block
Last active November 29, 2023 23:13
Radial network diagram
license: gpl-3.0
height: 600
@rpgove
rpgove / .block
Last active January 10, 2023 06:29
Group-in-a-box layout
license: gpl-3.0
height: 302
@rpgove
rpgove / .block
Last active January 10, 2023 06:29
Force-directed layout
license: gpl-3.0
height: 600
@rpgove
rpgove / .block
Last active August 30, 2020 17:21
Automatically Finding Better Force Directed Layout Parameters
license: gpl-3.0
height: 600
scrolling: no
border: yes
@rpgove
rpgove / .block
Last active July 29, 2020 11:27
EuroVis 2019 Twitter interaction network
license: bsd-3-clause
height: 600
@rpgove
rpgove / .block
Last active January 16, 2020 10:46
Force Directed Layout Quality Convergence
license: gpl-3.0
height: 600
scrolling: no
border: yes
@rpgove
rpgove / .block
Last active December 27, 2019 00:16 — forked from mbostock/.block
Spiral Treemap
border: no
height: 600
license: gpl-3.0
@rpgove
rpgove / README.md
Last active December 27, 2019 00:14
Showing tooltips above a different element with d3-tip

Sometimes you need to show a tooltip over a different element than the one firing the mouseover event. Fortunately, d3-tip allows you to do that by passing an SVGElement as the second parameter to tip.show(). Like this:

tip.show(data, target)

This SVGElement will be the new target of the tooltip.

@rpgove
rpgove / .block
Created November 19, 2018 03:15
d3-force-reuse speed comparison
license: gpl-3.0
height: 600