標籤:mahout taste cf
650) this.width=650;" src="http://s5.51cto.com/wyfs02/M01/76/1C/wKioL1ZKyGuzB55-AAZC2Akf-Og878.png" title="捕獲.PNG" alt="wKioL1ZKyGuzB55-AAZC2Akf-Og878.png" />
所需jar包
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M01/76/1C/wKioL1ZKx0fwtCPcAAALlJ7stHE703.png" title="捕獲.PNG" alt="wKioL1ZKx0fwtCPcAAALlJ7stHE703.png" />
資料格式以逗號分隔
1,101,5.01,102,3.01,103,2.52,101,2.02,102,2.52,103,5.02,104,2.03,101,2.03,104,4.03,105,4.53,107,5.04,101,5.04,103,3.04,104,4.54,106,4.05,101,4.05,102,3.05,103,2.05,104,4.05,105,3.55,106,4.06,102,4.06,103,2.06,105,3.56,107,4.0
基於使用者推薦
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.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.Recommender;import org.apache.mahout.cf.taste.similarity.UserSimilarity;public class UserItemRecommend {public static void main(String[] args) throws Exception{ //建立資料模型 DataModel dm = new FileDataModel(new File("C:/test.txt")); //使用user來推薦,計算相似性 UserSimilarity us=new PearsonCorrelationSimilarity(dm); //尋找K(3)近鄰 UserNeighborhood unb=new NearestNUserNeighborhood(3, us, dm); //構造Recommendation Engine Recommender re =new GenericUserBasedRecommender(dm, unb, us);//顯示推薦結果,為1號使用者推薦兩個商品 List<RecommendedItem> list = re.recommend(1, 2); for(RecommendedItem recommendedItem :list) { System.out.println(recommendedItem); }} }
推薦結果
RecommendedItem[item:104, value:4.257081]RecommendedItem[item:106, value:4.0]
基於商品
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.recommender.GenericItemBasedRecommender;import org.apache.mahout.cf.taste.impl.similarity.PearsonCorrelationSimilarity;import org.apache.mahout.cf.taste.model.DataModel;import org.apache.mahout.cf.taste.recommender.RecommendedItem;import org.apache.mahout.cf.taste.recommender.Recommender;import org.apache.mahout.cf.taste.similarity.ItemSimilarity;public class ItemUserRecommend {public static void main(String[] args) throws Exception{ //建立資料模型 DataModel dm = new FileDataModel(new File("C:/test.txt")); ItemSimilarity is=new PearsonCorrelationSimilarity(dm); //構造Recommendation Engine Recommender re =new GenericItemBasedRecommender(dm,is);//顯示推薦結果,為1號使用者推薦兩個商品 List<RecommendedItem> list = re.recommend(1, 2); for(RecommendedItem recommendedItem :list) { System.out.println(recommendedItem); }} }
slopeone演算法,0.9版本已移除,要使用只能用0.8
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.recommender.GenericItemBasedRecommender;import org.apache.mahout.cf.taste.impl.similarity.PearsonCorrelationSimilarity;import org.apache.mahout.cf.taste.model.DataModel;import org.apache.mahout.cf.taste.recommender.RecommendedItem;import org.apache.mahout.cf.taste.recommender.Recommender;import org.apache.mahout.cf.taste.similarity.ItemSimilarity;public class SlopeOneRecommend {public static void main(String[] args) throws Exception{ //建立資料模型 DataModel dm = new FileDataModel(new File("C:/test.txt")); //構造Recommendation Engine Recommender re =new SlopeOneRecommender(dm);;//顯示推薦結果,為1號使用者推薦兩個商品 List<RecommendedItem> list = re.recommend(1, 2); for(RecommendedItem recommendedItem :list) { System.out.println(recommendedItem); }} }
本文出自 “厚積薄發,磨刀不誤砍柴工” 部落格,請務必保留此出處http://tianxingzhe.blog.51cto.com/3390077/1713455
mahout0.11 taste架構Recommendation Engineapi