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

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


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


  @Test
  public void testRemove() throws Exception {
    DataModel model = getDataModel();
    MemoryDiffStorage storage = new MemoryDiffStorage(model, Weighting.UNWEIGHTED, Long.MAX_VALUE);
   
    RunningAverage average1 = storage.getDiff(0, 2);
    assertEquals(0.1, average1.getAverage(), EPSILON);
    assertEquals(3, average1.getCount());
   
    RunningAverage average2 = storage.getDiff(1, 2);
    assertEquals(0.23333332935969034, average2.getAverage(), EPSILON);
    assertEquals(3, average2.getCount());
   
    storage.removeItemPref(4, 2, 0.8f);
   
    average1 = storage.getDiff(0, 2);
    assertEquals(0.1, average1.getAverage(), EPSILON);
    assertEquals(2, average1.getCount());
   
    average2 = storage.getDiff(1, 2);
    assertEquals(0.1, average2.getAverage(), EPSILON);
    assertEquals(2, average2.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.nextLong(), itemID);
        if (pref != null) {
          average.addDatum(pref);
        }
      }
      return average.getAverage();
    }
View Full Code Here

    Factorization factorization = sgdFactorizer.factorize();

    log.info("Estimating validation preferences...");
    int prefsProcessed = 0;
    RunningAverage average = new FullRunningAverage();
    for (Pair<PreferenceArray,long[]> validationPair
        : new DataFileIterable(KDDCupDataModel.getValidationFile(dataFileDirectory))) {
      for (Preference validationPref : validationPair.getFirst()) {
        double estimate = estimatePreference(factorization, validationPref.getUserID(), validationPref.getItemID(),
            factorizablePreferences.getMinPreference(), factorizablePreferences.getMaxPreference());
        double error = validationPref.getValue() - estimate;
        average.addDatum(error * error);
        prefsProcessed++;
        if (prefsProcessed % 100000 == 0) {
          log.info("Computed {} estimations", prefsProcessed);
        }
      }
    }
    log.info("Computed {} estimations, done.", prefsProcessed);

    double rmse = Math.sqrt(average.getAverage());
    log.info("RMSE {}", rmse);

    log.info("Estimating test preferences...");
    OutputStream out = null;
    try {
View Full Code Here

    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) {
      RunningAverage itemAverage = diffStorage.getAverageItemPref(itemID);
      return itemAverage == null ? Float.NaN : (float) itemAverage.getAverage();
    } else {
      return (float) (totalPreference / count);
    }
  }
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) {
      return average == null ? null : average.inverse();
    } else {
      return average;
    }
  }
View Full Code Here

      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);
          }
          average.addDatum(bValue - prefValue);
        } else {
          FastByIDMap<RunningAverage> bMap = averageDiffs.get(itemIDB);
          if (bMap == null) {
            bMap = new FastByIDMap<RunningAverage>();
            averageDiffs.put(itemIDB, bMap);
          }
          RunningAverage average = bMap.get(itemIDA);
          if (average == null) {
            average = buildRunningAverage();
            bMap.put(itemIDA, average);
          }
          average.addDatum(prefValue - bValue);
        }
      }

    } finally {
      buildAverageDiffsLock.writeLock().unlock();
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) {
            average.changeDatum(-prefDelta);
          } else if (itemID == entry2.getKey()) {
            average.changeDatum(prefDelta);
          }
        }
      }
      RunningAverage itemAverage = averageItemPref.get(itemID);
      if (itemAverage != null) {
        itemAverage.changeDatum(prefDelta);
      }
    } finally {
      buildAverageDiffsLock.readLock().unlock();
    }
  }
View Full Code Here

        float bValue = userPreferences.getValue(i);

        if (itemIDA < itemIDB) {

          if (aMap != null) {
            RunningAverage average = aMap.get(itemIDB);
            if (average != null) {
              if (average.getCount() <= 1) {
                aMap.remove(itemIDB);
              } else {
                average.removeDatum(bValue - prefValue);
              }
            }
          }

        } else  if (itemIDA > itemIDB) {

          FastByIDMap<RunningAverage> bMap = averageDiffs.get(itemIDB);
          if (bMap != null) {
            RunningAverage average = bMap.get(itemIDA);
            if (average != null) {
              if (average.getCount() <= 1) {
                aMap.remove(itemIDA);
              } else {
                average.removeDatum(prefValue - bValue);
              }
            }
          }

        }
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

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.