python实现AGNES(凝聚层次聚类)算法
import numpy as npimport pandas as pdimport matplotlib.pyplot as pltfrom sklearn.cluster import AgglomerativeClusteringfrom sklearn import datasetsfrom sklearn.metrics import confusion_matrixiris=data
·
#AGNES(凝聚层次聚类)算法
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.cluster import AgglomerativeClustering
from sklearn import datasets
from sklearn.metrics import confusion_matrix
iris=datasets.load_iris()
irisdata=iris.data
clustering=AgglomerativeClustering(linkage='ward',n_clusters=3)
res=clustering.fit(irisdata)
print("各个簇的样本数目:")
print(pd.Series(clustering.labels_).value_counts())
print("聚类结果:")
print(confusion_matrix(iris.target,clustering.labels_))
plt.figure()
d0=irisdata[clustering.labels_==0]
plt.plot(d0[:,0],d0[:,1],'r.')
d1=irisdata[clustering.labels_==1]
plt.plot(d1[:,0],d1[:,1],'go')
d2=irisdata[clustering.labels_==0]
plt.plot(d2[:,0],d2[:,2],'b*')
plt.xlabel('petal length')
plt.ylabel('petal width')
plt.legend(loc=2)
plt.show()
更多推荐
所有评论(0)
您需要登录才能发言
加载更多