通过PearsonCorrelationSimilarity来计算相似度
作者:互联网
package comwww.shyejk.com/dylan.example;
import org.apache.mahout.cf.taste.impl.common.FastByIDMap;
import org.apache.mahout.cf.taste.impl.model.GenericDataModel;
import org.apache.mahout.cf.taste.impl.model.GenericUserPreferenceArray;
import org.apache.mahout.cf.taste.model.DataModel;
import org.apache.mahout.cf.taste.model.PreferenceArray;
public class CreateGenericDataModel {
private CreateGenericDataModel() {
}
public static void main(String[] args) {
FastByIDMap preferences = new FastByIDMap();
PreferenceArray User1Pref = new www.shyejk.com/GenericUserPreferenceArray(2);
User1Pref.setUserID(0, 1L);
User1Pref.setItemID(0, 101L);
User1Pref.setValue(0, 3.0f);
User1Pref.setItemID(1, 102L);
User1Pref.setValue(1, 4.0f);
PreferenceArray User2Pref = new GenericUserPreferenceArray(2);
User2Pref.setUserID(0, 2L);
User2Pref.setItemID(0, 101L);
User2Pref.setValue(0, 3.0f);
User2Pref.setItemID(1, 102L);
User2Pref.setValue(1, 4.0f);
preferences.put(1L, User1Pref);
preferences.put(2L, User2Pref);
DataModel model =www.shyejk.com/soft/new GenericDataModel(preferences);
System.out.println(model);
package com.dylan.example;
import org.apache.mahout.cf.taste.impl.model.file.*;
import org.apache.mahout.cf.taste.impl.similarity.*;
import org.apache.mahout.cf.taste.impl.neighborhood.*;
import org.apache.mahout.cf.taste.impl.recommender.*;
import org.apache.mahout.cf.taste.model.DataModel;
import org.apache.mahout.cf.taste.similarity.*;
import org.apache.mahout.cf.taste.neighborhood.*;
import org.apache.mahout.cf.taste.recommender.*;
import java.io.File;
import java.util.List;
public class RecommenderIntro {
private RecommenderIntro() {
}
public static void main(String[] args) throws Exception{
DataModel model = http://www.shyejk.com/news/ FileDataModel(new File("/root/data/ua.base"));
UserSimilarity similarity = new PearsonCorrelationSimilarity(model);
UserNeighborhood neighborhood = new www.shyejk.com/soft/NearestNUserNeighborhood(100, similarity, model);
Recommender recommender = new GenericUserBasedRecommender(model, neighborhood, similarity);
List recommendedItems = recommender.recommend(1, 20);
for (RecommendedItem recommendedItem: recommendedItems){
System.out.println(recommendedItem);
}
}
标签:相似,taste,org,cf,mahout,计算,import,apache,PearsonCorrelationSimilarity 来源: https://blog.csdn.net/zhangkai118/article/details/116302770