如何在拟合谱聚类后打印出重新排序的亲和度矩阵?

时间:2017-11-17 18:09:52

标签: python scikit-learn spectral

使用光谱聚类后,如何查看新的重新排序矩阵(亲和度矩阵)?我该如何打印?

1 个答案:

答案 0 :(得分:-1)

使用光谱聚类后,很容易访问亲和力矩阵

使用虹膜数据的示例:

from sklearn.datasets import load_iris
from sklearn.cluster import SpectralClustering

#load data
data = load_iris()
x = data.data
y = data.target

#define model and fit data
model = SpectralClustering(n_clusters = 4, random_state = 0)
model.fit(x,y)

#get the Affinity matrix used for clustering
print(model.affinity_matrix_)
print(model.affinity_matrix_.shape)

#get the Labels of each point
print(model.labels_)

<强>结果

#Affinity matrix

[[  1.00000000e+00   7.48263568e-01   7.71051586e-01 ...,   2.30082059e-09
    4.03840951e-10   3.59908895e-08]
 [  7.48263568e-01   1.00000000e+00   9.13931185e-01 ...,   1.62136087e-09
    2.15082380e-10   3.22418674e-08]
 [  7.71051586e-01   9.13931185e-01   1.00000000e+00 ...,   3.65410404e-10
    6.16221335e-11   9.42405852e-09]
 ...,
 [  2.30082059e-09   1.62136087e-09   3.65410404e-10 ...,   1.00000000e+00
    6.83861409e-01   6.63650250e-01]
 [  4.03840951e-10   2.15082380e-10   6.16221335e-11 ...,   6.83861409e-01
    1.00000000e+00   5.54327285e-01]
 [  3.59908895e-08   3.22418674e-08   9.42405852e-09 ...,   6.63650250e-01
    5.54327285e-01   1.00000000e+00]]


(150, 150)

#Labels

[2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
 2 2 2 2 2 2 2 2 2 2 2 2 2 1 1 1 3 1 3 1 3 1 3 3 3 3 1 3 1 3 3 1 3 1 3 1 1
 1 1 1 1 1 3 3 3 3 1 3 1 1 3 3 3 3 1 3 3 3 3 3 3 3 3 0 1 0 1 0 0 3 0 0 0 1
 1 0 1 1 1 1 0 0 1 0 1 0 1 0 0 1 1 1 0 0 0 1 1 1 0 1 1 1 0 0 1 1 0 0 1 1 1
 1 1]