mathajax

Cosine Similarity

The Java code measure the similarity between two vectors using cosine similarity formula. The vector's element can be integer or double of Java datatype.

The cosine similarity of two vectors found by a ratio of  dot product of those vectors and their magnitude.

Cosine Similarity


Dot product of 2-D Vector


Dot product of N-D Vector


Vector Magnitude




Cosine Similarity of 2-D vectors

A class Cosine defined two member functions named "similarity" with parameter type difference, in order to support  parameters  type int and double 2-D vectors. 
.
Both class (static) member function similarity can be invoked with two array parameters, which represents the vectors to measure similarity between them. These vectors must have 2 elements.

for example
the vectors  A =[2 3], B=[3 1]

Dot product of two vectors A.B

             A.B = 2x3 + 3x1
                 = 6  + 3
            A.B   = 9

Magnitude of vector A

             |A| =  sqrt ( 2^2 + 3^2 )         
                 =  sqrt ( 4 + 9 )
                 =  sqrt (13)
                 = 3.605

Magnitude of vector B

             |B| =  sqrt ( 3^2 + 1^2 )         
                 =  sqrt ( 9 + 1 )
                 =  sqrt (10)
                 = 3.162
Cosine similarity of vector A and B

                similarity =  9 / ( 3.605 * 3.162)
                            = 9 / 11.4
                            = 0.789

       



import java.util.Arrays;

public class Cosine {

 
public static double similarity(int vec1[],int vec2[]) 
  {
  
    int dop=vec1[0]*vec2[0] +  vec1[1]*vec2[1];    
    double mag1 =Math.sqrt(Math.pow(vec1[0],2)+Math.pow(vec1[1],2));
    double mag2 =Math.sqrt(Math.pow(vec2[0],2)+Math.pow(vec2[1],2));    
    double csim =dop / (mag1 * mag2);
    
    return csim; 
  }
 
 
public static double similarity(double vec1[],double vec2[]) 
  {
  
   double dop=vec1[0]*vec2[0] +  vec1[1]*vec2[1];    
   double mag1=Math.sqrt(Math.pow(vec1[0],2) + Math.pow(vec1[1],2));
   double mag2=Math.sqrt(Math.pow(vec2[0],2) + Math.pow(vec2[1],2));    
   double csim=dop/ (mag1 * mag2);
    
   return csim; 
  }
 
 
 public static void main(String[] args) 
 {
  
      int vector1 [] = {1,2};
      int vector2 [] = {4,5};
      double csim1= Cosine.similarity(vector1, vector2);
            
      System.out.println("\n Cosine  similarity between two 2D vectors");
      System.out.println("integer 2D vectors");
      System.out.println("Vector 1 :" + Arrays.toString(vector1));
      System.out.println("Vector 2 :" + Arrays.toString(vector2));
      System.out.println("similarity value :" + csim1);
      
      
      double vector3 [] = {5.2, 2.6};
      double vector4 [] = {9.8,7.6};
      System.out.println("double 2D vectors");
      double csim2=Cosine.similarity(vector3, vector4);
      System.out.println("Vector 1 :" + Arrays.toString(vector3));
      System.out.println("Vector 2 :" + Arrays.toString(vector4));
      System.out.println("similarity value :" + csim2);
      
                              
 }

}


Output
Cosine similarity between two 2D vectors

integer 2D vectors
Vector 1 :[1, 2]
Vector 2 :[4, 5]
similarity value :0.9778024140774094

double 2D vectors
Vector 1 :[5.2, 2.6]
Vector 2 :[9.8, 7.6]
similarity value :0.9808570865254814



Cosine Similarity for N-Dimensional vectors 

A class Cosine defined two member functions named "similarity" with parameter type difference, in order to support  parameters  type int and double of N dimensional vectors. 
.
Both member function can be invoked with two array parameters, which represents the vectors to measure similarity between them. These vectors must have N elements.



import java.util.Arrays;

public class Cosinend {

 
 public static double similarity(int vec1[],int vec2[]) 
 {
  
    int dop=0;
    for (int n=0;n<vec1.length;n++)
       dop +=vec1[n]*vec2[n] ;
    
     double mag1=0.0,mag2=0.0;    
     for (int n=0;n<vec1.length;n++) 
   {
     mag1 += Math.pow(vec1[n],2) ;
     mag2 += Math.pow(vec2[n],2) ;
   }
   
   mag1=Math.sqrt(mag1);
   mag2=Math.sqrt(mag2);
   
     double csim = dop / (mag1 * mag2);    
     return csim; 
 }
 
 
 public static double similarity(double vec1[],double vec2[]) 
 {  
     double dop=0;
     for (int n=0;n<vec1.length;n++)
        dop +=vec1[n]*vec2[n] ;
     
   double mag1=0.0,mag2=0.0;    
   for (int n=0;n<vec1.length;n++) 
     {
    mag1 +=  Math.pow(vec1[n],2) ;
    mag2 +=  Math.pow(vec2[n],2) ;
      }
    
     mag1=Math.sqrt(mag1);
     mag2=Math.sqrt(mag2);
    
    double csim = dop / (mag1 * mag2);    
    return csim;       
 }
 
public static void main(String[] args) 
  {

         int vector1 [] = {2,2,3,5};
  int vector2 [] = {4,1,2,1};
  double csim1= Cosinend.similarity(vector1, vector2);
             
     System.out.println("\n Cosine  similarity between two N-D vectors");
     System.out.println("integer N-D vectors");
     System.out.println("Vector 1 :" + Arrays.toString(vector1));
     System.out.println("Vector 2 :" + Arrays.toString(vector2));
     System.out.println("similarity value :" + csim1);
       
       
 double vector3 [] = {5.2,2.6,1.4,5.0};
 double vector4 [] = {9.8,7.6,3.2,4.6};
 System.out.println("double ND vectors");
 double csim2=Cosinend.similarity(vector3, vector4);
      System.out.println("Vector 1 :" + Arrays.toString(vector3));
      System.out.println("Vector 2 :" + Arrays.toString(vector4));
      System.out.println("similarity value :" + csim2);
   }

}


Output
Cosine  similarity between two N-D vectors

integer N-D vectors
Vector 1 :[2, 2, 3, 5]
Vector 2 :[4, 1, 2, 1]
similarity value :0.6908492797077574

double N-D vectors
Vector 1 :[5.2, 2.6, 1.4, 5.0]
Vector 2 :[9.8, 7.6, 3.2, 4.6]
similarity value :0.9257252592721228


References
Cosine similarity

Comments

Popular posts from this blog

Solving System of Linear Equations by Gauss Jordan Elimination

Matrix Forward and Back Substitution

Solve System of Linear Equations by LU Decompose

Chebyshev distance between two points

Binary 1's and 2's Complement