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([ algebra.distance(input1, input2) for input1 in get_values(cluster1) for input2 in get_values(cluster2) ])
def knn_classify(k, labeled_points, new_point): """each labeled point should be a pair (point, label)""" by_distance = sorted(labeled_points, key=lambda (point, _): algebra.distance(point, new_point)) k_nearest_labels = [label for _, label in by_distance[:k]] return majority_vote(k_nearest_labels)
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([algebra.distance(input1, input2) for input1 in get_values(cluster1) for input2 in get_values(cluster2)])