def knn_classify(k, labeled_points, new_point): """each labeled point should be a pair (point, label)""" # order the labeled points from nearest to farthest by_distance = sorted(labeled_points, key=lambda (point, _): distance(point, new_point)) # find the labels for the k closest k_nearest_labels = [label for _, label in by_distance[:k]] # voting return majority_votes(k_nearest_labels)
def random_distances(dim, num_pairs): return [distance(random_point(dim), random_point(dim)) for _ in range(num_pairs)]
def random_distances(dim, num_pairs): return [ distance(random_point(dim), random_point(dim)) for _ in range(num_pairs) ]
def cluster_distance(cluster1, cluster2, distance_agg=min): """compute all the pairwise distances between cluster1 and cluster2 and apply _distance_agg_ to the resulting list""" return distance_agg([distance(input1, input2) for input1 in get_values(cluster1) for input2 in get_values(cluster2)])