mahout推薦1

來源:互聯網
上載者:User

標籤:blog   java   io   檔案   資料   for   ar   div   

1、準備資料:

intro.csv:

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

 

2、編程實現:

  目的:為使用者1推薦一件商品看看:

package mahout;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.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;import org.apache.mahout.cf.taste.impl.similarity.PearsonCorrelationSimilarity;/** * 基於使用者的推薦程式 * @author Administrator * */public class RecommenderIntro {public static void main(String[] args) throws Exception {//裝載資料檔案,實現儲存,並為計算提供所需的所有偏好,使用者和物品資料DataModel model = new FileDataModel(new File("data/intro.csv"));//使用者相似性,給出兩個使用者的相似性,有多種度量方式UserSimilarity similarity = new PearsonCorrelationSimilarity(model);//使用者鄰居,與給定使用者最相似的一組使用者UserNeighborhood neighborhood = new NearestNUserNeighborhood(2,similarity, model);//Recommendation Engine,合并這些組件,實現推薦Recommender recommender = new GenericUserBasedRecommender(model,neighborhood, similarity);//為使用者1推薦一件物品1,1List<RecommendedItem> recommendedItems = recommender.recommend(1, 1);//輸出for (RecommendedItem item : recommendedItems) {System.out.println(item);}}}

 輸出結果:

14/08/04 08:46:31 INFO file.FileDataModel: Creating FileDataModel for file data\intro.csv14/08/04 08:46:31 INFO file.FileDataModel: Reading file info...14/08/04 08:46:31 INFO file.FileDataModel: Read lines: 2114/08/04 08:46:31 INFO model.GenericDataModel: Processed 5 usersRecommendedItem[item:104, value:4.257081]

 當然也可以推薦多件商品,那就是將recommender.recommend(1,N)即可。

推薦效果不錯。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.