本文整理汇总了Java中org.grouplens.lenskit.vectors.MutableSparseVector类的典型用法代码示例。如果您正苦于以下问题:Java MutableSparseVector类的具体用法?Java MutableSparseVector怎么用?Java MutableSparseVector使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
MutableSparseVector类属于org.grouplens.lenskit.vectors包,在下文中一共展示了MutableSparseVector类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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
}
}
示例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
}
}
示例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));
}
}
}
示例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: makeUserVector
import org.grouplens.lenskit.vectors.MutableSparseVector; //导入依赖的package包/类
private SparseVector makeUserVector(long user) {
// Get the user's ratings
List<Rating> userRatings = dao.getEventsForUser(user, Rating.class);
if (userRatings == null) {
// the user doesn't exist
return SparseVector.empty();
}
// Create a new vector over tags to accumulate the user profile
MutableSparseVector profile = model.newTagVector();
// Fill it with 0's initially - they don't like anything
profile.fill(0);
// Iterate over the user's ratings to build their profile
for (Rating r: userRatings) {
// In LensKit, ratings are expressions of preference
Preference p = r.getPreference();
// We'll never have a null preference. But in LensKit, ratings can have null
// preferences to express the user unrating an item
if (p != null && p.getValue() >= 3.5) {
// The user likes this item!
// Get the item's vector and add it to the user's profile
SparseVector iv = model.getItemVector(p.getItemId());
profile.add(iv);
}
}
// The profile is accumulated, return it.
// It is good practice to return a frozen vector.
return profile.freeze();
}
开发者ID:4DD8A19D69F5324F9D49D17EF78BBBCC,项目名称:Introd_uction_to_Recom_mander_S_ystem,代码行数:32,代码来源:TFIDFItemScorer.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);
// TODO score items in the domain of scores
// each item's score is the sum of its similarity to each item in items, if they are
// neighbors in the model.
}
开发者ID:4DD8A19D69F5324F9D49D17EF78BBBCC,项目名称:Introd_uction_to_Recom_mander_S_ystem,代码行数:14,代码来源:SimpleGlobalItemScorer.java
示例8: get
import org.grouplens.lenskit.vectors.MutableSparseVector; //导入依赖的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
示例9: getItemVectors
import org.grouplens.lenskit.vectors.MutableSparseVector; //导入依赖的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
示例10: 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);
// TODO Score this item and save the score into scores
}
}
开发者ID:4DD8A19D69F5324F9D49D17EF78BBBCC,项目名称:Introd_uction_to_Recom_mander_S_ystem,代码行数:17,代码来源:SimpleItemItemScorer.java
示例11: 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
示例12: getUserUserSimilarity
import org.grouplens.lenskit.vectors.MutableSparseVector; //导入依赖的package包/类
/**
* Get the cosine similarity between user-user
* @param user1 user ID 1
* @param user2 user ID 2
* @return the cosine similarity
*/
private double getUserUserSimilarity(long user1, long user2){
CosineVectorSimilarity csim = new CosineVectorSimilarity();
SparseVector uv1 = getUserRatingVector(user1);
SparseVector uv2 = getUserRatingVector(user2);
MutableSparseVector mv1 = uv1.mutableCopy();
MutableSparseVector mv2 = uv2.mutableCopy();
mv1.add(-uv1.mean());
mv2.add(-uv2.mean());
return csim.similarity(mv1,mv2);
}
开发者ID:4DD8A19D69F5324F9D49D17EF78BBBCC,项目名称:Introd_uction_to_Recom_mander_S_ystem,代码行数:18,代码来源:SimpleUserUserItemScorer.java
示例13: makeUserVector
import org.grouplens.lenskit.vectors.MutableSparseVector; //导入依赖的package包/类
private SparseVector makeUserVector(long user) {
// Get the user's ratings
List<Rating> userRatings = dao.getEventsForUser(user, Rating.class);
if (userRatings == null) {
// the user doesn't exist
return SparseVector.empty();
}
// Create a new vector over tags to accumulate the user profile
MutableSparseVector profile = model.newTagVector();
// Fill it with 0's initially - they don't like anything
profile.fill(0);
// Iterate over the user's ratings to build their profile
for (Rating r: userRatings) {
// In LensKit, ratings are expressions of preference
Preference p = r.getPreference();
// We'll never have a null preference. But in LensKit, ratings can have null
// preferences to express the user unrating an item
if (p != null && p.getValue() >= 3.5) {
SparseVector sparseVectorForItem = model.getItemVector(p.getItemId());
profile.add(sparseVectorForItem);
}
}
// The profile is accumulated, return it.
// It is good practice to return a frozen vector.
return profile.freeze();
}
示例14: 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);
}
}
示例15: getItemVectors
import org.grouplens.lenskit.vectors.MutableSparseVector; //导入依赖的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;
}