當前位置: 首頁>>代碼示例>>Java>>正文


Java ImmutableSparseVector類代碼示例

本文整理匯總了Java中org.grouplens.lenskit.vectors.ImmutableSparseVector的典型用法代碼示例。如果您正苦於以下問題:Java ImmutableSparseVector類的具體用法?Java ImmutableSparseVector怎麽用?Java ImmutableSparseVector使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


ImmutableSparseVector類屬於org.grouplens.lenskit.vectors包,在下文中一共展示了ImmutableSparseVector類的6個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: get

import org.grouplens.lenskit.vectors.ImmutableSparseVector; //導入依賴的package包/類
@Override
public SimpleItemItemModel get() {
    // Get the transposed rating matrix
    // This gives us a map of item IDs to those items' rating vectors
    Map<Long, ImmutableSparseVector> itemVectors = getItemVectors();

    // Get all items - you might find this useful
    LongSortedSet items = LongUtils.packedSet(itemVectors.keySet());
    // Map items to vectors of item similarities
    Map<Long,MutableSparseVector> itemSimilarities = new HashMap<Long, MutableSparseVector>();

    // TODO Compute the similarities between each pair of items
    // It will need to be in a map of longs to lists of Scored IDs to store in the model
    return new SimpleItemItemModel(Collections.EMPTY_MAP);
}
 
開發者ID:4DD8A19D69F5324F9D49D17EF78BBBCC,項目名稱:Introd_uction_to_Recom_mander_S_ystem,代碼行數:16,代碼來源:SimpleItemItemModelBuilder.java

示例2: getItemVectors

import org.grouplens.lenskit.vectors.ImmutableSparseVector; //導入依賴的package包/類
/**
 * Load the data into memory, indexed by item.
 * @return A map from item IDs to item rating vectors. Each vector contains users' ratings for
 * the item, keyed by user ID.
 */
public Map<Long,ImmutableSparseVector> getItemVectors() {
    // set up storage for building each item's rating vector
    LongSet items = itemDao.getItemIds();
    // map items to maps from users to ratings
    Map<Long,Map<Long,Double>> itemData = new HashMap<Long, Map<Long, Double>>();
    for (long item: items) {
        itemData.put(item, new HashMap<Long, Double>());
    }
    // itemData should now contain a map to accumulate the ratings of each item

    // stream over all user events
    Cursor<UserHistory<Event>> stream = userEventDao.streamEventsByUser();
    try {
        for (UserHistory<Event> evt: stream) {
            MutableSparseVector vector = RatingVectorUserHistorySummarizer.makeRatingVector(evt).mutableCopy();
            // vector is now the user's rating vector
            // TODO Normalize this vector and store the ratings in the item data
        }
    } finally {
        stream.close();
    }

    // This loop converts our temporary item storage to a map of item vectors
    Map<Long,ImmutableSparseVector> itemVectors = new HashMap<Long, ImmutableSparseVector>();
    for (Map.Entry<Long,Map<Long,Double>> entry: itemData.entrySet()) {
        MutableSparseVector vec = MutableSparseVector.create(entry.getValue());
        itemVectors.put(entry.getKey(), vec.immutable());
    }
    return itemVectors;
}
 
開發者ID:4DD8A19D69F5324F9D49D17EF78BBBCC,項目名稱:Introd_uction_to_Recom_mander_S_ystem,代碼行數:36,代碼來源:SimpleItemItemModelBuilder.java

示例3: getItemVectors

import org.grouplens.lenskit.vectors.ImmutableSparseVector; //導入依賴的package包/類
/**
 * Load the data into memory, indexed by item.
 * @return A map from item IDs to item rating vectors. Each vector contains users' ratings for
 * the item, keyed by user ID.
 */
