集群和尝试HDBSCAN集群相当新,但我很难弄清楚如何获得集群中心。使用KMeans,它可以通过群集进行设置。
如何获得群集中心?
这是我的代码:
#!/usr/bin/env python3
from sklearn.cluster import KMeans
from sklearn import metrics
import cv2
import numpy as np
import hdbscan
from pprint import pprint
# Read image into opencv
image = cv2.imread('4.jpg')
# Set color space
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
# reshape the image to be a list of pixels
pixels = image.reshape((image.shape[0] * image.shape[1], 3))
# Build the clusterer
cluster = hdbscan.RobustSingleLinkage(cut=0.125, k=7)
cluster.fit(pixels)
>>> pprint(vars(cluster))
{'_cluster_hierarchy_': <hdbscan.plots.SingleLinkageTree object at 0x110deda58>,
'_metric_kwargs': {},
'algorithm': 'best',
'alpha': 1.4142135623730951,
'core_dist_n_jobs': 4,
'cut': 0.125,
'gamma': 5,
'k': 7,
'labels_': array([ 0, 0, 0, ..., 360, 220, 172]),
'metric': 'euclidean'}
这就是KMeans输出给你的东西:
{'cluster_centers': (array([ 64.93473757, 65.65262431, 72.00103591]),
array([ 77.55381605, 85.80626223, 102.29549902]),
array([ 105.66884532, 115.81917211, 131.55555556]),
array([ 189.20149254, 197.00497512, 205.43034826]),
array([ 148.0922619 , 156.5 , 168.33333333])),
'cluster_centers_': array([[ 105.66884532, 115.81917211, 131.55555556],
[ 64.93473757, 65.65262431, 72.00103591],
[ 148.0922619 , 156.5 , 168.33333333],
[ 189.20149254, 197.00497512, 205.43034826],
[ 77.55381605, 85.80626223, 102.29549902]]),
'copy_x': True,
'inertia_': 1023155.888923295,
'init': 'k-means++',
'labels_': array([1, 1, 1, ..., 1, 1, 1], dtype=int32),
'max_iter': 300,
'n_clusters': 5,
'n_init': 10,
'n_iter_': 8,
'n_jobs': 1,
'precompute_distances': 'auto',
'random_state': None,
'tol': 0.0001,
'verbose': 0}
答案 0 :(得分:1)
(H)DBSCAN中的集群没有中心。
群集可能是非凸的,如果计算所有点的平均值(并且您的数据是点 - 它们不是必须的),那么它可能是群集的外
另请注意,DBSCAN还提供了根本没有中心的噪点。