本文整理汇总了Java中org.apache.mahout.cf.taste.impl.common.LongPrimitiveIterator类的典型用法代码示例。如果您正苦于以下问题:Java LongPrimitiveIterator类的具体用法?Java LongPrimitiveIterator怎么用?Java LongPrimitiveIterator使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
LongPrimitiveIterator类属于org.apache.mahout.cf.taste.impl.common包,在下文中一共展示了LongPrimitiveIterator类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: buildRecommend
import org.apache.mahout.cf.taste.impl.common.LongPrimitiveIterator; //导入依赖的package包/类
public void buildRecommend(String taskName) {
String itemmodelsPath = RecommendConfig.class.getResource("/").getPath() + "itemmodels.csv";
HadoopUtil.download(taskName, itemmodelsPath, true);
try {
DataModel dataModel = new FileDataModel(new File(itemmodelsPath));
UserSimilarity similarity = new SpearmanCorrelationSimilarity(dataModel);
UserNeighborhood userNeighborhood = new ThresholdUserNeighborhood(0.1, similarity, dataModel);
LongPrimitiveIterator userIDs = dataModel.getUserIDs();
while (userIDs.hasNext()) {
Long userID = userIDs.nextLong();
long[] neighborhoods = userNeighborhood.getUserNeighborhood(userID);
for (long neighborhood : neighborhoods) {
double userSimilarity = similarity.userSimilarity(userID, neighborhood);
System.out.printf("(%s,%s,%f)", userID, neighborhood, userSimilarity);
System.out.println();
}
}
} catch (TasteException | IOException e) {
log.error(e);
}
}
示例2: intializeDelegate
import org.apache.mahout.cf.taste.impl.common.LongPrimitiveIterator; //导入依赖的package包/类
private void intializeDelegate() {
try {
//iterate over all user ids
LongPrimitiveIterator it = easyrecDataModelDelegate.getUserIDs();
FastByIDMap<PreferenceArray> preferences = new FastByIDMap<PreferenceArray>(easyrecDataModelDelegate.getNumUsers());
FastByIDMap<FastByIDMap<Long>> timestamps = new FastByIDMap<FastByIDMap<Long>>(easyrecDataModelDelegate.getNumUsers());
while (it.hasNext()) {
Long userId = it.next();
//get preferences for each user
PreferenceArray prefs = easyrecDataModelDelegate.getPreferencesFromUser(userId);
preferences.put(userId,prefs);
//get preference times for each user
FastByIDMap<Long> timestampsForUser = new FastByIDMap<Long>(prefs.getIDs().length);
for (Long itemId: prefs.getIDs()) {
timestampsForUser.put(itemId,easyrecDataModelDelegate.getPreferenceTime(userId, itemId));
}
timestamps.put(userId, timestampsForUser);
}
//generate GenericDataModel
setMaxPreference(easyrecDataModelDelegate.getMaxPreference());
setMinPreference(easyrecDataModelDelegate.getMinPreference());
this.inMemoryDelegate = new GenericDataModel(preferences,timestamps);
} catch (TasteException e) {
logger.warn("caught exception while reading preference data", e);
}
}
示例3: getItemIDs
import org.apache.mahout.cf.taste.impl.common.LongPrimitiveIterator; //导入依赖的package包/类
@Override
public LongPrimitiveIterator getItemIDs() throws TasteException {
FastIDSet itemIDs = new FastIDSet();
try {
Query.Builder query = Query.newBuilder();
query.setLimit(Integer.MAX_VALUE);
query.addKindBuilder().setName(DEFAULT_IMPRESSIONS_ITEMS_KIND_NAME);
List<Key> results = runProjectionQuery(query.build(), "getItemIDs()");
for (Key key : results) {
itemIDs.add(key.getPathElement(0).getId());
}
} catch (DatastoreException e) {
throw new TasteException(e);
}
return itemIDs.iterator();
}
示例4: main
import org.apache.mahout.cf.taste.impl.common.LongPrimitiveIterator; //导入依赖的package包/类
public static void main(String[] args) throws TasteException, IOException {
String file = "other/testdata/pv.csv";
DataModel dataModel = RecommendFactory.buildDataModelNoPref(file);
//基于UserCF城市街区距离(曼哈顿)算法
RecommenderBuilder rb1 = RecommenderEvaluator.userCityBlock(dataModel);
//基于ItemCF对数自然相似度算法
RecommenderBuilder rb2 = RecommenderEvaluator.itemCityBlock(dataModel);
LongPrimitiveIterator iter = dataModel.getUserIDs();
while (iter.hasNext()) {
long uid = iter.nextLong();
System.out.print("userCityBlock =>");
result(uid, rb1, dataModel);
System.out.print("itemLoglikelihood=>");
result(uid, rb2, dataModel);
}
}
示例5: displayRecommendation
import org.apache.mahout.cf.taste.impl.common.LongPrimitiveIterator; //导入依赖的package包/类
public static void displayRecommendation(
List<RecommendedItem> recommendations, DataModel dataModel)
throws TasteException {
for (LongPrimitiveIterator users = dataModel.getUserIDs(); users
.hasNext();) {
long userId = users.nextLong();
// List<RecommendedItem> recommendations =
// recommender.recommend(userId, 5);
for (RecommendedItem recommendation : recommendations) {
System.out.println(userId + "," + recommendation.getItemID()
+ "," + recommendation.getValue());
}
}
}
示例6: removeNotUpdated
import org.apache.mahout.cf.taste.impl.common.LongPrimitiveIterator; //导入依赖的package包/类
private static void removeNotUpdated(LongPrimitiveIterator it,
FastIDSet updated,
FastIDSet recentlyActive,
Lock writeLock) {
writeLock.lock();
try {
while (it.hasNext()) {
long id = it.nextLong();
if (!updated.contains(id) && !recentlyActive.contains(id)) {
it.remove();
}
}
} finally {
writeLock.unlock();
}
}
示例7: writeKnownIDs
import org.apache.mahout.cf.taste.impl.common.LongPrimitiveIterator; //导入依赖的package包/类
private static void writeKnownIDs(ObjectOutputStream out, FastByIDMap<FastIDSet> knownItemIDs) throws IOException {
if (knownItemIDs == null) {
out.writeInt(NULL_COUNT);
} else {
out.writeInt(knownItemIDs.size());
for (FastByIDMap.MapEntry<FastIDSet> entry : knownItemIDs.entrySet()) {
out.writeLong(entry.getKey());
FastIDSet itemIDs = entry.getValue();
out.writeInt(itemIDs.size());
LongPrimitiveIterator it = itemIDs.iterator();
while (it.hasNext()) {
out.writeLong(it.nextLong());
}
}
}
}
示例8: writeClusters
import org.apache.mahout.cf.taste.impl.common.LongPrimitiveIterator; //导入依赖的package包/类
private static void writeClusters(Collection<IDCluster> clusters, ObjectOutputStream out) throws IOException {
if (clusters == null) {
out.writeInt(0);
} else {
out.writeInt(clusters.size());
for (IDCluster cluster : clusters) {
FastIDSet members = cluster.getMembers();
out.writeInt(members.size());
LongPrimitiveIterator it = members.iterator();
while (it.hasNext()) {
out.writeLong(it.nextLong());
}
float[] centroid = cluster.getCentroid();
out.writeInt(centroid.length);
for (float f : centroid) {
out.writeFloat(f);
}
}
}
}
示例9: getIDsFromKeys
import org.apache.mahout.cf.taste.impl.common.LongPrimitiveIterator; //导入依赖的package包/类
private static FastIDSet getIDsFromKeys(FastByIDMap<float[]> map, Lock readLock, FastIDSet tagIDs) {
readLock.lock();
try {
FastIDSet ids = new FastIDSet(map.size());
LongPrimitiveIterator it = map.keySetIterator();
while (it.hasNext()) {
long id = it.nextLong();
if (!tagIDs.contains(id)) {
ids.add(id);
}
}
return ids;
} finally {
readLock.unlock();
}
}
示例10: partialTransposeTimesSelf
import org.apache.mahout.cf.taste.impl.common.LongPrimitiveIterator; //导入依赖的package包/类
/**
* Like {@link MatrixUtils#transposeTimesSelf(FastByIDMap)}, but instead of computing MT * M,
* it computes MT * C * M, where C is a diagonal matrix of 1s and 0s. This is like pretending some
* rows of M are 0.
*
* @see MatrixUtils#transposeTimesSelf(FastByIDMap)
* @see #LOSS_IGNORES_UNSPECIFIED
*/
private static RealMatrix partialTransposeTimesSelf(FastByIDMap<float[]> M,
int dimension,
LongPrimitiveIterator keys) {
RealMatrix result = new Array2DRowRealMatrix(dimension, dimension);
while (keys.hasNext()) {
long key = keys.next();
float[] vector = M.get(key);
for (int row = 0; row < dimension; row++) {
float rowValue = vector[row];
for (int col = 0; col < dimension; col++) {
result.addToEntry(row, col, rowValue * vector[col]);
}
}
}
return result;
}
示例11: chooseAboutNFromStream
import org.apache.mahout.cf.taste.impl.common.LongPrimitiveIterator; //导入依赖的package包/类
/**
* @param n approximate number of items to choose
* @param stream stream to choose from randomly
* @param streamSize (approximate) stream size
* @param random random number generator
* @return up to n elements chosen uninformly at random from the stream
*/
public static long[] chooseAboutNFromStream(int n,
LongPrimitiveIterator stream,
int streamSize,
RandomGenerator random) {
LongPrimitiveIterator it;
if (n < streamSize) {
it = new SamplingLongPrimitiveIterator(random, stream, (double) n / streamSize);
} else {
it = stream;
}
FastIDSet chosen = new FastIDSet(n);
while (it.hasNext()) {
chosen.add(it.nextLong());
}
return chosen.toArray();
}
示例12: getUserIDs
import org.apache.mahout.cf.taste.impl.common.LongPrimitiveIterator; //导入依赖的package包/类
@ShortCacheable
@Override
public LongPrimitiveIterator getUserIDs(int tenantId, Date cutoffDate, int actionTypeId) {
Object[] args = new Object[]{tenantId, cutoffDate, actionTypeId};
int[] argTypes = new int[]{Types.INTEGER, Types.TIMESTAMP, Types.INTEGER};
return new LongResultSetIteratorMysql(getDataSource(), getUserIDsQuery, args, argTypes);
}
示例13: getItemIDs
import org.apache.mahout.cf.taste.impl.common.LongPrimitiveIterator; //导入依赖的package包/类
@ShortCacheable
@Override
public LongPrimitiveIterator getItemIDs(int tenantId, Date cutoffDate, int actionTypeId) {
Object[] args = new Object[]{tenantId, cutoffDate, actionTypeId};
int[] argTypes = new int[]{Types.INTEGER, Types.TIMESTAMP, Types.INTEGER};
return new LongResultSetIteratorMysql(getDataSource(), getItemIDsQuery, args, argTypes);
}
示例14: testEasyrecDataModel_getItemIDs
import org.apache.mahout.cf.taste.impl.common.LongPrimitiveIterator; //导入依赖的package包/类
@Test
public void testEasyrecDataModel_getItemIDs() throws TasteException {
EasyrecDataModel easyrecDataModel = new EasyrecDataModel(TENANT_ID, RATE_ACTION_TYPE_ID, HAS_RATING_VALUES, mahoutDataModelMappingDAO);
LongPrimitiveIterator itemIds = easyrecDataModel.getItemIDs();
String ids = "";
while (itemIds.hasNext()) {
ids += itemIds.next();
}
assertEquals("123", ids);
//Test peek function of the iterator
itemIds = easyrecDataModel.getItemIDs();
ids = "";
ids += itemIds.peek();
while (itemIds.hasNext()) {
ids += itemIds.peek() + "-" + itemIds.next();
if (itemIds.hasNext()) {
ids += "-" + itemIds.peek() + "-" + itemIds.peek() + "#";
} else {
ids += "#";
}
}
assertEquals("11-1-2-2#2-2-3-3#3-3#", ids);
}
示例15: testEasyrecDataModel_getUserIDs
import org.apache.mahout.cf.taste.impl.common.LongPrimitiveIterator; //导入依赖的package包/类
@Test
public void testEasyrecDataModel_getUserIDs() throws TasteException {
EasyrecDataModel easyrecDataModel = new EasyrecDataModel(TENANT_ID, RATE_ACTION_TYPE_ID, HAS_RATING_VALUES, mahoutDataModelMappingDAO);
LongPrimitiveIterator userIds = easyrecDataModel.getUserIDs();
String ids = "";
while (userIds.hasNext()) {
ids += userIds.next();
}
assertEquals("12", ids);
}