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

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


        // Then line is of form "userID,itemID,", meaning remove
        if (prefs != null) {
          // remove pref
          Iterator<Preference> prefsIterator = prefs.iterator();
          while (prefsIterator.hasNext()) {
            Preference pref = prefsIterator.next();
            if (pref.getItemID() == itemID) {
              prefsIterator.remove();
              break;
            }
          }
        }

        removeTimestamp(userID, itemID, timestamps);
       
      } else {

        float preferenceValue = Float.parseFloat(preferenceValueString);

        boolean exists = false;
        if (prefs != null) {
          for (Preference pref : prefs) {
            if (pref.getItemID() == itemID) {
              exists = true;
              pref.setValue(preferenceValue);
              break;
            }
          }
        }
View Full Code Here


    System.arraycopy(rawPrefs, 0, sortedPrefs, 0, length);
    Arrays.sort(sortedPrefs, Collections.reverseOrder(ByValuePreferenceComparator.getInstance()));
    // Cap this at NUM_TOP_PREFERENCES just to be brief
    int max = Math.min(NUM_TOP_PREFERENCES, length);
    for (int i = 0; i < max; i++) {
      Preference pref = sortedPrefs[i];
      writer.print(pref.getValue());
      writer.print('\t');
      writer.println(pref.getItem());
    }
    writer.println();
    writer.println("Recommendations:");
    for (RecommendedItem recommendedItem : items) {
      writer.print(recommendedItem.getValue());
View Full Code Here

    if (userID == null || itemID == null) {
      throw new IllegalArgumentException("userID or itemID is null");
    }
    DataModel model = getDataModel();
    User theUser = model.getUser(userID);
    Preference actualPref = theUser.getPreferenceFor(itemID);
    if (actualPref != null) {
      return actualPref.getValue();
    }
    checkClustersBuilt();
    List<RecommendedItem> topRecsForUser = topRecsByUserID.get(userID);
    if (topRecsForUser != null) {
      for (RecommendedItem item : topRecsForUser) {
View Full Code Here

    if (xPrefs.length == 0 || yPrefs.length == 0) {
      return Double.NaN;
    }

    Preference xPref = xPrefs[0];
    Preference yPref = yPrefs[0];
    Item xIndex = xPref.getItem();
    Item yIndex = yPref.getItem();
    int xPrefIndex = 1;
    int yPrefIndex = 1;

    double sumX = 0.0;
    double sumX2 = 0.0;
    double sumY = 0.0;
    double sumY2 = 0.0;
    double sumXY = 0.0;
    double sumXYdiff2 = 0.0;
    int count = 0;

    boolean hasInferrer = inferrer != null;
    boolean hasPrefTransform = prefTransform != null;

    while (true) {
      int compare = xIndex.compareTo(yIndex);
      if (hasInferrer || compare == 0) {
        double x;
        double y;
        if (compare == 0) {
          // Both users expressed a preference for the item
          if (hasPrefTransform) {
            x = prefTransform.getTransformedValue(xPref);
            y = prefTransform.getTransformedValue(yPref);
          } else {
            x = xPref.getValue();
            y = yPref.getValue();
          }
        } else {
          // Only one user expressed a preference, but infer the other one's preference and tally
          // as if the other user expressed that preference
          if (compare < 0) {
            // X has a value; infer Y's
            x = hasPrefTransform ? prefTransform.getTransformedValue(xPref) : xPref.getValue();
            y = inferrer.inferPreference(user2, xIndex);
          } else {
            // compare > 0
            // Y has a value; infer X's
            x = inferrer.inferPreference(user1, yIndex);
            y = hasPrefTransform ? prefTransform.getTransformedValue(yPref) : yPref.getValue();
          }
        }
        sumXY += x * y;
        sumX += x;
        sumX2 += x * x;
        sumY += y;
        sumY2 += y * y;
        double diff = x - y;
        sumXYdiff2 += diff * diff;
        count++;
      }
      if (compare <= 0) {
        if (xPrefIndex == xPrefs.length) {
          break;
        }
        xPref = xPrefs[xPrefIndex++];
        xIndex = xPref.getItem();
      }
      if (compare >= 0) {
        if (yPrefIndex == yPrefs.length) {
          break;
        }
        yPref = yPrefs[yPrefIndex++];
        yIndex = yPref.getItem();
      }
    }

    // "Center" the data. If my math is correct, this'll do it.
    double n = (double) count;
View Full Code Here

    if (xPrefs.length == 0 || yPrefs.length == 0) {
      return Double.NaN;
    }

    Preference xPref = xPrefs[0];
    Preference yPref = yPrefs[0];
    User xIndex = xPref.getUser();
    User yIndex = yPref.getUser();
    int xPrefIndex = 1;
    int yPrefIndex = 1;

    double sumX = 0.0;
    double sumX2 = 0.0;
    double sumY = 0.0;
    double sumY2 = 0.0;
    double sumXY = 0.0;
    double sumXYdiff2 = 0.0;
    int count = 0;

    // No, pref inferrers and transforms don't appy here. I think.

    while (true) {
      int compare = xIndex.compareTo(yIndex);
      if (compare == 0) {
        // Both users expressed a preference for the item
        double x = xPref.getValue();
        double y = yPref.getValue();
        sumXY += x * y;
        sumX += x;
        sumX2 += x * x;
        sumY += y;
        sumY2 += y * y;
        double diff = x - y;
        sumXYdiff2 += diff * diff;
        count++;
      }
      if (compare <= 0) {
        if (xPrefIndex == xPrefs.length) {
          break;
        }
        xPref = xPrefs[xPrefIndex++];
        xIndex = xPref.getUser();
      }
      if (compare >= 0) {
        if (yPrefIndex == yPrefs.length) {
          break;
        }
        yPref = yPrefs[yPrefIndex++];
        yIndex = yPref.getUser();
      }
    }

    // See comments above on these computations
    double n = (double) count;
View Full Code Here

    @Override
    public double estimate(Item item) {
      RunningAverage average = new FullRunningAverage();
      for (User user : cluster) {
        Preference pref = user.getPreferenceFor(item.getID());
        if (pref != null) {
          average.addDatum(pref.getValue());
        }
      }
      return average.getAverage();
    }
View Full Code Here

    } else {
      data = new FastMap<Object, Preference>();
      int size = preferences.size();
      values = new Preference[size];
      for (int i = 0; i < size; i++) {
        Preference preference = preferences.get(i);
        values[i] = preference;
        // Is this hacky?
        if (preference instanceof SettableUserPreference) {
          ((SettableUserPreference) preference).setUser(this);
        }
        data.put(preference.getItem().getID(), preference);
      }
      Arrays.sort(values, ByItemPreferenceComparator.getInstance());
    }
  }
View Full Code Here

  @Override
  public double estimatePreference(Object userID, Object itemID) throws TasteException {
    DataModel model = getDataModel();
    User theUser = model.getUser(userID);
    Preference actualPref = theUser.getPreferenceFor(itemID);
    if (actualPref != null) {
      return actualPref.getValue();
    }
    checkAverageDiffsBuilt();
    return doEstimatePreference(itemID);
  }
View Full Code Here

  public void setPreference(Object userID, Object itemID, double value) throws TasteException {
    DataModel dataModel = getDataModel();
    double prefDelta;
    try {
      User theUser = dataModel.getUser(userID);
      Preference oldPref = theUser.getPreferenceFor(itemID);
      prefDelta = oldPref == null ? value : value - oldPref.getValue();
    } catch (NoSuchUserException nsee) {
      prefDelta = value;
    }
    super.setPreference(userID, itemID, value);
    try {
View Full Code Here

  @Override
  public void removePreference(Object userID, Object itemID) throws TasteException {
    DataModel dataModel = getDataModel();
    User theUser = dataModel.getUser(userID);
    Preference oldPref = theUser.getPreferenceFor(itemID);
    super.removePreference(userID, itemID);
    if (oldPref != null) {
      try {
        buildAveragesLock.writeLock().lock();
        RunningAverage average = itemAverages.get(itemID);
        if (average == null) {
          throw new IllegalStateException("No preferences exist for item ID: " + itemID);
        } else {
          average.removeDatum(oldPref.getValue());
        }
      } finally {
        buildAveragesLock.writeLock().unlock();
      }
    }
View Full Code Here

TOP

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

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.