python 聚類分析實戰案例:K-means演算法(原理原始碼)
阿新 • • 發佈:2019-01-22
K-means演算法:
關於步驟:參考之前的部落格
關於程式碼與資料:暫時整理程式碼如下:後期會附上github地址,上傳原始資料與程式碼完整版,
各種聚類演算法的對比:參考連線
Kmeans演算法的缺陷
1.聚類中心的個數K 需要事先給定,但在實際中這個 K 值的選定是非常難以估計的,很多時候,事先並不知道給定的資料集應該分成多少個類別才最合適
2.Kmeans需要人為地確定初始聚類中心,不同的初始聚類中心可能導致完全不同的聚類結果。
#!usr/bin/env python
#_*_ coding:utf-8 _*_
import random
import math
'''
kMeans:2列資料對比,帶有head
'''
#1.load data
def importData():
f = lambda name,b,d: [name, float(b), float(d)]
with open('birth-death-rates.csv', 'r') as inputFile:
return [f(*line.strip().split('\t')) for line in inputFile]
寫入檔案型別
#2. calculate Distance
def euclideanDistance(x,y):
return math.sqrt(sum([(a-b)**2 for (a,b) in zip(x,y)]))
#L=points,
def partition(points, k, means, d=euclideanDistance):
# print('means={}'.format(means))
thePartition = [[] for _ in means] # list of k empty lists
indices = range(k)
# print('indices={}'.format(indices))
for x in points:
#index為indices索引,呼叫d函式,計算每個值與聚類中心的距離,將其分類
closestIndex = min(indices, key=lambda index: d(x, means[index]))#實現X與每個Y直接的求解:key=lambda index: d(x, means[index])
thePartition[closestIndex].append(x)
return thePartition
#3.尋找收斂點
def mean(points):
''' assume the entries of the list of points are tuples;
e.g. (3,4) or (6,3,1). '''
n = len(points)
# print(tuple(float(sum(x)) / n for x in zip(*points))) #*points將【[1,2],[2,3]】分割出來【1,2】
return tuple(float(sum(x)) / n for x in zip(*points)) #將最開始的[[4, 1], [1, 5]] 經過處理變成[(4, 1),(1, 5)]
def kMeans(points, k, initialMeans, d=euclideanDistance):
oldPartition = []
newPartition = partition(points, k, initialMeans, d)
while oldPartition != newPartition:
oldPartition = newPartition
newMeans = [mean(S) for S in oldPartition]
newPartition = partition(points, k, newMeans, d)
return newPartition
#0.函式呼叫初始中心點
if __name__ == "__main__":
L = [x[1:] for x in importData()] # remove names
# print (str(L).replace('[','{').replace(']', '}'))
import matplotlib.pyplot as plt
'''
plt.scatter(*zip(*L))
plt.show()
'''
import random
k = 3
partition = kMeans(L, k, random.sample(L, k)) #L是集合,K分類個數,random.sample(L, k)中心點
plt.scatter(*zip(*partition[0]), c='b')#[[],[],[]]
plt.scatter(*zip(*partition[1]), c='r')
plt.scatter(*zip(*partition[2]), c='g')
plt.show()