public Map<Long,ImmutableSparseVector> getItemVectors() {
	// set up storage for building each item's rating vector
	LongSet items = itemDao.getItemIds();
	// map items to maps from users to ratings
	Map<Long,Map<Long,Double>> itemData = new HashMap<Long, Map<Long, Double>>();
	for (long item: items) {
		itemData.put(item, new HashMap<Long, Double>());
	}
	// itemData should now contain a map to accumulate the ratings of each item

	// stream over all user events
	Cursor<UserHistory<Event>> stream = userEventDao.streamEventsByUser();
	try {
		for (UserHistory<Event> evt: stream) {
			MutableSparseVector vector = RatingVectorUserHistorySummarizer.makeRatingVector(evt).mutableCopy();
			// vector is now the user's rating vector
			// Normalize this vector
			vector.add(-vector.mean());
			// Store the ratings in the item data
			for (VectorEntry vectorEntry : vector.fast(VectorEntry.State.EITHER)) {
				long itemId = vectorEntry.getKey();
				double rating = vectorEntry.getValue();
				long userId = evt.getUserId();
				itemData.get(itemId).put(userId, rating);
			}
		}
	} finally {
		stream.close();
	}

	// This loop converts our temporary item storage to a map of item vectors
	Map<Long,ImmutableSparseVector> itemVectors = new HashMap<Long, ImmutableSparseVector>();
	for (Map.Entry<Long,Map<Long,Double>> entry: itemData.entrySet()) {
		MutableSparseVector vec = MutableSparseVector.create(entry.getValue());
		itemVectors.put(entry.getKey(), vec.immutable());
	}
	return itemVectors;
}
 
開發者ID:paolobarbaglia,項目名稱:coursera_recommender_systems,代碼行數:44,代碼來源:SimpleItemItemModelBuilder.java

示例4: getItemVectors

import org.grouplens.lenskit.vectors.ImmutableSparseVector; //導入依賴的package包/類
/**
 * Load the data into memory, indexed by item.
 * @return A map from item IDs to item rating vectors. Each vector contains users' ratings for
 * the item, keyed by user ID.
 */
public Map<Long,ImmutableSparseVector> getItemVectors() {
    // set up storage for building each item's rating vector
    LongSet items = itemDao.getItemIds();
    // map items to maps from users to ratings
    Map<Long,Map<Long,Double>> itemData = new HashMap<Long, Map<Long, Double>>();
    for (long item: items) {
        itemData.put(item, new HashMap<Long, Double>());
    }
    // itemData should now contain a map to accumulate the ratings of each item

    // stream over all user events
    Cursor<UserHistory<Event>> stream = userEventDao.streamEventsByUser();
    try {
        for (UserHistory<Event> evt: stream) {
            MutableSparseVector vector = RatingVectorUserHistorySummarizer.makeRatingVector(evt).mutableCopy();
            // vector is now the user's rating vector

            // Normalizing this vector and store the ratings in the item data
vector.add(-(vector.mean()));
for (VectorEntry e: vector) {
	itemData.get(e.getKey()).put(evt.getUserId(), e.getValue());
}
        }
    } finally {
        stream.close();
    }

    // This loop converts our temporary item storage to a map of item vectors
    Map<Long,ImmutableSparseVector> itemVectors = new HashMap<Long, ImmutableSparseVector>();
    for (Map.Entry<Long,Map<Long,Double>> entry: itemData.entrySet()) {
        MutableSparseVector vec = MutableSparseVector.create(entry.getValue());
        itemVectors.put(entry.getKey(), vec.immutable());
    }
    return itemVectors;
}
 
開發者ID:rohitsinha54,項目名稱:Coursera-Introduction-to-Recommender-Systems-Programming-Assignment-5,代碼行數:41,代碼來源:SimpleItemItemModelBuilder.java

示例5: get

