原文:https://blog.csdn.net/uyy203/article/details/90735664
聚類問題是數據挖掘的基本問題,它的本質是將n 個數據對象劃分為k個聚類,以便使得所獲得的聚 類滿足以下條件:同一聚類中的數據對象相似度較 高;不同聚類中的對象相似度較小。
它的基本思想是以空間中k個點為中心,進行聚類 ,對最靠近他們的對象歸類。通過迭代的方法,逐 次更新各聚類中心的值,直至得到最好的聚類結果 次更新各聚類中心的值,直至得到最好的聚類結果 。
原始數據:
劃分聚類數據:
算法的基本步驟
第一步:從n個數據對象任意選擇k個對象作為初始聚類中心,并設定最大迭代次數;
第二步:計算每個對象與k個中心點的距離,并根據最小距離對相應對象進行劃分,即,把對象劃分到與他們最近的中心所代表的類別中去 ;
第三步:對于每一個中心點,遍歷他們所包含的對象,計算這些對象所有維度的和的均值,獲得新的中心點;
第四步:如果聚類中心與上次迭代之前相比,有所改變,或者,算法迭代次數小于給定的最大迭代次數,則繼續執行第2 、3兩步,否則,程序結束返回聚類結果。
K-means算法運行過程
def main():
#step1: load data
print("load data...")
dataSet=[]
dataSetFile = open('./testSet/testSet.txt')
for line in dataSetFile:
lineAttrubute = line.strip().split('\t')
dataSet.append([float(lineAttrubute[0]),float(lineAttrubute[1])])
#step2: clustering
print("clustering...")
dataSet=np.mat(dataSet)
k=4
n=10000
centers_result,clusters_assignment_result = kmeans(dataSet,k,n)
#step3: show the clusters and centers
print("show the clusters and centers...")
showCluster(dataSet,k,centers_result,clusters_assignment_result)
initialCenters函數通過使用numpy庫的 ?Initialize center函數通過使用numpy庫的 zeros函數和random.uniform函數,隨機選取 了k個數據做聚類中心,并將結果存放在 了k個數據做聚類中心,并將結果存放在 Numpy的Array對象centers中
#create centers, the number of centers is k
def initialCenters(data,k):
nameSample,dim = data.shape
centers = np.zeros((k,dim))
for i in range(k):
index = int(np.random.uniform(0,nameSample))
centers[i,:] = data[index,:]
return centers
distanceToCenters這個函數用來計算一個數據點到所有 聚類中心的距離,將其存放在distance2Centers 中返回
#calculate distance from each point to each center
def distanceToCenters(sample, centers):
k = centers.shape[0]
distance2Centers = np.zeros(k)
for i in range(k):
distance2Centers[i] = np.sqrt(np.sum(power((sample-centers[i,:]),2)))
return distance2Centers
這部分代碼完成了k-means算法中為數據點決定所屬類別以及迭代更新類中心點的主要功能。
請注意numpy庫的返回最小值索引的argmin函數,以及計算平均值的mean函數的使用方法。
#k-means
def kmeans(data,k,n):
#initialize
iterCount = 0
centerChanged = True
numSample = data.shape[0]
centersAssignment = np.zeros(numSample)
#step1 find the centers by random
centers = initialCenters(data,k)
while centerChanged and iterCount < n:
#step2 calculate and mark index of the closest center from each point to create the clusters
centerChanged = False
iterCount = iterCount+1
for i in range(numSample):
sample2Centers = distanceToCenters(data[i,:],centers)
minIndex = np.argmin(sample2Centers)
if centersAssignment[i] != minIndex:
centersAssignment[i] = minIndex
centerChanged = True
#step3 calculate the mean point in each cluster, which become new center of each cluster
for j in range(k):
pointsInCluster = data[np.nonzero(centersAssignment[:] == j)[0]]
centers[j,:] = np.mean(pointsInCluster , axis = 0)
return centers,centersAssignment
showCluster函數中,利用matplotlib庫的plot函數將不同類別數據以不同顏色展現出來
def showCluster(data,k,centers,clustersAssignment):
numSample = data.shape[0]
#draw all samples
mark = ['or','ob','og','om']
for i in range(numSample):
markIndex = int(clustersAssignment[i])
plt.plot(data[i,0],data[i,1],mark[markIndex])
#draw the centers
mark = ['Dr','Db','Dg','Dm']
for i in range(k):
plt.plot(centers[i,0],centers[i,1],mark[i],markersize=10)
plt.show()
完整代碼:
#k-means
#author xyz.
from numpy import *
import numpy as np
from matplotlib import *
import matplotlib.pyplot as plt
#create centers, the number of centers is k
def initialCenters(data,k):
nameSample,dim = data.shape
centers = np.zeros((k,dim))
for i in range(k):
index = int(np.random.uniform(0,nameSample))
centers[i,:] = data[index,:]
return centers
#calculate distance from each point to each center
def distanceToCenters(sample, centers):
k = centers.shape[0]
distance2Centers = np.zeros(k)
for i in range(k):
distance2Centers[i] = np.sqrt(np.sum(power((sample-centers[i,:]),2)))
return distance2Centers
#k-means
def kmeans(data,k,n):
#initialize
iterCount = 0
centerChanged = True
numSample = data.shape[0]
centersAssignment = np.zeros(numSample)
#step1 find the centers by random
centers = initialCenters(data,k)
while centerChanged and iterCount < n:
#step2 calculate and mark index of the closest center from each point to create the clusters
centerChanged = False
iterCount = iterCount+1
for i in range(numSample):
sample2Centers = distanceToCenters(data[i,:],centers)
minIndex = np.argmin(sample2Centers)
if centersAssignment[i] != minIndex:
centersAssignment[i] = minIndex
centerChanged = True
#step3 calculate the mean point in each cluster, which become new center of each cluster
for j in range(k):
pointsInCluster = data[np.nonzero(centersAssignment[:] == j)[0]]
centers[j,:] = np.mean(pointsInCluster , axis = 0)
return centers,centersAssignment
def showCluster(data,k,centers,clustersAssignment):
numSample = data.shape[0]
#draw all samples
mark = ['or','ob','og','om']
for i in range(numSample):
markIndex = int(clustersAssignment[i])
plt.plot(data[i,0],data[i,1],mark[markIndex])
#draw the centers
mark = ['Dr','Db','Dg','Dm']
for i in range(k):
plt.plot(centers[i,0],centers[i,1],mark[i],markersize=10)
plt.show()
def main():
#step1: load data
print("load data...")
dataSet=[]
dataSetFile = open('./testSet/testSet.txt')
for line in dataSetFile:
lineAttrubute = line.strip().split('\t')
dataSet.append([float(lineAttrubute[0]),float(lineAttrubute[1])])
#step2: clustering
print("clustering...")
dataSet=np.mat(dataSet)
k=4
n=10000
centers_result,clusters_assignment_result = kmeans(dataSet,k,n)
#step3: show the clusters and centers
print("show the clusters and centers...")
showCluster(dataSet,k,centers_result,clusters_assignment_result)
if __name__=="__main__":
main()