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.

Unsupervised Learning

Figure 3: Randomly generated data, from three randomly selected centroids, plotted

We define the function closest_centroids() to assign each point to the centroid it

is closest to:

def closest_centroids(points, centroids):

distances = tf.reduce_sum(tf.square(tf.subtract(points,

centroids[:,None])), 2)

assignments = tf.argmin(distances, 0)

return assignments

We create another function move_centroids(). It recalculates the centroids such

that the sum of squared distances decreases:

def move_centroids(points, closest, centroids):

return np.array([points[closest==k].mean(axis=0) for k in

range(centroids.shape[0])])

Now we call these two functions iteratively for 100 iterations. We have chosen the

number of iterations arbitrarily; you can increase and decrease it to see the effect:

for step in range(iteration_n):

closest = closest_centroids(points, centroids)

centroids = move_centroids(points, closest, centroids)

In the following graph, you can see the final centroids after 100 iterations. We have

also colored the points based on which centroid they are closest to. The yellow points

correspond to one cluster (nearest the cross in its center), and the same is true for the

purple and green cluster points:

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

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

plt.show()

[ 382 ]

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

Saved successfully!

Ooh no, something went wrong!