09.05.2023 Views

pdfcoffee

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

3. Recompute the centroids using current cluster membership, such that the

sum of squared distances decreases

4. Repeat the last two steps until convergence is met

Chapter 10

In the previous TensorFlow versions the KMeans class was implemented in the

Contrib module; however, the class is no longer available in TensorFlow 2.0. Here

we will instead use the advanced mathematical functions provided in TensorFlow

2.0 to implement k-means clustering.

K-means in TensorFlow 2.0

To demonstrate k-means in TensorFlow, we will use randomly generated data in

the code that follows. Our randomly generated data will contain 200 samples, and

we will divide them into three clusters. We start with importing all the required

modules and defining the variables, determining the number of sample points

(points_n), the number of clusters to be formed (clusters_n), and the number of

iterations we will be doing (iteration_n). We also set the seed for random number

to ensure that our work is reproducible:

import matplotlib.pyplot as plt

import numpy as np

import tensorflow as tf

points_n = 200

clusters_n = 3

iteration_n = 100

seed = 123

np.random.seed(seed)

tf.random.set_seed(seed)

Now we randomly generate data and from the data select three centroids randomly:

points = np.random.uniform(0, 10, (points_n, 2))

centroids = tf.slice(tf.random.shuffle(points), [0, 0], [clusters_n,

-1])

You can see the scatter plot of all the points and the randomly selected three

centroids in the following graph:

plt.scatter(points[:, 0], points[:, 1], s=50, alpha=0.5)

plt.plot(centroids[:, 0], centroids[:, 1], 'kx', markersize=15)

plt.show()

[ 381 ]

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!