org.apache.mahout.cf.taste.recommender.RecommendedItem Java Examples

The following examples show how to use org.apache.mahout.cf.taste.recommender.RecommendedItem. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SimpleTest.java    From myrrix-recommender with Apache License 2.0 6 votes vote down vote up
@Test
public void testSet() throws Exception {
  ClientRecommender client = getClient();

  client.setPreference(0L, 1L);
  List<RecommendedItem> recs = client.recommend(0L, 1);
  assertEquals(50L, recs.get(0).getItemID());

  client.setPreference(0L, 2L, 3.0f);
  recs = client.recommend(0L, 1);
  assertEquals(117L, recs.get(0).getItemID());

  client.setPreference(0L, 2L, -3.0f);
  recs = client.recommend(0L, 1);
  assertEquals(117L, recs.get(0).getItemID());

  client.setPreference(0L, 1L, -1.0f);
  // Don't really know/care what will be recommend at this point; the feature vec is nearly 0
  assertEquals(1, client.recommend(0L, 1).size());
}
 
Example #2
Source File: ByValueAscComparator.java    From myrrix-recommender with Apache License 2.0 6 votes vote down vote up
@Override
public int compare(RecommendedItem a, RecommendedItem b) {
  float aValue = a.getValue();
  float bValue = b.getValue();
  if (aValue < bValue) {
    return -1;
  }
  if (aValue > bValue) {
    return 1;
  }
  // Break ties by item ID, *de*scending. It's rare but at least gives predictable ordering.
  long aItem = a.getItemID();
  long bItem = b.getItemID();
  if (aItem > bItem) {
    return -1;
  }
  if (aItem < bItem) {
    return 1;
  }
  return 0;
}
 
Example #3
Source File: OnlineRecommendation.java    From Machine-Learning-in-Java with MIT License 6 votes vote down vote up
public List<RecommendedItem> recommend(long userId, PreferenceArray preferences) throws TasteException {

		if (userExistsInDataModel(userId)) {
			return recommender.recommend(userId, noItems);
		}
		else {
			PlusAnonymousConcurrentUserDataModel plusModel = (PlusAnonymousConcurrentUserDataModel) recommender.getDataModel();

			// Take an available anonymous user form the poll
			Long anonymousUserID = plusModel.takeAvailableUser();

			// Set temporary preferences
			PreferenceArray tempPrefs = preferences;
			tempPrefs.setUserID(0, anonymousUserID);
			// tempPrefs.setItemID(0, itemID);
			plusModel.setTempPrefs(tempPrefs, anonymousUserID);

			List<RecommendedItem> results = recommender.recommend(anonymousUserID, noItems);

			// Release the user back to the poll
			plusModel.releaseUser(anonymousUserID);

			return results;

		}
	}
 
Example #4
Source File: MovieUserRecommender.java    From hiped2 with Apache License 2.0 6 votes vote down vote up
private static void recommend(String ratingsFile, int ... userIds)
    throws TasteException, IOException {
  DataModel model = new FileDataModel(new File(ratingsFile));

  UserSimilarity similarity = new PearsonCorrelationSimilarity(model);

  UserNeighborhood neighborhood =
      new NearestNUserNeighborhood(
          100, similarity, model);

  Recommender recommender =  new GenericUserBasedRecommender(
      model, neighborhood, similarity);

  Recommender cachingRecommender = new CachingRecommender(recommender);

  for(int userId: userIds) {
    System.out.println("UserID " + userId);
    List<RecommendedItem> recommendations =
        cachingRecommender.recommend(userId, 2);
    for(RecommendedItem item: recommendations) {
      System.out.println("  item " + item.getItemID() + " score " + item.getValue());
    }
  }
}
 
