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

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


                              FastByIDMap<PreferenceArray> testUserPrefs,
                              long userID,
                              DataModel dataModel) throws TasteException {
    List<Preference> trainingPrefs = null;
    List<Preference> testPrefs = null;
    PreferenceArray prefs = dataModel.getPreferencesFromUser(userID);
    int size = prefs.length();
    for (int i = 0; i < size; i++) {
      Preference newPref = new GenericPreference(userID, prefs.getItemID(i), prefs.getValue(i));
      if (random.nextDouble() < trainingPercentage) {
        if (trainingPrefs == null) {
          trainingPrefs = new ArrayList<Preference>(3);
        }
        trainingPrefs.add(newPref);
View Full Code Here


    itemIDsFromUser456.add(2L);

    List<Preference> prefs = new ArrayList<Preference>();
    prefs.add(new GenericPreference(123L, 1L, 1.0f));
    prefs.add(new GenericPreference(456L, 1L, 1.0f));
    PreferenceArray preferencesForItem1 = new GenericItemPreferenceArray(prefs);

    DataModel dataModel = EasyMock.createMock(DataModel.class);
    EasyMock.expect(dataModel.getItemIDsFromUser(123L)).andReturn(itemIDsFromUser123);
    EasyMock.expect(dataModel.getPreferencesForItem(1L)).andReturn(preferencesForItem1);
    EasyMock.expect(dataModel.getItemIDsFromUser(123L)).andReturn(itemIDsFromUser123);
View Full Code Here

    writer.println(recommender);
    writer.println();
    writer.print("Top ");
    writer.print(NUM_TOP_PREFERENCES);
    writer.println(" Preferences:");
    PreferenceArray rawPrefs = dataModel.getPreferencesFromUser(userID);
    int length = rawPrefs.length();
    PreferenceArray sortedPrefs = rawPrefs.clone();
    sortedPrefs.sortByValueReversed();
    // 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.get(i);
      writer.print(pref.getValue());
      writer.print('\t');
      writer.println(pref.getItemID());
    }
    writer.println();
View Full Code Here

                    {1.0, 1.0, 1.0, 1.0, 1.0},
            });

    InverseUserFrequency iuf = new InverseUserFrequency(dataModel, 10.0);

    PreferenceArray user5Prefs = dataModel.getPreferencesFromUser(5);

    for (int i = 0; i < 5; i++) {
      Preference pref = user5Prefs.get(i);
      assertNotNull(pref);
      assertEquals(Math.log(5.0 / (double) (5 - i)) / Math.log(iuf.getLogBase()),
          iuf.getTransformedValue(pref),
          EPSILON);
    }
