Package org.apache.mahout.cf.taste.model

Examples of org.apache.mahout.cf.taste.model.PreferenceArray


    FastIDSet itemIDSet = new FastIDSet();
    int currentCount = 0;
    float maxPrefValue = Float.NEGATIVE_INFINITY;
    float minPrefValue = Float.POSITIVE_INFINITY;
    for (Map.Entry<Long, PreferenceArray> entry : preferenceFromUsers.entrySet()) {
      PreferenceArray prefs = entry.getValue();
      prefs.sortByItem();
      for (Preference preference : prefs) {
        long itemID = preference.getItemID();
        itemIDSet.add(itemID);
        List<Preference> prefsForItem = (List<Preference>) prefsForItems.get(itemID);
        if (prefsForItem == null) {
View Full Code Here


   * @throws NoSuchUserException
   *           if there is no such user
   */
  @Override
  public PreferenceArray getPreferencesFromUser(long userID) throws NoSuchUserException {
    PreferenceArray prefs = preferenceFromUsers.get(userID);
    if (prefs == null) {
      throw new NoSuchUserException();
    }
    return prefs;
  }
View Full Code Here

    return prefs;
  }
 
  @Override
  public FastIDSet getItemIDsFromUser(long userID) throws TasteException {
    PreferenceArray prefs = getPreferencesFromUser(userID);
    int size = prefs.length();
    FastIDSet result = new FastIDSet(size);
    for (int i = 0; i < size; i++) {
      result.add(prefs.getItemID(i));
    }
    return result;
  }
View Full Code Here

    return new LongPrimitiveArrayIterator(itemIDs);
  }
 
  @Override
  public PreferenceArray getPreferencesForItem(long itemID) throws NoSuchItemException {
    PreferenceArray prefs = preferenceForItems.get(itemID);
    if (prefs == null) {
      throw new NoSuchItemException();
    }
    return prefs;
  }
View Full Code Here

    return prefs;
  }
 
  @Override
  public Float getPreferenceValue(long userID, long itemID) throws TasteException {
    PreferenceArray prefs = getPreferencesFromUser(userID);
    int size = prefs.length();
    for (int i = 0; i < size; i++) {
      if (prefs.getItemID(i) == itemID) {
        return prefs.getValue(i);
      }
    }
    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);
    PreferenceArray prefs1 = preferenceForItems.get(itemIDs[0]);
    if (prefs1 == null) {
      return 0;
    }

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

    // itemIDs.length == 2)
    PreferenceArray prefs2 = preferenceForItems.get(itemIDs[1]);
    if (prefs2 == null) {
      return 0;
    }
    FastIDSet users1 = new FastIDSet(prefs1.length());
    int size1 = prefs1.length();
    for (int i = 0; i < size1; i++) {
      users1.add(prefs1.getUserID(i));
    }
    FastIDSet users2 = new FastIDSet(prefs2.length());
    int size2 = prefs2.length();
    for (int i = 0; i < size2; i++) {
      users2.add(prefs2.getUserID(i));
    }
    users1.retainAll(users2);
    return users1.size();
  }
View Full Code Here

    this.dataModel = dataModel;
  }
 
  @Override
  public double userSimilarity(long userID1, long userID2) throws TasteException {
    PreferenceArray xPrefs = dataModel.getPreferencesFromUser(userID1);
    PreferenceArray yPrefs = dataModel.getPreferencesFromUser(userID2);
    int xLength = xPrefs.length();
    int yLength = yPrefs.length();
   
    if ((xLength <= 1) || (yLength <= 1)) {
      return Double.NaN;
    }
   
    // Copy prefs since we need to modify pref values to ranks
    xPrefs = xPrefs.clone();
    yPrefs = yPrefs.clone();
   
    // First sort by values from low to high
    xPrefs.sortByValue();
    yPrefs.sortByValue();
   
    // Assign ranks from low to high
    float nextRank = 1.0f;
    for (int i = 0; i < xLength; i++) {
      // ... but only for items that are common to both pref arrays
      if (yPrefs.hasPrefWithItemID(xPrefs.getItemID(i))) {
        xPrefs.setValue(i, nextRank);
        nextRank += 1.0f;
      }
      // Other values are bogus but don't matter
    }
    nextRank = 1.0f;
    for (int i = 0; i < yLength; i++) {
      if (xPrefs.hasPrefWithItemID(yPrefs.getItemID(i))) {
        yPrefs.setValue(i, nextRank);
        nextRank += 1.0f;
      }
    }
   
    xPrefs.sortByItem();
    yPrefs.sortByItem();
   
    long xIndex = xPrefs.getItemID(0);
    long yIndex = yPrefs.getItemID(0);
    int xPrefIndex = 0;
    int yPrefIndex = 0;
   
    double sumXYRankDiff2 = 0.0;
    int count = 0;
   
    while (true) {
      int compare = xIndex < yIndex ? -1 : xIndex > yIndex ? 1 : 0;
      if (compare == 0) {
        double diff = xPrefs.getValue(xPrefIndex) - yPrefs.getValue(yPrefIndex);
        sumXYRankDiff2 += diff * diff;
        count++;
      }
      if (compare <= 0) {
        if (++xPrefIndex >= xLength) {
          break;
        }
        xIndex = xPrefs.getItemID(xPrefIndex);
      }
      if (compare >= 0) {
        if (++yPrefIndex >= yLength) {
          break;
        }
        yIndex = yPrefs.getItemID(yPrefIndex);
      }
    }
   
    if (count <= 1) {
      return Double.NaN;
View Full Code Here

   */
  @Override
  protected FastIDSet doGetCandidateItems(long[] preferredItemIDs, DataModel dataModel) throws TasteException {
    FastIDSet possibleItemsIDs = new FastIDSet();
    for (long itemID : preferredItemIDs) {
      PreferenceArray itemPreferences = dataModel.getPreferencesForItem(itemID);
      int numUsersPreferringItem = itemPreferences.length();
      for (int index = 0; index < numUsersPreferringItem; index++) {
        possibleItemsIDs.addAll(dataModel.getItemIDsFromUser(itemPreferences.getUserID(index)));
      }
    }
    possibleItemsIDs.removeAll(preferredItemIDs);
    return possibleItemsIDs;
  }
View Full Code Here

    log.info("Loaded model in {}s", (end - start) / 1000);
    start = end;

    Collection<Track2Callable> callables = Lists.newArrayList();
    for (Pair<PreferenceArray,long[]> tests : new DataFileIterable(KDDCupDataModel.getTestFile(dataFileDirectory))) {
      PreferenceArray userTest = tests.getFirst();
      callables.add(new Track2Callable(recommender, userTest));
    }

    int cores = Runtime.getRuntime().availableProcessors();
    log.info("Running on {} cores", cores);
View Full Code Here

    log.info("Loaded model in {}s", (end - start) / 1000);
    start = end;

    Collection<Track1Callable> callables = Lists.newArrayList();
    for (Pair<PreferenceArray,long[]> tests : new DataFileIterable(KDDCupDataModel.getTestFile(dataFileDirectory))) {
      PreferenceArray userTest = tests.getFirst();
      callables.add(new Track1Callable(recommender, userTest));
    }

    int cores = Runtime.getRuntime().availableProcessors();
    log.info("Running on {} cores", cores);
View Full Code Here

TOP

Related Classes of org.apache.mahout.cf.taste.model.PreferenceArray

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.