本文整理汇总了Java中org.apache.mahout.cf.taste.model.DataModel类的典型用法代码示例。如果您正苦于以下问题:Java DataModel类的具体用法?Java DataModel怎么用?Java DataModel使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
DataModel类属于org.apache.mahout.cf.taste.model包,在下文中一共展示了DataModel类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: buildRecommend
import org.apache.mahout.cf.taste.model.DataModel; //导入依赖的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.model.DataModel; //导入依赖的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.model.DataModel; //导入依赖的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.model.DataModel; //导入依赖的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: testBuildKNNRecommender
import org.apache.mahout.cf.taste.model.DataModel; //导入依赖的package包/类
@Test
public void testBuildKNNRecommender() {
GenericRecommenderBuilder rb = new GenericRecommenderBuilder();
FastByIDMap<PreferenceArray> userData = new FastByIDMap<PreferenceArray>();
userData.put(1, new GenericUserPreferenceArray(Arrays.asList(new GenericPreference(1, 1, 1),
new GenericPreference(1, 2, 1), new GenericPreference(1, 3, 1))));
userData.put(2, new GenericUserPreferenceArray(Arrays.asList(new GenericPreference(2, 1, 1),
new GenericPreference(2, 2, 1), new GenericPreference(2, 4, 1))));
DataModel dm = new GenericDataModel(userData);
Recommender rec = null;
String recommenderType = "org.apache.mahout.cf.taste.impl.recommender.GenericUserBasedRecommender";
String similarityType = "org.apache.mahout.cf.taste.impl.similarity.PearsonCorrelationSimilarity";
try {
rec = rb.buildRecommender(dm, recommenderType, similarityType);
} catch (RecommenderException e) {
e.printStackTrace();
}
assertTrue(rec instanceof GenericUserBasedRecommender);
}
示例6: main
import org.apache.mahout.cf.taste.model.DataModel; //导入依赖的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);
}
}
示例7: loadInMemory
import org.apache.mahout.cf.taste.model.DataModel; //导入依赖的package包/类
public DataModel loadInMemory() {
// In-memory DataModel - GenericDataModels
FastByIDMap<PreferenceArray> preferences = new FastByIDMap<PreferenceArray>();
PreferenceArray prefsForUser1 = new GenericUserPreferenceArray(10);
prefsForUser1.setUserID(0, 1L);
prefsForUser1.setItemID(0, 101L);
prefsForUser1.setValue(0, 3.0f);
prefsForUser1.setItemID(1, 102L);
prefsForUser1.setValue(1, 4.5F);
preferences.put(1L, prefsForUser1); // use userID as the key
//TODO: add others users
// Return preferences as new data model
DataModel dataModel = new GenericDataModel(preferences);
return dataModel;
}
开发者ID:PacktPublishing,项目名称:Machine-Learning-End-to-Endguide-for-Java-developers,代码行数:22,代码来源:BookRecommender.java
示例8: getGenericDataModel
import org.apache.mahout.cf.taste.model.DataModel; //导入依赖的package包/类
private DataModel getGenericDataModel() {
FastByIDMap<PreferenceArray> preferences =
new FastByIDMap<PreferenceArray>();
List<User> users = userService.getAllUsers();
for (int j = 0; j < users.size(); j++) {
List<Preference2d> preference2ds =
preference2dService.getPreferencesByUserId(users.get(j).getId());
PreferenceArray prefsForUser =
new GenericUserPreferenceArray(preference2ds.size());
prefsForUser.setUserID(j, users.get(j).getId());
for (int i = 0; i < preference2ds.size(); i++) {
prefsForUser.setItemID(i, preference2ds.get(i).getRestaurantId());
prefsForUser.setValue(i, preference2ds.get(i).getScore().floatValue());
}
preferences.put(j, prefsForUser);
}
return new GenericDataModel(preferences);
}
示例9: main
import org.apache.mahout.cf.taste.model.DataModel; //导入依赖的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());
}
示例10: userSimilarity
import org.apache.mahout.cf.taste.model.DataModel; //导入依赖的package包/类
/**
* 构建距离算法类 基於UserCF类
*/
public static UserSimilarity userSimilarity(SIMILARITY type, DataModel m) throws TasteException {
switch (type) {
case PEARSON:
return new PearsonCorrelationSimilarity(m);
case COSINE:
return new UncenteredCosineSimilarity(m);
case TANIMOTO:
return new TanimotoCoefficientSimilarity(m);
case LOGLIKELIHOOD:
return new LogLikelihoodSimilarity(m);
case SPEARMAN:
return new SpearmanCorrelationSimilarity(m);
case CITYBLOCK:
return new CityBlockSimilarity(m);
case EUCLIDEAN:
default:
return new EuclideanDistanceSimilarity(m);
}
}
示例11: itemSimilarity
import org.apache.mahout.cf.taste.model.DataModel; //导入依赖的package包/类
/**
* 构建距离算法类 基于ItemCF类
*/
public static ItemSimilarity itemSimilarity(SIMILARITY type, DataModel m) throws TasteException {
switch (type) {
case PEARSON:
return new PearsonCorrelationSimilarity(m);
case COSINE:
return new UncenteredCosineSimilarity(m);
case TANIMOTO:
return new TanimotoCoefficientSimilarity(m);
case LOGLIKELIHOOD:
return new LogLikelihoodSimilarity(m);
case CITYBLOCK:
return new CityBlockSimilarity(m);
case EUCLIDEAN:
default:
return new EuclideanDistanceSimilarity(m);
}
}
示例12: main
import org.apache.mahout.cf.taste.model.DataModel; //导入依赖的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);
}
}
示例13: generate
import org.apache.mahout.cf.taste.model.DataModel; //导入依赖的package包/类
private void generate() throws IOException, TasteException {
final DataModel dataModel = new FileDataModel(FileUtils.getFile(this.getFile()));
EvalItemVO evalItemVO = new EvalItemVO(2);
evalItemVO.evalMap.put(EvalName.MAE, new EvaluationVO(0.7, 1.0));
evalItemVO.evalMap.put(EvalName.RMS, new EvaluationVO(0.7, 1.0));
AbstractAnalyzer eval = new Evaluator(dataModel, evalItemVO);
eval.analyze();
UserItemVO userItemVO = new UserItemVO();
userItemVO.userMap.put(UserName.PEARSON, new UserAffinityVO(2, 6, 1));
userItemVO.userMap.put(UserName.EUCLIDEAN, new UserAffinityVO(2, 6, 1));
userItemVO.userMap.put(UserName.COSINE, new UserAffinityVO(2, 6, 1));
AbstractAnalyzer user = new User(dataModel, userItemVO);
user.analyze();
ItemVO itemVO = new ItemVO();
itemVO.itemMap.put(ItemName.TANIMOTO, new ItemAffinityVO(1, 5));
itemVO.itemMap.put(ItemName.CITY_BLOCK, new ItemAffinityVO(1, 5));
itemVO.itemMap.put(ItemName.LOG_LIKE, new ItemAffinityVO(1, 5));
itemVO.itemMap.put(ItemName.EUCLIDEAN, new ItemAffinityVO(1, 5));
itemVO.itemMap.put(ItemName.COSINE, new ItemAffinityVO(1, 5));
AbstractAnalyzer item = new Item(dataModel, itemVO);
item.analyze();
}
示例14: displayRecommendation
import org.apache.mahout.cf.taste.model.DataModel; //导入依赖的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());
}
}
}
示例15: testBuildDefaultRecommender
import org.apache.mahout.cf.taste.model.DataModel; //导入依赖的package包/类
@Test
public void testBuildDefaultRecommender() {
RecommenderBuilder rb = new GenericRecommenderBuilder();
FastByIDMap<PreferenceArray> userData = new FastByIDMap<PreferenceArray>();
userData.put(1, new GenericUserPreferenceArray(Arrays.asList(new GenericPreference(1, 1, 1),
new GenericPreference(1, 2, 1), new GenericPreference(1, 3, 1))));
userData.put(2, new GenericUserPreferenceArray(Arrays.asList(new GenericPreference(2, 1, 1),
new GenericPreference(2, 2, 1), new GenericPreference(2, 4, 1))));
DataModel dm = new GenericDataModel(userData);
Recommender rec = null;
try {
rec = rb.buildRecommender(dm);
} catch (TasteException e) {
e.printStackTrace();
}
assertTrue(rec instanceof RandomRecommender);
}