Example #5
Source File: EstimatedStrengthEvaluator.java    From myrrix-recommender with Apache License 2.0 6 votes vote down vote up
@Override
public EvaluationResult evaluate(MyrrixRecommender recommender,
                                 RescorerProvider provider, // ignored
                                 Multimap<Long,RecommendedItem> testData) throws TasteException {
  DoubleWeightedMean score = new DoubleWeightedMean();
  int count = 0;
  for (Map.Entry<Long,RecommendedItem> entry : testData.entries()) {
    long userID = entry.getKey();
    RecommendedItem itemPref = entry.getValue();
    try {
      float estimate = recommender.estimatePreference(userID, itemPref.getItemID());
      Preconditions.checkState(LangUtils.isFinite(estimate));
      score.increment(1.0 - estimate, itemPref.getValue());
    } catch (NoSuchItemException nsie) {
      // continue
    } catch (NoSuchUserException nsue) {
      // continue
    }
    if (++count % 100000 == 0) {
      log.info("Score: {}", score);
    }
  }
  log.info("Score: {}", score);
  return new EvaluationResultImpl(score.getResult());
}
 
Example #6
Source File: ClientRecommender.java    From myrrix-recommender with Apache License 2.0 6 votes vote down vote up
private static List<RecommendedItem> consumeItems(URLConnection connection) throws IOException {
  List<RecommendedItem> result = Lists.newArrayList();
  BufferedReader reader = IOUtils.bufferStream(connection.getInputStream());
  try {
    CharSequence line;
    while ((line = reader.readLine()) != null) {
      Iterator<String> tokens = COMMA.split(line).iterator();
      long itemID = Long.parseLong(tokens.next());
      float value = LangUtils.parseFloat(tokens.next());
      result.add(new GenericRecommendedItem(itemID, value));
    }
  } finally {
    reader.close();
  }
  return result;
}
 
Example #7
Source File: TinyTagsTest.java    From myrrix-recommender with Apache License 2.0 6 votes vote down vote up
@Test
public void testTag() throws Exception {
  ClientRecommender client = getClient();
  
  client.setUserTag(5, "bar", -10.0f);
  List<RecommendedItem> recs = client.recommend(5, 1);
  log.info("{}", recs);
  assertEquals(2, recs.get(0).getItemID());
  assertEquals(0.5184667f, recs.get(0).getValue());
  
  client.setItemTag("bing", 4);
  recs = client.recommend(5, 1);
  log.info("{}", recs);
  assertEquals(2, recs.get(0).getItemID());
  assertEquals(0.5184667f, recs.get(0).getValue());
}
 
Example #8
Source File: MostPopularItemsIterator.java    From myrrix-recommender with Apache License 2.0 6 votes vote down vote up
@Override
public RecommendedItem next() {
  FastByIDFloatMap.MapEntry entry = countsIterator.next();
  long id = entry.getKey();
  float value = entry.getValue();
  IDRescorer theRescorer = rescorer;
  if (theRescorer != null) {
    if (theRescorer.isFiltered(id)) {
      return null;
    }
    value = (float) theRescorer.rescore(id, value);
    if (!LangUtils.isFinite(value)) {
      return null;
    }
  }
  delegate.set(id, value);
  return delegate;
}
 
Example #9
Source File: SimpleTest.java    From myrrix-recommender with Apache License 2.0 6 votes vote down vote up
@Test
public void testMostPopular() throws Exception {

  ClientRecommender client = getClient();
  List<RecommendedItem> popular = client.mostPopularItems(3);

  assertNotNull(popular);
  assertEquals(3, popular.size());

  log.info("{}", popular);

  assertEquals(50L, popular.get(0).getItemID());
  assertEquals(258L, popular.get(1).getItemID());
  assertEquals(100L, popular.get(2).getItemID());
  assertEquals(583.0f, popular.get(0).getValue());
  assertEquals(509.0f, popular.get(1).getValue());
  assertEquals(508.0f, popular.get(2).getValue());
}
 
