您好,登錄后才能下訂單哦!
所需jar包
數據格式以逗號分隔
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.0 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 6,102,4.0 6,103,2.0 6,105,3.5 6,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); //構造推薦引擎 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); //構造推薦引擎 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")); //構造推薦引擎 Recommender re =new SlopeOneRecommender(dm);; //顯示推薦結果,為1號用戶推薦兩個商品 List<RecommendedItem> list = re.recommend(1, 2); for(RecommendedItem recommendedItem :list) { System.out.println(recommendedItem); } } }
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。