Mahout--简单推荐系统Demo
mahout简单推荐系统
工具:
mahout 0.8
Myeclipse10
maven3.2.5
代码:
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.util.List; import org.apache.mahout.cf.taste.common.TasteException; import org.apache.mahout.cf.taste.impl.common.LongPrimitiveIterator; 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.CachingRecommender; import org.apache.mahout.cf.taste.impl.recommender.GenericBooleanPrefItemBasedRecommender; import org.apache.mahout.cf.taste.impl.recommender.GenericItemBasedRecommender; import org.apache.mahout.cf.taste.impl.recommender.GenericUserBasedRecommender; import org.apache.mahout.cf.taste.impl.recommender.slopeone.SlopeOneRecommender; import org.apache.mahout.cf.taste.impl.similarity.EuclideanDistanceSimilarity; import org.apache.mahout.cf.taste.impl.similarity.GenericUserSimilarity; import org.apache.mahout.cf.taste.impl.similarity.LogLikelihoodSimilarity; 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.recommender.UserBasedRecommender; import org.apache.mahout.cf.taste.similarity.ItemSimilarity; import org.apache.mahout.cf.taste.similarity.UserSimilarity; /** * @author lyx * * 2015-7-17上午11:32:03 * *mahoutMvn..App */ @SuppressWarnings("deprecation") public class App { //输入文件 static final String inputFile ="ml-1m/ratings.dat"; //输出文件 static final String outputFile="ml-1m/ratings.csv"; public static void main(String[] argv)throws IOException, TasteException { //转换成csv文件 CreateCsvRatingsFile(); MoviceFile.CreateCsvMovice(); MoviceFile.CreateCsvUser(); //-------------------读取ratings的数据 //读取ratings.csv文件 File ratingsFile = new File(outputFile); //建立模型 DataModel model = new FileDataModel(ratingsFile); //新建推荐引擎 三种方式 //1-----依据用户类似度建立推荐引擎 /*UserSimilarity userSim = new EuclideanDistanceSimilarity(model); NearestNUserNeighborhood userNei = new NearestNUserNeighborhood(3, userSim, model); Recommender cachingRecommender = new GenericUserBasedRecommender(model, userNei, userSim);*/ //----end //2----依据Item类似度建立推荐引擎 /* ItemSimilarity itemSim = new LogLikelihoodSimilarity(model); Recommender cachingRecommender = new GenericItemBasedRecommender(model, itemSim);*/ //----end //3----缓存推荐引擎 CachingRecommender cachingRecommender = new CachingRecommender(new SlopeOneRecommender(model)); //遍历获得的所实用户 for(LongPrimitiveIterator it =model.getUserIDs();it.hasNext();) { //获取单个用户ID long userId =it.nextLong(); //20表示推荐个数 List<RecommendedItem> recommendations = cachingRecommender.recommend(userId, 10); //该用户没有推荐 if(recommendations.size() ==0) { System.out.println("UserId"+userId+":no Recommendations"); } //输出推荐后的数据 for (RecommendedItem recommendedItem : recommendations) { System.out.println("UserId"+userId+":"+recommendedItem); } } } //转换成csv格式函数 private static void CreateCsvRatingsFile() throws IOException { BufferedReader br = new BufferedReader(new FileReader(inputFile)); BufferedWriter bw = new BufferedWriter(new FileWriter(outputFile)); String line =null; String line2write=null; String[] temp; int i=0; //读取ratings.bat文件并写入ratings.csv文件里 //10000控制读取文件里的行数 while( (line=br.readLine())!=null && i<10000) { i++; //将bat的"::" 替换成csv的"," temp=line.split("::"); //temp[0]相应UserId;temp[1]表示itemId;temp[2]表示preference用户评分;temp[3]表示timestamp时间戳 line2write =temp[0]+","+temp[1]+","+temp[2]; //写入文件 bw.write(line2write); bw.newLine(); bw.flush(); } br.close(); bw.close(); } }
測试文件:http://download.csdn.net/detail/u013147600/8908241
源码:http://download.csdn.net/detail/u013147600/8908287
结果:
(数据太多我仅仅列了部分结果)
2015-7-17 10:51:22 org.apache.mahout.cf.taste.impl.model.file.FileDataModel <init>
信息: Creating FileDataModel for file ml-1m\ratings.csv
2015-7-17 10:51:22 org.apache.mahout.cf.taste.impl.model.file.FileDataModel processFile
信息: Reading file info...
2015-7-17 10:51:23 org.apache.mahout.cf.taste.impl.model.file.FileDataModel processFile
信息: Read lines: 10000
2015-7-17 10:51:23 org.apache.mahout.cf.taste.impl.model.GenericDataModel <init>
信息: Processed 70 users
2015-7-17 10:51:23 org.apache.mahout.cf.taste.impl.recommender.slopeone.MemoryDiffStorage buildAverageDiffs
信息: Building average diffs...
UserId1:RecommendedItem[item:3584, value:6.2180653]
UserId1:RecommendedItem[item:321, value:6.0]
UserId1:RecommendedItem[item:617, value:5.7071066]
UserId1:RecommendedItem[item:207, value:5.628753]
UserId1:RecommendedItem[item:3159, value:5.5]
UserId1:RecommendedItem[item:2447, value:5.37199]
UserId1:RecommendedItem[item:2266, value:5.351284]
UserId1:RecommendedItem[item:45, value:5.2991066]
UserId1:RecommendedItem[item:465, value:5.2564435]
UserId1:RecommendedItem[item:1964, value:5.216388]
UserId2:RecommendedItem[item:511, value:5.8099294]
UserId2:RecommendedItem[item:947, value:5.4393396]
UserId2:RecommendedItem[item:2565, value:5.303187]
UserId2:RecommendedItem[item:905, value:5.2949805]
UserId2:RecommendedItem[item:959, value:5.246918]
UserId2:RecommendedItem[item:1799, value:5.1516027]
UserId2:RecommendedItem[item:617, value:5.1447325]
UserId2:RecommendedItem[item:207, value:5.142663]
UserId2:RecommendedItem[item:3536, value:5.1051]
UserId2:RecommendedItem[item:3871, value:5.1012936]
UserId3:RecommendedItem[item:46, value:6.5]
UserId3:RecommendedItem[item:949, value:5.563662]
UserId3:RecommendedItem[item:465, value:5.5]
UserId3:RecommendedItem[item:1353, value:5.5]
UserId3:RecommendedItem[item:691, value:5.5]
UserId3:RecommendedItem[item:959, value:5.426777]
UserId3:RecommendedItem[item:3871, value:5.422468]
UserId3:RecommendedItem[item:207, value:5.4084086]
UserId3:RecommendedItem[item:2183, value:5.3106756]
UserId3:RecommendedItem[item:905, value:5.2015433]
UserId4:RecommendedItem[item:3079, value:6.0]
UserId4:RecommendedItem[item:2750, value:6.0]
UserId4:RecommendedItem[item:229, value:5.7071066]
UserId4:RecommendedItem[item:959, value:5.7071066]
UserId4:RecommendedItem[item:41, value:5.7071066]
UserId4:RecommendedItem[item:3194, value:5.6955457]
UserId4:RecommendedItem[item:1046, value:5.5857863]
UserId4:RecommendedItem[item:909, value:5.554097]
UserId4:RecommendedItem[item:3871, value:5.5368433]
UserId4:RecommendedItem[item:321, value:5.5]
UserId5:RecommendedItem[item:3682, value:5.1450996]
UserId5:RecommendedItem[item:3873, value:5.0]
UserId5:RecommendedItem[item:2262, value:4.916423]
UserId5:RecommendedItem[item:911, value:4.824826]
UserId5:RecommendedItem[item:905, value:4.6265926]
UserId5:RecommendedItem[item:2565, value:4.5703297]
UserId5:RecommendedItem[item:3028, value:4.554097]
UserId5:RecommendedItem[item:2629, value:4.5]
UserId5:RecommendedItem[item:947, value:4.5]
UserId5:RecommendedItem[item:2266, value:4.481881]
UserId6:RecommendedItem[item:947, value:6.5]
UserId6:RecommendedItem[item:1799, value:5.764298]
UserId6:RecommendedItem[item:207, value:5.248078]
UserId6:RecommendedItem[item:949, value:5.073223]
UserId6:RecommendedItem[item:3690, value:5.0]
UserId6:RecommendedItem[item:3584, value:4.9543366]
UserId6:RecommendedItem[item:2183, value:4.9174514]
UserId6:RecommendedItem[item:905, value:4.9054875]
UserId6:RecommendedItem[item:46, value:4.810987]
UserId6:RecommendedItem[item:3871, value:4.7513065]
UserId7:RecommendedItem[item:511, value:6.138071]
UserId7:RecommendedItem[item:3871, value:6.0428934]
UserId7:RecommendedItem[item:1964, value:6.0]
UserId7:RecommendedItem[item:3584, value:6.0]
UserId7:RecommendedItem[item:2969, value:6.0]
UserId7:RecommendedItem[item:3079, value:5.969005]
UserId7:RecommendedItem[item:959, value:5.9497476]
UserId7:RecommendedItem[item:1799, value:5.6596065]
UserId7:RecommendedItem[item:3365, value:5.5]
UserId7:RecommendedItem[item:2731, value:5.5]
UserId8:RecommendedItem[item:3159, value:6.114009]
UserId8:RecommendedItem[item:511, value:5.861929]
UserId8:RecommendedItem[item:905, value:5.780505]
UserId8:RecommendedItem[item:959, value:5.7237544]
UserId8:RecommendedItem[item:2565, value:5.715391]
UserId8:RecommendedItem[item:3871, value:5.6441703]
UserId8:RecommendedItem[item:3469, value:5.539504]
UserId8:RecommendedItem[item:1284, value:5.517845]
UserId8:RecommendedItem[item:947, value:5.5]
UserId8:RecommendedItem[item:1658, value:5.5]
UserId9:RecommendedItem[item:1799, value:5.655904]
UserId9:RecommendedItem[item:511, value:5.5]
UserId9:RecommendedItem[item:3159, value:5.399016]
UserId9:RecommendedItem[item:3548, value:5.235342]
UserId9:RecommendedItem[item:2565, value:5.111815]
UserId9:RecommendedItem[item:2262, value:5.0786123]
UserId9:RecommendedItem[item:3871, value:5.0401726]
UserId9:RecommendedItem[item:949, value:5.0369573]
UserId9:RecommendedItem[item:3469, value:5.0322886]
UserId9:RecommendedItem[item:905, value:5.028183]
UserId10:RecommendedItem[item:207, value:5.672013]
UserId10:RecommendedItem[item:511, value:5.6541953]
UserId10:RecommendedItem[item:949, value:5.469813]
UserId10:RecommendedItem[item:2266, value:5.44176]
UserId10:RecommendedItem[item:3612, value:5.4363294]
UserId10:RecommendedItem[item:2262, value:5.4325333]
UserId10:RecommendedItem[item:911, value:5.3296833]
UserId10:RecommendedItem[item:1218, value:5.3140836]
UserId10:RecommendedItem[item:905, value:5.2804785]
UserId10:RecommendedItem[item:1284, value:5.23317]
UserId11:RecommendedItem[item:511, value:5.2928934]
UserId11:RecommendedItem[item:2318, value:5.177858]
UserId11:RecommendedItem[item:2560, value:5.177858]
UserId11:RecommendedItem[item:949, value:5.17725]
UserId11:RecommendedItem[item:3682, value:5.1679707]
UserId11:RecommendedItem[item:959, value:5.151557]
UserId11:RecommendedItem[item:2565, value:5.1177316]
UserId11:RecommendedItem[item:3617, value:5.0]
UserId11:RecommendedItem[item:3469, value:5.0]
UserId11:RecommendedItem[item:3399, value:5.0]
UserId12:RecommendedItem[item:511, value:6.5]
UserId12:RecommendedItem[item:3728, value:6.0]
UserId12:RecommendedItem[item:207, value:5.8562026]
UserId12:RecommendedItem[item:3308, value:5.7819347]
UserId12:RecommendedItem[item:3543, value:5.543335]
UserId12:RecommendedItem[item:1046, value:5.5]
UserId12:RecommendedItem[item:2236, value:5.5]
UserId12:RecommendedItem[item:2750, value:5.5]
UserId12:RecommendedItem[item:2693, value:5.420008]
UserId12:RecommendedItem[item:1218, value:5.269752]
UserId13:RecommendedItem[item:959, value:5.6407547]
UserId13:RecommendedItem[item:3871, value:5.4714265]
UserId13:RecommendedItem[item:2565, value:5.353783]
UserId13:RecommendedItem[item:1964, value:5.3458047]
UserId13:RecommendedItem[item:511, value:5.289365]
UserId13:RecommendedItem[item:3612, value:5.200032]
UserId13:RecommendedItem[item:905, value:5.128152]
UserId13:RecommendedItem[item:3350, value:5.1120386]
UserId13:RecommendedItem[item:949, value:5.0913043]
UserId13:RecommendedItem[item:3469, value:5.0816817]
UserId14:RecommendedItem[item:3682, value:5.408847]
UserId14:RecommendedItem[item:3028, value:5.0]
UserId14:RecommendedItem[item:421, value:5.0]
UserId14:RecommendedItem[item:2370, value:5.0]
UserId14:RecommendedItem[item:13, value:5.0]
UserId14:RecommendedItem[item:678, value:4.8048396]
UserId14:RecommendedItem[item:3466, value:4.7156296]
UserId14:RecommendedItem[item:1216, value:4.7071066]
UserId14:RecommendedItem[item:2560, value:4.6875987]
UserId14:RecommendedItem[item:2318, value:4.6875987]
UserId15:RecommendedItem[item:3399, value:5.2426405]
UserId15:RecommendedItem[item:959, value:5.0525646]
UserId15:RecommendedItem[item:511, value:4.9936624]
UserId15:RecommendedItem[item:3871, value:4.9365416]
UserId15:RecommendedItem[item:2565, value:4.8587527]
UserId15:RecommendedItem[item:1799, value:4.7627077]
UserId15:RecommendedItem[item:207, value:4.7132444]
UserId15:RecommendedItem[item:949, value:4.703938]
UserId15:RecommendedItem[item:905, value:4.6290293]
UserId15:RecommendedItem[item:3159, value:4.6267886]
UserId16:RecommendedItem[item:207, value:7.5]
UserId16:RecommendedItem[item:2261, value:7.0]
UserId16:RecommendedItem[item:1658, value:7.0]
UserId16:RecommendedItem[item:2546, value:6.5]
UserId16:RecommendedItem[item:3916, value:6.5]
UserId16:RecommendedItem[item:550, value:6.5]
UserId16:RecommendedItem[item:2262, value:6.290908]
UserId16:RecommendedItem[item:3219, value:6.0]
UserId16:RecommendedItem[item:3423, value:6.0]
UserId16:RecommendedItem[item:3308, value:5.8719974]
UserId17:RecommendedItem[item:905, value:5.7276125]
UserId17:RecommendedItem[item:1964, value:5.7101846]
UserId17:RecommendedItem[item:511, value:5.6666665]
UserId17:RecommendedItem[item:3612, value:5.6384935]
UserId17:RecommendedItem[item:2565, value:5.5928273]
UserId17:RecommendedItem[item:3871, value:5.5852685]
UserId17:RecommendedItem[item:949, value:5.5183544]
UserId17:RecommendedItem[item:959, value:5.5101953]
UserId17:RecommendedItem[item:947, value:5.5]
UserId17:RecommendedItem[item:46, value:5.5]
UserId18:RecommendedItem[item:3873, value:6.0]
UserId18:RecommendedItem[item:1964, value:5.7274976]
UserId18:RecommendedItem[item:3871, value:5.6701856]
UserId18:RecommendedItem[item:905, value:5.637785]
UserId18:RecommendedItem[item:959, value:5.555419]
UserId18:RecommendedItem[item:1327, value:5.5]
UserId18:RecommendedItem[item:3469, value:5.4503527]
UserId18:RecommendedItem[item:947, value:5.4142137]
UserId18:RecommendedItem[item:2560, value:5.346069]
UserId18:RecommendedItem[item:2318, value:5.346069]
UserId19:RecommendedItem[item:947, value:5.9930615]
UserId19:RecommendedItem[item:511, value:5.426007]
UserId19:RecommendedItem[item:2750, value:5.3801413]
UserId19:RecommendedItem[item:207, value:5.3290377]
UserId19:RecommendedItem[item:1964, value:5.2887883]
UserId19:RecommendedItem[item:465, value:5.2238283]
UserId19:RecommendedItem[item:911, value:5.203409]
UserId19:RecommendedItem[item:3159, value:5.2017245]
UserId19:RecommendedItem[item:905, value:5.1945677]
UserId19:RecommendedItem[item:949, value:5.1646047]
UserId20:RecommendedItem[item:2565, value:6.0790086]
UserId20:RecommendedItem[item:3399, value:6.0]
UserId20:RecommendedItem[item:2941, value:5.909199]
UserId20:RecommendedItem[item:959, value:5.831554]
UserId20:RecommendedItem[item:2045, value:5.826309]
UserId20:RecommendedItem[item:905, value:5.785153]
UserId20:RecommendedItem[item:3871, value:5.7160187]
UserId20:RecommendedItem[item:1046, value:5.5644345]
UserId20:RecommendedItem[item:928, value:5.555299]
UserId20:RecommendedItem[item:949, value:5.539504]
UserId21:RecommendedItem[item:1799, value:7.0]
UserId21:RecommendedItem[item:3871, value:6.5]
UserId21:RecommendedItem[item:465, value:6.5]
UserId21:RecommendedItem[item:3468, value:6.25]
UserId21:RecommendedItem[item:2287, value:6.0]
UserId21:RecommendedItem[item:3197, value:6.0]
UserId21:RecommendedItem[item:961, value:6.0]
UserId21:RecommendedItem[item:1964, value:6.0]
UserId21:RecommendedItem[item:949, value:6.0]
UserId21:RecommendedItem[item:1284, value:5.7807336]
UserId22:RecommendedItem[item:3617, value:5.0]
UserId22:RecommendedItem[item:949, value:4.9622054]
UserId22:RecommendedItem[item:959, value:4.6872873]
UserId22:RecommendedItem[item:2560, value:4.6729345]
UserId22:RecommendedItem[item:2318, value:4.6729345]
UserId22:RecommendedItem[item:2565, value:4.5550027]
UserId22:RecommendedItem[item:207, value:4.5313635]
UserId22:RecommendedItem[item:1964, value:4.5184402]
UserId22:RecommendedItem[item:3469, value:4.512255]
UserId22:RecommendedItem[item:2183, value:4.5077686]
UserId23:RecommendedItem[item:511, value:5.512344]
UserId23:RecommendedItem[item:1799, value:5.322148]
UserId23:RecommendedItem[item:207, value:5.1152883]
UserId23:RecommendedItem[item:3028, value:4.9154577]
UserId23:RecommendedItem[item:905, value:4.897649]
UserId23:RecommendedItem[item:949, value:4.8598137]
UserId23:RecommendedItem[item:465, value:4.799731]
UserId23:RecommendedItem[item:2969, value:4.767767]
UserId23:RecommendedItem[item:1284, value:4.7526493]
UserId23:RecommendedItem[item:3612, value:4.7513022]
UserId24:RecommendedItem[item:511, value:5.5382524]
UserId24:RecommendedItem[item:949, value:5.3961554]
UserId24:RecommendedItem[item:905, value:5.3580794]
UserId24:RecommendedItem[item:207, value:5.2839913]
UserId24:RecommendedItem[item:959, value:5.242953]
UserId24:RecommendedItem[item:2565, value:5.2087903]
UserId24:RecommendedItem[item:3871, value:5.204028]
UserId24:RecommendedItem[item:2183, value:5.102916]
UserId24:RecommendedItem[item:1284, value:5.09685]
UserId24:RecommendedItem[item:1964, value:5.050856]
UserId25:RecommendedItem[item:1327, value:6.5]
UserId25:RecommendedItem[item:207, value:5.990818]
UserId25:RecommendedItem[item:949, value:5.939524]
UserId25:RecommendedItem[item:511, value:5.7693305]
UserId25:RecommendedItem[item:959, value:5.7378225]
UserId25:RecommendedItem[item:905, value:5.686606]
UserId25:RecommendedItem[item:1964, value:5.630602]
UserId25:RecommendedItem[item:3871, value:5.6263237]
UserId25:RecommendedItem[item:922, value:5.563307]
UserId25:RecommendedItem[item:3469, value:5.5]
UserId26:RecommendedItem[item:465, value:4.89534]
UserId26:RecommendedItem[item:1284, value:4.4695115]
UserId26:RecommendedItem[item:949, value:4.452193]
UserId26:RecommendedItem[item:905, value:4.4508224]
UserId26:RecommendedItem[item:3429, value:4.445903]
UserId26:RecommendedItem[item:3871, value:4.408113]
UserId26:RecommendedItem[item:959, value:4.3552437]
UserId26:RecommendedItem[item:3159, value:4.334936]
UserId26:RecommendedItem[item:1950, value:4.317882]
UserId26:RecommendedItem[item:1964, value:4.3053885]
UserId27:RecommendedItem[item:2236, value:5.815301]