当前位置: 首页>>代码示例>>Java>>正文


Java PearsonCorrelationSimilarity类代码示例

本文整理汇总了Java中org.apache.mahout.cf.taste.impl.similarity.PearsonCorrelationSimilarity的典型用法代码示例。如果您正苦于以下问题:Java PearsonCorrelationSimilarity类的具体用法?Java PearsonCorrelationSimilarity怎么用?Java PearsonCorrelationSimilarity使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


PearsonCorrelationSimilarity类属于org.apache.mahout.cf.taste.impl.similarity包,在下文中一共展示了PearsonCorrelationSimilarity类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: IRState

import org.apache.mahout.cf.taste.impl.similarity.PearsonCorrelationSimilarity; //导入依赖的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();
    }
}
 
开发者ID:babymm,项目名称:mmsns,代码行数:26,代码来源:MahoutRecommender.java

示例2: main

import org.apache.mahout.cf.taste.impl.similarity.PearsonCorrelationSimilarity; //导入依赖的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");
}
 
开发者ID:tensorchen,项目名称:rrs,代码行数:22,代码来源:RecommenderIntro.java

示例3: recommend

import org.apache.mahout.cf.taste.impl.similarity.PearsonCorrelationSimilarity; //导入依赖的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());
    }
  }
}
 
开发者ID:Hanmourang,项目名称:hiped2,代码行数:25,代码来源:MovieUserRecommender.java

示例4: main

import org.apache.mahout.cf.taste.impl.similarity.PearsonCorrelationSimilarity; //导入依赖的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());
}
 
开发者ID:tensorchen,项目名称:rrs,代码行数:25,代码来源:RecommenderIRStatsEvaluatorTest.java

示例5: main

import org.apache.mahout.cf.taste.impl.similarity.PearsonCorrelationSimilarity; //导入依赖的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();
    }

}
 
开发者ID:laozhaokun,项目名称:movie_recommender,代码行数:24,代码来源:RecommenderWithMahout.java

示例6: main

import org.apache.mahout.cf.taste.impl.similarity.PearsonCorrelationSimilarity; //导入依赖的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);
	}
}
 
开发者ID:PacktPublishing,项目名称:Building-Recommendation-Engines,代码行数:13,代码来源:UserbasedRecommender.java

示例7: itemBased

import org.apache.mahout.cf.taste.impl.similarity.PearsonCorrelationSimilarity; //导入依赖的package包/类
public static ItemBasedRecommender itemBased() throws Exception {

		// Load the data
		StringItemIdFileDataModel dataModel = loadFromFile("data/BX-Book-Ratings.csv", ";");
		// Collection<GenericItemSimilarity.ItemItemSimilarity> correlations =
		// null;
		// ItemItemSimilarity iis = new ItemItemSimilarity(0, 0, 0);
		// ItemSimilarity itemSimilarity = new
		// GenericItemSimilarity(correlations);
		ItemSimilarity itemSimilarity = new PearsonCorrelationSimilarity(dataModel);

		ItemBasedRecommender recommender = new GenericItemBasedRecommender(
				dataModel, itemSimilarity);

		IDRescorer rescorer = new MyRescorer();

		// List recommendations = recommender.recommend(2, 3, rescorer);
		String itemISBN = "042513976X";
		long itemID = dataModel.readItemIDFromString(itemISBN);
		int noItems = 10;

		System.out.println("Recommendations for item: " + books.get(itemISBN));

		System.out.println("\nMost similar items:");
		List<RecommendedItem> recommendations = recommender.mostSimilarItems(
				itemID, noItems);
		for (RecommendedItem item : recommendations) {
			itemISBN = dataModel.getItemIDAsString(item.getItemID());
			System.out.println("Item: " + books.get(itemISBN) + " | Item id: "
					+ itemISBN + " | Value: " + item.getValue());
		}
		
		return recommender;
	}
 
开发者ID:PacktPublishing,项目名称:Machine-Learning-End-to-Endguide-for-Java-developers,代码行数:35,代码来源:BookRecommender.java

示例8: buildSimilarityIndex

