def cluster(self, points, kernel_bandwidth, iteration_callback=None): if (iteration_callback): iteration_callback(points, 0) shift_points = np.array(points) max_min_dist = 1 iteration_number = 0 still_shifting = [True] * points.shape[0] while max_min_dist > MIN_DISTANCE: # print max_min_dist max_min_dist = 0 iteration_number += 1 for i in range(0, len(shift_points)): if not still_shifting[i]: continue p_new = shift_points[i] p_new_start = p_new p_new = self._shift_point(p_new, points, kernel_bandwidth) dist = ms_utils.euclidean_dist(p_new, p_new_start) if dist > max_min_dist: max_min_dist = dist if dist < MIN_DISTANCE: still_shifting[i] = False shift_points[i] = p_new if iteration_callback: iteration_callback(shift_points, iteration_number) point_grouper = pg.PointGrouper() group_assignments = point_grouper.group_points(shift_points.tolist()) return MeanShiftResult(points, shift_points, group_assignments)
def cluster(self, points, distance, kernel_bandwidth, iteration_callback=None): """Recibe una lista de puntos, el tamaño de la distancia a tener en cuenta al utilizar el Kernel, y un parametro para sacar los estados a mitad del algoritmo(Debug). """ if (iteration_callback): iteration_callback(points, 0) shift_points = np.array(points) max_min_dist = 1 dist = 0 iteration_number = 0 still_shifting = [True] * points.shape[0] # Cambiado a 100 iteraciones para que pare y no se quede dando vueltas en el etereo infinito. # Con 100 iteraciones suele sobrar para que se clasifiquen casi todos los puntos. for _ in range(50): # Imprime por pantalla la maxima de las miminas distancias de movimiento de los puntos, # y cuantos quedan por mover. print(max_min_dist) print("Puntos en movimiento: {}".format( len([x for x in still_shifting if x == True]))) max_min_dist = 0 iteration_number += 1 for i in range(0, len(shift_points)): if not still_shifting[i]: continue p_new = shift_points[i] p_new_start = p_new # Para el shifting solo consideramos los puntos dentro del vecindario del punto que estamos moviendo. p_new = self._shift_point( p_new_start, ms_utils.neighbourhood_points(shift_points, p_new_start, distance), kernel_bandwidth) # Distancia euclidea entre el punto antiguo y el movido dist = ms_utils.euclidean_dist(p_new, p_new_start) if dist > max_min_dist: max_min_dist = dist if dist < MIN_DISTANCE: still_shifting[i] = False shift_points[i] = p_new dist_after = dist if iteration_callback: iteration_callback(shift_points, iteration_number) point_grouper = pg.PointGrouper() group_assignments = point_grouper.group_points(shift_points.tolist()) return MeanShiftResult(points, shift_points, group_assignments)
def filt(self): #直接从data里面过滤去 point_grouper = pg.PointGrouper() for t in self.typelist: group_assignments = point_grouper.group_points(self.data[t].tolist()) tmplist=self.data[t].tolist() tmplist1=copy.deepcopy(tmplist) for i in self.getunsameindex(group_assignments): val=tmplist1[i] tmplist.remove(val) self.data[t]=np.array(tmplist)
def cluster(self, points, kernel_bandwidth, pressureWeights, iteration_callback=None): if (iteration_callback): iteration_callback(points, 0) shift_points = np.array(points) max_min_dist = 1 iteration_number = 0 still_shifting = [True] * points.shape[0] old_max_min_dist = max_min_dist + 1 while max_min_dist > MIN_DISTANCE and np.linalg.norm( old_max_min_dist - max_min_dist) > 0.0001 and iteration_number < 50: print('old_max_min_dis is ' + str(old_max_min_dist)) print('max_min_dis is ' + str(max_min_dist)) print('diff is ' + str(np.linalg.norm(old_max_min_dist - max_min_dist))) # print max_min_dist old_max_min_dist = max_min_dist max_min_dist = 0 iteration_number += 1 for i in range(0, len(shift_points)): if not still_shifting[i]: continue p_new = shift_points[i] p_new_start = p_new p_new = self._shift_point(p_new, points, kernel_bandwidth, pressureWeights) dist = ms_utils.euclidean_dist(p_new, p_new_start) if dist > max_min_dist: max_min_dist = dist if dist < MIN_DISTANCE: still_shifting[i] = False shift_points[i] = p_new if iteration_callback: iteration_callback(shift_points, iteration_number) point_grouper = pg.PointGrouper() group_assignments = point_grouper.group_points(shift_points.tolist()) return MeanShiftResult(points, shift_points, group_assignments)
def cluster(self, points, kernel_bandwidth, iteration_callback=None): if (iteration_callback): iteration_callback(points, 0) #将这些点的集合变成np的数组 shift_points = np.array(points) #最大最小距离? max_min_dist = 1 iteration_number = 0 #shape[0] 代表行数,[true........] 多少个点就多少个,这就是个标志位 still_shifting = [True] * points.shape[0] #应该是这个最小的欧氏距离小到一定的程度,我就停止移位我的点 while max_min_dist > MIN_DISTANCE: # print max_min_dist max_min_dist = 0 iteration_number += 1 #这个小循环遍历每一个点集合X for i in range(0, len(shift_points)): if not still_shifting[i]: #标志位,如果没有漂移过 continue p_new = shift_points[i] p_new_start = p_new #这个点已经漂移完了 p_new = self._shift_point(p_new, points, kernel_bandwidth) #计算漂移的欧氏距离 dist = ms_utils.euclidean_dist(p_new, p_new_start) #一轮轮得到最小的欧氏距离 if dist > max_min_dist: max_min_dist = dist if dist < MIN_DISTANCE: still_shifting[i] = False shift_points[i] = p_new if iteration_callback: iteration_callback(shift_points, iteration_number) point_grouper = pg.PointGrouper() group_assignments = point_grouper.group_points(shift_points.tolist()) return MeanShiftResult(points, shift_points, group_assignments)