def cluster_distance(cluster1, cluster2, distance_agg=min): """finds the aggregate distance between elements of cluster1 and elements of cluster2""" return distance_agg([ 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)""" # 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]] # and let them vote return majority_vote(k_nearest_labels)
y, theta_0, alpha_0) if __name__ == "__main__": print "using the gradient" v = [random.randint(-10, 10) for i in range(3)] tolerance = 0.0000001 count = 0 while True: #print v, sum_of_squares(v) gradient = sum_of_squares_gradient(v) # compute the gradient at v next_v = step(v, gradient, -0.01) # take a negative gradient step if distance(next_v, v) < tolerance: # stop if we're converging break v = next_v # continue if we're not # print v count += 1 print(str(sum_of_squares(v)) + str(v)) print "count=" + str(count) print "minimum v", v print "minimum value", sum_of_squares(v) print print "using minimize_batch" v = [random.randint(-10, 10) for i in range(3)]
def random_distances(dim, num_pairs): return [distance(random_point(dim), random_point(dim)) for _ in range(num_pairs)]