简介
KMeans算法是一种简单的分类算法,算法思想是先随机选N个点分别生成N个集合,然后把剩余点往集合归类。由于选择的质心并不是确定正确的,每次填充完数据需要更新质心,重新归类直到质心不再改变。。
python实现
import numpy as np
import random
import math
class XYZClusterer(object):
def __init__(self, cluster_num:int):
assert cluster_num > 0
self.cluster_num = cluster_num
self.X = np.zeros(0)
self.Y = np.zeros(0)
self.ndot = 0
self.un_ind = np.zeros(0, dtype=np.int) # unknown point index
self.clas = [[] for i in range(cluster_num)] # point index in cluster
self.clas_ctx = np.zeros(self.cluster_num) # point center x
self.clas_cty = np.zeros(self.cluster_num) # point center y
def fit(self, X:np.ndarray, Y:np.ndarray):
assert X.shape[0] == Y.shape[0] >= self.cluster_num
self.X = X
self.