Package org.apache.mahout.cf.taste.impl.common

Examples of org.apache.mahout.cf.taste.impl.common.FastIDSet


  public static FastByIDMap<FastIDSet> toDataMap(FastByIDMap<PreferenceArray> data) {
    for (Map.Entry<Long,Object> entry : ((FastByIDMap<Object>) (FastByIDMap<?>) data).entrySet()) {
      PreferenceArray prefArray = (PreferenceArray) entry.getValue();
      int size = prefArray.length();
      FastIDSet itemIDs = new FastIDSet(size);
      for (int i = 0; i < size; i++) {
        itemIDs.add(prefArray.getItemID(i));
      }
      entry.setValue(itemIDs);
    }
    return (FastByIDMap<FastIDSet>) (FastByIDMap<?>) data;
  }
View Full Code Here


   * @throws NoSuchUserException
   *           if there is no such user
   */
  @Override
  public PreferenceArray getPreferencesFromUser(long userID) throws NoSuchUserException {
    FastIDSet itemIDs = preferenceFromUsers.get(userID);
    if (itemIDs == null) {
      throw new NoSuchUserException();
    }
    PreferenceArray prefArray = new BooleanUserPreferenceArray(itemIDs.size());
    int i = 0;
    LongPrimitiveIterator it = itemIDs.iterator();
    while (it.hasNext()) {
      prefArray.setUserID(i, userID);
      prefArray.setItemID(i, it.next());
      i++;
    }
View Full Code Here

  @Override
  public List<RecommendedItem> recommend(long userID, int howMany, IDRescorer rescorer) throws TasteException {
    Preconditions.checkArgument(howMany >= 1, "howMany must be at least 1");
    log.debug("Recommending items for user ID '{}'", userID);

    FastIDSet possibleItemIDs = diffStorage.getRecommendableItemIDs(userID);

    TopItems.Estimator<Long> estimator = new Estimator(userID);

    List<RecommendedItem> topItems = TopItems.getTopItems(howMany, possibleItemIDs.iterator(), rescorer,
      estimator);

    log.debug("Recommendations are: {}", topItems);
    return topItems;
  }
View Full Code Here

    return prefArray;
  }
 
  @Override
  public FastIDSet getItemIDsFromUser(long userID) throws TasteException {
    FastIDSet itemIDs = preferenceFromUsers.get(userID);
    if (itemIDs == null) {
      throw new NoSuchUserException();
    }
    return itemIDs;
  }
