Java Cosine Similarity cosineSimilarity(double[] vector1, double[] vector2)

Here you can find the source of cosineSimilarity(double[] vector1, double[] vector2)

Description

cosine Similarity

License

Open Source License

Declaration

private static double cosineSimilarity(double[] vector1, double[] vector2) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    private static double cosineSimilarity(double[] vector1, double[] vector2) {
        double dotProduct = dotProduct(vector1, vector2);
        double euclideanDist = euclideanDistance(vector1) * euclideanDistance(vector2);
        return dotProduct / euclideanDist;
    }/*from  w w  w . j  av  a  2s  .c  om*/

    private static double dotProduct(double[] vector1, double[] vector2) {
        double result = 0.0;

        for (int i = 0; i < vector1.length; i++) {
            result += vector1[i] * vector2[i];
        }

        return result;
    }

    private static double euclideanDistance(double[] vector) {
        double result = 0.0;

        for (double aVector : vector) {
            result += aVector * aVector;
        }

        return Math.sqrt(result);
    }
}

Related

  1. cosDegrees(double angleInDegrees)
  2. cosDistance(float[] v1, float[] v2)
  3. cosine_similarity(double[] vec1, double[] vec2)
  4. cosineLawGetDegree(double a, double b, double c)
  5. cosineSim(double[] a, double[] b)
  6. cosineSimilarity(double[] x, double[] y)
  7. cosineSimilarity(float[] f1, float[] f2)
  8. cosineSimilarity(float[] vectorA, float[] vectorB)
  9. cosineSimilarity(String[] tkn0, String[] tkn1)