Hello Mahout

Hello Mahout

1.新建java project

测试代码:

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 RecommendInfo {

    /**
     * @param args
     */
    public static void main(String[] args) throws Exception {
        //加载数据
        DataModel model = new FileDataModel (new File("/home/name/Documents/java/helloMahout/src/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, 1);

        for(RecommendedItem recommendation : recommendations)
        {
            System.out.println(recommendation);
        }
    }

}



2. 环境配置

properties->java build path. 加入如图所示jar


3. 运行结果

RecommendedItem[item:104, value:4.257081]

RecommendedItem[item:106, value:4.0]

你可能感兴趣的:(Hello Mahout)