首页 > 代码库 > mahout推荐1

mahout推荐1

1、准备数据:

intro.csv:

1,101,5.0
1,102,3.0
1,103,2.5

2,101,2.0
2,102,2.5
2,103,5.0
2,104,2.0

3,101,2.5
3,104,4.0
3,105,4.5
3,107,5.0

4,101,5.0
4,103,3.0
4,104,4.5
4,106,4.0

5,101,4.0
5,102,3.0
5,103,2.0
5,104,4.0
5,105,3.5
5,106,4.0

 

2、编程实现:

  目的:为用户1推荐一件商品看看:

package mahout;import java.io.File;import java.util.List;import org.apache.mahout.cf.taste.impl.model.file.FileDataModel;import org.apache.mahout.cf.taste.impl.neighborhood.NearestNUserNeighborhood;import org.apache.mahout.cf.taste.impl.recommender.GenericUserBasedRecommender;import org.apache.mahout.cf.taste.model.DataModel;import org.apache.mahout.cf.taste.neighborhood.UserNeighborhood;import org.apache.mahout.cf.taste.recommender.RecommendedItem;import org.apache.mahout.cf.taste.recommender.Recommender;import org.apache.mahout.cf.taste.similarity.UserSimilarity;import org.apache.mahout.cf.taste.impl.similarity.PearsonCorrelationSimilarity;/** * 基于用户的推荐程序 * @author Administrator * */public class RecommenderIntro {	public static void main(String[] args) throws Exception {		//装载数据文件,实现存储,并为计算提供所需的所有偏好,用户和物品数据		DataModel model = new FileDataModel(new File("data/intro.csv"));		//用户相似度,给出两个用户的相似度,有多种度量方式		UserSimilarity similarity = new PearsonCorrelationSimilarity(model);		//用户邻居,与给定用户最相似的一组用户		UserNeighborhood neighborhood = new NearestNUserNeighborhood(2,				similarity, model);		//推荐引擎,合并这些组件,实现推荐		Recommender recommender = new GenericUserBasedRecommender(model,				neighborhood, similarity);		//为用户1推荐一件物品1,1		List<RecommendedItem> recommendedItems = recommender.recommend(1, 1);		//输出		for (RecommendedItem item : recommendedItems) {			System.out.println(item);		}	}}

 输出结果:

14/08/04 08:46:31 INFO file.FileDataModel: Creating FileDataModel for file data\intro.csv14/08/04 08:46:31 INFO file.FileDataModel: Reading file info...14/08/04 08:46:31 INFO file.FileDataModel: Read lines: 2114/08/04 08:46:31 INFO model.GenericDataModel: Processed 5 usersRecommendedItem[item:104, value:4.257081]

 当然也可以推荐多件商品,那就是将recommender.recommend(1,N)即可。

推荐效果不错。