mahout0.11 taste架構Recommendation Engineapi

來源:互聯網
上載者:User

標籤: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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.