Understanding the theory behind the algorithm is still important. Let's try to run K-means clustering in order to separate three clusters in a two-dimensional space.
Clustering two groups in a 2D space
The three clusters
Three clusters are generated from the Gaussian distribution. Like we did in the previous chapter, we can use tf.randomNormal in TensorFlow.js to sample data points from the Gaussian distribution. The mean of the distribution is [0, 0] in a two-dimensional space. It is necessary to move the center by adding constants:
const N = 30;
const c1 = tf.randomNormal([N, 2]).add([2.0, 1.0]);
const c2 = tf.randomNormal([N, 2]).add([-2.0, -1.0]);
const c3 = tf.randomNormal([N, 2]).add([-2.0, 2.0]);
const xs = c1.concat...