scikit-learn kmeans 聚类的初始质心
initial centroids for scikit-learn kmeans clustering
如果我已经有一个可以作为初始质心的numpy数组,我该如何正确初始化kmeans算法?我正在使用 scikit-learn Kmeans class
this post () 表示如果我使用 numpy 数组作为初始质心,我只需要设置 n_init=1 但我不确定我的是否初始化工作正常
Naftali Harris 出色的可视化页面显示了我正在尝试做的事情
http://www.naftaliharris.com/blog/visualizing-k-means-clustering/
"I'll choose" --> "Packed Circles" --> 运行 kmeans
#numpy array of initial centroids
startpts=np.array([[-0.12, 0.939, 0.321, 0.011], [0.0, 0.874, -0.486, 0.862], [0.0, 1.0, 0.0, 0.033], [0.12, 0.939, 0.321, -0.7], [0.0, 1.0, 0.0, -0.203], [0.12, 0.939, -0.321, 0.25], [0.0, 0.874, 0.486, -0.575], [-0.12, 0.939, -0.321, 0.961]], np.float64)
centroids= sk.KMeans(n_clusters=8, init=startpts, n_init=1)
centroids.fit(actual_data_points)
#get the array
centroids_array=centroids.cluster_centers_
是的,通过 init
设置初始质心应该可行。这是 scikit-learn documentation:
的引述
init : {‘k-means++’, ‘random’ or an ndarray}
Method for initialization, defaults to ‘k-means++’:
If an ndarray is passed, it should be of shape (n_clusters, n_features)
and gives the initial centers.
What is the shape (n_clusters, n_features)
referring to?
形状要求是指init
必须恰好有n_clusters
行,每行的元素个数要匹配actual_data_points
:
的维数
>>> init = np.array([[-0.12, 0.939, 0.321, 0.011],
[0.0, 0.874, -0.486, 0.862],
[0.0, 1.0, 0.0, 0.033],
[0.12, 0.939, 0.321, -0.7],
[0.0, 1.0, 0.0, -0.203],
[0.12, 0.939, -0.321, 0.25],
[0.0, 0.874, 0.486, -0.575],
[-0.12, 0.939, -0.321, 0.961]],
np.float64)
>>> init.shape[0] == 8
True # n_clusters
>>> init.shape[1] == actual_data_points.shape[1]
True # n_features
What is n_features?
n_features
是样本的维度。例如,如果您要在 2D 平面上聚类点,则 n_features
将为 2.
如果我已经有一个可以作为初始质心的numpy数组,我该如何正确初始化kmeans算法?我正在使用 scikit-learn Kmeans class
this post (
Naftali Harris 出色的可视化页面显示了我正在尝试做的事情 http://www.naftaliharris.com/blog/visualizing-k-means-clustering/
"I'll choose" --> "Packed Circles" --> 运行 kmeans
#numpy array of initial centroids
startpts=np.array([[-0.12, 0.939, 0.321, 0.011], [0.0, 0.874, -0.486, 0.862], [0.0, 1.0, 0.0, 0.033], [0.12, 0.939, 0.321, -0.7], [0.0, 1.0, 0.0, -0.203], [0.12, 0.939, -0.321, 0.25], [0.0, 0.874, 0.486, -0.575], [-0.12, 0.939, -0.321, 0.961]], np.float64)
centroids= sk.KMeans(n_clusters=8, init=startpts, n_init=1)
centroids.fit(actual_data_points)
#get the array
centroids_array=centroids.cluster_centers_
是的,通过 init
设置初始质心应该可行。这是 scikit-learn documentation:
init : {‘k-means++’, ‘random’ or an ndarray}
Method for initialization, defaults to ‘k-means++’:
If an ndarray is passed, it should be of shape (n_clusters, n_features)
and gives the initial centers.
What is the shape
(n_clusters, n_features)
referring to?
形状要求是指init
必须恰好有n_clusters
行,每行的元素个数要匹配actual_data_points
:
>>> init = np.array([[-0.12, 0.939, 0.321, 0.011],
[0.0, 0.874, -0.486, 0.862],
[0.0, 1.0, 0.0, 0.033],
[0.12, 0.939, 0.321, -0.7],
[0.0, 1.0, 0.0, -0.203],
[0.12, 0.939, -0.321, 0.25],
[0.0, 0.874, 0.486, -0.575],
[-0.12, 0.939, -0.321, 0.961]],
np.float64)
>>> init.shape[0] == 8
True # n_clusters
>>> init.shape[1] == actual_data_points.shape[1]
True # n_features
What is n_features?
n_features
是样本的维度。例如,如果您要在 2D 平面上聚类点,则 n_features
将为 2.