mahout 基于用户推荐

package org.conan.mymahout.recommendation;

import java.io.File;
import java.io.IOException;
import java.util.List;
import org.apache.mahout.cf.taste.common.TasteException;
import org.apache.mahout.cf.taste.common.Weighting;
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.impl.similarity.PearsonCorrelationSimilarity;
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;

public class UserCF {
    public static void main(String[] args) throws IOException, TasteException { 
        List recommendations = null;  
        DataModel model = new FileDataModel(new File("C:\\Users\\dell\\Desktop\\new_ratings.csv"));//构造数据模型
        //计算相似度,相似度算法有很多种,欧几里得、皮尔逊等等。
        UserSimilarity similarity = new PearsonCorrelationSimilarity(model,Weighting.WEIGHTED);
        //计算最近邻域,邻居有两种算法,基于固定数量的邻居和基于相似度的邻居,这里使用基于固定数量的邻居
        UserNeighborhood userNeighborhood = new NearestNUserNeighborhood(100, similarity, model);
        Recommender recommender = new GenericUserBasedRecommender(model, userNeighborhood, similarity);
        recommendations = recommender.recommend(3363,10);
        //打印推荐的结果
        if(recommendations!=null) {
            for (RecommendedItem recommendedItem : recommendations) {
                System.out.println(recommendedItem);
        }
            System.out.println(recommender.estimatePreference(3363,542));
        }
    }
}
mahout 基于用户推荐_第1张图片

2.评估结果

package org.conan.mymahout.recommendation;

import java.io.File;
import java.io.IOException;
import org.apache.mahout.cf.taste.common.TasteException;
import org.apache.mahout.cf.taste.eval.RecommenderBuilder;
import org.apache.mahout.cf.taste.eval.RecommenderEvaluator;
import org.apache.mahout.cf.taste.impl.eval.AverageAbsoluteDifferenceRecommenderEvaluator;
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.impl.similarity.PearsonCorrelationSimilarity;
import org.apache.mahout.cf.taste.model.DataModel;
import org.apache.mahout.cf.taste.neighborhood.UserNeighborhood;
import org.apache.mahout.cf.taste.recommender.Recommender;
import org.apache.mahout.cf.taste.similarity.UserSimilarity;
import org.apache.mahout.common.RandomUtils;

public class UserCF {
    public static void main(String[] args) throws IOException, TasteException { 
        RandomUtils.useTestSeed();
        DataModel model = new FileDataModel(new File("C:\\Users\\dell\\Desktop\\new_ratings.csv"));//构造数据模型
        RecommenderEvaluator evaluator=new AverageAbsoluteDifferenceRecommenderEvaluator();//均方根评分用RMSRecommenderEvaluator
        RecommenderBuilder builder=new RecommenderBuilder() {   
            public Recommender buildRecommender(DataModel model) throws TasteException{
                UserSimilarity similarity=new PearsonCorrelationSimilarity(model);
                UserNeighborhood neighborhood=new NearestNUserNeighborhood(2,similarity,model);
                return new GenericUserBasedRecommender(model,neighborhood,similarity);
            }
        };
        double score=evaluator.evaluate(builder,null,model,0.7,1.0);//训练70%的数据,测试30%
        System.out.println(score);
    }
}
mahout 基于用户推荐_第2张图片
分值4.103173790518768表示平均而言所给出的估计值与实际值的偏差

你可能感兴趣的:(mahout 基于用户推荐)