当前位置: 首页>>代码示例>>Java>>正文


Java MutableSparseVector.set方法代码示例

本文整理汇总了Java中org.grouplens.lenskit.vectors.MutableSparseVector.set方法的典型用法代码示例。如果您正苦于以下问题:Java MutableSparseVector.set方法的具体用法?Java MutableSparseVector.set怎么用?Java MutableSparseVector.set使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在org.grouplens.lenskit.vectors.MutableSparseVector的用法示例。


在下文中一共展示了MutableSparseVector.set方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: score

import org.grouplens.lenskit.vectors.MutableSparseVector; //导入方法依赖的package包/类
/**
 * Generate item scores personalized for a particular user.  For the TFIDF scorer, this will
 * prepare a user profile and compare it to item tag vectors to produce the score.
 *
 * @param user   The user to score for.
 * @param output The output vector.  The contract of this method is that the caller creates a
 *               vector whose possible keys are all items that should be scored; this method
 *               fills in the scores.
 */
@Override
public void score(long user, @Nonnull MutableSparseVector output) {
    // Get the user's profile, which is a vector with their 'like' for each tag
    SparseVector userVector = makeUserVector(user);

    // Loop over each item requested and score it.
    // The *domain* of the output vector is the items that we are to score.
    for (VectorEntry e: output.fast(VectorEntry.State.EITHER)) {
        // Score the item represented by 'e'.
        // Get the item vector for this item
        SparseVector iv = model.getItemVector(e.getKey());

        double similarity = iv.dot(userVector)/(iv.norm()*userVector.norm());
        output.set(e.getKey(), similarity);
        // DA FARE Compute the cosine of this item and the user's profile, store it in the output vector
    }
}
 
开发者ID:paolobarbaglia,项目名称:coursera_recommender_systems,代码行数:27,代码来源:TFIDFItemScorer.java

示例2: score

import org.grouplens.lenskit.vectors.MutableSparseVector; //导入方法依赖的package包/类
/**
 * Generate item scores personalized for a particular user.  For the TFIDF scorer, this will
 * prepare a user profile and compare it to item tag vectors to produce the score.
 *
 * @param user   The user to score for.
 * @param output The output vector.  The contract of this method is that the caller creates a
 *               vector whose possible keys are all items that should be scored; this method
 *               fills in the scores.
 */
@Override
public void score(long user, @Nonnull MutableSparseVector output) {
    // Get the user's profile, which is a vector with their 'like' for each tag
    SparseVector userVector = makeUserVector(user);

    // Loop over each item requested and score it.
    // The *domain* of the output vector is the items that we are to score.
    for (VectorEntry e: output.fast(VectorEntry.State.EITHER)) {
        // Score the item represented by 'e'.
        // Get the item vector for this item
    	SparseVector iv = model.getItemVector(e.getKey());
    	

        double similarity = iv.dot(userVector)/(iv.norm()*userVector.norm());
        output.set(e.getKey(), similarity);
        // DA FARE Compute the cosine of this item and the user's profile, store it in the output vector
    }
}
 
开发者ID:paolobarbaglia,项目名称:coursera_recommender_systems,代码行数:28,代码来源:TFIDFItemScorer.java

示例3: score

import org.grouplens.lenskit.vectors.MutableSparseVector; //导入方法依赖的package包/类
/**
 * Score items in a vector. The key domain of the provided vector is the
 * items to score, and the score method sets the values for each item to its
 * score (or unsets it, if no score can be provided). The previous values
 * are discarded.
 * 
 * @param user
 *            The user ID.
 * @param scores
 *            The score vector.
 */
@Override
public void score(long user, @Nonnull MutableSparseVector scores) {
	// P = b + U.S.Vt
	if (model.getUserVector(user) == null) {
		scores.clear();
	} else {
		RealMatrix U = model.getUserVector(user);
		RealMatrix S = model.getFeatureWeights();

		for (VectorEntry e : scores.fast(VectorEntry.State.EITHER)) {
			long item = e.getKey();
			RealMatrix V = model.getItemVector(item);
			scores.set(item,
					baselineScorer.score(user, item)
							+ (U.multiply(S)).multiply(V.transpose())
									.getEntry(0, 0));
		}
	}
}
 
