Unsupervised Machine Learning

You are currently auditing this course.
3 / 3

Clustering - Prediction

Note: Please run the previous exercise before attempting this exercise

From the scatter plot of the previous exercise, you saw that the points separate into 3 clear clusters.

Now let's try creating a KMeans model to find these 3 clusters by fitting the data from the 'points' array from the previous exercise. After the model has been fit, you'll obtain the cluster labels associated with all the observations in the 'points' array. Next, create an array of 3 new points ('new_points') and try to predict which cluster it falls into by using the .predict() method.

Remember , use the array 'points' from the previous exercise. Please follow the instructions given below.

INSTRUCTIONS

Import KMeans from the sklearn package

from sklearn.cluster import KMeans

Using KMeans(), create a KMeans instance called model to find 3 clusters. To specify the number of clusters, use the n_clusters keyword argument. You will notice an additional argument called the random_state. We are assigning a constant value , 33 to this. This is to preserve the same result every time to run the program, since in the k-means algorithm, the assignment of the initial centroids is random.

model = KMeans( n_clusters=3, random_state= 33)

Use the fit() method of KMeans to fit the model to the array of points that we have already loaded.

model.fit(points)

Use the .predict() method of model to predict the cluster labels of new_points, assigning the result to labels.

Tip: To see which clusters these points belong to , use the fit_predict

clusters = model.fit_predict(points)

Display the variable 'clusters' . ( This will show which cluster each instance in the 'points' data array falls into)

your code here

Define a new set of 3 points as 'new_points' and see which cluster each belong to

new_points = [(4,3),(-4,1),(-3,-5)]

Now use the predict() function to see to which cluster each of these instances in new_points belong to

new_cluster = model.predict(new_points)

Print the new_cluster variable to see where they belong to

your code comes here

No hints are availble for this assesment

Answer is not availble for this assesment

Loading comments...