本文整理汇总了Java中org.apache.mahout.cf.taste.common.TasteException类的典型用法代码示例。如果您正苦于以下问题:Java TasteException类的具体用法?Java TasteException怎么用?Java TasteException使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
TasteException类属于org.apache.mahout.cf.taste.common包,在下文中一共展示了TasteException类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: buildRecommend
import org.apache.mahout.cf.taste.common.TasteException; //导入依赖的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: IRState
import org.apache.mahout.cf.taste.common.TasteException; //导入依赖的package包/类
public void IRState(String taskName) {
String itemmodelsPath = RecommendConfig.class.getResource("/").getPath() + "itemmodels.csv";
HadoopUtil.download(taskName, itemmodelsPath, false);
try {
DataModel fileDataModel = new FileDataModel(new File(itemmodelsPath));
RecommenderIRStatsEvaluator irStatsEvaluator = new GenericRecommenderIRStatsEvaluator();
IRStatistics irStatistics = irStatsEvaluator.evaluate(new RecommenderBuilder() {
@Override
public org.apache.mahout.cf.taste.recommender.Recommender buildRecommender(final DataModel dataModel) throws TasteException {
UserSimilarity userSimilarity = new PearsonCorrelationSimilarity(dataModel);
UserNeighborhood userNeighborhood = new NearestNUserNeighborhood(5, userSimilarity, dataModel);
return new GenericUserBasedRecommender(dataModel, userNeighborhood, userSimilarity);
}
}, new DataModelBuilder() {
@Override
public DataModel buildDataModel(final FastByIDMap<PreferenceArray> fastByIDMap) {
return new GenericDataModel(fastByIDMap);
}
}, fileDataModel, null, 5, GenericRecommenderIRStatsEvaluator.CHOOSE_THRESHOLD, 1.0);
System.out.println("查准率:" + irStatistics.getPrecision());
System.out.println("查全率:" + irStatistics.getRecall());
} catch (TasteException | IOException e) {
e.printStackTrace();
}
}
示例3: main
import org.apache.mahout.cf.taste.common.TasteException; //导入依赖的package包/类
public static void main(String[] args) throws IOException, TasteException {
DataModel model =
new FileDataModel(new File("data/ua.base"));
UserSimilarity similarity =
new PearsonCorrelationSimilarity(model);
UserNeighborhood neighborhood =
new NearestNUserNeighborhood(2, similarity, model);
Recommender recommender = new GenericUserBasedRecommender(
model, neighborhood, similarity);
List<RecommendedItem> recommendations = recommender.recommend(2, 1);
for (RecommendedItem recommendation : recommendations) {
logger.info(recommendation.toString());
}
logger.info("over");
}
示例4: recommend
import org.apache.mahout.cf.taste.common.TasteException; //导入依赖的package包/类
private static void recommend(String ratingsFile, int ... userIds)
throws TasteException, IOException {
DataModel model = new FileDataModel(new File(ratingsFile));
UserSimilarity similarity = new PearsonCorrelationSimilarity(model);
UserNeighborhood neighborhood =
new NearestNUserNeighborhood(
100, similarity, model);
Recommender recommender = new GenericUserBasedRecommender(
model, neighborhood, similarity);
Recommender cachingRecommender = new CachingRecommender(recommender);
for(int userId: userIds) {
System.out.println("UserID " + userId);
List<RecommendedItem> recommendations =
cachingRecommender.recommend(userId, 2);
for(RecommendedItem item: recommendations) {
System.out.println(" item " + item.getItemID() + " score " + item.getValue());
}
}
}
示例5: main
import org.apache.mahout.cf.taste.common.TasteException; //导入依赖的package包/类
public static void main(String[] args) throws TasteException, IOException {
DataModel model = new FileDataModel(new File("data/dataset.csv"));
ItemSimilarity similarity = new LogLikelihoodSimilarity(model);
//UserNeighborhood neighborhood = new ThresholdUserNeighborhood(0.1, similarity, model);
GenericItemBasedRecommender recommender = new GenericItemBasedRecommender(model, similarity);
List<RecommendedItem> recommendations = recommender.mostSimilarItems(18, 3);
for (RecommendedItem recommendation : recommendations) {
System.out.println(recommendation);
}
}
示例6: readItemIDFromString
import org.apache.mahout.cf.taste.common.TasteException; //导入依赖的package包/类
@Override
protected long readItemIDFromString(String value) {
if (memIdMigtr == null) {
memIdMigtr = new ItemMemIDMigrator();
}
long retValue = memIdMigtr.toLongID(value);
if (null == memIdMigtr.toStringID(retValue)) {
try {
memIdMigtr.singleInit(value);
} catch (TasteException e) {
e.printStackTrace();
}
}
return retValue;
}
开发者ID:PacktPublishing,项目名称:Machine-Learning-End-to-Endguide-for-Java-developers,代码行数:18,代码来源:StringItemIdFileDataModel.java
示例7: recommend
import org.apache.mahout.cf.taste.common.TasteException; //导入依赖的package包/类
public List<RecommendedItem> recommend(long userId, PreferenceArray preferences) throws TasteException {
if (userExistsInDataModel(userId)) {
return recommender.recommend(userId, noItems);
}
else {
PlusAnonymousConcurrentUserDataModel plusModel = (PlusAnonymousConcurrentUserDataModel) recommender.getDataModel();
// Take an available anonymous user form the poll
Long anonymousUserID = plusModel.takeAvailableUser();
// Set temporary preferences
PreferenceArray tempPrefs = preferences;
tempPrefs.setUserID(0, anonymousUserID);
// tempPrefs.setItemID(0, itemID);
plusModel.setTempPrefs(tempPrefs, anonymousUserID);
List<RecommendedItem> results = recommender.recommend(anonymousUserID, noItems);
// Release the user back to the poll
plusModel.releaseUser(anonymousUserID);
return results;
}
}
开发者ID:PacktPublishing,项目名称:Machine-Learning-End-to-Endguide-for-Java-developers,代码行数:27,代码来源:OnlineRecommendation.java
示例8: main
import org.apache.mahout.cf.taste.common.TasteException; //导入依赖的package包/类
public static void main(String[] args) throws IOException, TasteException {
RandomUtils.useTestSeed();
final DataModel model = new FileDataModel(new File("data/intro.csv"));
RecommenderIRStatsEvaluator evaluator =
new GenericRecommenderIRStatsEvaluator();
RecommenderBuilder recommenderBuilder = new RecommenderBuilder() {
public Recommender buildRecommender(DataModel dataModel) throws TasteException {
UserSimilarity similarity = new PearsonCorrelationSimilarity(model);
UserNeighborhood neighborhood =
new NearestNUserNeighborhood(2, similarity, model);
return new GenericUserBasedRecommender(model, neighborhood, similarity);
}
};
IRStatistics stats = evaluator.evaluate(recommenderBuilder, null, model, null, 2,
GenericRecommenderIRStatsEvaluator.CHOOSE_THRESHOLD, 1.0);
System.out.println(stats.getPrecision());
System.out.println(stats.getRecall());
}
示例9: intializeDelegate
import org.apache.mahout.cf.taste.common.TasteException; //导入依赖的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);
}
}
示例10: getItemIDs
import org.apache.mahout.cf.taste.common.TasteException; //导入依赖的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();
}
示例11: getPreferenceValue
import org.apache.mahout.cf.taste.common.TasteException; //导入依赖的package包/类
@Override
public Float getPreferenceValue(long userID, long itemID) throws TasteException {
try {
Query.Builder query = Query.newBuilder();
query.setLimit(Integer.MAX_VALUE);
query.addKindBuilder().setName(DEFAULT_IMPRESSIONS_KIND_NAME);
// Filter by userId
query.setFilter(DatastoreHelper.makeFilter(
USER_ID_COLUMN,
PropertyFilter.Operator.EQUAL,
DatastoreHelper.makeValue(userID)
));
query.setFilter(DatastoreHelper.makeFilter(
ITEM_ID_COLUMN,
PropertyFilter.Operator.EQUAL,
DatastoreHelper.makeValue(userID)
));
List<Entity> results = runQuery(query.build(), "getPreferenceValue()");
if(results.isEmpty()) {
return null;
}
return 1f; // Boolean/Binary preference values
} catch (DatastoreException e) {
throw new TasteException(e);
}
}
示例12: getPreferenceTime
import org.apache.mahout.cf.taste.common.TasteException; //导入依赖的package包/类
@Override
public Long getPreferenceTime(long userID, long itemID) throws TasteException {
try {
Query.Builder query = Query.newBuilder();
query.setLimit(Integer.MAX_VALUE);
query.addKindBuilder().setName(DEFAULT_IMPRESSIONS_KIND_NAME);
// Filter by userId
query.setFilter(DatastoreHelper.makeFilter(
USER_ID_COLUMN,
PropertyFilter.Operator.EQUAL,
DatastoreHelper.makeValue(userID)
));
query.setFilter(DatastoreHelper.makeFilter(
ITEM_ID_COLUMN,
PropertyFilter.Operator.EQUAL,
DatastoreHelper.makeValue(userID)
));
List<Entity> results = runQuery(query.build(), "getPreferenceTime()");
if(results.isEmpty()) {
return null;
}
return DatastoreHelper.getLong(DatastoreHelper.getPropertyMap(results.get(0)).get(TIMESTAMP_COLUMN));
} catch (DatastoreException e) {
throw new TasteException(e);
}
}
示例13: getNumItems
import org.apache.mahout.cf.taste.common.TasteException; //导入依赖的package包/类
@Override
public int getNumItems() throws TasteException {
Integer itemCount = itemCountCache.get();
if (itemCount == null) {
try {
Query.Builder query = Query.newBuilder();
query.addKindBuilder().setName(DEFAULT_IMPRESSIONS_ITEMS_KIND_NAME);
query.addProjection(PropertyExpression.newBuilder().setProperty(
PropertyReference.newBuilder().setName("__key__")));
List<Key> results = runProjectionQuery(query.build(), "getNumItems()");
itemCount = results.size();
itemCountCache.set(itemCount);
} catch (DatastoreException e) {
e.printStackTrace();
throw new TasteException(e);
}
}
return itemCount;
}
示例14: getNumUsers
import org.apache.mahout.cf.taste.common.TasteException; //导入依赖的package包/类
@Override
public int getNumUsers() throws TasteException {
Integer userCount = userCountCache.get();
if (userCount == null) {
try {
Query.Builder query = Query.newBuilder();
query.setLimit(Integer.MAX_VALUE);
query.addKindBuilder().setName(DEFAULT_IMPRESSIONS_USER_KIND_NAME);
List<Entity> results = runQuery(query.build(), "getNumUsers()");
userCount = results.size();
userCountCache.set(userCount);
} catch (DatastoreException e) {
throw new TasteException(e);
}
}
return userCount;
}
示例15: get
import org.apache.mahout.cf.taste.common.TasteException; //导入依赖的package包/类
@Override
public PreferenceArray get(Long userID) throws TasteException {
try {
List<Entity> results = getItemsForUser(userID);
if(results == null || results.isEmpty()) {
throw new NoSuchUserException(userID);
}
int i = 0;
PreferenceArray prefs = new GenericUserPreferenceArray(results.size());
prefs.setUserID(0, userID);
for (Entity entity : results) {
prefs.setItemID(
i,
DatastoreHelper.getLong(
DatastoreHelper.getPropertyMap(entity).get(ITEM_ID_COLUMN)
)
);
prefs.setValue(i, 1f);
i++;
}
return prefs;
} catch (DatastoreException e) {
throw new TasteException(e);
}
}