Get startedGet started for free

How many dominant colors?

We have loaded the following image using the imread() function of the image class of matplotlib.

The RGB values are stored in a DataFrame, batman_df. The RGB values have been standardized used the whiten() function, stored in columns, scaled_red, scaled_blue and scaled_green.

Construct an elbow plot with the DataFrame. How many dominant colors are present?

This exercise is part of the course

Cluster Analysis in Python

View Course

Exercise instructions

  • Create a list of distortions based on each value in num_clusters by running the kmeans() function.
  • Create a DataFrame elbow_plot with the lists: num_clusters and distortions.
  • Plot the data with seaborn's .lineplot() method with num_clusters on the x-axis and distortions on the y-axis.

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(____)

# Create a DataFrame with two lists, num_clusters and distortions
elbow_plot = pd.DataFrame(____)

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