K-means implementation

The KMeans implementation has the following properties. The shape of the input data is assumed to be [batch size, dimension]. This would be [90, 2] in this case:

export class KMeans {  // The desired number of clusters  k: number;  // The dimension of data points.   dim: number;  centroids: tf.Tensor;  // Given data points  xs: tf.Tensor;  clusterAssignment: tf.Tensor;  constructor(xs: tf.Tensor, k: number) {    this.dim = xs.shape[1];    this.k = k;    // Initialize centroids by picking up K random points.    this.centroids = tf.randomNormal([this.k, this.dim]);    this.xs = xs;  }}

clusterAssignment is a tensor that stores the index of the cluster that each data point is assigned to. Thus, its shape will be [N, 1]. N is the number of data ...

Get Hands-On Machine Learning with TensorFlow.js now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.