Example #10
Source File: SimpleTest.java    From myrrix-recommender with Apache License 2.0 6 votes vote down vote up
@Test
public void testAnonymous() throws Exception {

  ClientRecommender client = getClient();
  List<RecommendedItem> recs = client.recommendToAnonymous(new long[] {190L}, 3);

  assertNotNull(recs);
  assertEquals(3, recs.size());

  log.info("{}", recs);

  assertEquals(83L, recs.get(0).getItemID());
  assertEquals(213L, recs.get(1).getItemID());
  assertEquals(86L, recs.get(2).getItemID());

  try {
    client.recommendToAnonymous(new long[]{0L}, 3);
    fail();
  } catch (NoSuchItemException nsie) {
    // good
  }
}
 
Example #11
Source File: TranslatingClientRecommender.java    From myrrix-recommender with Apache License 2.0 6 votes vote down vote up
@Override
public List<TranslatedRecommendedItem> recommendToAnonymous(String[] itemIDs,
                                                            float[] values,
                                                            int howMany,
                                                            String[] rescorerParams,
                                                            String contextUserID)
    throws TasteException {
  long[] longItemIDs = translateItems(itemIDs);
  Collection<RecommendedItem> originals;
  if (contextUserID == null) {
    originals = delegate.recommendToAnonymous(longItemIDs, values, howMany, rescorerParams, null);
  } else {
    originals =
        delegate.recommendToAnonymous(longItemIDs, values, howMany, rescorerParams, translateUser(contextUserID));
  }
  return translate(originals);
}
 
Example #12
Source File: SimpleTest.java    From myrrix-recommender with Apache License 2.0 6 votes vote down vote up
@Test
public void testAnonymous2() throws Exception {

  ClientRecommender client = getClient();
  List<RecommendedItem> recs =
      client.recommendToAnonymous(new long[] {190L}, new float[] {1.0f}, 3);

  assertNotNull(recs);
  assertEquals(3, recs.size());

  log.info("{}", recs);

  assertEquals(83L, recs.get(0).getItemID());
  assertEquals(213L, recs.get(1).getItemID());
  assertEquals(86L, recs.get(2).getItemID());

  try {
    client.recommendToAnonymous(new long[]{0L}, 3);
    fail();
  } catch (NoSuchItemException nsie) {
    // good
  }
}
 
Example #13
Source File: RecommendedBecauseIterator.java    From myrrix-recommender with Apache License 2.0 6 votes vote down vote up
@Override
public RecommendedItem next() {
  FastByIDMap.MapEntry<float[]> entry = toFeaturesIterator.next();
  long itemID = entry.getKey();
  if (userTagIDs.contains(itemID)) {
    return null;
  }
  float[] candidateFeatures = entry.getValue();
  double candidateFeaturesNorm = SimpleVectorMath.norm(candidateFeatures);
  double estimate = SimpleVectorMath.dot(candidateFeatures, features) / (candidateFeaturesNorm * featuresNorm);
  if (!LangUtils.isFinite(estimate)) {
    return null;
  }
  delegate.set(itemID, (float) estimate);
  return delegate;
}
 
Example #14
Source File: SimpleTest.java    From myrrix-recommender with Apache License 2.0 6 votes vote down vote up
@Test
public void testRecommendToAnonymous() throws Exception {

  ClientRecommender client = getClient();
  // Adding non-existent item to make sure it is ignored
  List<RecommendedItem> recs = client.recommendToAnonymous(new long[] {1L, 3L, Integer.MAX_VALUE}, 3);

  assertNotNull(recs);
  assertEquals(3, recs.size());

  log.info("{}", recs);

  assertEquals(151L, recs.get(0).getItemID());
  assertEquals(50L, recs.get(1).getItemID());
  assertEquals(181L, recs.get(2).getItemID());
}
 
