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 predict(self, sam: list): sam = Vec(sam) dis_dict = {} for data in self.datas: dis = sam.distance(data) if dis not in dis_dict: dis_dict[dis] = [data] else: dis_dict[dis].append(data) largest_dis = heapq.nlargest(self.k, dis_dict.keys()) kn_datas = [] for dis in largest_dis: kn_datas.extend(dis_dict[dis]) if len(kn_datas) >= self.k: break fre = {} for data in kn_datas: fre[self.lab[data]] = fre.get(self.lab[data], 0) + 1 lab, max_fre = None, 0 for k in fre: if fre[k] > max_fre: lab, max_fre = k, fre[k] return lab
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
def rows(self): l = [] for row in self.mat: l.append(Vec(row)) return l
def egi_vecs(self): url = "http://www.yunsuanzi.com/cgi-bin/eigen_decomp.py" text = egi.send_to_ysz(self.mat, url) return [Vec(vec) for vec in egi.parse_egi_result(text)[1]]
def orth_by_row(self): row_vecs = self.rows() return Mat.construct_by_rows(Vec.orthogonal(row_vecs))
def orth_by_col(self): col_vecs = self.cols() return Mat.conconstruct_by_cols(Vec.orthogonal(col_vecs))
def getCol(self, n): return Vec([self.mat[row][n] for row in range(len(self.mat))])
def getRow(self, n): return Vec(self.mat[n][:])