本文整理汇总了Python中sklearn.cluster.SpectralClustering.predict方法的典型用法代码示例。如果您正苦于以下问题:Python SpectralClustering.predict方法的具体用法?Python SpectralClustering.predict怎么用?Python SpectralClustering.predict使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sklearn.cluster.SpectralClustering
的用法示例。
在下文中一共展示了SpectralClustering.predict方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: main
# 需要导入模块: from sklearn.cluster import SpectralClustering [as 别名]
# 或者: from sklearn.cluster.SpectralClustering import predict [as 别名]
def main(cm_file, perm_file, steps, labels_file, limit_classes=None):
"""Run optimization and generate output."""
# Load confusion matrix
with open(cm_file) as f:
cm = json.load(f)
cm = np.array(cm)
# Load labels
if os.path.isfile(labels_file):
with open(labels_file, "r") as f:
labels = json.load(f)
else:
labels = list(range(len(cm)))
n_clusters = 14 # hyperparameter
spectral = SpectralClustering(n_clusters=n_clusters,
eigen_solver='arpack',
affinity="nearest_neighbors")
spectral.fit(cm)
if hasattr(spectral, 'labels_'):
y_pred = spectral.labels_.astype(np.int)
else:
y_pred = spectral.predict(cm)
sscore = silhouette_score(cm, y_pred)
print("silhouette_score={} with {} clusters"
.format(sscore, n_clusters))
grouping = [[] for _ in range(n_clusters)]
for label, y in zip(labels, y_pred):
grouping[y].append(label)
for group in grouping:
print(" {}: {}".format(len(group), group))