Java Cosine Similarity cosine_similarity(double[] vec1, double[] vec2)

Here you can find the source of cosine_similarity(double[] vec1, double[] vec2)

Description

cosinsimilarity

License

Apache License

Declaration

public static double cosine_similarity(double[] vec1, double[] vec2) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    public static double cosine_similarity(double[] vec1, double[] vec2) {
        double cosim = vector_dot(vec1, vec2) / (vector_norm(vec1) * vector_norm(vec2));
        return cosim;
    }//from  w w w. j  a  va 2s . c  o m

    public static double vector_dot(double[] vec1, double[] vec2) {
        double sum = 0;
        for (int i = 0; i < vec1.length && i < vec2.length; i++)
            sum += vec1[i] * vec2[i];
        return sum;
    }

    public static double vector_norm(double[] vec) {
        double sum = 0;
        for (double v : vec)
            sum += v * v;
        return Math.sqrt(sum);
    }
}

Related

  1. cosDegrees(double angleInDegrees)
  2. cosDistance(float[] v1, float[] v2)
  3. cosineLawGetDegree(double a, double b, double c)
  4. cosineSim(double[] a, double[] b)
  5. cosineSimilarity(double[] vector1, double[] vector2)
  6. cosineSimilarity(double[] x, double[] y)