import org.grouplens.lenskit.vectors.ImmutableSparseVector; //導入依賴的package包/類
@Override
public SimpleItemItemModel get() {
	// Get the transposed rating matrix
	// This gives us a map of item IDs to those items' rating vectors
	Map<Long, ImmutableSparseVector> itemVectors = getItemVectors();

	// Get all items - you might find this useful
	LongSortedSet items = LongUtils.packedSet(itemVectors.keySet());
	// Map items to vectors of item similarities
	@SuppressWarnings("unused")
	Map<Long,MutableSparseVector> itemSimilarities = new HashMap<Long, MutableSparseVector>();

	// Compute the similarities between each pair of items
	// It will need to be in a map of longs to lists of Scored IDs to store in the model
	Map<Long, List<ScoredId>> neighborhoods = new HashMap<Long, List<ScoredId>>();


	// Compute the similarities between each pair of items
	CosineVectorSimilarity cosine = new CosineVectorSimilarity();

	for(long item : items){

		// get this item ratings
		ImmutableSparseVector itemRatings = itemVectors.get(item);

		// create the accumulator for this item
		TopNScoredItemAccumulator accumulator = new TopNScoredItemAccumulator(items.size() - 1);

		for(long neighbor : items){

			// skip itself
			if(item == neighbor) continue;

			ImmutableSparseVector neighRatings = itemVectors.get(neighbor);

			// cosine similarity
			double similarity = cosine.similarity(itemRatings, neighRatings);

			//accumulate positive similarities
			if(similarity >= 0.0){
				accumulator.put(neighbor, similarity);
			}
		}

		//get the final list of sorted neighbors
		List<ScoredId> similarities = accumulator.finish();

		// update the map of similarity
		neighborhoods.put(item, similarities);
	}

	// It will need to be in a map of longs to lists of Scored IDs to store in the model
	return new SimpleItemItemModel(neighborhoods);
}
 
開發者ID:paolobarbaglia,項目名稱:coursera_recommender_systems,代碼行數:55,代碼來源:SimpleItemItemModelBuilder.java

示例6: get

import org.grouplens.lenskit.vectors.ImmutableSparseVector; //導入依賴的package包/類
@Override
  public SimpleItemItemModel get() {
      // Get the transposed rating matrix
      // This gives us a map of item IDs to those items' rating vectors
      Map<Long, ImmutableSparseVector> itemVectors = getItemVectors();

      // Get all items - you might find this useful
      LongSortedSet items = LongUtils.packedSet(itemVectors.keySet());
      // Map items to vectors of item similarities
      //Map<Long,MutableSparseVector> itemSimilarities = new HashMap<Long, MutableSparseVector>();
Map<Long, List<ScoredId>> neighborhoods = new HashMap<Long, List<ScoredId>>();

// Computing the similarities between each pair of items
      // It will need to be in a map of longs to lists of Scored IDs to store in the model
for(Iterator outerIter = items.iterator(); outerIter.hasNext() ; ) {
	Long thisItemId = (Long) outerIter.next();

	TopNScoredItemAccumulator accumulator = new TopNScoredItemAccumulator(items.size()-1);

	// Calculate similiarity with other item one by one and
	for(Iterator innerIter = items.iterator(); innerIter.hasNext() ; ) {
		Long nghbrItemId = (Long) innerIter.next();

		if(thisItemId.equals(nghbrItemId)) continue;

		// cosine similarity
		double similarity = new CosineVectorSimilarity().similarity(itemVectors.get(thisItemId),
				itemVectors.get(nghbrItemId));

		//accumulate
		if (similarity > 0) {
			accumulator.put(nghbrItemId, similarity);
		}
	}
	//put in the final list of sorted neighbors
	List<ScoredId> similarities = accumulator.finish();
	neighborhoods.put(thisItemId, similarities);
}
return new SimpleItemItemModel(neighborhoods);
      //return new SimpleItemItemModel(Collections.EMPTY_MAP);
  }
 
開發者ID:rohitsinha54,項目名稱:Coursera-Introduction-to-Recommender-Systems-Programming-Assignment-5,代碼行數:42,代碼來源:SimpleItemItemModelBuilder.java


注:本文中的org.grouplens.lenskit.vectors.ImmutableSparseVector類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。