我在2D (x行)中有很多点,并希望对它们进行聚类,以便每个集群都有直径完全联动,那么应该达到直径条件。为什么下面的代码会产生完全不正确的结果?(单个集群中的点数相距很远)
from scipy.cluster.hierarchy import linkage,fcluster
from scipy.spatial.distance import pdist
import matplotlib.pyplot as plot1
import pandas,numpy
n=100; x=pandas.DataFrame(data=numpy.random.rand(n,2))
plot1.figure(figsize=(10,7)); plot1.scatter(x.iloc[:,0],x.iloc[:,1],s=3); plot1.show()
ll=fcluster(linkage(pdist(x),'complete'),t=0.2,criterion='distance'); l=numpy.unique(ll)
print('number of clusters:',len(l))
xx= [x.iloc[numpy.where(ll==i)[0]] for i in l] #xx=clusters of x
for x in xx[:5]:
plot1.figure(figsize=(10,7))
plot1.scatter(x.iloc[:,0],x.iloc[:,1],s=3)
plot1.show(); #plot each cluster in a separate figure
发布于 2020-06-10 10:02:40
抱歉,我的初始代码工作正常,只是看起来不正确,因为轴变小了,所以只剩下plot1.axis([0,1,0,1])
了。
from scipy.cluster.hierarchy import linkage,fcluster
from scipy.spatial.distance import pdist
import matplotlib.pyplot as plot1
import pandas,numpy
n=100; x=pandas.DataFrame(data=numpy.random.rand(n,2))
plot1.figure(figsize=(10,7)); plot1.scatter(x.iloc[:,0],x.iloc[:,1],s=3); plot1.show()
ll=fcluster(linkage(pdist(x),'complete'),t=0.2,criterion='distance'); l=numpy.unique(ll)
print('number of clusters:',len(l))
xx= [x.iloc[numpy.where(ll==i)[0]] for i in l] #xx=clusters of x
for x in xx[:5]:
plot1.figure(figsize=(10,7))
plot1.scatter(x.iloc[:,0],x.iloc[:,1],s=3)
plot1.axis([0,1,0,1])
plot1.show(); #plot each cluster in a separate figure
https://stackoverflow.com/questions/62206406
复制