本文整理汇总了Java中org.apache.mahout.cf.taste.recommender.Recommender类的典型用法代码示例。如果您正苦于以下问题:Java Recommender类的具体用法?Java Recommender怎么用?Java Recommender使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Recommender类属于org.apache.mahout.cf.taste.recommender包,在下文中一共展示了Recommender类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: main
import org.apache.mahout.cf.taste.recommender.Recommender; //导入依赖的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");
}
示例2: recommend
import org.apache.mahout.cf.taste.recommender.Recommender; //导入依赖的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());
}
}
}
示例3: testBuildKNNRecommender
import org.apache.mahout.cf.taste.recommender.Recommender; //导入依赖的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);
}
示例4: main
import org.apache.mahout.cf.taste.recommender.Recommender; //导入依赖的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());
}
示例5: run
import org.apache.mahout.cf.taste.recommender.Recommender; //导入依赖的package包/类
@Override
public void run(RecommenderConfiguration configuration,
Environment environment) {
PGPoolingDataSource pgPoolingDataSource = configuration.getDataSourceFactory().build(environment);
ReloadFromJDBCDataModel dataModel = null;
try {
dataModel = configuration.getDataModelFactory().build(pgPoolingDataSource);
} catch (TasteException e) {
System.err.println(e);
System.exit(-1);
}
Recommender userBasedRecommender = configuration.getRecommenderFactory().buildUserBasedRecommender(dataModel);
ItemBasedRecommender itemBasedRecommender = configuration.getRecommenderFactory().buildItemBasedRecommender(dataModel);
final RecommendationResource userRecommendationResource = new RecommendationResource(userBasedRecommender, itemBasedRecommender);
final DataModelResource dataModelResource = new DataModelResource(dataModel);
environment.jersey().register(userRecommendationResource);
environment.jersey().register(dataModelResource);
}
示例6: main
import org.apache.mahout.cf.taste.recommender.Recommender; //导入依赖的package包/类
public static void main(String[] args) {
try {
MysqlDataSource dataSource = new MysqlDataSource();
dataSource.setServerName("localhost");
dataSource.setUser("root");
dataSource.setPassword("root");
dataSource.setDatabaseName("rec");
JDBCDataModel dm = new MySQLJDBCDataModel(dataSource,"ratings","userid","itemid","rating","");
UserSimilarity similarity = new PearsonCorrelationSimilarity(dm);
UserNeighborhood neighbor = new NearestNUserNeighborhood(2,similarity, dm);
Recommender recommender = new GenericUserBasedRecommender(dm, neighbor, similarity);
List<RecommendedItem> list = recommender.recommend(1, 3);// recommend
// one item
// to user
// 1
for (RecommendedItem ri : list) {
System.out.println(ri);
}
} catch (Exception e) {
e.printStackTrace();
}
}
示例7: testBuildDefaultRecommender
import org.apache.mahout.cf.taste.recommender.Recommender; //导入依赖的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);
}
示例8: buildRecommender
import org.apache.mahout.cf.taste.recommender.Recommender; //导入依赖的package包/类
public Recommender buildRecommender(DataModel arg0) {
try {
return BookRecommender.itemBased();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
开发者ID:PacktPublishing,项目名称:Machine-Learning-End-to-Endguide-for-Java-developers,代码行数:10,代码来源:BookRecommender.java
示例9: main
import org.apache.mahout.cf.taste.recommender.Recommender; //导入依赖的package包/类
public static void main(String[] args) throws IOException, TasteException {
RandomUtils.useTestSeed();
final DataModel model = new FileDataModel(new File("data/ua.base"));
RecommenderEvaluator evaluator = new AverageAbsoluteDifferenceRecommenderEvaluator();
RecommenderBuilder builder = 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);
}
};
double score = evaluator.evaluate(builder, null, model, 0.7, 1.0);
System.out.println(score);
}
示例10: mahoutSlopeoneGeneratorTest_testBoolRecommender
import org.apache.mahout.cf.taste.recommender.Recommender; //导入依赖的package包/类
@Test
public void mahoutSlopeoneGeneratorTest_testBoolRecommender() throws TasteException {
EasyrecDataModel easyrecDataModel = new EasyrecDataModel(TENANT_ID, BUY_ACTION_TYPE_ID, false, mahoutDataModelMappingDAO);
/*TanimotoCoefficientSimilarity is intended for "binary" data sets where a user either expresses a generic "yes" preference for an item or has no preference.*/
UserSimilarity userSimilarity = new TanimotoCoefficientSimilarity(easyrecDataModel);
/*ThresholdUserNeighborhood is preferred in situations where we go in for a similarity measure between neighbors and not any number*/
UserNeighborhood neighborhood = new ThresholdUserNeighborhood(0.1d, userSimilarity, easyrecDataModel);
/*GenericBooleanPrefUserBasedRecommender is appropriate for use when no notion of preference value exists in the data. */
Recommender recommender = new GenericBooleanPrefUserBasedRecommender(easyrecDataModel, neighborhood, userSimilarity);
Assert.assertEquals(30, recommender.recommend(3, 1).get(0).getItemID());
Assert.assertEquals(1,(int) recommender.recommend(3, 1).get(0).getValue());
}
示例11: mahoutSlopeoneGeneratorTest_testRecommender
import org.apache.mahout.cf.taste.recommender.Recommender; //导入依赖的package包/类
@Test
public void mahoutSlopeoneGeneratorTest_testRecommender() throws TasteException {
EasyrecDataModel easyrecDataModel = new EasyrecDataModel(TENANT_ID, RATE_ACTION_TYPE_ID, true, mahoutDataModelMappingDAO);
Recommender recommender = new SlopeOneRecommender(easyrecDataModel);
Assert.assertEquals(3, recommender.recommend(3, 1).get(0).getItemID());
Assert.assertEquals(10, (int) recommender.recommend(3, 1).get(0).getValue());
}
示例12: buildRecommender
import org.apache.mahout.cf.taste.recommender.Recommender; //导入依赖的package包/类
@Override
public Recommender buildRecommender(DataModel model)
throws TasteException {
UserSimilarity similarity =
new PearsonCorrelationSimilarity(model);
UserNeighborhood neighborhood =
new NearestNUserNeighborhood(
100,
similarity, model);
return new GenericUserBasedRecommender(
model, neighborhood, similarity);
}
示例13: recommend
import org.apache.mahout.cf.taste.recommender.Recommender; //导入依赖的package包/类
/**
* レコメンデーションを生成して出力
* @param datamodel
* @param similarity
* @param userId
* @param howMany
* @throws TasteException
*/
private void recommend(DataModel datamodel, UserSimilarity similarity, UserAffinityVO dto) throws TasteException {
super.i("◆ " + similarity.getClass());
similarity.setPreferenceInferrer(new AveragingPreferenceInferrer(datamodel));
UserNeighborhood neighbor = new NearestNUserNeighborhood(dto.size, similarity, datamodel);
Recommender recommender = new GenericUserBasedRecommender(datamodel, neighbor, similarity);
List<RecommendedItem> items = recommender.recommend(dto.userId, dto.howMany);
for (RecommendedItem item : items) {
super.i("◆ " + item);
}
}
示例14: analyze
import org.apache.mahout.cf.taste.recommender.Recommender; //导入依赖的package包/类
/**
* <b>[推薦の評価方法]</b><br>
* cf.)70%のデータを使用してレコメンダーを生成→30%のデータを使用して評価
* @param dataModel
* @throws TasteException
*/
@Override
public void analyze() throws TasteException {
super.i("◆ EVAL ======= Start ======");
super.i("◆ -------------------------");
final int size = super.dto.size;
final Map<EvalName, EvaluationVO> evalMap = super.dto.evalMap;
RandomUtils.useTestSeed();
/* 実際の評価値と推定値の誤差 */
// 絶対値を求める=意味平均誤差(mean average error)
RecommenderEvaluator maeEval = new AverageAbsoluteDifferenceRecommenderEvaluator();
// 自乗を足し合わせ平方根を取ったRMS(root mean squared)
RecommenderEvaluator rmsEval = new RMSRecommenderEvaluator();
RecommenderBuilder builder = new RecommenderBuilder() {
@Override
public Recommender buildRecommender(DataModel dataModel) throws TasteException {
UserSimilarity similarity = new PearsonCorrelationSimilarity(dataModel);
UserNeighborhood neighborhood = new NearestNUserNeighborhood(size, similarity, dataModel);
return new GenericUserBasedRecommender(dataModel, neighborhood, similarity);
}
};
// evaluate(RecommenderBuilder, DataModelBuilder, DataModel, 学習用データの割合, 検証用データの割合)
EvaluationVO mae = evalMap.get(EvalName.MAE);
EvaluationVO rms = evalMap.get(EvalName.RMS);
double maeScore = maeEval.evaluate(builder, null, super.dataModel, mae.trainingPercentage, mae.evaluationPercentage);
double rmsScore = rmsEval.evaluate(builder, null, super.dataModel, rms.trainingPercentage, rms.evaluationPercentage);
super.i("◆ " + EvalName.MAE.name + " : " + maeScore);
super.i("◆ " + EvalName.RMS.name + " : " + rmsScore);
super.i("◆ -------------------------");
super.iln("◆ EVAL ======= END ======");
}
示例15: recommend
import org.apache.mahout.cf.taste.recommender.Recommender; //导入依赖的package包/类
public List<RecommendedItem> recommend(int userid,int size) throws TasteException, IOException{
List<RecommendedItem> list = null;
// MovieDataModel model = new MovieDataModel();
String file=ServletActionContext.getServletContext().getRealPath("/u1.base");
DataModel model = new FileDataModel(new File(file));
Recommender recommender = new CachingRecommender(new SlopeOneRecommender(model));
list = recommender.recommend(userid, size);
return list;
}