I have this function to work out the cosine between two vectors, except it isn't working for some reason.
def length(x):
return math.sqrt(x.dot(x))
def unitVectorNormalize(u):
if length(u) == 0:
return "Your vector is null."
return u / length(u)
def cosine(vector1, vector2):
return vector1.dot(vector2) / (unitVectorNormalize(vector1) * unitVectorNormalize(vector2))
Is there something I am doing wrong?
I am aware that there is a NumPy method to figure out the cosine similarity, but I am told (for my course) to write my own fucntion.