警告:Python中的未压缩距离矩阵

3

我正在尝试制作一个与聚合层次聚类相关的树状图,并需要距离矩阵。我从以下内容开始:

import numpy as np 
import pandas as pd
from scipy import ndimage 
from scipy.cluster import hierarchy 
from scipy.spatial import distance_matrix 
from matplotlib import pyplot as plt 
from sklearn import manifold, datasets 
from sklearn.cluster import AgglomerativeClustering 
from sklearn.datasets.samples_generator import make_blobs 
%matplotlib inline
X1, y1 = make_blobs(n_samples=50, centers=[[4,4], [-2, -1], [1, 1], [10,4]], cluster_std=0.9)
plt.scatter(X1[:, 0], X1[:, 1], marker='o') 
agglom = AgglomerativeClustering(n_clusters = 4, linkage = 'average')
agglom.fit(X1,y1)
# Create a figure of size 6 inches by 4 inches.
plt.figure(figsize=(6,4))

# These two lines of code are used to scale the data points down,
# Or else the data points will be scattered very far apart.

# Create a minimum and maximum range of X1.
x_min, x_max = np.min(X1, axis=0), np.max(X1, axis=0)

# Get the average distance for X1.
X1 = (X1 - x_min) / (x_max - x_min)

# This loop displays all of the datapoints.
for i in range(X1.shape[0]):
    # Replace the data points with their respective cluster value 
    # (ex. 0) and is color coded with a colormap (plt.cm.spectral)
    plt.text(X1[i, 0], X1[i, 1], str(y1[i]),
             color=plt.cm.nipy_spectral(agglom.labels_[i] / 10.),
             fontdict={'weight': 'bold', 'size': 9})

# Remove the x ticks, y ticks, x and y axis
plt.xticks([])
plt.yticks([])
#plt.axis('off')



# Display the plot of the original data before clustering
plt.scatter(X1[:, 0], X1[:, 1], marker='.')
# Display the plot
plt.show()
dist_matrix = distance_matrix(X1,X1) 
print(dist_matrix)

当我写下这个时,出现了错误:

Z = hierarchy.linkage(dist_matrix, 'complete')

/home/jupyterlab/conda/envs/python/lib/python3.6/site-packages/ipykernel_launcher.py:1: 集群警告: scipy.cluster: 对称的非负空洞观察矩阵看起来很像未压缩的距离矩阵 """入口点启动IPython内核。

首先,这是什么意思,我该如何解决它?谢谢


看起来像是一个警告,而不是一个错误。 - desertnaut
是的,还是不明白这是什么意思。至今还没有找到合适的答案。 - AKP
2个回答

5

scipy.cluster.heirarchy.linkage需要一个压缩距离矩阵,而不是一个方形/未压缩距离矩阵。您已经计算了一个方形距离矩阵,需要将其转换为压缩形式。我建议使用scipy.spatial.distance.squareform。以下代码片段可以复现您的功能(我已删除了绘图以节省篇幅),没有警告。

from sklearn.cluster import AgglomerativeClustering 
from sklearn.datasets import make_blobs
from scipy.spatial import distance_matrix
from scipy.cluster import hierarchy
from scipy.spatial.distance import squareform

X1, y1 = make_blobs(n_samples=50, centers=[[4,4],
                                           [-2, -1],
                                           [1, 1],
                                           [10,4]], cluster_std=0.9)

agglom = AgglomerativeClustering(n_clusters = 4, linkage = 'average')
agglom.fit(X1,y1)

dist_matrix = distance_matrix(X1,X1)
print(dist_matrix.shape)
condensed_dist_matrix = squareform(dist_matrix)
print(condensed_dist_matrix.shape)
Z = hierarchy.linkage(condensed_dist_matrix, 'complete')

1
你能解释一下压缩和未压缩距离矩阵之间的区别吗? - Soerendip
2
由于距离矩阵通常是在一组点之间计算的(即在点集中的所有元素对之间),因此它是对称的,对角线上的值都为零。Squareform/condensed通过仅表示上三角点来节省内存,而uncondensed则是完整的矩阵。这有帮助吗? - ahagen
1
好的,压缩形式只包括距离矩阵的上(或下)三角形,不包括对角线。你可以添加两张图片来更清楚地说明。谢谢。 - Soerendip

4
这句话的意思是X1在X1.T的附近过于接近。
agglom.fit(X1,y1)

您可以在头文件中添加以下代码来忽略它!
from scipy.cluster.hierarchy import ClusterWarning
from warnings import simplefilter
simplefilter("ignore", ClusterWarning)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接