Clustering 2D points
From the scatter plot of the previous exercise, you saw that the points seem to separate into 3 clusters. You'll now create a KMeans model to find 3 clusters, and fit it to the data points from the previous exercise. After the model has been fit, you'll obtain the cluster labels for some new points using the .predict()
method.
You are given the array points
from the previous exercise, and also an array new_points
.
This exercise is part of the course
Unsupervised Learning in Python
Exercise instructions
- Import
KMeans
fromsklearn.cluster
. - Using
KMeans()
, create aKMeans
instance calledmodel
to find3
clusters. To specify the number of clusters, use then_clusters
keyword argument. - Use the
.fit()
method ofmodel
to fit the model to the array of pointspoints
. - Use the
.predict()
method ofmodel
to predict the cluster labels ofnew_points
, assigning the result tolabels
. - Hit submit to see the cluster labels of
new_points
.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Import KMeans
____
# Create a KMeans instance with 3 clusters: model
model = ____
# Fit model to points
____
# Determine the cluster labels of new_points: labels
labels = ____
# Print cluster labels of new_points
print(labels)