def __init__(self, k, datas): self.datas = [Vec(data[:-1]) for data in datas] self.lab = {self.datas[i]: datas[i][-1] for i in range(len(datas))} Vec.sta(self.datas) # ini_centers = [self.datas[random.randint(0,len(self.datas)-1)] for i in range(k)] ini_centers = random.sample(self.datas, k) while 1: ini_cls = {cent: [] for cent in ini_centers} for data in self.datas: sho_dis, cls = float("inf"), None for cent in ini_centers: dis = data.distance(cent) if dis < sho_dis: sho_dis, cls = dis, cent ini_cls[cls].append(data) new_centers = [ Vec.get_clustercenter(ini_cls[key]) for key in ini_cls ] if Vec.is_converg(ini_centers, new_centers, 1e-5): self.cls = ini_cls break else: ini_centers = new_centers
def __init__(self, datas: list, k: int): self.datas = [Vec(data[:-1]) for data in datas] Vec.sta(self.datas) self.lab = {self.datas[i]: datas[i][-1] for i in range(len(datas))} self.k = int(sqrt(len(datas))) if k > sqrt(len(datas)) else k