分类、回归---监督过程,解决“在标签数据前提下,输入X预测Y”
非监督---数据本身有什么特点,数据簇
K均值---给定K个聚类中心,根据与聚类中心距离聚类,更新聚类中心。代码实践如下:
def kMeans(dataSet, k, distMeas=distEclud, createCent=randCent):
#输入:数据集,类别数,距离计算方式,初始聚类中心
m = shape(dataSet)[0]
clusterAssment = mat(zeros((m, 2)))
centroids = createCent(dataSet, k) #初始聚类中心
clusterChanged = True
while clusterChanged:
clusterChanged = False
for i in range(m):
minDist = inf;
minIndex = -1
for j in range(k):
distJI = distMeas(centroids[j, :], dataSet[i, :])
if distJI < minDist: #判断标准是所有点都处于距离最小聚类中心
minDist = distJI;
minIndex = j
if clusterAssment[i, 0] != minIndex: clusterChanged = True #更新聚类中心
clusterAssment[i, :] = minIndex, minDist ** 2
print(centroids)
for cent in range(k):
ptsInClust = dataSet[nonzero(clusterAssment[:, 0].A == cent)[0]]
centroids[cent, :] = mean(ptsInClust, axis=0)
return centroids, clusterAssment
上述是否为最优聚类中心的判断是---各点距离聚类中心最近。当然也可以求解聚类效果的误差平方和SSE,当SSE最小时,此时聚类中心为最优。
K均值的衍生算法---二分K均值算法:每次仅将一个簇分为两类,选择待分类簇的原则是该簇分类后SSE最大程度降低,直到分类数达到预期数目K。代码实践如下:
def biKmeans(dataSet, k, distMeas=distEclud):
#输入与K均值一致
m = shape(dataSet)[0]
clusterAssment = mat(zeros((m, 2)))
centroid0 = mean(dataSet, axis=0).tolist()[0]
centList = [centroid0]
for j in range(m):
clusterAssment[j, 1] = distMeas(mat(centroid0), dataSet[j, :]) ** 2
while (len(centList) < k):
lowestSSE = inf #初始化SSE为无穷大
for i in range(len(centList)):
ptsInCurrCluster = dataSet[nonzero(clusterAssment[:, 0].A == i)[0], :]
centroidMat, splitClustAss = kMeans(ptsInCurrCluster, 2, distMeas)
sseSplit = sum(splitClustAss[:, 1])
sseNotSplit = sum(clusterAssment[nonzero(clusterAssment[:, 0].A != i)[0], 1])
print("sseSplit, and notSplit: ", sseSplit, sseNotSplit)
if (sseSplit + sseNotSplit) < lowestSSE: #判断该簇是否为最优分割簇
bestCentToSplit = i
bestNewCents = centroidMat
bestClustAss = splitClustAss.copy()
lowestSSE = sseSplit + sseNotSplit
bestClustAss[nonzero(bestClustAss[:, 0].A == 1)[0], 0] = len(centList)
bestClustAss[nonzero(bestClustAss[:, 0].A == 0)[0], 0] = bestCentToSplit
print('the bestCentToSplit is: ', bestCentToSplit)
print('the len of bestClustAss is: ', len(bestClustAss))
centList[bestCentToSplit] = bestNewCents[0, :].tolist()[0]
centList.append(bestNewCents[1, :].tolist()[0])
clusterAssment[nonzero(clusterAssment[:, 0].A == bestCentToSplit)[0],:] = bestClustAss
return mat(centList), clusterAssment