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

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


    }
   
    if (!mostSimilar.isEmpty()) {
      theNeighborhood[mostSimilar.size()] = itemID;
      for (int i = 0; i < theNeighborhood.length; i++) {
        PreferenceArray usersNeighborhood = dataModel.getPreferencesForItem(theNeighborhood[i]);
        int size1 = usersRatedNeighborhood.isEmpty() ? usersNeighborhood.length() : usersRatedNeighborhood.size();
        for (int j = 0; j < size1; j++) {
          if (i == 0) {
            usersRatedNeighborhood.add(usersNeighborhood.getUserID(j));
          } else {
            if (j >= usersRatedNeighborhood.size()) {
              break;
            }
            long index = usersRatedNeighborhood.get(j);
            if (!usersNeighborhood.hasPrefWithUserID(index) || index == theUserID) {
              usersRatedNeighborhood.remove(index);
              j--;
            }
          }
        }
View Full Code Here


  }
 
  private float doEstimatePreference(long userID, long itemID) throws TasteException {
    double count = 0.0;
    double totalPreference = 0.0;
    PreferenceArray prefs = getDataModel().getPreferencesFromUser(userID);
    RunningAverage[] averages = diffStorage.getDiffs(userID, itemID, prefs);
    int size = prefs.length();
    for (int i = 0; i < size; i++) {
      RunningAverage averageDiff = averages[i];
      if (averageDiff != null) {
        double averageDiffValue = averageDiff.getAverage();
        if (weighted) {
          double weight = averageDiff.getCount();
          if (stdDevWeighted) {
            double stdev = ((RunningAverageAndStdDev) averageDiff).getStandardDeviation();
            if (!Double.isNaN(stdev)) {
              weight /= 1.0 + stdev;
            }
            // If stdev is NaN, then it is because count is 1. Because we're weighting by count,
            // the weight is already relatively low. We effectively assume stdev is 0.0 here and
            // that is reasonable enough. Otherwise, dividing by NaN would yield a weight of NaN
            // and disqualify this pref entirely
            // (Thanks Daemmon)
          }
          totalPreference += weight * (prefs.getValue(i) + averageDiffValue);
          count += weight;
        } else {
          totalPreference += prefs.getValue(i) + averageDiffValue;
          count += 1.0;
        }
      }
    }
    if (count <= 0.0) {
View Full Code Here

    return averageItemPref.get(itemID);
  }

  @Override
  public void addItemPref(long userID, long itemIDA, float prefValue) throws TasteException {
    PreferenceArray userPreferences = dataModel.getPreferencesFromUser(userID);
    try {
      buildAverageDiffsLock.writeLock().lock();

      FastByIDMap<RunningAverage> aMap = averageDiffs.get(itemIDA);
      if (aMap == null) {
        aMap = new FastByIDMap<RunningAverage>();
        averageDiffs.put(itemIDA, aMap);
      }

      int length = userPreferences.length();
      for (int i = 0; i < length; i++) {
        long itemIDB = userPreferences.getItemID(i);
        float bValue = userPreferences.getValue(i);
        if (itemIDA < itemIDB) {
          RunningAverage average = aMap.get(itemIDB);
          if (average == null) {
            average = buildRunningAverage();
            aMap.put(itemIDB, average);
View Full Code Here

    }
  }

  @Override
  public void removeItemPref(long userID, long itemIDA, float prefValue) throws TasteException {
    PreferenceArray userPreferences = dataModel.getPreferencesFromUser(userID);
    try {
      buildAverageDiffsLock.writeLock().lock();

      FastByIDMap<RunningAverage> aMap = averageDiffs.get(itemIDA);

      int length = userPreferences.length();
      for (int i = 0; i < length; i++) {

        long itemIDB = userPreferences.getItemID(i);
        float bValue = userPreferences.getValue(i);

        if (itemIDA < itemIDB) {

          if (aMap != null) {
            RunningAverage average = aMap.get(itemIDB);
View Full Code Here

  }
 
  private long processOneUser(long averageCount, long userID) throws TasteException {
    log.debug("Processing prefs for user {}", userID);
    // Save off prefs for the life of this loop iteration
    PreferenceArray userPreferences = dataModel.getPreferencesFromUser(userID);
    int length = userPreferences.length();
    for (int i = 0; i < length; i++) { // Loop to length-1, not length-2, not for diffs but average item pref
      float prefAValue = userPreferences.getValue(i);
      long itemIDA = userPreferences.getItemID(i);
      FastByIDMap<RunningAverage> aMap = averageDiffs.get(itemIDA);
      if (aMap == null) {
        aMap = new FastByIDMap<RunningAverage>();
        averageDiffs.put(itemIDA, aMap);
      }
      for (int j = i + 1; j < length; j++) {
        // This is a performance-critical block
        long itemIDB = userPreferences.getItemID(j);
        RunningAverage average = aMap.get(itemIDB);
        if (average == null && averageCount < maxEntries) {
          average = buildRunningAverage();
          aMap.put(itemIDB, average);
          averageCount++;
        }
        if (average != null) {
          average.addDatum(userPreferences.getValue(j) - prefAValue);
        }
      }
      RunningAverage itemAverage = averageItemPref.get(itemIDA);
      if (itemAverage == null) {
        itemAverage = buildRunningAverage();
View Full Code Here

    }
    return ids;
  }

  private Matrix buildConfidenceMatrixForItem(long itemId) throws TasteException {
    PreferenceArray prefs = dataModel.getPreferencesForItem(itemId);
    Matrix confidenceMatrix = new SparseMatrix(dataModel.getNumUsers(), dataModel.getNumUsers());
    for (Preference pref : prefs) {
      long userId = pref.getUserID();
      int userIdx = userIndex(userId);
      confidenceMatrix.setQuick(userIdx, userIdx, 1);
View Full Code Here

    }
    return new DiagonalMatrix(confidenceMatrix);
  }

  private Matrix buildConfidenceMatrixForUser(long userId) throws TasteException {
    PreferenceArray prefs = dataModel.getPreferencesFromUser(userId);
    Matrix confidenceMatrix = new SparseMatrix(dataModel.getNumItems(), dataModel.getNumItems());
    for (Preference pref : prefs) {
      long itemId = pref.getItemID();
      int itemIdx = itemIndex(itemId);
      confidenceMatrix.setQuick(itemIdx, itemIdx, 1);
View Full Code Here

    RunningAverage average = new FullRunningAverage();
    LongPrimitiveIterator it = dataModel.getUserIDs();
    while (it.hasNext()) {
      int count = 0;
      try {
        PreferenceArray prefs = dataModel.getPreferencesFromUser(it.nextLong());
        for (Preference pref : prefs) {
          average.addDatum(pref.getValue());
          count++;
        }
      } catch (NoSuchUserException ex) {
View Full Code Here

  private int countPreferences() throws TasteException {
    int numPreferences = 0;
    LongPrimitiveIterator userIDs = dataModel.getUserIDs();
    while (userIDs.hasNext()) {
      PreferenceArray preferencesFromUser = dataModel.getPreferencesFromUser(userIDs.nextLong());
      numPreferences += preferencesFromUser.length();
    }
    return numPreferences;
  }
View Full Code Here

    LongPrimitiveIterator userIDs = dataModel.getUserIDs();
    int index = 0;
    while (userIDs.hasNext()) {
      long userID = userIDs.nextLong();
      PreferenceArray preferencesFromUser = dataModel.getPreferencesFromUser(userID);
      for (Preference preference : preferencesFromUser) {
        cachedUserIDs[index] = userID;
        cachedItemIDs[index] = preference.getItemID();
        index++;
      }
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.