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

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


    if (Double.isNaN(evaluationPercentage) || (evaluationPercentage <= 0.0) || (evaluationPercentage > 1.0)) {
      throw new IllegalArgumentException("Invalid evaluationPercentage: " + evaluationPercentage);
    }
   
    int numItems = dataModel.getNumItems();
    RunningAverage precision = new FullRunningAverage();
    RunningAverage recall = new FullRunningAverage();
    RunningAverage fallOut = new FullRunningAverage();
    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) {
            precision.addDatum((double) intersectionSize / (double) numRecommendedItems);
          }
          recall.addDatum((double) intersectionSize / (double) numRelevantItems);
          if (numRelevantItems < size) {
            fallOut.addDatum((double) (numRecommendedItems - intersectionSize)
                             / (double) (numItems - numRelevantItems));
          }
         
          long end = System.currentTimeMillis();
          GenericRecommenderIRStatsEvaluator.log
              .info("Evaluated with user {} in {}ms", userID, (end - start));
          log.info("Precision/recall/fall-out: {} / {} / {}",
            new Object[] {precision.getAverage(), recall.getAverage(), fallOut.getAverage()});
        }
      }
    }
   
    return new IRStatisticsImpl(precision.getAverage(), recall.getAverage(), fallOut.getAverage());
  }
View Full Code Here


public class MemoryDiffStorageTest extends TasteTestCase {

  public void testGetDiff() throws Exception {
    DataModel model = getDataModel();
    MemoryDiffStorage storage = new MemoryDiffStorage(model, Weighting.UNWEIGHTED, false, Long.MAX_VALUE);
    RunningAverage average = storage.getDiff(1, 2);
    assertEquals(0.23333333333333334, average.getAverage(), EPSILON);
    assertEquals(3, average.getCount());
  }
View Full Code Here

  public void testUpdate() throws Exception {
    DataModel model = getDataModel();
    MemoryDiffStorage storage = new MemoryDiffStorage(model, Weighting.UNWEIGHTED, false, Long.MAX_VALUE);
    storage.updateItemPref(1, 0.5f, false);
    RunningAverage average = storage.getDiff(1, 2);
    assertEquals(0.06666666666666668, average.getAverage(), EPSILON);
    assertEquals(3, average.getCount());
  }
View Full Code Here

  public void testRemove() throws Exception {
    DataModel model = getDataModel();
    MemoryDiffStorage storage = new MemoryDiffStorage(model, Weighting.UNWEIGHTED, false, Long.MAX_VALUE);
    storage.updateItemPref(1, 0.5f, true);
    RunningAverage average = storage.getDiff(1, 2);
    assertEquals(0.1, average.getAverage(), EPSILON);
    assertEquals(2, average.getCount());
  }
View Full Code Here

    }
   
    @Override
    public double estimate(Long itemID) throws TasteException {
      DataModel dataModel = getDataModel();
      RunningAverage average = new FullRunningAverage();
      LongPrimitiveIterator it = cluster.iterator();
      while (it.hasNext()) {
        Float pref = dataModel.getPreferenceValue(it.next(), itemID);
        if (pref != null) {
          average.addDatum(pref);
        }
      }
      return average.getAverage();
    }
View Full Code Here

      buildAverageDiffsLock.readLock().lock();
      level2Map = averageDiffs.get(itemID1);
    } finally {
      buildAverageDiffsLock.readLock().unlock();
    }
    RunningAverage average = null;
    if (level2Map != null) {
      average = level2Map.get(itemID2);
    }
    if (inverted) {
      if (average == null) {
View Full Code Here

    try {
      buildAverageDiffsLock.readLock().lock();
      for (Map.Entry<Long,FastByIDMap<RunningAverage>> entry : averageDiffs.entrySet()) {
        boolean matchesItemID1 = itemID == entry.getKey();
        for (Map.Entry<Long,RunningAverage> entry2 : entry.getValue().entrySet()) {
          RunningAverage average = entry2.getValue();
          if (matchesItemID1) {
            if (remove) {
              average.removeDatum(prefDelta);
            } else {
              average.changeDatum(-prefDelta);
            }
          } else if (itemID == entry2.getKey()) {
            if (remove) {
              average.removeDatum(-prefDelta);
            } else {
              average.changeDatum(prefDelta);
            }
          }
        }
      }
      RunningAverage itemAverage = averageItemPref.get(itemID);
      if (itemAverage != null) {
        itemAverage.changeDatum(prefDelta);
      }
    } finally {
      buildAverageDiffsLock.readLock().unlock();
    }
  }
View Full Code Here

    Iterator<Map.Entry<Long,FastByIDMap<RunningAverage>>> it1 = averageDiffs.entrySet().iterator();
    while (it1.hasNext()) {
      FastByIDMap<RunningAverage> map = it1.next().getValue();
      Iterator<Map.Entry<Long,RunningAverage>> it2 = map.entrySet().iterator();
      while (it2.hasNext()) {
        RunningAverage average = it2.next().getValue();
        if (average.getCount() <= 1) {
          it2.remove();
        }
      }
      if (map.isEmpty()) {
        it1.remove();
View Full Code Here

        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();
        averageItemPref.put(itemIDA, itemAverage);
      }
      itemAverage.addDatum(prefAValue);
    }
    return averageCount;
  }
View Full Code Here

  }
 
  private float doEstimatePreference(long userID, long itemID) {
    buildAveragesLock.readLock().lock();
    try {
      RunningAverage itemAverage = itemAverages.get(itemID);
      if (itemAverage == null) {
        return Float.NaN;
      }
      RunningAverage userAverage = userAverages.get(userID);
      if (userAverage == null) {
        return Float.NaN;
      }
      double userDiff = userAverage.getAverage() - overallAveragePrefValue.getAverage();
      return (float) (itemAverage.getAverage() + userDiff);
    } finally {
      buildAveragesLock.readLock().unlock();
    }
  }
View Full Code Here

TOP

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

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.