如何使用 Elbow 方法获得自组织映射 "SOM" 的最佳 K?

How to get the best K for self organizing maps "SOM" using Elbow method?

我正在尝试使用 SOM 对我的数据进行聚类,首先我想获得最佳 K。但我需要一条线或其他东西来检测绘图上的最佳 K。我尝试使用 KElbowVisualizer() 但它总是显示错误:

YellowbrickTypeError: The supplied model is not a clustering estimator; try a classifier or regression score visualizer instead!

这是我的代码:

from sklearn_som.som import SOM
som = SOM(m = 1, n = i, dim = data.shape[1])
visualizer = KElbowVisualizer(som, k = (1,11))
visualizer.fit(data)
visualizer.show()

我也用了matplotlib的普通Plot(),但是看不到Best k, 我的代码:

inertia = []
for i in range (1,31):
    som = SOM(m = 1, n = i, dim = data.shape[1])
    som.fit_predict(data)
    inertia.append(som.inertia_)
plt.plot(range(1,31), inertia)
plt.title('elbow method for SOM')
plt.xlabel('number of clusters')
plt.ylabel('WCSS')
plt.show()

that's the plot I got from Plot()

那么,请问我怎样才能在情节中或使用代码做到这一点?

我刚刚找到了解决我的问题的最佳方法。我决定在这里post。可能有人需要它。

from kneed import KneeLocator

解决方法在这里 只需将此库与 matplotlib 库一起使用

实现是这样的:

inertia = []
for i in range (1,31):
    som = SOM(m = 1, n = i, dim = x_lda_train.shape[1])
    som.fit_predict(x_lda_train)
    inertia.append(som.inertia_)
# identify the knee by using the kneelocator function
kneeloc1 = KneeLocator(range(1,11), wcss, curve='convex', direction='decreasing')
plt.plot(range(1,31), inertia)
plt.title('elbow method for SOM')
plt.xlabel('number of clusters')
plt.ylabel('WCSS')
# print it by using the vlines
plt.vlines(kneeloc1.knee, plt.ylim()[0], plt.ylim()[1], linestyles='dashed')
plt.show()
# you can see this also as just a number by printing it
print(kneeloc1.knee)

有关详细信息,您可以查看文档: 访问 https://kneed.readthedocs.io/en/stable/parameters.html