开始使用免费开始使用

肘部法

在上一个练习中,您实现了使用 8 个簇的 MiniBatch K-means,但还没有实际检查应该使用多少个簇。对于我们第一个欺诈检测方法,选对簇的数量很重要,尤其当您打算将这些簇的离群点作为欺诈预测时更是如此。为确定将使用的簇数,让我们应用肘部法(Elbow method),根据该方法来看看最优的簇数应是多少。

X_scaled 仍可供您使用,且已从 sklearn 导入 MiniBatchKMeans

本练习是课程的一部分

Python 中的欺诈检测

查看课程

练习说明

  • 将簇的数量范围设为 1 到 5。
  • 使用列表推导在该范围内对所有簇运行 MiniBatch K-means。
  • 在缩放后的数据上拟合每个模型,并从缩放后的数据获取分数。
  • 绘制簇数量及其对应的分数。运行需要几秒钟。

交互式实操练习

通过完成这段示例代码来试试这个练习。

# Define the range of clusters to try
clustno = range(____, ____)

# Run MiniBatch Kmeans over the number of clusters
kmeans = [____(n_clusters=i, random_state=0) for ____ in ____]

# Obtain the score for each model
score = [kmeans[i].fit(____).score(____) for i in range(len(kmeans))]

# Plot the models and their respective score 
plt.plot(____, ____)
plt.xlabel('Number of Clusters')
plt.ylabel('Score')
plt.title('Elbow Curve')
plt.show()
编辑并运行代码