Aan de slagGa gratis aan de slag

How many clusters of grain?

In the video, you learned how to choose a good number of clusters for a dataset using the k-means inertia graph. You are given an array samples containing the measurements (such as area, perimeter, length, and several others) of samples of grain. What's a good number of clusters in this case?

KMeans and PyPlot (plt) have already been imported for you.

This dataset was sourced from the UCI Machine Learning Repository.

Deze oefening maakt deel uit van de cursus

Unsupervised Learning in Python

Cursus bekijken

Oefeninstructies

  • For each of the given values of k, perform the following steps:
  • Create a KMeans instance called model with k clusters.
  • Fit the model to the grain data samples.
  • Append the value of the inertia_ attribute of model to the list inertias.
  • The code to plot ks vs inertias has been written for you, so hit submit to see the plot!

Praktische interactieve oefening

Probeer deze oefening eens door deze voorbeeldcode in te vullen.

ks = range(1, 6)
inertias = []

for k in ks:
    # Create a KMeans instance with k clusters: model
    ____
    
    # Fit model to samples
    ____
    
    # Append the inertia to the list of inertias
    ____
    
# Plot ks vs inertias
plt.plot(ks, inertias, '-o')
plt.xlabel('number of clusters, k')
plt.ylabel('inertia')
plt.xticks(ks)
plt.show()
Code bewerken en uitvoeren