Eclipse下mahout實現推薦的簡單一實例

來源:互聯網
上載者:User

資料準備:test.txt

第一列為UserID ,第二列為ItemID,第三列為Preference Value 即評分 [plain] view plain copy print ? 1,101,5   1,102,3   1,103,2.5   2,101,2   2,102,2.5   2,103,5   2,104,2   3,101,2.5   3,104,4   3,105,4.5   3,107,5   4,101,5   4,103,3   4,104,4.5   4,106,4   5,101,4   5,102,3   5,103,2   5,104,4   5,105,3.5   5,106,4  

1,101,51,102,31,103,2.52,101,22,102,2.52,103,52,104,23,101,2.53,104,43,105,4.53,107,54,101,54,103,34,104,4.54,106,45,101,45,102,35,103,25,104,45,105,3.55,106,4

eclipse中建立一個java工程,然後在src目錄下建立一個類RecommenderIntro (這裡只是做測試,就放在預設包下,不建立包了),代碼如下: [java] view plain copy print ? import org.apache.mahout.cf.taste.impl.model.file.*;   import org.apache.mahout.cf.taste.impl.neighborhood.*;   import org.apache.mahout.cf.taste.impl.recommender.*;   import org.apache.mahout.cf.taste.impl.similarity.*;   import org.apache.mahout.cf.taste.model.*;   import org.apache.mahout.cf.taste.neighborhood.*;   import org.apache.mahout.cf.taste.recommender.*;   import org.apache.mahout.cf.taste.similarity.*;   import java.io.*;   import java.util.*;   public class RecommenderIntro {       private RecommenderIntro(){};              public static void main (String args[])throws Exception{                   // step:1 構建模型 2 計算相似性 3 尋找k緊鄰 4 構造Recommendation Engine            DataModel  model =new FileDataModel(new File("/home/test/test-in/test.txt"));//檔案名稱一定要是絕對路徑            UserSimilarity similarity =new PearsonCorrelationSimilarity(model);           UserNeighborhood neighborhood =new NearestNUserNeighborhood(2,similarity,model);           Recommender recommender= new GenericUserBasedRecommender(model,neighborhood,similarity);           List<RecommendedItem> recommendations =recommender.recommend(1, 2);//為使用者1推薦兩個ItemID            for(RecommendedItem recommendation :recommendations){               System.out.println(recommendation);           }                  }   }  

import org.apache.mahout.cf.taste.impl.model.file.*;import org.apache.mahout.cf.taste.impl.neighborhood.*;import org.apache.mahout.cf.taste.impl.recommender.*;import org.apache.mahout.cf.taste.impl.similarity.*;import org.apache.mahout.cf.taste.model.*;import org.apache.mahout.cf.taste.neighborhood.*;import org.apache.mahout.cf.taste.recommender.*;import org.apache.mahout.cf.taste.similarity.*;import java.io.*;import java.util.*;public class RecommenderIntro {private RecommenderIntro(){};public static void main (String args[])throws Exception{                // step:1 構建模型 2 計算相似性 3 尋找k緊鄰 4 構造Recommendation EngineDataModel  model =new FileDataModel(new File("/home/test/test-in/test.txt"));//檔案名稱一定要是絕對路徑UserSimilarity similarity =new PearsonCorrelationSimilarity(model);UserNeighborhood neighborhood =new NearestNUserNeighborhood(2,similarity,model);Recommender recommender= new GenericUserBasedRecommender(model,neighborhood,similarity);List<RecommendedItem> recommendations =recommender.recommend(1, 2);//為使用者1推薦兩個ItemIDfor(RecommendedItem recommendation :recommendations){System.out.println(recommendation);}}}

此時肯定會報錯,因為還沒有匯入任何相關Jar包,將mahout-core-0.4.jar導到工程中,此時代碼不會顯示有錯了

然後Run As 選擇Java Application,單機運行(當然叢集如果成功啟動的話,也可以在叢集上啟動並執行)。此時在運行後,會相繼報一些包不存在,根據提示,依次將包導到工程中。我在測試的過程中,先後匯入了slf4j-api-1.6.0.jar,slf4j-jcl-1.6.0.jar,google-collection-1.0-rc2.jar,commons-logging-1.1.1.jar,mahout-math-0.4.jar,uncommons-maths-1.2.jar,這些包都可以在mahout安裝目錄下的lib目錄下找到。經過一步步差包導包(控制台報什麼包不存在,我就找到相應包匯入)調試後,最後運行成功,結果如下:

RecommendedItem[item:104, value:4.257081]

RecommendedItem[item:106, value:4.0]

結果分析:找到使用者1的相似使用者2和使用者5,使用者2和使用者5共同有104,所以推薦分數高,但我不知道這個分數是怎麼算出來的。使用者2中沒有其它可以推薦,然後使用者5中還有105和106可以推薦,而106的評分比105高,所以第二個結果是106,不知道理解的對不對。只是自己猜測的,有知道其中內幕的和知道推薦分數怎麼算的,請留言分享一下,開源是促進相互學習的好方法,呵呵

然後看了一下mahout的taste工作原理,我將mahout安裝目錄下的mahout-taste-webapp-0.4.war放在tomcat安裝目錄下的webapps中,然後啟動tomcat,然後在瀏覽器中輸入:http://localhost:8080/mahout-taste-webapp-0.4/RecommenderService.jws ,可以看到Taste 提供的Web 服務提供者(即WebServices服務),但卻不能根據使用者ID查詢,估計應該還要下載相應的資料集和jar包,詳見 基於 Apache Mahout 構建社會化Recommendation Engine

參考資料:

1.http://www.ibm.com/developerworks/cn/java/j-mahout/

2.http://blog.csdn.net/zhzhl202/article/details/6316570

3.http://www.ibm.com/developerworks/cn/java/j-lo-mahout/

 

聯繫我們

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