import org.apache.mahout.cf.taste.impl.similarity.PearsonCorrelationSimilarity; //导入依赖的package包/类
private UserSimilarity buildSimilarityIndex(DataModel model) {
    log.info("Building student subject similarity index");

    try {
        return new PearsonCorrelationSimilarity(model);
    } catch (TasteException e) {
        throw new RuntimeException(e);
    }
}
 
开发者ID:university-information-system,项目名称:uis,代码行数:10,代码来源:StudentNeighborhoodStoreImpl.java

示例9: main

import org.apache.mahout.cf.taste.impl.similarity.PearsonCorrelationSimilarity; //导入依赖的package包/类
public static void main(String[] args) throws Exception {
    String base = "C:\\Users\\smallnest\\Desktop\\test\\";
    File file = new File(base + "user_movies.csv");
    DoubanFileDataModel model = new DoubanFileDataModel(file);

    //http://www.cnphp6.com/archives/84955
    //曼哈顿相似度
    //UserSimilarity similarity = new org.apache.mahout.cf.taste.impl.similarity.CityBlockSimilarity(model);
    //欧几里德相似度
    //UserSimilarity similarity = new org.apache.mahout.cf.taste.impl.similarity.EuclideanDistanceSimilarity(model);
    //对数似然相似度
    //UserSimilarity similarity = new org.apache.mahout.cf.taste.impl.similarity.LogLikelihoodSimilarity(model);
    //斯皮尔曼相似度
    //UserSimilarity similarity = new org.apache.mahout.cf.taste.impl.similarity.SpearmanCorrelationSimilarity(model);
    //Tanimoto 相似度
    //UserSimilarity similarity = new org.apache.mahout.cf.taste.impl.similarity.TanimotoCoefficientSimilarity(model)
    //Cosine相似度
    //UserSimilarity similarity = new org.apache.mahout.cf.taste.impl.similarity.UncenteredCosineSimilarity();

    //皮尔逊相似度
    ItemSimilarity similarity = new PearsonCorrelationSimilarity(model);
    ItemBasedRecommender recommender = new GenericItemBasedRecommender(model, similarity);

    BatchItemSimilarities batch = new MultithreadedBatchItemSimilarities(recommender, 5);
    int numSimilarities = batch.computeItemSimilarities(Runtime.getRuntime().availableProcessors(), 1, new FileSimilarItemsWriter(new File(base + "item_result.csv")));

    System.out.println("Computed " + numSimilarities + " similarities for " + model.getNumItems() + " items " + "and saved them to file " + base + "item_result.csv");
}
 
开发者ID:smallnest,项目名称:mahout-douban-recommender,代码行数:29,代码来源:DoubanItemBasedRecommender.java

示例10: main

import org.apache.mahout.cf.taste.impl.similarity.PearsonCorrelationSimilarity; //导入依赖的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);
}
 
开发者ID:tensorchen,项目名称:rrs,代码行数:22,代码来源:RecommenderEvaluatorTest.java

示例11: getRecommendation

import org.apache.mahout.cf.taste.impl.similarity.PearsonCorrelationSimilarity; //导入依赖的package包/类
@Override
public RecResponseBean getRecommendation(long _userId, ResultsType _resultsType)
        throws DatastoreException, DocumentNotFoundException, TasteException {

    //RecommenderIRStatsEvaluator evaluator = new GenericRecommenderIRStatsEvaluator();
    RecommendationBuilderWrapper builder =
            new RecommendationBuilderWrapper(
                    new PearsonCorrelationSimilarity(dataModel));

    List<RecommendedItem> recommendations =
            builder.buildRecommender(dataModel).recommend(
                    _userId, _resultsType.getDaoResultLimit());

    Collection<DocumentBean> results = new ArrayList<>(recommendations.size());

    for (RecommendedItem recommendedItem : recommendations) {
        try {
            results.add(dao.getDocument(recommendedItem.getItemID()));
        }
        catch (DocumentNotFoundException e) {
            logger.warn(e);
        }
    }

    // Filter down the result (select randomly from the top results)
    results = ResultsType.getResultsForCF(results, ResultsType.RANDOM_10);
    results = ResultsType.getResultsForCF(results, _resultsType);

    RecResponseBean response = new RecResponseBean(
            results, builder.getSimilarityClass().getSimpleName());

    return response;
}
 