View Full Code Here

  @Override
  public PreferenceArray getPreferencesForItem(long itemID) throws TasteException {
    if (tempPrefs == null) {
      return delegate.getPreferencesForItem(itemID);
    }
    PreferenceArray delegatePrefs = null;
    try {
      delegatePrefs = delegate.getPreferencesForItem(itemID);
    } catch (NoSuchItemException nsie) {
      // OK. Probably an item that only the anonymous user has
    }
    for (int i = 0; i < tempPrefs.length(); i++) {
      if (tempPrefs.getItemID(i) == itemID) {
        int length = delegatePrefs == null ? 0 : delegatePrefs.length();
        PreferenceArray newPreferenceArray = new GenericItemPreferenceArray(length + 1);
        for (int j = 0; j < length; j++) {
          newPreferenceArray.setUserID(j, delegatePrefs.getUserID(j));
          newPreferenceArray.setItemID(j, delegatePrefs.getItemID(j));
          newPreferenceArray.setValue(j, delegatePrefs.getValue(j));
        }
        newPreferenceArray.setUserID(length, tempPrefs.getUserID(i));
        newPreferenceArray.setItemID(length, tempPrefs.getItemID(i));
        newPreferenceArray.setValue(length, tempPrefs.getValue(i));
        newPreferenceArray.sortByUser();
        return newPreferenceArray;
      }
    }
    if (delegatePrefs == null) {
      // No, didn't find it among the anonymous user prefs
View Full Code Here

  }

  private DataModel removeUserItem(long userID, Iterable<List<String>> items) {
    FastByIDMap<PreferenceArray> rawData = ((GenericDataModel) delegate).getRawUserData();
    for (List<String> item : items) {
      PreferenceArray prefs = rawData.get(userID);
      long itemID = Long.parseLong(item.get(0));
      if (prefs != null) {
        boolean exists = false;
        int length = prefs.length();
        for (int i = 0; i < length; i++) {
          if (prefs.getItemID(i) == itemID) {
            exists = true;
            break;
          }
        }
        if (exists) {
          rawData.remove(userID);
          if (length > 1) {
            PreferenceArray newPrefs = new GenericUserPreferenceArray(length - 1);
            for (int i = 0, j = 0; i < length; i++, j++) {
              if (prefs.getItemID(i) == itemID) {
                j--;
              } else {
                newPrefs.set(j, prefs.get(i));
              }
            }
            rawData.put(userID, newPrefs);
          }
          log.info("Removing userID: {} itemID: {}", userID, itemID);
View Full Code Here

    return new GenericDataModel(rawData);
  }

  private DataModel addUserItem(long userID, Iterable<List<String>> items) {
    FastByIDMap<PreferenceArray> rawData = ((GenericDataModel) delegate).getRawUserData();
    PreferenceArray prefs = rawData.get(userID);
    for (List<String> item : items) {
      long itemID = Long.parseLong(item.get(0));
      float preferenceValue = Float.parseFloat(item.get(1));
      boolean exists = false;
      if (prefs != null) {
        for (int i = 0; i < prefs.length(); i++) {
          if (prefs.getItemID(i) == itemID) {
            exists = true;
            prefs.setValue(i, preferenceValue);
            break;
          }
        }
      }
      if (!exists) {
        if (prefs == null) {
          prefs = new GenericUserPreferenceArray(1);
        } else {
          PreferenceArray newPrefs = new GenericUserPreferenceArray(prefs.length() + 1);
          for (int i = 0, j = 1; i < prefs.length(); i++, j++) {
            newPrefs.set(j, prefs.get(i));
          }
          prefs = newPrefs;
        }
        prefs.setUserID(0, userID);
        prefs.setItemID(0, itemID);
View Full Code Here

   
    DataModel dataModel = getDataModel();
   
    int numUsers = getDataModel().getNumUsers();
    for (long iitem : itemNeighborhood) {
      PreferenceArray iPrefs = getDataModel().getPreferencesForItem(iitem);
      int iSize = iPrefs.length();
      int j = 0;
      for (long jitem : itemNeighborhood) {
        double value = 0.0;
        for (int pi = 0; pi < iSize; pi++) {
          long v = iPrefs.getUserID(pi);
          if (v == userID) {
            continue;
          }
          Float pj = dataModel.getPreferenceValue(userID, jitem);
          if (pj != null) {
            value += iPrefs.getValue(pi) * pj;
          }
        }
        aMatrix[i][j] = value / numUsers;
        j++;
      }
      i++;
    }
   
    PreferenceArray iPrefs = getDataModel().getPreferencesForItem(itemID);
    int iSize = iPrefs.length();
    i = 0;
    for (long jitem : itemNeighborhood) {
      double value = 0.0;
      for (int pi = 0; pi < iSize; pi++) {
        long v = iPrefs.getUserID(pi);
        if (v == userID) {
          continue;
        }
        Float pj = dataModel.getPreferenceValue(userID, jitem);
        if (pj != null) {
          value += iPrefs.getValue(pi) * pj;
        }
      }
      b[i] = value / numUsers;
      i++;
    }
View Full Code Here

 
  @Override
  protected float doEstimatePreference(long theUserID, long itemID) throws TasteException {
   
    DataModel dataModel = getDataModel();
    PreferenceArray prefs = dataModel.getPreferencesFromUser(theUserID);
    int size = prefs.length();
    FastIDSet possibleItemIDs = new FastIDSet(size);
    for (int i = 0; i < size; i++) {
      possibleItemIDs.add(prefs.getItemID(i));
    }
    possibleItemIDs.remove(itemID);
   
    List<RecommendedItem> mostSimilar = mostSimilarItems(itemID, possibleItemIDs.iterator(),
      neighborhoodSize, null);
View Full Code Here

    Preconditions.checkArgument(howMany >= 1, "howMany must be at least 1");

    DataModel model = getDataModel();
    TopItems.Estimator<Long> estimator = new RecommendedBecauseEstimator(userID, itemID, similarity);

    PreferenceArray prefs = model.getPreferencesFromUser(userID);
    int size = prefs.length();
    FastIDSet allUserItems = new FastIDSet(size);
    for (int i = 0; i < size; i++) {
      allUserItems.add(prefs.getItemID(i));
    }
    allUserItems.remove(itemID);

    return TopItems.getTopItems(howMany, allUserItems.iterator(), null, estimator);
  }
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.