標籤:Lucene style blog http java color
Mahout In Aciotn
Jack Zhang 來自開拓者部落 ,qq群:248087140,歡迎加入我們!
本文歡迎轉載,轉載請註明出處 http://my.oschina.net/u/1866370/blog/287907
i.Java和IDE(略)
ii.Maven(略) iii.Mahout開發環境搭建
1、Mahout官網:http://mahout.apache.org/
2、Mahout官網上關於Mahout依賴的頁面 http://mahout.apache.org/general/downloads.html
在2中可以看到Mahout的Maven座標
<dependency> <groupId>org.apache.mahout</groupId> <artifactId>mahout-core</artifactId> <version>${mahout.version}</version></dependency>
具體安裝過程
使用Maven使Mahout的環境搭建變得簡單方便,只需在pom中添加如下內容即可。
<properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><mahout.version>0.6</mahout.version></properties><dependencies><dependency><groupId>org.apache.mahout</groupId><artifactId>mahout-core</artifactId><version>${mahout.version}</version></dependency><dependency><groupId>org.apache.mahout</groupId><artifactId>mahout-integration</artifactId><version>${mahout.version}</version><exclusions><exclusion><groupId>org.mortbay.jetty</groupId><artifactId>jetty</artifactId></exclusion><exclusion><groupId>org.apache.cassandra</groupId><artifactId>cassandra-all</artifactId></exclusion><exclusion><groupId>me.prettyprint</groupId><artifactId>hector-core</artifactId></exclusion></exclusions></dependency></dependencies>
說明:
1、Maven的配置說明<mahout.version>0.6</mahout.version>以上為將Mahout的版本設定為Maven的全域變數,可以在pom的其他位置,以<version>${mahout.version}</version>的方式引用<exclusion> 標籤<exclusion>標籤的作用是,在匯入依賴的時候排除,<exclusion>標籤中的jar包此處排除了org.mortbay.jetty.jetty,org.apache.cassandra.cassandra-all,me.prettyprint.hector-core其中第二個jar包,在以後的文章中會提到,此處不做說明2、jar包的說明mahout-core 為mahout核心包mahout-integration 將Mahout整合如其他項目的jar包這座標的引入將會導致以下全部jar包被引入項目(包括,mahout的相關包,httpclient,solr,lucene,mongodb和一些在java項目中常常使用的工具包。)org\apache\mahout\mahout-core\0.6\mahout-core-0.6.jarorg\apache\mahout\mahout-math\0.6\mahout-math-0.6.jarorg\uncommons\maths\uncommons-maths\1.2.2\uncommons-maths-1.2.2.jarjfree\jcommon\1.0.12\jcommon-1.0.12.jarcom\google\guava\guava\r09\guava-r09.jarorg\apache\mahout\mahout-collections\1.0\mahout-collections-1.0.jarorg\apache\hadoop\hadoop-core\0.20.204.0\hadoop-core-0.20.204.0.jarcommons-cli\commons-cli\1.2\commons-cli-1.2.jarcommons-httpclient\commons-httpclient\3.0.1\commons-httpclient-3.0.1.jarcommons-logging\commons-logging\1.0.3\commons-logging-1.0.3.jarcommons-codec\commons-codec\1.4\commons-codec-1.4.jarcommons-configuration\commons-configuration\1.6\commons-configuration-1.6.jarcommons-collections\commons-collections\3.2.1\commons-collections-3.2.1.jarcommons-digester\commons-digester\1.8\commons-digester-1.8.jarcommons-beanutils\commons-beanutils\1.7.0\commons-beanutils-1.7.0.jarcommons-beanutils\commons-beanutils-core\1.8.0\commons-beanutils-core-1.8.0.jarorg\codehaus\jackson\jackson-core-asl\1.8.2\jackson-core-asl-1.8.2.jarorg\codehaus\jackson\jackson-mapper-asl\1.8.2\jackson-mapper-asl-1.8.2.jarorg\slf4j\slf4j-api\1.6.1\slf4j-api-1.6.1.jarcommons-lang\commons-lang\2.6\commons-lang-2.6.jarorg\uncommons\watchmaker\watchmaker-framework\0.6.2\watchmaker-framework-0.6.2.jarcom\thoughtworks\xstream\xstream\1.3.1\xstream-1.3.1.jarxpp3\xpp3_min\1.1.4c\xpp3_min-1.1.4c.jarorg\apache\lucene\lucene-core\3.4.0\lucene-core-3.4.0.jarorg\apache\lucene\lucene-analyzers\3.4.0\lucene-analyzers-3.4.0.jarorg\apache\mahout\commons\commons-cli\2.0-mahout\commons-cli-2.0-mahout.jarorg\apache\commons\commons-math\2.2\commons-math-2.2.jarorg\apache\mahout\mahout-integration\0.6\mahout-integration-0.6.jarcommons-dbcp\commons-dbcp\1.4\commons-dbcp-1.4.jarcommons-pool\commons-pool\1.5.6\commons-pool-1.5.6.jarorg\apache\solr\solr-commons-csv\3.4.0\solr-commons-csv-3.4.0.jarorg\mongodb\mongo-java-driver\2.5\mongo-java-driver-2.5.jarorg\mongodb\bson\2.5\bson-2.5.jar
iv.Hadoop(略)
hadoop的安裝可查看本部落格Hadoop類目下的相關文章
Mahout程式編寫
1、向一個使用者推薦一個商品
第一步:建立一個文字檔,重新命名為intro,複製以下內容到intro.txt,將尾碼修改為csv。在實際的開發中,我們也將取得類似的日誌資料,作為輸入檔案。
1,101,5.01,102,3.01,103,2.5 2,101,2.02,102,3.02,103,5.02,104,2.0 3,101,2.53,104,4.03,105,4.53,107,5.0 4,101,5.04,103,3.04,104,4.54,106,4.0 5,101,4.05,102,3.05,103,2.05,104,4.05,105,3.55,106,4.0
第二步:編寫基於使用者的協同過濾 程式
需求:向ID為1的使用者,推薦物品,推薦的物品數為1。
class RecommenderIntro { final static int NEIGHBORHOOD_NUM = 2; final static int USER_ID = 1; final static int RECOMMEND_NUM = 1; public static void main(String[] args) throws IOException, TasteException { /**構建檔案對象,注意檔案路徑要正確*/ DataModel model = new FileDataModel(new File("intro.csv")); /**使用者相識度*/ UserSimilarity user = new PearsonCorrelationSimilarity(model); /**近鄰*/ UserNeighborhood neighborhood = new NearestNUserNeighborhood(NEIGHBORHOOD_NUM, user, model); /**產生推薦器*/ Recommender recommender = new GenericUserBasedRecommender(model, neighborhood, user); /**進行推薦 ,向ID為1的使用者推薦 1個物品*/ List<RecommendedItem> recommendations = recommender.recommend(USER_ID, RECOMMEND_NUM); for(RecommendedItem recommendation:recommendations){ System.out.println(recommendation); } }}
推薦結果
RecommendedItem[item:104, value:4.257081]
2、為推薦程式打分