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

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


      List<FastIDSet> clusters = new ArrayList<FastIDSet>();
      // Begin with a cluster for each user:
      LongPrimitiveIterator it = model.getUserIDs();
      while (it.hasNext()) {
        FastIDSet newCluster = new FastIDSet();
        newCluster.add(it.nextLong());
        clusters.add(newCluster);
      }

      boolean done = false;
      while (!done) {
View Full Code Here


      if (clusteringByThreshold && (top.getSimilarity() < clusteringThreshold)) {
        done = true;
        break;
      }
     
      FastIDSet cluster1 = top.getCluster1();
      FastIDSet cluster2 = top.getCluster2();
     
      // Pull out current two clusters from clusters
      Iterator<FastIDSet> clusterIterator = clusters.iterator();
      boolean removed1 = false;
      boolean removed2 = false;
      while (clusterIterator.hasNext() && !(removed1 && removed2)) {
        FastIDSet current = clusterIterator.next();
        // Yes, use == here
        if (!removed1 && (cluster1 == current)) {
          clusterIterator.remove();
          removed1 = true;
        } else if (!removed2 && (cluster2 == current)) {
          clusterIterator.remove();
          removed2 = true;
        }
      }
     
      // The only catch is if a cluster showed it twice in the list of best cluster pairs;
      // have to remove the others. Pull out anything referencing these clusters from queue
      for (Iterator<ClusterClusterPair> queueIterator = queue.iterator(); queueIterator.hasNext();) {
        ClusterClusterPair pair = queueIterator.next();
        FastIDSet pair1 = pair.getCluster1();
        FastIDSet pair2 = pair.getCluster2();
        if ((pair1 == cluster1) || (pair1 == cluster2) || (pair2 == cluster1) || (pair2 == cluster2)) {
          queueIterator.remove();
        }
      }
     
      // Make new merged cluster
      FastIDSet merged = new FastIDSet(cluster1.size() + cluster2.size());
      merged.addAll(cluster1);
      merged.addAll(cluster2);
     
      // Compare against other clusters; update queue if needed
      // That new pair we're just adding might be pretty close to something else, so
      // catch that case here and put it back into our queue
      for (FastIDSet cluster : clusters) {
View Full Code Here

                                                       List<FastIDSet> clusters) throws TasteException {
    Queue<ClusterClusterPair> queue =
        new PriorityQueue<ClusterClusterPair>(numUsers + 1, Collections.<ClusterClusterPair>reverseOrder());
    int size = clusters.size();
    for (int i = 0; i < size; i++) {
      FastIDSet cluster1 = clusters.get(i);
      for (int j = i + 1; j < size; j++) {
        FastIDSet cluster2 = clusters.get(j);
        double similarity = clusterSimilarity.getSimilarity(cluster1, cluster2);
        if (!Double.isNaN(similarity)) {
          if (queue.size() < numUsers) {
            queue.add(new ClusterClusterPair(cluster1, cluster2, similarity));
          } else if (similarity > queue.poll().getSimilarity()) {
View Full Code Here

  }
 
  private List<RecommendedItem> computeTopRecsForCluster(FastIDSet cluster) throws TasteException {
   
    DataModel dataModel = getDataModel();
    FastIDSet possibleItemIDs = new FastIDSet();
    LongPrimitiveIterator it = cluster.iterator();
    while (it.hasNext()) {
      possibleItemIDs.addAll(dataModel.getItemIDsFromUser(it.next()));
    }
   
    TopItems.Estimator<Long> estimator = new Estimator(cluster);
   
    List<RecommendedItem> topItems = TopItems.getTopItems(NUM_CLUSTER_RECS,
      possibleItemIDs.iterator(), null, estimator);
   
    log.debug("Recommendations are: {}", topItems);
    return Collections.unmodifiableList(topItems);
  }
View Full Code Here

  public GenericDataModel(FastByIDMap<PreferenceArray> userData, FastByIDMap<FastByIDMap<Long>> timestamps) {
    Preconditions.checkArgument(userData != null, "userData is null");

    this.preferenceFromUsers = userData;
    FastByIDMap<Collection<Preference>> prefsForItems = new FastByIDMap<Collection<Preference>>();
    FastIDSet itemIDSet = new FastIDSet();
    int currentCount = 0;
    float maxPrefValue = Float.NEGATIVE_INFINITY;
    float minPrefValue = Float.POSITIVE_INFINITY;
    for (Map.Entry<Long, PreferenceArray> entry : preferenceFromUsers.entrySet()) {
      PreferenceArray prefs = entry.getValue();
      prefs.sortByItem();
      for (Preference preference : prefs) {
        long itemID = preference.getItemID();
        itemIDSet.add(itemID);
        List<Preference> prefsForItem = (List<Preference>) prefsForItems.get(itemID);
        if (prefsForItem == null) {
          prefsForItem = new ArrayList<Preference>(2);
          prefsForItems.put(itemID, prefsForItem);
        }
        prefsForItem.add(preference);
        float value = preference.getValue();
        if (value > maxPrefValue) {
          maxPrefValue = value;
        }
        if (value < minPrefValue) {
          minPrefValue = value;
        }
      }
      if (++currentCount % 10000 == 0) {
        log.info("Processed {} users", currentCount);
      }
    }
    log.info("Processed {} users", currentCount);

    setMinPreference(minPrefValue);
    setMaxPreference(maxPrefValue);

    this.itemIDs = itemIDSet.toArray();
    itemIDSet = null; // Might help GC -- this is big
    Arrays.sort(itemIDs);

    this.preferenceForItems = toDataMap(prefsForItems, false);
View Full Code Here

 
  @Override
  public FastIDSet getItemIDsFromUser(long userID) throws TasteException {
    PreferenceArray prefs = getPreferencesFromUser(userID);
    int size = prefs.length();
    FastIDSet result = new FastIDSet(size);
    for (int i = 0; i < size; i++) {
      result.add(prefs.getItemID(i));
    }
    return result;
  }
View Full Code Here

    // itemIDs.length == 2)
    PreferenceArray prefs2 = preferenceForItems.get(itemIDs[1]);
    if (prefs2 == null) {
      return 0;
    }
    FastIDSet users1 = new FastIDSet(prefs1.length());
    int size1 = prefs1.length();
    for (int i = 0; i < size1; i++) {
      users1.add(prefs1.getUserID(i));
    }
    FastIDSet users2 = new FastIDSet(prefs2.length());
    int size2 = prefs2.length();
    for (int i = 0; i < size2; i++) {
      users2.add(prefs2.getUserID(i));
    }
    users1.retainAll(users2);
    return users1.size();
  }
View Full Code Here

      stmt.setLong(1, userID);
      stmt.setLong(2, userID);
      stmt.setLong(3, userID);
      log.debug("Executing SQL query: {}", getRecommendableItemsSQL);
      rs = stmt.executeQuery();
      FastIDSet itemIDs = new FastIDSet();
      while (rs.next()) {
        itemIDs.add(rs.getLong(1));
      }
      return itemIDs;
    } catch (SQLException sqle) {
      log.warn("Exception while retrieving recommendable items", sqle);
      throw new TasteException(sqle);
View Full Code Here

    context.write(EasyMock.eq(new VarIntWritable(34)), prefOfVectorOrPrefWritableMatches(123L, 0.5f));
    context.write(EasyMock.eq(new VarIntWritable(56)), prefOfVectorOrPrefWritableMatches(123L, 0.7f));

    EasyMock.replay(context);

    FastIDSet usersToRecommendFor = new FastIDSet();
    usersToRecommendFor.add(123L);

    UserVectorSplitterMapper mapper = new UserVectorSplitterMapper();
    setField(mapper, "maxPrefsPerUserConsidered", 10);
    setField(mapper, "usersToRecommendFor", usersToRecommendFor);
View Full Code Here

      log.debug("Executing SQL query: {}", getAllUsersSQL);
      rs = stmt.executeQuery(getAllUsersSQL);

      boolean currentUserIDSet = false;
      long currentUserID = 0L; // value isn't used
      FastIDSet currentItemIDs = new FastIDSet(2);
      while (rs.next()) {
        long nextUserID = getLongColumn(rs, 1);
        if (currentUserIDSet && (currentUserID != nextUserID) && !currentItemIDs.isEmpty()) {
          result.put(currentUserID, currentItemIDs);
          currentItemIDs = new FastIDSet(2);
        }
        currentItemIDs.add(getLongColumn(rs, 2));
        currentUserID = nextUserID;
        currentUserIDSet = true;
      }
      if (!currentItemIDs.isEmpty()) {
        result.put(currentUserID, currentItemIDs);
      }

      return result;
View Full Code Here

TOP

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

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.