@Test public void testCosineSimilarity() { TIntDoubleHashMap zeroVector = zeroVector(keyList1); TIntDoubleHashMap testVector1 = testVector(keyList2, 0); TIntDoubleHashMap testVector2 = testVector(keyList2, 1); assertEquals("Cosine similarity between a vector and itself must be 1", 1.0, SimUtils.cosineSimilarity(testVector1, testVector1), 0.0); assertEquals("Cosine similarity between a vector and zero vector must be 0", 0.0, SimUtils.cosineSimilarity(testVector1, zeroVector), 0.0); }
public static double cosineSimilarity(TIntDoubleMap X, TIntDoubleMap Y) { double xDotX = 0.0; double yDotY = 0.0; double xDotY = 0.0; for (int id : X.keys()) { double x = X.get(id); xDotX += x * x; if (Y.containsKey(id)) { xDotY += x * Y.get(id); } } for (double y : Y.values()) { yDotY += y * y; } return xDotX * yDotY != 0 ? xDotY / Math.sqrt(xDotX * yDotY): 0.0; }
SimUtils { public static double cosineSimilarity(TIntDoubleMap X, TIntDoubleMap Y) { double xDotX = 0.0; double yDotY = 0.0; double xDotY = 0.0; for (int id : X.keys()) { double x = X.get(id); xDotX += x * x; if (Y.containsKey(id)) { xDotY += x * Y.get(id); } } for (double y : Y.values()) { yDotY += y * y; } return xDotX * yDotY != 0 ? xDotY / Math.sqrt(xDotX * yDotY): 0.0; } }
SimUtils { public static double cosineSimilarity(TIntDoubleMap X, TIntDoubleMap Y) { double xDotX = 0.0; double yDotY = 0.0; double xDotY = 0.0; for (int id : X.keys()) { double x = X.get(id); xDotX += x * x; if (Y.containsKey(id)) { xDotY += x * Y.get(id); } } for (double y : Y.values()) { yDotY += y * y; } return xDotX * yDotY != 0 ? xDotY / Math.sqrt(xDotX * yDotY): 0.0; }  }
SimUtils { public static double cosineSimilarity(TIntDoubleMap X, TIntDoubleMap Y) { double xDotX = 0.0; double yDotY = 0.0; double xDotY = 0.0; for (int id : X.keys()) { double x = X.get(id); xDotX += x * x; if (Y.containsKey(id)) { xDotY += x * Y.get(id); } } for (double y : Y.values()) { yDotY += y * y; } return xDotX * yDotY != 0 ? xDotY / Math.sqrt(xDotX * yDotY): 0.0; }  static double cosineSimilarity(TIntDoubleMap X, TIntDoubleMap Y); static double cosineSimilarity(TIntFloatMap X, TIntFloatMap Y); static double cosineSimilarity(MatrixRow a, MatrixRow b); static double googleSimilarity(int sizeA, int sizeB, int intersection, int numTotal); static TIntDoubleMap normalizeVector(TIntDoubleMap X); static TIntFloatMap normalizeVector(TIntFloatMap X); static Map sortByValue(TIntDoubleHashMap unsortMap); static WikiBrainScoreDoc[] pruneSimilar(WikiBrainScoreDoc[] wikibrainScoreDocs); static double cosineSimilarity(float[] X, float[] Y); }
SimUtils { public static double cosineSimilarity(TIntDoubleMap X, TIntDoubleMap Y) { double xDotX = 0.0; double yDotY = 0.0; double xDotY = 0.0; for (int id : X.keys()) { double x = X.get(id); xDotX += x * x; if (Y.containsKey(id)) { xDotY += x * Y.get(id); } } for (double y : Y.values()) { yDotY += y * y; } return xDotX * yDotY != 0 ? xDotY / Math.sqrt(xDotX * yDotY): 0.0; }  static double cosineSimilarity(TIntDoubleMap X, TIntDoubleMap Y); static double cosineSimilarity(TIntFloatMap X, TIntFloatMap Y); static double cosineSimilarity(MatrixRow a, MatrixRow b); static double googleSimilarity(int sizeA, int sizeB, int intersection, int numTotal); static TIntDoubleMap normalizeVector(TIntDoubleMap X); static TIntFloatMap normalizeVector(TIntFloatMap X); static Map sortByValue(TIntDoubleHashMap unsortMap); static WikiBrainScoreDoc[] pruneSimilar(WikiBrainScoreDoc[] wikibrainScoreDocs); static double cosineSimilarity(float[] X, float[] Y);  }