您好,登錄后才能下訂單哦!
所需jar包
數(shù)據(jù)格式以逗號分隔
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{ //創(chuàng)建數(shù)據(jù)模型 DataModel dm = new FileDataModel(new File("C:/test.txt")); //使用user來推薦,計(jì)算相似度 UserSimilarity us=new PearsonCorrelationSimilarity(dm); //查找K(3)近鄰 UserNeighborhood unb=new NearestNUserNeighborhood(3, us, dm); //構(gòu)造推薦引擎 Recommender re =new GenericUserBasedRecommender(dm, unb, us); //顯示推薦結(jié)果,為1號用戶推薦兩個商品 List<RecommendedItem> list = re.recommend(1, 2); for(RecommendedItem recommendedItem :list) { System.out.println(recommendedItem); } } }
推薦結(jié)果
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{ //創(chuàng)建數(shù)據(jù)模型 DataModel dm = new FileDataModel(new File("C:/test.txt")); ItemSimilarity is=new PearsonCorrelationSimilarity(dm); //構(gòu)造推薦引擎 Recommender re =new GenericItemBasedRecommender(dm,is); //顯示推薦結(jié)果,為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{ //創(chuàng)建數(shù)據(jù)模型 DataModel dm = new FileDataModel(new File("C:/test.txt")); //構(gòu)造推薦引擎 Recommender re =new SlopeOneRecommender(dm);; //顯示推薦結(jié)果,為1號用戶推薦兩個商品 List<RecommendedItem> list = re.recommend(1, 2); for(RecommendedItem recommendedItem :list) { System.out.println(recommendedItem); } } }
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。