1. 程式人生 > >基於python3的k-means程式碼實現

基於python3的k-means程式碼實現

k-means演算法是非監督學習的一種,其中k值是隨機選取的,在本程式碼中是人為指定為2,準備聚兩個類。
演算法描述:

1. 載入資料

2. 聚類

2.1、 初始化聚類中心,隨機選取兩個點作為聚類中心點。
2.2、while直到clusterChanged=false
2.3、計算每個點離中心點的距離,記錄最小距離,並標識是屬於哪個類。
2.4、更新聚類集合的點。
2.5、 更新聚類中心

程式碼實現前先瀏覽一下資料,資料分佈如下
這裡寫圖片描述
從資料分佈可以看出,是7個點。
程式碼實現分為兩個python檔案,一個是聚類的實現檔案,k_means.py,一個是測試檔案test_kmeans.py.
k_means.py

如下:

“`# -- coding: utf-8 --
“””
Created on Thu Nov 17 16:13:56 2016

@author: phl
“””
print(“k-means演算法程式”)
from numpy import *
import time
import matplotlib.pyplot as plt
def euclDistance(vector1, vector2):
return sqrt(sum(power(vector2 - vector1, 2)))
def initCentriods(dataSet,k):
print(dataSet)
numSamples,dim = dataSet.shape #dim列數
centroids = zeros((k, dim))
print(“行數:”,numSamples)
print(“列數:”,dim)
for i in range(k):
index = int(random.uniform(0, numSamples))
centroids[i, :] = dataSet[index, :]
return centroids
def kmeans(dataSet, k):
numSamples = dataSet.shape[0] #dataSet.shape是幾行幾列的意思,這裡是7行2列
print(“行數:”,numSamples)
clusterAssment = mat(zeros((numSamples, 2)))#初始化一個行兩列的0矩陣
clusterChanged = True
## step 1: 初始化聚類中心
centroids = initCentriods(dataSet, k)
print(“隨機初始化的兩個點:”,centroids)
## 迴圈遍歷資料
while clusterChanged:
clusterChanged = False
for i in range(numSamples):
minDist = 100000.0
minIndex = 0
## 迴圈遍歷中心點
## step 2:計算離中心點的距離
for j in range(k):
distance = euclDistance(centroids[j, :], dataSet[i, :])
if distance < minDist:
minDist = distance
minIndex = j #minIndex代表類別
##更新聚類分配
if clusterAssment[i,0] != minIndex:
clusterChanged = True
clusterAssment[i, :] = minIndex, minDist**2
## step 4: 更新聚類中心
for j in range(k):
pointsInCluster = dataSet[nonzero(clusterAssment[:, 0].A == j)[0]]
centroids[j, :] = mean(pointsInCluster, axis = 0)
print(‘恭喜你,聚類完成’)
return centroids, 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 Zouxy")  
    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() 

def showData(dataSet):
x = []
y = []
plt.figure(figsize=(9,6))
for i in dataSet:
x.append([float(i[0])])
y.append([float(i[1])])
plt.scatter(x,y,c=”b”,s=25,alpha=0.4,marker=’o’)
#T:散點的顏色
#s:散點的大小
#alpha:是透明程度
plt.show()
test_kmeans.py如下:
# -- coding: utf-8 --
“””
Created on Thu Nov 17 16:35:03 2016

@author: phl
“””
from numpy import *
import time
import matplotlib.pyplot as plt
from k_means import *
print(“step 1: 載入資料”)
dataSet = []
fileIn = open(‘F:/python/testSet.txt’)
for line in fileIn.readlines():
lineArr = line.strip().split(‘\t’)
dataSet.append([float(lineArr[0]), float(lineArr[1])])
showData(dataSet)
print(“step 2: 聚類”)
dataSet = mat(dataSet) #mat是把資料格式化成列的形式[[1. 1.][1.5 2.][3. 4.][5. 7.]]
k = 2
centroids, clusterAssment = kmeans(dataSet, k)
print(“step 3: 展示聚類結果”)
showCluster(dataSet, k, centroids, clusterAssment) “`
結果介面如下:
這裡寫圖片描述