我正在尝试使用选定的初始质心进行 k 均值聚类。 它说here 指定您的初始中心:
init : {‘k-means++’, ‘random’ or an ndarray}
如果传递了一个 ndarray
,它的形状应该是 (n_clusters
, n_features
) 并给出初始中心。
我的 Python 代码:
X = np.array([[-19.07480000, -8.536],
[22.010800000,-10.9737],
[12.659700000,19.2601]], np.float64)
km = KMeans(n_clusters=3,init=X).fit(data)
# print km
centers = km.cluster_centers_
print centers
返回错误:
RuntimeWarning: Explicit initial center position passed: performing only one init in k-means instead of n_init=10
n_jobs=self.n_jobs)
并返回相同的初始中心。知道如何形成初始中心以便它可以被接受吗?
最佳答案
KMeans
的默认行为是使用不同的随机质心(即 Forgy method)多次初始化算法。然后由 n_init=
参数 ( docs ) 控制随机初始化的次数:
n_init : int, default: 10
Number of time the k-means algorithm will be run with different centroid seeds. The final results will be the best output of
n_init
consecutive runs in terms of inertia.
如果您将数组作为 init=
参数传递,则只会使用数组中明确指定的质心执行单个 初始化。您收到 RuntimeWarning
,因为您仍在传递默认值 n_init=10
(here 是源代码的相关行)。
忽略此警告实际上完全没问题,但如果您的 init=
参数是一个数组,您可以通过传递 n_init=1
使其完全消失。
关于python - 具有选定初始中心的 k-means,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28862334/