本文整理汇总了Java中org.apache.mahout.cf.taste.impl.neighborhood.ThresholdUserNeighborhood类的典型用法代码示例。如果您正苦于以下问题:Java ThresholdUserNeighborhood类的具体用法?Java ThresholdUserNeighborhood怎么用?Java ThresholdUserNeighborhood使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
ThresholdUserNeighborhood类属于org.apache.mahout.cf.taste.impl.neighborhood包,在下文中一共展示了ThresholdUserNeighborhood类的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: buildRecommend
import org.apache.mahout.cf.taste.impl.neighborhood.ThresholdUserNeighborhood; //导入依赖的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: main
import org.apache.mahout.cf.taste.impl.neighborhood.ThresholdUserNeighborhood; //导入依赖的package包/类
public static void main( String[] args ) throws IOException, TasteException
{
//user based recommender model
DataModel model = new FileDataModel(new File("data/dataset.csv"));
UserSimilarity similarity = new PearsonCorrelationSimilarity(model);
UserNeighborhood neighborhood = new ThresholdUserNeighborhood(0.1, similarity, model);
UserBasedRecommender recommender = new GenericUserBasedRecommender(model, neighborhood, similarity);
List<RecommendedItem> recommendations = recommender.recommend(2, 3);
for (RecommendedItem recommendation : recommendations) {
System.out.println(recommendation);
}
}
示例3: getStudentNeighborhood
import org.apache.mahout.cf.taste.impl.neighborhood.ThresholdUserNeighborhood; //导入依赖的package包/类
@Override
@Cacheable(STUDENT_NEIGHBORHOOD_CACHE_NAME)
public UserNeighborhood getStudentNeighborhood() {
DataModel model = buildDataModel();
UserSimilarity similarity = buildSimilarityIndex(model);
return new ThresholdUserNeighborhood(0.3, similarity, model);
}
示例4: mahoutSlopeoneGeneratorTest_testBoolRecommender
import org.apache.mahout.cf.taste.impl.neighborhood.ThresholdUserNeighborhood; //导入依赖的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());
}
示例5: buildRecommender
import org.apache.mahout.cf.taste.impl.neighborhood.ThresholdUserNeighborhood; //导入依赖的package包/类
@Override
public UserBasedRecommender buildRecommender(DataModel dataModel) throws TasteException {
UserNeighborhood neighborhood =
new ThresholdUserNeighborhood(
0.1, new PearsonCorrelationSimilarity(dataModel), dataModel);
return new GenericBooleanPrefUserBasedRecommender(
dataModel,
neighborhood,
similarity);
}
示例6: userNeighborhood
import org.apache.mahout.cf.taste.impl.neighborhood.ThresholdUserNeighborhood; //导入依赖的package包/类
public static UserNeighborhood userNeighborhood(NEIGHBORHOOD type, UserSimilarity s, DataModel m, double num) throws TasteException {
switch (type) {
case NEAREST:
/**
* 根据数量构建最近的距离
*/
return new NearestNUserNeighborhood((int) num, s, m);
case THRESHOLD:
default:
/**
* 根据百分比去构建
*/
return new ThresholdUserNeighborhood(num, s, m);
}
}
示例7: getNeighborhood
import org.apache.mahout.cf.taste.impl.neighborhood.ThresholdUserNeighborhood; //导入依赖的package包/类
/**
* Get the user neighborhood instance, with the correct neighborhood size and algorithm
* as specified during the construction of this RecommendEntityServlet.
* @param similarity The similarity to form the neighborhood
* @param model The database model to get the neighborhood of
* @return The UserNeighborhood instance
* @throws Exception If an exception is thrown by Mahout it is forwarded upwards.
*/
private UserNeighborhood getNeighborhood(UserSimilarity similarity, DataModel model) throws TasteException {
if(this.neighborhoodAlg.equalsIgnoreCase(N_THRESHOLD)){
return new ThresholdUserNeighborhood(this.neighborhoodSize, similarity, model);
} else if(this.neighborhoodAlg.equalsIgnoreCase(N_NUSER) || this.neighborhoodAlg == null){ // == null is the default case
return new NearestNUserNeighborhood((int)this.neighborhoodSize, similarity, model);
} else {
throw new TasteException("Unknown neighborhood algorithm type: " + this.neighborhoodAlg);
}
}
示例8: getRecommenderItem
import org.apache.mahout.cf.taste.impl.neighborhood.ThresholdUserNeighborhood; //导入依赖的package包/类
public static String getRecommenderItem(int id) throws IOException, TasteException {
//DB연동
MysqlDataSource datasource = new MysqlDataSource();
datasource.setServerName("localhost");
datasource.setUser("root");
datasource.setPassword("465651");
datasource.setDatabaseName("tourOfAll2");
DataModel model = new ReloadFromJDBCDataModel(new MySQLJDBCDataModel(datasource, "evaluations", "user_id", "item_id", "score", null));
// DataModel model = new FileDataModel(
// new
// File("C:/Users/Administrator/git/RestfulMahoutRecommender/RestfulRecommenderApi/src/main/resources/ddd.csv"));
//유사도 측정을 캐쉬로 저장
UserSimilarity similarity = new CachingUserSimilarity(new EuclideanDistanceSimilarity(model),model);
// new SpearmanCorrelationSimilarity(model);
//유저 이웃 계산 결과를 캐쉬로 저장
UserNeighborhood neighborhood = new CachingUserNeighborhood(new ThresholdUserNeighborhood(0.75, similarity, model),model);
// new NearestNUserNeighborhood(5,similarity,model);
Recommender recommender = new GenericUserBasedRecommender(model, neighborhood, similarity);
LoadEvaluator.runLoad(recommender);
String json = "{" + "\"Items\"" + ":" + "[";
IDRescorer testRescorer = new GenreRescorer(id);
List<RecommendedItem> recommendations = recommender.recommend(id, 20, testRescorer);
// List<RecommendedItem> recommendations = recommender.recommend(id, 10);
// String Parsing 아이디값만 찾음
Iterator<RecommendedItem> itr = recommendations.iterator();
while (itr.hasNext()) {
RecommendedItem item = itr.next();
String str = item.toString();
String ItemId = str.substring(str.indexOf(":") + 1, str.indexOf(","));
String value = str.substring(str.indexOf("value:") + 6, str.indexOf("value:") + 9);
getPlaceURL url = new getPlaceURL(Integer.parseInt(ItemId));
GetPlaceTitle title = new GetPlaceTitle(Integer.parseInt(ItemId));
if (itr.hasNext())
json = json + "{" + "\"ID\"" + ":" + "\"" + ItemId + "\""
+ ", " + "\"Value\"" + ":" + "\"" + value + "\""
+ ", " + "\"URL\"" + ":" + "\"" + url.getURL() + "\""
+ ", " + "\"Title\"" + ":" + "\"" + title.getTitle() + "\""
+ "}" + ", ";
else
json = json + "{" + "\"ID\"" + ":" + "\"" + ItemId + "\""
+ ", " + "\"Value\"" + ":" + "\"" + value + "\""
+ ", " + "\"URL\"" + ":" + "\"" + url.getURL() + "\""
+ ", " + "\"Title\"" + ":" + "\"" + title.getTitle() + "\""
+ "}";
}
json = json + "]" + "}";
return (json);
}
示例9: buildRecommender
import org.apache.mahout.cf.taste.impl.neighborhood.ThresholdUserNeighborhood; //导入依赖的package包/类
public Recommender buildRecommender(DataModel dataModel) throws TasteException {
UserSimilarity similarity = new PearsonCorrelationSimilarity(dataModel);
UserNeighborhood neighborhood = new ThresholdUserNeighborhood(0.1, similarity, dataModel);
return new GenericUserBasedRecommender(dataModel, neighborhood, similarity);
}
示例10: getThreshold
import org.apache.mahout.cf.taste.impl.neighborhood.ThresholdUserNeighborhood; //导入依赖的package包/类
public static UserNeighborhood getThreshold(DataModel dataModel, UserSimilarity userSimilarity,
double threshold) throws TasteException {
System.out.println("Threshold");
return new ThresholdUserNeighborhood(threshold, userSimilarity, dataModel);
}