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


Java MutableSparseVector.create方法代码示例

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


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

示例1: 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

示例2: 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;
}
 
开发者ID:paolobarbaglia,项目名称:coursera_recommender_systems,代码行数:44,代码来源:SimpleItemItemModelBuilder.java

示例3: createRatingMatrix

import org.grouplens.lenskit.vectors.MutableSparseVector; //导入方法依赖的package包/类
/**
 * Build a rating matrix from the rating data.  Each user's ratings are first normalized
 * by subtracting a baseline score (usually a mean).
 *
 * @param userMapping The index mapping of user IDs to column numbers.
 * @param itemMapping The index mapping of item IDs to row numbers.
 * @return A matrix storing the <i>normalized</i> user ratings.
 */
private RealMatrix createRatingMatrix(IdIndexMapping userMapping, IdIndexMapping itemMapping) {
    final int nusers = userMapping.size();
    final int nitems = itemMapping.size();

    // Create a matrix with users on rows and items on columns
    logger.info("creating {} by {} rating matrix", nusers, nitems);
    RealMatrix matrix = MatrixUtils.createRealMatrix(nusers, nitems);

    // populate it with data
    Cursor<UserHistory<Event>> users = userEventDAO.streamEventsByUser();
    try {
        for (UserHistory<Event> user: users) {
            // Get the row number for this user
            int u = userMapping.getIndex(user.getUserId());
            MutableSparseVector ratings = Ratings.userRatingVector(user.filter(Rating.class));
            MutableSparseVector baselines = MutableSparseVector.create(ratings.keySet());
            baselineScorer.score(user.getUserId(), baselines);
            
            for(VectorEntry v : ratings.fast())
            {
            	matrix.setEntry(u, itemMapping.getIndex(v.getKey()), v.getValue() - baselineScorer.score(user.getUserId(), v.getKey()));
            }
            
        }
    } finally {
        users.close();
    }

    return matrix;
}
 
开发者ID:paolobarbaglia,项目名称:coursera_recommender_systems,代码行数:39,代码来源:SVDModelBuilder.java

示例4: moviesetVocab

import org.grouplens.lenskit.vectors.MutableSparseVector; //导入方法依赖的package包/类
/**
 * A vector of tagids just within the movies in the set.
 * @param recommendations
 * @return
 */
private MutableSparseVector moviesetVocab(TagVocabulary vocab, ItemTagDAO tagDAO, Map<Long, Set<Long>> moviemap) {
    Set<Long> subsetVocab = new HashSet<Long>();
    for (long movie : moviemap.keySet()) {
        Set<Long> tagids = moviemap.get(movie);
        for (long tagid : tagids) {
            subsetVocab.add(tagid);
        }
    }
    return MutableSparseVector.create(subsetVocab);
}
 
开发者ID:paolobarbaglia,项目名称:coursera_recommender_systems,代码行数:16,代码来源:TagEntropyMetric.java

示例5: 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

示例6: get

import org.grouplens.lenskit.vectors.MutableSparseVector; //导入方法依赖的package包/类
@Override
public PopularityItemScorer get() {
    MutableSparseVector vec = MutableSparseVector.create(itemDAO.getItemIds(), 0);
    Cursor<Event> stream = eventDAO.streamEvents();
    try {
        for (Event e: stream) {
            vec.add(e.getItemId(), 1);
        }
    } finally {
        stream.close();
    }
    return new PopularityItemScorer(vec);
}
 
开发者ID:paolobarbaglia,项目名称:coursera_recommender_systems,代码行数:14,代码来源:PopularityItemScorer.java

示例7: 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

            // 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

示例8: newTagVector

import org.grouplens.lenskit.vectors.MutableSparseVector; //导入方法依赖的package包/类
public MutableSparseVector newTagVector() {
    return MutableSparseVector.create(tagMap.values());
}
 
开发者ID:paolobarbaglia,项目名称:coursera_recommender_systems,代码行数:4,代码来源:TagVocabulary.java

示例9: newTagVector

import org.grouplens.lenskit.vectors.MutableSparseVector; //导入方法依赖的package包/类
/**
 * Create a new mutable vector over all tag IDs.  The vector is initially empty, and its key
 * domain is the set of all tag IDs.
 *
 * @return A fresh vector over tag IDs.
 */
public MutableSparseVector newTagVector() {
    return MutableSparseVector.create(tagIds.values());
}
 
开发者ID:4DD8A19D69F5324F9D49D17EF78BBBCC,项目名称:Introd_uction_to_Recom_mander_S_ystem,代码行数:10,代码来源:TFIDFModel.java


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