Skip to content

Instantly share code, notes, and snippets.

View rpgove's full-sized avatar

Robert Gove rpgove

View GitHub Profile
@rpgove
rpgove / index.html
Created November 5, 2012 09:08
US Electoral Map Sized by Electoral Votes
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
<title>US Electoral Map</title>
<link rel="stylesheet" href="style.css"/>
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.css" />
<script src="http://d3js.org/d3.v2.min.js?2.10.1"></script>
<script src="http://code.jquery.com/jquery-1.8.2.min.js"></script>
<script src="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.js"></script>
@rpgove
rpgove / data.tsv
Last active May 5, 2019 14:01
Force-Directed Scatterplot
sepalLength sepalWidth petalLength petalWidth species
5.1 3.5 1.4 0.2 setosa
4.9 3.0 1.4 0.2 setosa
4.7 3.2 1.3 0.2 setosa
4.6 3.1 1.5 0.2 setosa
5.0 3.6 1.4 0.2 setosa
5.4 3.9 1.7 0.4 setosa
4.6 3.4 1.4 0.3 setosa
5.0 3.4 1.5 0.2 setosa
4.4 2.9 1.4 0.2 setosa
@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 / 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 / README.md
Last active December 6, 2015 20:34 — forked from mbostock/.block
Heatmap of Force-Directed Graph Node Positions
@rpgove
rpgove / README.md
Last active June 27, 2017 17:00
Heatmap with 2D Kernel Density Estimation of Force Layout
@rpgove
rpgove / .block
Last active January 10, 2023 06:29
Force-directed layout
license: gpl-3.0
height: 600
@rpgove
rpgove / .block
Last active April 20, 2018 09:03
Arc diagram
license: gpl-3.0
height: 400
@rpgove
rpgove / .block
Last active April 14, 2017 00:40
Semantic substrate
license: gpl-3.0
height: 500
@rpgove
rpgove / .block
Last active April 27, 2017 01:59
Matrix diagram
license: gpl-3.0
height: 800