Example #15
Source File: DataFileContents.java    From myrrix-recommender with Apache License 2.0 5 votes vote down vote up
DataFileContents(Multimap<Long,RecommendedItem> data, 
                 Multimap<String,RecommendedItem> itemTags, 
                 Multimap<String,RecommendedItem> userTags) {
  Preconditions.checkNotNull(data);
  Preconditions.checkNotNull(itemTags);
  Preconditions.checkNotNull(userTags);
  this.data = data;
  this.itemTags = itemTags;
  this.userTags = userTags;
}
 
Example #16
Source File: ByValueAscComparatorTest.java    From myrrix-recommender with Apache License 2.0 5 votes vote down vote up
@Test
public void testCompare() {
  RecommendedItem a = new SimpleRecommendedItem(1L, 2.0f);
  RecommendedItem b = new SimpleRecommendedItem(5L, 1.0f);
  assertTrue(ByValueAscComparator.INSTANCE.compare(a, b) > 0);
  assertTrue(ByValueAscComparator.INSTANCE.compare(b, a) < 0);
  assertEquals(0, ByValueAscComparator.INSTANCE.compare(a, a));
}
 
Example #17
Source File: ReconstructionEvaluator.java    From myrrix-recommender with Apache License 2.0 5 votes vote down vote up
private static Multimap<Long,RecommendedItem> readAndCopyDataFiles(File dataDir, File tempDir) throws IOException {
  Multimap<Long,RecommendedItem> data = ArrayListMultimap.create();
  for (File dataFile : dataDir.listFiles(new PatternFilenameFilter(".+\\.csv(\\.(zip|gz))?"))) {
    log.info("Reading {}", dataFile);
    int count = 0;
    for (CharSequence line : new FileLineIterable(dataFile)) {
      Iterator<String> parts = COMMA_TAB_SPLIT.split(line).iterator();
      long userID = Long.parseLong(parts.next());
      long itemID = Long.parseLong(parts.next());
      if (parts.hasNext()) {
        String token = parts.next().trim();
        if (!token.isEmpty()) {
          data.put(userID, new GenericRecommendedItem(itemID, LangUtils.parseFloat(token)));
        }
        // Ignore remove lines
      } else {
        data.put(userID, new GenericRecommendedItem(itemID, 1.0f));
      }
      if (++count % 1000000 == 0) {
        log.info("Finished {} lines", count);
      }
    }

    Files.copy(dataFile, new File(tempDir, dataFile.getName()));
  }
  return data;
}
 
Example #18
Source File: ReconstructionEvaluator.java    From myrrix-recommender with Apache License 2.0 5 votes vote down vote up
public EvaluationResult evaluate(File originalDataDir) throws TasteException, IOException, InterruptedException {

    Preconditions.checkArgument(originalDataDir.exists() && originalDataDir.isDirectory(),
                                "%s is not a directory", originalDataDir);
    File tempDir = Files.createTempDir();

    ServerRecommender recommender = null;
    try {

      Multimap<Long,RecommendedItem> data;
      try {
        data = readAndCopyDataFiles(originalDataDir, tempDir);
      } catch (IOException ioe) {
        throw new TasteException(ioe);
      }

      recommender = new ServerRecommender(tempDir);
      recommender.await();

      Generation generation = recommender.getGenerationManager().getCurrentGeneration();
      FastByIDMap<float[]> X = generation.getX();
      FastByIDMap<float[]> Y = generation.getY();

      Mean averageError = new Mean();
      // Only compute average over existing entries...
      for (Map.Entry<Long,RecommendedItem> entry : data.entries()) {
        long userID = entry.getKey();
        long itemID = entry.getValue().getItemID();
        // Each of which was a "1" in the factor P matrix
        double value = SimpleVectorMath.dot(X.get(userID), Y.get(itemID));
        // So store abs(1-value), except, don't penalize for reconstructing > 1. Error is 0 in this case.
        averageError.increment(FastMath.max(0.0, 1.0 - value));
      }

      return new EvaluationResultImpl(averageError.getResult());
    } finally {
      recommender.close();
      IOUtils.deleteRecursively(tempDir);
    }
  }
 