开发者ID:paolobarbaglia,项目名称:coursera_recommender_systems,代码行数:31,代码来源:SVDItemScorer.java

示例4: globalScore

import org.grouplens.lenskit.vectors.MutableSparseVector; //导入方法依赖的package包/类
/**
 * Score items with respect to a set of reference items.
 * 
 * @param items
 *            The reference items.
 * @param scores
 *            The score vector. Its domain is the items to be scored, and the scores should be stored into this vector.
 */
@Override
public void globalScore(@Nonnull Collection<Long> items, @Nonnull MutableSparseVector scores) {
	scores.fill(0);
	// each item's score is the sum of its similarity to each item in items, if they are
	// neighbors in the model.

	for (VectorEntry e : scores.fast(VectorEntry.State.EITHER)) {
		long item = e.getKey();
		List<ScoredId> neighbors = model.getNeighbors(item);
		double sumScore = 0;
		for (ScoredId thisNghbr : neighbors) {
			if (items.contains(thisNghbr.getId()))
				sumScore += thisNghbr.getScore();
		}
		scores.set(item, sumScore);
	}
}
 
开发者ID:rohitsinha54,项目名称:Coursera-Introduction-to-Recommender-Systems-Programming-Assignment-5,代码行数:26,代码来源:SimpleGlobalItemScorer.java

示例5: score

import org.grouplens.lenskit.vectors.MutableSparseVector; //导入方法依赖的package包/类
/**
  * Score items for a user.
  * @param user The user ID.
  * @param scores The score vector.  Its key domain is the items to score, and the scores
  *               (rating predictions) should be written back to this vector.
  */
 @Override
 public void score(long user, @Nonnull MutableSparseVector scores) {
     SparseVector ratings = getUserRatingVector(user);

     for (VectorEntry e: scores.fast(VectorEntry.State.EITHER)) {
         long item = e.getKey();
         List<ScoredId> neighbors = model.getNeighbors(item);
int nghbrCount = 0;
double numerator = 0, denominator = 0;

for(ScoredId thisNghbr : neighbors){
	if(nghbrCount < neighborhoodSize){
		if (!ratings.containsKey(thisNghbr.getId())) continue;
		double thisItemRating = ratings.get(thisNghbr.getId());
		double thisItemSimilarity = thisNghbr.getScore();

		numerator += thisItemRating * thisItemSimilarity;
		denominator += thisItemSimilarity;
		nghbrCount++;
	}
}
scores.set(item, numerator/denominator);
     }
 }
 
开发者ID:rohitsinha54,项目名称:Coursera-Introduction-to-Recommender-Systems-Programming-Assignment-5,代码行数:31,代码来源:SimpleItemItemScorer.java

示例6: score

import org.grouplens.lenskit.vectors.MutableSparseVector; //导入方法依赖的package包/类
@Override
public void score(long user, @Nonnull MutableSparseVector scores) {
    SparseVector userVector = getUserRatingVector(user);

    // TODO Score items for this user using user-user collaborative filtering



    // This is the loop structure to iterate over items to score
    for (VectorEntry e: scores.fast(VectorEntry.State.EITHER)) {
        long itemid = e.getKey();

        //Weird method to get the top30   Please Ignore
        TreeMap<Double, Long> simMap = new TreeMap<Double, Long>();
        for (long uid: itemDao.getUsersForItem(itemid)){
            if (user != uid){
                simMap.put(-getUserUserSimilarity(user,uid),uid);
            }
        }
        int counter=0;
        double simscore = 0.0;
        double simsum = 0.0;

        for(Map.Entry<Double,Long> ent:simMap.entrySet()){
            double sim = -ent.getKey();
            long userid = ent.getValue();

            simsum+=Math.abs(sim);
            simscore+= sim* (getUserRatingVector(userid).get(itemid)- getUserRatingVector(userid).mean());
            counter++;
            if(counter>=30) break;
        }

        double result = simscore / simsum +userVector.mean();
        scores.set(e,result);


    }
}
 
开发者ID:4DD8A19D69F5324F9D49D17EF78BBBCC,项目名称:Introd_uction_to_Recom_mander_S_ystem,代码行数:40,代码来源:SimpleUserUserItemScorer.java

