Java Cosine Similarity cosineSimilarity(double[] x, double[] y)

Here you can find the source of cosineSimilarity(double[] x, double[] y)

Description

cosine Similarity

License

Apache License

Declaration

public static double cosineSimilarity(double[] x, double[] y) 

Method Source Code

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

public class Main {
    public static double cosineSimilarity(double[] x, double[] y) {
        if (x.length != y.length) {
            throw new IllegalArgumentException("Vector dimensions should be equal");
        }//from  w w  w  . j  a v a  2  s.  c o m
        double xySum = 0;
        double xSum = 0;
        double ySum = 0;
        for (int i = 0; i < x.length; i++) {
            xySum += x[i] * y[i];
            xSum += x[i] * x[i];
            ySum += y[i] * y[i];
        }
        return xySum / (Math.sqrt(xSum) * Math.sqrt(ySum));
    }
}

Related

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