Files
  • main.py
main.py
1
2
3
4
5
6
7
8
9
10
11
12
import math

def length(x):
    return math.sqrt(x[0]*x[0] + x[1]*x[1])

def cosine_similarity(x, y):
    return (x[0]*y[0] + x[1]*y[1])/length(x)/length(y)

def cosine_distance(x, y):
    return 1 - cosine_similarity(x, y)

print cosine_distance([6.0, 6.0], [9.0, 9.0]) < 0
Python 2.7.10 (default, Jul 14 2015, 19:46:27) [GCC 4.8.2] on linux