粮食样本有多少个聚类?
在视频中,您学习了如何使用 k-means 惯性图为数据集选择合适的聚类数。现在给您一个数组 samples,其中包含粮食样本的多种测量值(如面积、周长、长度等)。在这个例子中,合适的聚类数是多少?
KMeans 和 PyPlot(plt)已为您导入。
本数据集来源于 UCI Machine Learning Repository。
本练习是课程的一部分
Python 中的无监督学习
练习说明
- 对给定的每个
k值,执行以下步骤: - 创建一个包含
k个聚类的KMeans实例,命名为model。 - 将模型拟合到粮食数据
samples上。 - 将
model的inertia_属性值追加到列表inertias。 - 已为您写好绘制
ks与inertias的代码,点击 提交答案 查看图表!
交互式实操练习
通过完成这段示例代码来试试这个练习。
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()