今天太晚了,参考链接先放在这里,明天再根据自己的实际情况整体一下,http://www.cnblogs.com/qianlifeng/archive/2012/02/13/2350086.html
实际上遇到的源代码作为例子贴上。
# show your cluster only available with 2-D data
#centroids为k个类别,其中保存着每个类别的质心
#clusterAssment为样本的标记,第一列为此样本的类别号,第二列为到此类别质心的距离
def showCluster(dataSet, k, centroids, clusterAssment):
numSamples, dim = dataSet.shape
if dim != 2:
print ("Sorry! I can not draw because the dimension of your data is not 2!")
return 1
mark = [‘or‘, ‘ob‘, ‘og‘, ‘ok‘, ‘^r‘, ‘+r‘, ‘sr‘, ‘dr‘, ‘<r‘, ‘pr‘]
if k > len(mark):
print ("Sorry! Your k is too large! please contact wojiushimogui")
return 1
# draw all samples
for i in range(numSamples):
markIndex = int(clusterAssment[i, 0]) #为样本指定颜色
plt.plot(dataSet[i, 0], dataSet[i, 1], mark[markIndex])
mark = [‘Dr‘, ‘Db‘, ‘Dg‘, ‘Dk‘, ‘^b‘, ‘+b‘, ‘sb‘, ‘db‘, ‘<b‘, ‘pb‘]
# draw the centroids
for i in range(k): #画每个类的质心点
plt.plot(centroids[i, 0], centroids[i, 1], mark[i], markersize = 12)
plt.show()
版权声明:本文为博主原创文章,未经博主允许不得转载。
时间: 2024-12-15 07:13:08