Cosine Similarity Code (non-urgent vectors)

I try to find the cosine similarity between two vectors (x, y Points), and I am making some kind of silly mistake that I cannot nail. Forgive me for being a beginner, and sorry if I make a very simple mistake (which is most likely me).

thanks for the help

public static double GetCosineSimilarity(List<Point> V1, List<Point> V2) { double sim = 0.0d; int N = 0; N = ((V2.Count < V1.Count)?V2.Count : V1.Count); double dotX = 0.0d; double dotY = 0.0d; double magX = 0.0d; double magY = 0.0d; for (int n = 0; n < N; n++) { dotX += V1[n].X * V2[n].X; dotY += V1[n].Y * V2[n].Y; magX += Math.Pow(V1[n].X, 2); magY += Math.Pow(V1[n].Y, 2); } return (dotX + dotY)/(Math.Sqrt(magX) * Math.Sqrt(magY)); } 

Edit: Besides the syntax, my question also related to the logical construction, given that I am dealing with vectors of various lengths. In addition, as described above, it is generalized to vectors of m dimensions. thanks

+6
source share
2 answers

If you are in two-dimensional space, then you can have vectors represented as (V1.X, V1.Y) and (V2.X, V2.Y) , and then use

 public static double GetCosineSimilarity(Point V1, Point V2) { return (V1.X*V2.X + V1.Y*V2.Y) / ( Math.Sqrt( Math.Pow(V1.X,2)+Math.Pow(V1.Y,2)) Math.Sqrt( Math.Pow(V2.X,2)+Math.Pow(V2.Y,2)) ); } 

If you are in higher dimensions, you can represent each vector as a List<double> . So, in the four-dimensional case, the first vector will have the components V1 = (V1[0], V1[1], V1[2], V1[3]) .

 public static double GetCosineSimilarity(List<double> V1, List<double> V2) { int N = 0; N = ((V2.Count < V1.Count) ? V2.Count : V1.Count); double dot = 0.0d; double mag1 = 0.0d; double mag2 = 0.0d; for (int n = 0; n < N; n++) { dot += V1[n] * V2[n]; mag1 += Math.Pow(V1[n], 2); mag2 += Math.Pow(V2[n], 2); } return dot / (Math.Sqrt(mag1) * Math.Sqrt(mag2)); } 
+11
source

The last line should be

 return (dotX + dotY)/(Math.Sqrt(magX) * Math.Sqrt(magY)) 
+1
source

Source: https://habr.com/ru/post/898107/


All Articles