#! /Usr/bin/pythonfrom math import sqrtdef Euclidean (V1, V2): length = min (LEN (V1), Len (V2) If length = 0: return 0 D = 0 for I in range (length): D + = POW (V1 [I]-V2 [I]), 2) # Return SQRT (d) return 1/float (1 + d) def cosine (V1, V2): length = min (LEN (V1), Len (V2) If length = 0: return 0 dp = 0 # dot product M1 = 0 # modulus of V 1 m2 = 0 # modulus of V2 for I in range (length ): DP + = V1 [I] * V2 [I] M1 + = V1 [I] * V1 [I] M2 + = v2 [I] * V2 [I] If M1 = 0 or M2 = 0: return 0 distance = DP/(SQRT (M1) * SQRT (m2) return distancedef Pearson (V1, V2): length = min (LEN (V1), Len (V2 )) if length = 0: Return 0 # E of V1 V2 e1 = 0 e2 = 0 for I in range (length ): e1 + = V1 [I] E2 + = v2 [I] E1/= float (length) E2/= float (length) cov = 0 # cov of V1 V2 d1 = 0 # variance of V2 D2 = 0 # variance of V2 for I in range (length ): diff1 = V1 [I]-E1 diff2 = v2 [I]-E2 cov + = diff1 * diff2 D1 + = diff1 * diff1 D2 + = diff2 * diff2 cov/= float (Length) d1/= float (length) D2/= float (length) If d1 = 0 or D2 = 0: Return 0 return cov/SQRT (D1 * D2)