def clusterDistance(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 getValues(cluster1) for input2 in getValues(cluster2) ])
def knnClassify(k, labeledPoints, newPoint): byDistance = sorted( labeledPoints, key=lambda pointlabel: distance(pointlabel[0], newPoint)) kNearestLabels = [label for _, label in byDistance[:k]] return majorityVote(kNearestLabels)
def find_eigenvector(A, tolerance=0.00001): guess = [1 for __ in A] while True: result = matrix_operate(A, guess) length = magnitude(result) next_guess = scalarMultiply(1 / length, result) if distance(guess, next_guess) < tolerance: return next_guess, length # eigenvector, eigenvalue guess = next_guess
def find_eigenvector(A, tolerance=0.00001): guess = [1 for __ in A] while True: result = matrix_operate(A, guess) length = magnitude(result) next_guess = scalarMultiply(1/length, result) if distance(guess, next_guess) < tolerance: return next_guess, length # eigenvector, eigenvalue guess = next_guess
def knnClassify(k, labeledPoints, newPoint): byDistance = sorted(labeledPoints, key=lambda pointlabel: distance(pointlabel[0], newPoint)) kNearestLabels = [label for _, label in byDistance[:k]] return majorityVote(kNearestLabels)
def clusterDistance(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 getValues(cluster1) for input2 in getValues(cluster2)])