首先讲解什么是基于用户的推荐系统:
基于用户的推荐系统顾名思义就是以用户为基础的推荐系统,以用户之间已知的兴趣爱好的相似程度,来判断用户之间未表明但是可能出现的兴趣爱好。例如:小明和小李都喜欢跑步、打球、游泳、买衣服。由此我们可以推测他们两个有共同的兴趣爱好。现在已知小明喜欢打台球,我们就可以推断小李可能也喜欢打台球。这就是基于用户推荐的一个简单的例子。
下面代码调用mahout算法里边的基于用户的推荐算法在单机上来实现:
数据格式如下:第一列代表用户ID,第二列代表商品ID,大三列代表评分值。
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
package recommenderIntro;
import java.io.File;
import java.util.List;
import org.apache.mahout.cf.taste.impl.model.GenericBooleanPrefDataModel;
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.impl.similarity.PearsonCorrelationSimilarity;
import org.apache.mahout.cf.taste.impl.similarity.TanimotoCoefficientSimilarity;
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;
public class RecommenderIntro {
public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub
DataModel model = new FileDataModel(new File("/home/mylover/data/user")); //加载数据文件
//UserSimilarity similarity = new TanimotoCoefficientSimilarity(model);
UserSimilarity similarity = new PearsonCorrelationSimilarity(model);//构建相似度计算方式
UserNeighborhood neighborhood =
new NearestNUserNeighborhood(2, similarity, model);
Recommender recommender = new GenericUserBasedRecommender(
model, neighborhood, similarity); //创建推荐引擎
List<RecommendedItem> recommendations =
recommender.recommend(1, 1); //给用户1推荐一个item
for (RecommendedItem recommendation : recommendations) {
System.out.println(recommendation);
System.out.println(recommendation.getItemID());
System.out.println(recommendation.getValue());
}
}
}
Datamodel:提供存储和访问用户、项和偏好数据,用于计算
Usersimilarity:计算用户之间的相似度,可以从多种度量进行计算。
UserNeighborhood:计算用户的邻居
Recommender :将上述组件组织在一起,为用户提供item推荐
输出结果:RecommendedItem[item:104, value:4.257081]
104
4.257081
实现以上程序需要导入一下包:
commons-logging-1.1.1.jar
commons-math-2.2.jar
slf4j-api-1.6.1.jar
slf4j-jcl-1.6.1.jar
uncommons-maths-1.2.2.jar
mahout-core-0.6.jar
mahout-mat-0.6.jar
guava-r09.jar
mahout-core-0.9-job.jar