Example #19
Source File: TopNTest.java    From myrrix-recommender with Apache License 2.0 5 votes vote down vote up
@Test
public void testTopExactly() {
  List<RecommendedItem> candidates = makeNCandidates(3);
  List<RecommendedItem> top3 = TopN.selectTopN(candidates.iterator(), 3);
  assertNotNull(top3);
  assertEquals(3, top3.size());
  assertEquals(3L, top3.get(0).getItemID());
  assertEquals(3.0f, top3.get(0).getValue());
  assertEquals(1L, top3.get(2).getItemID());
  assertEquals(1.0f, top3.get(2).getValue());
}
 
Example #20
Source File: ClientRecommender.java    From myrrix-recommender with Apache License 2.0 5 votes vote down vote up
/**
 * {@link Rescorer}s are not available at this time in the model.
 *
 * @return {@link #mostSimilarItems(long, int)} if rescorer is null
 * @throws UnsupportedOperationException otherwise
 * @deprecated use {@link #mostSimilarItems(long, int)} instead
 */
@Deprecated
@Override
public List<RecommendedItem> mostSimilarItems(long itemID, int howMany, Rescorer<LongPair> rescorer)
    throws TasteException {
  if (rescorer != null) {
    throw new UnsupportedOperationException();
  }
  return mostSimilarItems(itemID, howMany);
}
 
Example #21
Source File: ClientRecommender.java    From myrrix-recommender with Apache License 2.0 5 votes vote down vote up
/**
 * {@code excludeItemIfNotSimilarToAll} is not applicable in this implementation.
 *
 * @return {@link #mostSimilarItems(long[], int)} if excludeItemIfNotSimilarToAll is false
 * @throws UnsupportedOperationException otherwise
 * @deprecated use {@link #mostSimilarItems(long[], int)} instead
 */
@Deprecated
@Override
public List<RecommendedItem> mostSimilarItems(long[] itemIDs,
                                              int howMany,
                                              boolean excludeItemIfNotSimilarToAll) throws TasteException {
  if (excludeItemIfNotSimilarToAll) {
    throw new UnsupportedOperationException();
  }
  return mostSimilarItems(itemIDs, howMany);
}
 
Example #22
Source File: ClientRecommender.java    From myrrix-recommender with Apache License 2.0 5 votes vote down vote up
/**
 * {@link Rescorer}s are not available at this time in the model.
 *
 * @return {@link #mostSimilarItems(long[], int)} if rescorer is null
 * @throws UnsupportedOperationException otherwise
 * @deprecated use {@link #mostSimilarItems(long[], int)} instead
 */
@Deprecated
@Override
public List<RecommendedItem> mostSimilarItems(long[] itemIDs, int howMany, Rescorer<LongPair> rescorer)
    throws TasteException {
  if (rescorer != null) {
    throw new UnsupportedOperationException();
  }
  return mostSimilarItems(itemIDs, howMany);
}
 
Example #23
Source File: TopNTest.java    From myrrix-recommender with Apache License 2.0 5 votes vote down vote up
private static List<RecommendedItem> makeNCandidates(int n) {
  List<RecommendedItem> candidates = Lists.newArrayListWithCapacity(n);
  for (int i = 1; i <= n; i++) {
    candidates.add(new GenericRecommendedItem(i, i));
  }
  return candidates;
}
 
Example #24
Source File: SimpleTest.java    From myrrix-recommender with Apache License 2.0 5 votes vote down vote up
@Test
public void testRecommendVersusToMany2() throws Exception {
  ClientRecommender client = getClient();
  List<RecommendedItem> recs =
      client.recommendToMany(new long[] {4L, 2L}, 3, true, (String[]) null);
  List<RecommendedItem> recs2 =
      client.recommendToMany(new long[] {2L, 4L}, 3, true, (String[]) null);
  assertEquals(recs, recs2);
}
 
