Aan de slagGa gratis aan de slag

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?

Deze oefening maakt deel uit van de cursus

Cluster Analysis in Python

Cursus bekijken

Oefeninstructies

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

Praktische interactieve oefening

Probeer deze oefening eens door deze voorbeeldcode in te vullen.

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 bewerken en uitvoeren