I am trying to calculate Euclidean distance in python using the following steps outlined as comments. It keeps on saying my calculation is wrong. I'm not sure why.
def euclidean_distance(vector1 , vector2):
## the sum_squares variable will contain the current value of the sum of squares of each i-th coordinate pair
sum_squares = 0
numberOfIterations = len(vector1)
## TODO: Complete loop below ##
# The number of times the loop will be executed is the length of the vectors.
#
# At each loop iteration, you will:
# Step 1. index into each vector and find the difference between
#the ith element in vector2 and vector1
# Step 2. square the difference
# Step 3. update the value of the 'sum_squares' variable by
#adding the result in Step 2 to
# the existing value of sum_squares
for i in range(numberOfIterations):
# Inside this loop follow steps 1-3 to update the value of THE
#'sum_squares' variable by adding the squared difference of the i'th coordinate pair to the sum.
dist = ((vector2[0] - vector1[i]) **2 (vector1[0] - vector2[i]) **2)
sum_squares = sum_squares dist
### TODO: Compute the Distance ###
# Compute the square root of the variable 'sum_squares' and assign
# that result to a new variable named 'distance'
import math
distance = math.sqrt(sum_squares)
# return the Euclidean distance
return distance
CodePudding user response:
The problem is just with your code of calculating the distance.
# Step 1. index into each vector and find the difference between
#the ith element in vector2 and vector1
# Step 2. square the difference
Using these two steps, the correct line of code should be:
dist = (vector2[i] - vector1[i]) ** 2
This will square the distance between the i'th coordinate pair, and the next line will add it to the variable sum_squares
We can see that changing this line of code will give us the correct answer:
print(euclidean_distance([1,2],[2,3])) #1.414
print(euclidean_distance([1,2,3],[4,8,10])) #9.695
as:
sqrt( (2 - 1)^2 (3 - 2)^2 ) = sqrt(2) = 1.414
sqrt( (4 - 1)^2 (8 - 2)^2 (10 - 3)^2 ) = sqrt(94) = 9.695
I hope this helped! Let me know if you need any further help or clarification :)
CodePudding user response:
Iteration over the lists (vectors) is best achieved with zip(). Summation of the squares is best achieved with sum(). Therefore:
def euclidean_distance(vx, vy):
return sum((y-x)**2 for x, y in zip(vx, vy)) ** 0.5
print(euclidean_distance([1,2,3,4],[4,8,10,12]))
Output:
12.569805089976535