Example #25
Source File: AUCEvaluator.java    From myrrix-recommender with Apache License 2.0 5 votes vote down vote up
@Override
public EvaluationResult evaluate(MyrrixRecommender recommender,
                                 RescorerProvider provider, // ignored
                                 Multimap<Long,RecommendedItem> testData) throws TasteException {
  FastByIDMap<FastIDSet> converted = new FastByIDMap<FastIDSet>(testData.size());
  for (long userID : testData.keySet()) {
    Collection<RecommendedItem> userTestData = testData.get(userID);
    FastIDSet itemIDs = new FastIDSet(userTestData.size());
    converted.put(userID, itemIDs);
    for (RecommendedItem datum : userTestData) {
      itemIDs.add(datum.getItemID());
    }
  }
  return evaluate(recommender, converted);
}
 
Example #26
Source File: TranslatingClientRecommender.java    From myrrix-recommender with Apache License 2.0 5 votes vote down vote up
private List<TranslatedRecommendedItem> translate(Collection<RecommendedItem> originals) {
  List<TranslatedRecommendedItem> translated = Lists.newArrayListWithCapacity(originals.size());
  for (RecommendedItem original : originals) {
    long id = original.getItemID();
    String untranslation = untranslateItem(id);
    String translatedItemID = untranslation == null ? Long.toString(id) : untranslation;
    translated.add(new GenericTranslatedRecommendedItem(translatedItemID, original.getValue()));
  }
  return translated;
}
 
Example #27
Source File: TranslatingClientRecommender.java    From myrrix-recommender with Apache License 2.0 5 votes vote down vote up
@Override
public List<TranslatedRecommendedItem> mostSimilarItems(String[] itemIDs,
                                                        int howMany,
                                                        String[] rescorerParams,
                                                        String contextUserID) throws TasteException {
  long[] longItemIDs = translateItems(itemIDs);
  Collection<RecommendedItem> originals;
  if (contextUserID == null) {
    originals = delegate.mostSimilarItems(longItemIDs, howMany, rescorerParams, null);
  } else {
    originals = delegate.mostSimilarItems(longItemIDs, howMany, rescorerParams, translateUser(contextUserID));
  }
  return translate(originals);
}
 
Example #28
Source File: TinyTagsTest.java    From myrrix-recommender with Apache License 2.0 5 votes vote down vote up
@Test
public void testTinyMostPopular() throws Exception {
  ClientRecommender client = getClient();
  List<RecommendedItem> popular = client.mostPopularItems(5);
  log.info("{}", popular);
  assertEquals(2, popular.get(0).getItemID());
  assertEquals(2.0f, popular.get(0).getValue(), BIG_EPSILON);
}
 
Example #29
Source File: TranslatingClientRecommender.java    From myrrix-recommender with Apache License 2.0 5 votes vote down vote up
@Override
public List<TranslatedRecommendedItem> recommend(String userID,
                                                 int howMany,
                                                 boolean considerKnownItems,
                                                 String[] rescorerParams) throws TasteException {
  long longUserID = translateUser(userID);
  Collection<RecommendedItem> originals =
      delegate.recommend(longUserID, howMany, considerKnownItems, rescorerParams);
  return translate(originals);
}
 
Example #30
Source File: TranslatingClientRecommender.java    From myrrix-recommender with Apache License 2.0 5 votes vote down vote up
@Override
public List<TranslatedRecommendedItem> recommendToMany(String[] userIDs,
                                                       int howMany,
                                                       boolean considerKnownItems,
                                                       String[] rescorerParams) throws TasteException {
  long[] longUserIDs = translateUsers(userIDs);
  Collection<RecommendedItem> originals =
      delegate.recommendToMany(longUserIDs, howMany, considerKnownItems, rescorerParams);
  return translate(originals);
}