List of usage examples for org.apache.mahout.cf.taste.impl.common FastIDSet intersectionSize
public int intersectionSize(FastIDSet other)
From source file:com.yelp.recommend.RestaurantSimilarity.java
License:Apache License
@Override public double itemSimilarity(long itemID1, long itemID2) { if (itemID1 == itemID2) { return 1.0; }//from w w w .j a v a2 s .c om RestaurantData data1 = RestaurantData.get(itemID1); RestaurantData data2 = RestaurantData.get(itemID2); if (data1 == null || data2 == null) { return 0.0; } // Tanimoto coefficient similarity based on attributes of restaurant FastIDSet attributes1 = data1.getAttributeSet(); FastIDSet attributes2 = data2.getAttributeSet(); if (attributes1 == null || attributes2 == null) { return 0.0; } int intersectionSize = attributes1.intersectionSize(attributes2); if (intersectionSize == 0) { return 0.0; } int unionSize = attributes1.size() + attributes2.size() - intersectionSize; return (double) intersectionSize / (double) unionSize; }
From source file:edu.uci.ics.sourcerer.ml.HammingDistanceSimilarity.java
License:Open Source License
@Override public double userSimilarity(long userID1, long userID2) throws TasteException { FastIDSet prefs1 = dataModel.getItemIDsFromUser(userID1); FastIDSet prefs2 = dataModel.getItemIDsFromUser(userID2); int prefs1Size = prefs1.size(); int prefs2Size = prefs2.size(); int intersectionSize = prefs1Size < prefs2Size ? prefs2.intersectionSize(prefs1) : prefs1.intersectionSize(prefs2); if (intersectionSize == 0) { return Double.NaN; }//from w w w . ja v a 2s . c o m //int numItems = dataModel.getNumItems(); //int numItems = Math.max(prefs1Size, prefs2Size); //int numItems = prefs1Size; // double distance1 = (double) (prefs1Size - intersectionSize)/(double) 2; // double distance2 = (double) (prefs2Size - intersectionSize)/(double) 2; // double distance = (distance1 + distance2); double distance = (double) (prefs1Size - intersectionSize); double similarity = 1.0 / (1.0 + distance); //////System.out.println( prefs1Size + ", " + prefs2Size + ", " + intersectionSize + ", " + similarity); return similarity; }
From source file:norbert.mynemo.core.recommendation.similarity.OriginalSpearmanCorrelationSimilarity.java
License:Apache License
/** * Returns the number of items that are rated by the two given users. An item rated by only one * user is not counted.//from w ww .ja v a 2s . co m */ private int numCommonPreferences(final long userId1, final long userId2) throws TasteException { final FastIDSet prefs1 = dataModel.getItemIDsFromUser(userId1); final FastIDSet prefs2 = dataModel.getItemIDsFromUser(userId2); return prefs1.size() < prefs2.size() ? prefs2.intersectionSize(prefs1) : prefs1.intersectionSize(prefs2); }