LoslegenKostenlos loslegen

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?

Diese Übung ist Teil des Kurses

Cluster Analysis in Python

Kurs anzeigen

Anleitung zur Übung

  • 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.

Interaktive Übung

Versuche dich an dieser Übung, indem du diesen Beispielcode vervollständigst.

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()
Code bearbeiten und ausführen