0
votes

I have a matrice named vectors[i][j].I would like to calculate cosine similarity between each row. For example for this matrice

    1 0 1 0 1 0 0
v=  0 0 1 1 1 0 1
    1 1 0 0 1 0 1

I want to have similarity calculation ,between row1 and row 2 , row1 and row3, row2 and row3.Further more if similarity between row1 and row2 equal = 0.6 and others 0.5 and 0.4 respectively. I would like to add this value on every element(e=!0) of these to rows and get final matrice like this.

    2.1    0    2.1   0   2.1    0    0
v=  0      0     2    2    2     0    2
    1.9   1.9    0    0   1.9    0   1.9

Here is the part of code where i defined and filled my matrice;

string text = Request.Form["TextBox1"]; ; // text
            string[] textInArray = text.Split(new char[] { '.' }, StringSplitOptions.RemoveEmptyEntries);
            int[,] vectors = new int[textInArray.Length, keywords.Length];

            for (int i = 0; i < textInArray.Length; i++)
            {
                string[] words = textInArray[i].Split(' ');
                for (int j = 0; j < keywords.Length; j++)
                {
                    foreach (var word in words)
                    {
                        if (word.Contains(keywords[j]))
                        {
                            vectors[i, j]++;
                        }
                    }
                }
            }

and Here is my code to calculate similarity but i think it is not complete somewhere I have mistakes and I have no idea how can i add this value on elements of current two rows.

for(i=1 i<matrix.GetLength(0) i++){
   for(j=1 j<matrix.GetLength(0) j++){
            dot += vectors[i] * vectors[j];
            mag1 += Math.Pow(vectors[i], 2);
            mag2 += Math.Pow(vectors[j], 2);
        }

        float M= dot / (Math.Sqrt(mag1) * Math.Sqrt(mag2));  

}
}
1
Removed asp.net tag as question does not seem to relate to asp.net in any way, added C#Andrei

1 Answers

2
votes

Decompose your solution! Extract Similarity method

private static double Similarity(double[] left, double[] right) {
  double ab = 0.0;
  double aa = 0.0;
  double bb = 0.0;

  for (int i = 0; i < left.length; ++i) {
    aa += left[i] * left[i];
    ab += left[i] * right[i];
    bb += right[i] * right[i]; 
  }

  // do not forget degenerated cases: all-zeroes vectors 
  if (aa == 0) 
    return bb == 0 ? 1.0 : 0.0;
  else if (bb == 0) 
    return 0.0;
  else
    return ab / Math.Sqrt(aa) / Math.Sqrt(bb);
}

And then put the simple logic

// vectors[][] is an array of array, so we can get lines easily by vectors[0] etc.
double sim12 = Similarity(vectors[0], vectors[1]);
double sim23 = Similarity(vectors[1], vectors[2]);
double sim13 = Similarity(vectors[0], vectors[2]);

// compare double with tolerance
if ((Math.Abs(sim12 - 0.6) < 1e-10) &&
    (Math.Abs(sim13 - 0.5) < 1e-10) &&
    (Math.Abs(sim23 - 0.4) < 1e-10)) {
  //TODO: update the matrix
}

Edit: since, in fact vectors is double[,] (2d array)

private static double Similarity(double[,] matrix, int left, int right) {
  double ab = 0.0;
  double aa = 0.0;
  double bb = 0.0;

  for (int i = 0; i < matrix.GetLength(1); ++i) {
    aa += matrix[left, i] * matrix[left, i];
    ab += matrix[left, i] * matrix[right, i];
    bb += matrix[right, i] * matrix[right, i]; 
  }

  if (aa == 0) 
    return bb == 0 ? 1.0 : 0.0;
  else if (bb == 0) 
    return 0.0;
  else
    return ab / Math.Sqrt(aa) / Math.Sqrt(bb);
}

....

double sim12 = Similarity(vectors, 0, 1);
double sim23 = Similarity(vectors, 1, 2);
double sim13 = Similarity(vectors, 0, 2);