Get startedGet started for free

Elbow method on distinct clusters

Let us use the comic con dataset to see how the elbow plot looks on a dataset with distinct, well-defined clusters. You may want to display the data points before proceeding with the exercise.

The data is stored in a pandas DataFrame, comic_con. x_scaled and y_scaled are the column names of the standardized X and Y coordinates of people at a given point in time.

This exercise is part of the course

Cluster Analysis in Python

View Course

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

distortions = []
num_clusters = range(1, 7)

# Create a list of distortions from the kmeans function
for i in ____:
    cluster_centers, distortion = ____
    distortions.append(distortion)

# Create a DataFrame with two lists - num_clusters, distortions
elbow_plot = pd.DataFrame({'num_clusters': ____, 'distortions': ____})

# Creat a line plot of num_clusters and distortions
sns.lineplot(x=____, y=____, data = ____)
plt.xticks(num_clusters)
plt.show()
Edit and Run Code