9951 explained code solutions for 126 technologies


javascript-d3How can I use the d3.js library to implement K-means clustering?


K-means clustering is an unsupervised machine learning algorithm that can be used to group data into clusters. The d3.js library can be used to implement K-means clustering by using the data-driven documents (d3) library to create a visualization of the data and then applying the K-means algorithm to the data.

Example code

// Load data
var data = d3.csv("data.csv");

// Create a visualization of the data
var svg = d3.select("body").append("svg")
  .attr("width", width)
  .attr("height", height);

svg.selectAll("circle")
  .data(data)
  .enter()
  .append("circle")
  .attr("cx", function(d) {
    return xScale(d.x);
  })
  .attr("cy", function(d) {
    return yScale(d.y);
  })
  .attr("r", 5);

// Apply K-means algorithm
var kmeans = new KMeans(data, 3);
kmeans.run();

// Visualize the clusters
svg.selectAll("circle")
  .data(data)
  .enter()
  .append("circle")
  .attr("cx", function(d) {
    return xScale(d.x);
  })
  .attr("cy", function(d) {
    return yScale(d.y);
  })
  .attr("r", 5)
  .style("fill", function(d) {
    return colorScale(d.cluster);
  });

The code above loads the data from a CSV file, creates a visualization of the data points, applies the K-means algorithm to the data, and then visualizes the clusters by coloring the data points according to their assigned cluster.

Code explanation

  • var data = d3.csv("data.csv"); - Load the data from a CSV file.
  • var svg = d3.select("body").append("svg") - Create a SVG element.
  • .attr("cx", function(d) { return xScale(d.x); }) - Set the x-coordinate of each data point.
  • .attr("cy", function(d) { return yScale(d.y); }) - Set the y-coordinate of each data point.
  • var kmeans = new KMeans(data, 3); - Create a new KMeans object with the data and the number of clusters.
  • kmeans.run(); - Run the K-means algorithm.
  • .style("fill", function(d) { return colorScale(d.cluster); }) - Color the data points according to their assigned cluster.

Helpful links

Edit this code on GitHub