Initializing the algorithm

In this section, we'll be working in the kmeans.js file. The first thing to do is to add our functions for mean and distance to the top of the file. Since these are generic functions that can be called statistically, we will not define them inside a class. Add the following to the top of the file:

/** * Calculate the mean of an array of numbers. * @param {Array.<number>} numbers * @return {number} */const mean = numbers => numbers.reduce((sum, val) => sum + val, 0) / numbers.length;/** * Calculate the distance between two points. * Points must be given as arrays or objects with equivalent keys. * @param {Array.<number>} a * @param {Array.<number>} b * @return {number} */const distance = (a, b) => Math.sqrt(    a.

Get Hands-on Machine Learning with JavaScript 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.