View Full Code Here

    return new LongPrimitiveArrayIterator(itemIDs);
  }
 
  @Override
  public PreferenceArray getPreferencesForItem(long itemID) throws NoSuchItemException {
    FastIDSet userIDs = preferenceForItems.get(itemID);
    if (userIDs == null) {
      throw new NoSuchItemException();
    }
    PreferenceArray prefArray = new BooleanItemPreferenceArray(userIDs.size());
    int i = 0;
    LongPrimitiveIterator it = userIDs.iterator();
    while (it.hasNext()) {
      prefArray.setUserID(i, it.next());
      prefArray.setItemID(i, itemID);
      i++;
    }
View Full Code Here

    LongPrimitiveIterator it = dataModel.getUserIDs();
    while (it.hasNext()) {
      long userID = it.nextLong();
      if (random.nextDouble() < evaluationPercentage) {
        long start = System.currentTimeMillis();
        FastIDSet relevantItemIDs = new FastIDSet(at);
        PreferenceArray prefs = dataModel.getPreferencesFromUser(userID);
        int size = prefs.length();
        if (size < 2 * at) {
          // Really not enough prefs to meaningfully evaluate this user
          continue;
        }

        // List some most-preferred items that would count as (most) "relevant" results
        double theRelevanceThreshold =
            Double.isNaN(relevanceThreshold) ? computeThreshold(prefs) : relevanceThreshold;
        prefs.sortByValueReversed();
        for (int i = 0; (i < size) && (relevantItemIDs.size() < at); i++) {
          if (prefs.getValue(i) >= theRelevanceThreshold) {
            relevantItemIDs.add(prefs.getItemID(i));
          }
        }
        int numRelevantItems = relevantItemIDs.size();
        if (numRelevantItems > 0) {
          FastByIDMap<PreferenceArray> trainingUsers = new FastByIDMap<PreferenceArray>(dataModel
              .getNumUsers());
          LongPrimitiveIterator it2 = dataModel.getUserIDs();
          while (it2.hasNext()) {
            processOtherUser(userID, relevantItemIDs, trainingUsers, it2
                .nextLong(), dataModel);
          }

          DataModel trainingModel = dataModelBuilder == null ? new GenericDataModel(trainingUsers)
              : dataModelBuilder.buildDataModel(trainingUsers);
          Recommender recommender = recommenderBuilder.buildRecommender(trainingModel);

          try {
            trainingModel.getPreferencesFromUser(userID);
          } catch (NoSuchUserException nsee) {
            continue; // Oops we excluded all prefs for the user -- just move on
          }

          int intersectionSize = 0;
          List<RecommendedItem> recommendedItems = recommender.recommend(userID, at, rescorer);
          for (RecommendedItem recommendedItem : recommendedItems) {
            if (relevantItemIDs.contains(recommendedItem.getItemID())) {
              intersectionSize++;
            }
          }
          int numRecommendedItems = recommendedItems.size();
          if (numRecommendedItems > 0) {
View Full Code Here

    return prefArray;
  }
 
  @Override
  public Float getPreferenceValue(long userID, long itemID) throws NoSuchUserException {
    FastIDSet itemIDs = preferenceFromUsers.get(userID);
    if (itemIDs == null) {
      throw new NoSuchUserException();
    }
    if (itemIDs.contains(itemID)) {
      return 1.0f;
    }
    return null;
  }
View Full Code Here

 
  @Override
  public int getNumUsersWithPreferenceFor(long... itemIDs) {
    Preconditions.checkArgument(itemIDs != null, "itemIDs is null");
    Preconditions.checkArgument(itemIDs.length == 1 || itemIDs.length == 2, "Illegal number of IDs", itemIDs.length);
    FastIDSet userIDs1 = preferenceForItems.get(itemIDs[0]);
    if (userIDs1 == null) {
      return 0;
    }

    if (itemIDs.length == 1) {
      return userIDs1.size();
    }

    // itemIDs.length == 2
    FastIDSet userIDs2 = preferenceForItems.get(itemIDs[1]);
    if (userIDs2 == null) {
      return 0;
    }
    return userIDs1.intersectionSize(userIDs2);
View Full Code Here

  @Override
  public List<RecommendedItem> recommend(long userID, int howMany, IDRescorer rescorer) throws TasteException {
    Preconditions.checkArgument(howMany >= 1, "howMany must be at least 1");
    log.debug("Recommending items for user ID '{}'", userID);

    FastIDSet possibleItemIDs = getAllOtherItems(userID);

    TopItems.Estimator<Long> estimator = new Estimator(userID);

    List<RecommendedItem> topItems = TopItems.getTopItems(howMany, possibleItemIDs.iterator(), rescorer,
      estimator);

    log.debug("Recommendations are: {}", topItems);
    return topItems;
  }
View Full Code Here

    this.compactAverages = compactAverages;
    this.maxEntries = maxEntries;
    this.averageDiffs = new FastByIDMap<FastByIDMap<RunningAverage>>();
    this.averageItemPref = new FastByIDMap<RunningAverage>();
    this.buildAverageDiffsLock = new ReentrantReadWriteLock();
    this.allRecommendableItemIDs = new FastIDSet(dataModel.getNumItems());
    this.refreshHelper = new RefreshHelper(new Callable<Object>() {
      @Override
      public Object call() throws TasteException {
        buildAverageDiffs();
        return null;
View Full Code Here

TOP

Related Classes of org.apache.mahout.cf.taste.impl.common.FastIDSet

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.