Mahout 快速入门(一)

本实例是基于用户推荐的事例教程,来源于apache mahout http://mahout.apache.org/users/recommender/userbased-5-minutes.html

1. 准备数据。利用mahout 提供的简单的数据,生成数据文件dataset.csv。每一列分别表明 用户ID ItemID value(表明一种关系,用户对该商品的评级)
1,10,1.0
1,11,2.0
1,12,5.0
1,13,5.0
1,14,5.0
1,15,4.0
1,16,5.0
1,17,1.0
1,18,5.0
2,10,1.0
2,11,2.0
2,15,5.0
2,16,4.5
2,17,1.0
2,18,5.0
3,11,2.5
3,12,4.5
3,13,4.0
3,14,3.0
3,15,3.5
3,16,4.5
3,17,4.0
3,18,5.0
4,10,5.0
4,11,5.0
4,12,5.0
4,13,0.0
4,14,2.0
4,15,3.0
4,16,1.0
4,17,4.0
4,18,1.0

2. 构造数据模型。创建一个类,命名为SampleRecommender。
 
DataModel model = new FileDataModel(new File("/path/to/dataset.csv"));


3. 计算用户的相关度。
UserSimilarity similarity = new PearsonCorrelationSimilarity(model);


4. 定义相关度为多少的用户是我们期望的。
UserNeighborhood neighborhood = new ThresholdUserNeighborhood(0.1, similarity, model);


5. 创建用户推荐
UserBasedRecommender recommender = new GenericUserBasedRecommender(model, neighborhood, similarity);


6. 输出推荐结果
List recommendations = recommender.recommend(2, 3);
for (RecommendedItem recommendation : recommendations) {
  System.out.println(recommendation);
}


7. 完整的代码如下
import java.io.File;
import java.io.IOException;
import java.util.List;

import org.apache.mahout.cf.taste.impl.model.file.FileDataModel;
import org.apache.mahout.cf.taste.impl.neighborhood.ThresholdUserNeighborhood;
import org.apache.mahout.cf.taste.impl.recommender.GenericUserBasedRecommender;
import org.apache.mahout.cf.taste.impl.similarity.PearsonCorrelationSimilarity;
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.UserBasedRecommender;
import org.apache.mahout.cf.taste.similarity.UserSimilarity;

/** 
 * ClassName:SampleRecommender <br/> 
 * Function: TODO ADD FUNCTION. <br/> 
 * Reason:   TODO ADD REASON. <br/> 
 * Date:     2014年3月29日 上午9:56:31 <br/> 
 * @author   zhangzhaoyu 
 * @version   
 * @since    JDK 1.7
 * @see       
 */
public class SampleRecommender {
	
	public static void main(String[] args) throws IOException, Exception {
		
		DataModel model = new FileDataModel(new File("src/main/java/edu/bjfu/imovie/mahout/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 reItem : recommendations) {
			System.out.println(reItem);
		} 
		
	}

}

8. 输出结果
RecommendedItem[item:12, value:4.8328104]
RecommendedItem[item:13, value:4.6656213]
RecommendedItem[item:14, value:4.331242]

9. Maven 导入依赖包

<!-- mahout -->
<dependency>
<groupId>org.apache.mahout</groupId>
<artifactId>mahout-core</artifactId>
<version>${mahout.version}</version>
</dependency>

<dependency>
<groupId>org.apache.mahout</groupId>
<artifactId>mahout-integration</artifactId>
<version>${mahout.version}</version>
</dependency>
<dependency>
<groupId>org.apache.mahout</groupId>
<artifactId>mahout-math</artifactId>
<version>${mahout.version}</version>
</dependency>

你可能感兴趣的:(Mahout)