def delete_longer_edges(points, k): k -= 1 distance_list = [] used = set() for point in points: if used.__contains__(point): continue queue = [point] while len(queue) > 0: v = queue.pop(0) used.add(v) for vertex in v.vertex_set: if not used.__contains__(vertex): distance_list.append(dist(v.x, v.y, vertex.x, vertex.y)) queue.append(vertex) distance_list.sort(reverse=True) for i in range(k): is_deleted = False for point in points: if is_deleted: break for neighbour in point.vertex_set: if dist(neighbour.x, neighbour.y, point.x, point.y) == distance_list[i]: neighbour.vertex_set.remove(point) point.vertex_set.remove(neighbour) is_deleted = True break
def calculate_decision_function(points, centers): function_result = 0 for point in points: probability = point.cluster_membership_probability for i in range(0, len(probability)): function_result += probability[i] * dist(point.x, point.y, centers[i].x, centers[i].y) return function_result
def noise_distribution(points): for noise in points: if noise.noise: noise.noise = False min_dist = 1000 for point in points: if not point.noise: current_min = dist(noise.x, noise.y, point.x, point.y) if point != noise and min_dist > current_min: noise.cluster = point.cluster min_dist = current_min
def min_dist_find(current, points): min_dist = None next = None for point in points: if point == current: continue candidate_dist = dist(current.x, current.y, point.x, point.y) if min_dist is None or candidate_dist < min_dist: next = point min_dist = candidate_dist return next
def init_centroid(points, k, x_center, y_center): R = 0 n = len(points) for i in range(0, n): r = dist(x_center, y_center, points[i].x, points[i].y) if r > R: R = r x_cc = [R * np.cos(2 * np.pi * i / k) + x_center for i in range(k)] y_cc = [R * np.sin(2 * np.pi * i / k) + y_center for i in range(k)] result = [] for i in range(0, k): result.append(Point(x_cc[i], y_cc[i])) return result
def find_start(points): min_distance = None start = None neighbour = None for point in points: candidate = min_dist_find(point, points) candidate_dist = dist(candidate.x, candidate.y, point.x, point.y) if min_distance is None or min_distance > candidate_dist: min_distance = candidate_dist start = point neighbour = candidate start.vertex_set.add(neighbour) neighbour.vertex_set.add(start) return start
def knn(clusters, test_data, k, clusters_count): for point in test_data: distance_list = [] for cluster in clusters: for cluster_element in cluster.elements: distance_list.append( ClassifierKnn( cluster.number, dist(point.x, point.y, cluster_element.x, cluster_element.y))) sorted_distance_list = sort_classifier_data(distance_list) k_minimal_distance_list = sorted_distance_list[0:k] cluster_counter = [0 for i in range(clusters_count)] for classifier in k_minimal_distance_list: cluster_counter[classifier.cluster] += 1 max_cluster_hit = 0 counter = 0 for i in range(clusters_count): if cluster_counter[i] > counter: counter = cluster_counter[i] max_cluster_hit = i point.cluster = max_cluster_hit
def find_neighbors(point, points, eps): result = set() for candidate in points: if dist(point.x, point.y, candidate.x, candidate.y) <= eps: result.add(candidate) return result
def membership_coefficient(cluster_num, centers, point, m): result = 0 distance_for_cluster = dist(centers[cluster_num].x, centers[cluster_num].y, point.x, point.y) for center in centers: result += pow(distance_for_cluster / dist(center.x, center.y, point.x, point.y), 2 / (1 - m)) return result