开发者ID:balarj,项目名称:rmend-be,代码行数:34,代码来源:CFRecommender.java

示例12: buildRecommender

import org.apache.mahout.cf.taste.impl.similarity.PearsonCorrelationSimilarity; //导入依赖的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);
}
 
开发者ID:balarj,项目名称:rmend-be,代码行数:13,代码来源:CFRecommender.java

示例13: buildRecommender

import org.apache.mahout.cf.taste.impl.similarity.PearsonCorrelationSimilarity; //导入依赖的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);
}
 
开发者ID:Hanmourang,项目名称:hiped2,代码行数:15,代码来源:MovieUserEvaluator.java

示例14: getSimularity

import org.apache.mahout.cf.taste.impl.similarity.PearsonCorrelationSimilarity; //导入依赖的package包/类
/**
 * Get the similarity instance that is used to check which users and items
 * are similar. There are several implementations for this, these are specified
 * by the this.alg value of the RecommendEntityServlet instance.
 * NOTE: Since there are two interfaces: ItemSimilarity and UserSimilarity the
 * returned type is of the interface Refreshable since both interfaces inherit
 * that type as well. Cast it to the right type.
 * @param model The database model to use for the similarity calculations
 * @return The UserSimilarity or ItemSimilarity instance.
 * @throws TasteException If an exception is thrown by Mahout it is forwarded upwards.
 */
private Refreshable getSimularity(DataModel model) throws TasteException {
    if(this.alg.equalsIgnoreCase(A_EUCLIDEAN)){
        return new EuclideanDistanceSimilarity(model);
    } else if(this.alg.equalsIgnoreCase(A_PEARSON)){
        return new PearsonCorrelationSimilarity(model);
    } else if(this.alg.equalsIgnoreCase(A_TANIMOTO)){
        return new TanimotoCoefficientSimilarity(model);
    } else if(this.alg.equalsIgnoreCase(A_LOGLIKELIHOOD) || this.alg == null){ // == null is the default case
        return new LogLikelihoodSimilarity(model);
    } else {
        throw new TasteException("Unknown algorithm type: " + this.alg);
    }
}
 
开发者ID:webdsl,项目名称:webdsl,代码行数:25,代码来源:RecommendEntityServlet.java

示例15: analyze

import org.apache.mahout.cf.taste.impl.similarity.PearsonCorrelationSimilarity; //导入依赖的package包/类
/**
 * <b>[推薦の評価方法]</b><br>
 * 70%のデータを使用してレコメンダーを生成→30%のデータを使用して評価
 * @param dataModel
 * @throws TasteException
 */
@Override
public void analyze() throws TasteException {

    // ユーザの類似性を定義
    super.i("◆ USER ======= Start ======");
    super.i("◆ -------------------------");

    try {

        final Map<UserName, UserAffinityVO> userMap = super.dto.userMap;

        // ピアソン相関
        UserSimilarity a_pearson = new PearsonCorrelationSimilarity(super.dataModel);
        UserAffinityVO pearson = userMap.get(UserName.PEARSON);
        recommend(super.dataModel, a_pearson, pearson);

        // ユークリッド距離
        UserSimilarity a_euclid = new EuclideanDistanceSimilarity(super.dataModel);
        UserAffinityVO euclidean = userMap.get(UserName.EUCLIDEAN);
        recommend(super.dataModel, a_euclid, euclidean);

        // コサイン類似度
        UserSimilarity a_cosine = new UncenteredCosineSimilarity(super.dataModel);
        UserAffinityVO cosine = userMap.get(UserName.COSINE);
        recommend(super.dataModel, a_cosine, cosine);

    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    }

    super.i("◆ -------------------------");
    super.iln("◆ USER ======= END ======");
}
 
开发者ID:pollseed,项目名称:machine-learning,代码行数:40,代码来源:User.java


注:本文中的org.apache.mahout.cf.taste.impl.similarity.PearsonCorrelationSimilarity类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。