基於numpy實現的kmeans要比基於tensorflow的好寫,基於tensorflow的實現可以參考我的這篇博文:
import numpy as np
import copy
import random
# 計算兩個向量之間距離
def cal_distance(a,b):
# ** 代表乘方
return np.sum((a-b) ** 2) ** 0.5
# 計算簇的中心
def cal_cluster_center(cluster):
return np.mean(cluster,axis=0)
def kmeans(data,k,max_iter):
num_iter = 1
# 隨機找到k個初始中心點
r_indexs = [i for i in range(len(data))]
random.shuffle(r_indexs)
centers =
for i in range(k):
r_idx = r_indexs[i]
pre_centers = copy.deepcopy(centers)
while num_iter < max_iter:
cluster_dict = {}
for x in data:
# 與每個簇心計算距離
cluster_dist =
for i in range(k):
distance = cal_distance(pre_centers[i],x)
cluster_dist.sort(key = lambda x:x[1])
min_cluster_idx, min_dist = cluster_dist[0]
# 將資料新增到不同簇中
if min_cluster_idx not in cluster_dict:
cluster_dict[min_cluster_idx] =
# 更新簇中心
for idx in cluster_dict.keys():
centers[idx] = cal_cluster_center(cluster_dict[idx])
# 如果簇中心點不再變化,那麼結束
if(np.allclose(pre_centers,centers)):
break
else:
pre_centers = copy.deepcopy(centers)
num_iter += 1
return cluster_dict
Kmeans演算法實現
include opencv2 highgui highgui.hpp include opencv2 core core.hpp include using namespace cv using namespace std static void help int main int argc ch...
K means演算法實現
import math import numpy class point data reader file name str def init self,file name point data reader.file name file name defget data list self,num...
K means演算法實現
首先隨機生成k個聚類中心點 根據聚類中心點,將資料分為k類。分類的原則是資料離哪個中心點近就將它分為哪一類別。再根據分好的類別的資料,重新計算聚類的類別中心點。不斷的重複2和3步,直到中心點不再變化。from numpy import import csv import matplotlib.pyp...