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.
This exercise is part of the course
Unsupervised Learning in Python
Exercise instructions
- For each of the given values of
k, perform the following steps: - Create a
KMeansinstance calledmodelwithkclusters. - Fit the model to the grain data
samples. - Append the value of the
inertia_attribute ofmodelto the listinertias. - The code to plot
ksvsinertiashas been written for you, so hit submit to see the plot!
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
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()