Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

This is a manual implementation of k-means algorithm to cluster a scattered data points 2D please edit this code: (don't use the built in k

This is a manual implementation of k-means algorithm to cluster a scattered data points 2D please edit this code: (don't use the built in k means function) only edit the provided code to do the following:

Define two Arrays x [from 1 to 10] and y [from 1 to 10] . choose 4 centroids random or preferably (2.5 and 7.5) for x and y. apply k means clustering. Thank you.

from sklearn.metrics import pairwise_distances_argmin def find_clusters(X, n_clusters, rseed=2): # 1. Randomly choose clusters rng = np.random.RandomState(rseed) i = rng.permutation(X.shape[0])[:n_clusters] centers = X[i] while True: # 2a. Assign labels based on closest center labels = pairwise_distances_argmin(X, centers) # 2b. Find new centers from means of points new_centers = np.array([X[labels == i].mean(0) for i in range(n_clusters)]) # 2c. Check for convergence if np.all(centers == new_centers): break centers = new_centers return centers, labels centers, labels = find_clusters(X, 4) plt.scatter(X[:, 0], X[:, 1], c=labels, s=50, cmap='viridis');

Result should be something like this:

image text in transcribed

3 2 0 -1 -2 -3 -4 10 8 2 0

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Essentials of Database Management

Authors: Jeffrey A. Hoffer, Heikki Topi, Ramesh Venkataraman

1st edition

133405680, 9780133547702 , 978-0133405682

More Books

Students also viewed these Databases questions

Question

Discuss the techniques of sales forecasting.

Answered: 1 week ago