-2

I'm fairly new to python so I hope somebody can help me. we have two arrays X and Y

X=np.array([[ 5.43840675, -1.05259078, -0.21793506,  8.56686818, -2.58056957,
        -0.07310339, -0.31181501,  0.02696586],
       [ 5.72318296, -0.99665473, -0.14540062,  8.32051008, -3.36201189,
        -0.04897565, -0.34271698, -0.0339766 ]])

Y=np.array([[ 5.72318296, -0.99665473, -0.14540062,  8.32051008, -3.36201189,
        -0.04897565, -0.34271698, -0.0339766 ],
       [ 5.43840675, -1.05259078, -0.21793506,  8.56686818, -2.58056957,
        -0.07310339, -0.31181501,  0.02696586]])

I was interested in calculating Euclidean distance of two numpy arrays (X and Y). for example the distance between X[1], Y[0] should be zero since there is no difference between those vectors.

X[1]=[ 5.72318296, -0.99665473, -0.14540062,  8.32051008, -3.36201189, -0.04897565, -0.34271698, -0.0339766 ] is equal to 
Y[0]=[ 5.72318296, -0.99665473, -0.14540062,  8.32051008, -3.36201189, -0.04897565, -0.34271698, -0.0339766 ]

I need to get this idea to work so if anyone can show me what I need to do to get this to work I would really appreciate it.

4
  • 1
    There is a very clear formula to calculate Euclidean distance. What did you try? Commented Aug 8, 2020 at 22:02
  • Does this help you? reference Commented Aug 8, 2020 at 22:02
  • 1
    @ DeepSpace clear formula did not help because I have vectors inside an array Commented Aug 9, 2020 at 7:52
  • @MisterNox thank you but it's not help Commented Aug 9, 2020 at 7:52

1 Answer 1

1

based on the Euclidean distance formula:

import math
x = [ 5.72318296, -0.99665473, -0.14540062,  8.32051008, -3.36201189, -0.04897565, -0.34271698, -0.0339766 ]
y = [ 5.72318296, -0.99665473, -0.14540062,  8.32051008, -3.36201189, -0.04897565, -0.34271698, -0.0339766 ]
Index = 0
sum = 0
while Index < len(x):
    sum += (x[Index] - y[Index]) ** 2
    Index += 1
print(math.sqrt(sum))
Sign up to request clarification or add additional context in comments.

2 Comments

thank you but this is not what i want, i have vectors inside an array not simple array !!!!!!!!! @pooya
just wrap this code in a function and call it for each pair of vectors.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.