您现在的位置是:主页 > news > 企业服务云平台/谷歌优化排名公司

企业服务云平台/谷歌优化排名公司

admin2025/5/3 7:18:25news

简介企业服务云平台,谷歌优化排名公司,php网站首页模板,加强三农网站建设的意义K-means算法: K-means算法是一个被广泛使用且简单的无监督算法。 K-means算法将数据分为k个簇类,使得每个簇类内部数据尽可能的相似,而簇之间的数据尽可能的不同。 K-means算法中的簇类数目为k,是用户认为给定的。 算法流程: K-…

企业服务云平台,谷歌优化排名公司,php网站首页模板,加强三农网站建设的意义K-means算法: K-means算法是一个被广泛使用且简单的无监督算法。 K-means算法将数据分为k个簇类,使得每个簇类内部数据尽可能的相似,而簇之间的数据尽可能的不同。 K-means算法中的簇类数目为k,是用户认为给定的。 算法流程: K-…

K-means算法:
K-means算法是一个被广泛使用且简单的无监督算法。
K-means算法将数据分为k个簇类,使得每个簇类内部数据尽可能的相似,而簇之间的数据尽可能的不同。
K-means算法中的簇类数目为k,是用户认为给定的。

算法流程:
在这里插入图片描述
K-means代码:

from numpy import *#加载数据
def loadDataSet(fileName):      #general function to parse tab -delimited floatsdataMat = []                #assume last column is target valuefr = open(fileName)for line in fr.readlines():curLine = line.strip().split('\t')fltLine = map(float,curLine) #map all elements to float()dataMat.append(fltLine)return dataMat#计算欧式距离
def distEclud(vecA, vecB):return sqrt(sum(power(vecA - vecB, 2))) #la.norm(vecA-vecB)#随机生成k个质点
def randCent(dataSet, k):n = shape(dataSet)[1]centroids = mat(zeros((k,n)))#create centroid matfor j in range(n):#create random cluster centers, within bounds of each dimensionminJ = min(dataSet[:,j]) rangeJ = float(max(dataSet[:,j]) - minJ)centroids[:,j] = mat(minJ + rangeJ * random.rand(k,1))return centroids#K-means算法
def kMeans(dataSet, k, distMeas=distEclud, createCent=randCent):m = shape(dataSet)[0]clusterAssment = mat(zeros((m,2)))#create mat to assign data points #to a centroid, also holds SE of each pointcentroids = createCent(dataSet, k)clusterChanged = Truewhile clusterChanged:clusterChanged = Falsefor i in range(m):#for each data point assign it to the closest centroidminDist = inf; minIndex = -1for j in range(k):distJI = distMeas(centroids[j,:],dataSet[i,:])if distJI < minDist:minDist = distJI; minIndex = jif clusterAssment[i,0] != minIndex: clusterChanged = TrueclusterAssment[i,:] = minIndex,minDist**2print centroidsfor cent in range(k):#recalculate centroidsptsInClust = dataSet[nonzero(clusterAssment[:,0].A==cent)[0]]#get all the point in this clustercentroids[cent,:] = mean(ptsInClust, axis=0) #assign centroid to mean return centroids, clusterAssment

实验结果:
在这里插入图片描述


由于簇类数目k是用户给定的,并且K-means算法对初始的质点非常的敏感,很容易进入局部最小值。
因此,一开始就随机生成k个质点是存在问题的。
二分K-means算法很好的解决了上述的问题。
二分K-means算法并不一开始就生成k个质点,而是慢慢的增加质点的数目。

二分K-means算法流程:
在这里插入图片描述
在这里插入图片描述
二分K-means算法代码:

def biKmeans(dataSet, k, distMeas=distEclud):m = shape(dataSet)[0]clusterAssment = mat(zeros((m,2)))centroid0 = mean(dataSet, axis=0).tolist()[0]centList =[centroid0] #create a list with one centroidfor j in range(m):#calc initial ErrorclusterAssment[j,1] = distMeas(mat(centroid0), dataSet[j,:])**2while (len(centList) < k):lowestSSE = inffor i in range(len(centList)):ptsInCurrCluster = dataSet[nonzero(clusterAssment[:,0].A==i)[0],:]#get the data points currently in cluster icentroidMat, splitClustAss = kMeans(ptsInCurrCluster, 2, distMeas)sseSplit = sum(splitClustAss[:,1])#compare the SSE to the currrent minimumsseNotSplit = sum(clusterAssment[nonzero(clusterAssment[:,0].A!=i)[0],1])print "sseSplit, and notSplit: ",sseSplit,sseNotSplitif (sseSplit + sseNotSplit) < lowestSSE:bestCentToSplit = ibestNewCents = centroidMatbestClustAss = splitClustAss.copy()lowestSSE = sseSplit + sseNotSplitbestClustAss[nonzero(bestClustAss[:,0].A == 1)[0],0] = len(centList) #change 1 to 3,4, or whateverbestClustAss[nonzero(bestClustAss[:,0].A == 0)[0],0] = bestCentToSplitprint 'the bestCentToSplit is: ',bestCentToSplitprint 'the len of bestClustAss is: ', len(bestClustAss)centList[bestCentToSplit] = bestNewCents[0,:].tolist()[0]#replace a centroid with two best centroids centList.append(bestNewCents[1,:].tolist()[0])clusterAssment[nonzero(clusterAssment[:,0].A == bestCentToSplit)[0],:]= bestClustAss#reassign new clusters, and SSEreturn mat(centList), clusterAssment