示例7: globalScore

import org.grouplens.lenskit.vectors.MutableSparseVector; //导入方法依赖的package包/类
/**
 * Score items with respect to a set of reference items.
 * @param items The reference items.
 * @param scores The score vector. Its domain is the items to be scored, and the scores should
 *               be stored into this vector.
 */
@Override
public void globalScore(@Nonnull Collection<Long> items, @Nonnull MutableSparseVector scores) {
	scores.fill(0);
	// score items in the domain of scores
	for (VectorEntry e: scores.fast(VectorEntry.State.EITHER)) {
		// each item's score is the sum of its similarity to each item in items, if they are
		// neighbors in the model.
		long itemId = e.getKey();
		
		// getting neighbors
		List<ScoredId> neighbors = model.getNeighbors(itemId);
		Map<Long, Double> neighMap = new HashMap<Long, Double>();
		for (ScoredId scoredId : neighbors) {
			neighMap.put(scoredId.getId(), scoredId.getScore());
		}
		
		// scoring similarity
		double score = 0.0;
		for(Long basketItem: items){
			Double similarity = 0.0;
			if(neighMap.containsKey(basketItem)) similarity = neighMap.get(basketItem);
			score += similarity;
		}
		
		// asserting score
		scores.set(e, score);
	}
}
 
开发者ID:paolobarbaglia,项目名称:coursera_recommender_systems,代码行数:35,代码来源:SimpleGlobalItemScorer.java

示例8: score

import org.grouplens.lenskit.vectors.MutableSparseVector; //导入方法依赖的package包/类
/**
* Calculate the predicted scores of a set of items for the input user
*/
  @Override
  public void score(long user, @Nonnull MutableSparseVector scores) {
      SparseVector userVector = getUserRatingVector(user);
      
      for (long itemToScore : scores.keyDomain()) {
      	
      	double predictedRating = 0.0;
      	double weight = 0.0;
      	LongSet Neighbors = getTopNeighbours(user, itemToScore);
      	
      	//For each neighbor increment the predicted score
      	for (Long neighbor : Neighbors) {
      		SparseVector neighbourVector = getUserRatingVector(neighbor);
		double neighbourMeanRating = neighbourVector.mean();
		double neighbourRating = neighbourVector.get(itemToScore);
		double offsetFromMean = neighbourRating - neighbourMeanRating;
		double similarity = similarityMap.get(neighbor);
		predictedRating = predictedRating + offsetFromMean*similarity;
		weight = weight + Math.abs(similarity);
	}
      	
      	//Divide by total weight
      	predictedRating = predictedRating/weight + userVector.mean();
      	scores.set(itemToScore, predictedRating);
}
  }
 
开发者ID:paolobarbaglia,项目名称:coursera_recommender_systems,代码行数:30,代码来源:SimpleUserUserItemScorer.java

示例9: entropy

import org.grouplens.lenskit.vectors.MutableSparseVector; //导入方法依赖的package包/类
private double entropy(TagVocabulary vocab, ItemTagDAO tagDAO, List<ScoredId> recommendations) {
    MutableSparseVector tagProbs = tagProbabilities(vocab, tagDAO, recommendations);
    MutableSparseVector logProbs = MutableSparseVector.create(tagProbs.keyDomain());
    for (VectorEntry entry : logProbs.fast(State.UNSET)) {
        long tagid = entry.getKey();
        double probability = tagProbs.get(tagid);
        double logprob = Math.log(probability) / Math.log(2);
        logProbs.set(entry, logprob);
    }
    return -1.0 * logProbs.dot(tagProbs);
}
 
开发者ID:paolobarbaglia,项目名称:coursera_recommender_systems,代码行数:12,代码来源:TagEntropyMetric.java

示例10: score

import org.grouplens.lenskit.vectors.MutableSparseVector; //导入方法依赖的package包/类
@Override
public void score(long l, @Nonnull MutableSparseVector vectorEntries) {
    vectorEntries.set(itemPopularity);
}
 
开发者ID:paolobarbaglia,项目名称:coursera_recommender_systems,代码行数:5,代码来源:PopularityItemScorer.java


注:本文中的org.grouplens.lenskit.vectors.MutableSparseVector.set方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。