• mahout学习-1


    一. 安装软件

    需要安装如下文件:

    java, Eclipse, Maven,Hadoop,mahout


    二. 推荐系统简介

    每天,我们都会对一些事物表达自己的看法,喜欢,或不喜欢,或不在乎。这些都在无意识地发生。当你从电台听到一首歌曲的时候,要么会注意到它(因为它好听或它难听),要么完全不会注意它。对其他事物也是如此,像T-shirt, 色拉,发型,电视节目等等。

    尽管人们的品味不同,但是他们都遵从某种模式,人们若喜欢某件事物,若现在有另一个事物与起相似,那么人们对该事物也会表现喜欢的态度。因为我喜欢bacon-lettuce-and-tomato三明治,所以你可以猜测我也喜欢club三明治,因为这两种三明治很相似。

    相应的,人们倾向于喜欢与其喜欢相似事物的其他人所喜欢的事物。

    我们可以使用这些模式来预测喜欢或不喜欢。当我们放一个陌生人在你面前的时候,当问你他是否喜欢指环王III时,你没有其他办法,只能猜测,但是,若他告诉我们他喜欢指环王I和指环王II时,那么若他不喜欢指环王III的话,你会很震惊。

    推荐即是预测这些品味模式,然后使用这些模式来发现你所不知道的新的或想要的事物。


    前面提到过,我们可以从和我们品味相似的人那里来发现我们喜欢的事物,另一方面,我们可以寻找与已经喜欢的事物相似的事物。这两种类型是推荐引擎算法中最广泛的类别:
    “基于用户”的推荐和“基于事件”的推荐


    以下介绍一个推荐系统实例,主要用来mahout工程在Eclipse下建立以及运行

    1.首先,准备好输入的数据。在本实例中,保存在文件intro.csv中。

    1,101,5.0
    1,102,3.0
    1,103,2.5
    
    2,101,2.0
    2,102,2.5
    2,103,5.0
    2,104,2.0
    
    3,101,2.5
    3,104,4.0
    3,105,4.5
    3,107,5.0
    
    4,101,5.0
    4,103,3.0
    4,104,4.5
    4,106,4.0
    
    5,101,4.0
    5,102,3.0
    5,103,2.0
    5,104,4.0
    5,105,3.5
    5,106,4.0


    其中,第一列是:User ID, 第二列是:Item ID, 第三列是:Preference Value


    2.创建一个推荐系统工程,输入如下代码:

    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 RecommenderIntro {
    	public static void main(String[] args) throws Exception{
    		DataModel model = new FileDataModel(new File("intro.csv"));
    		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);
    			
    		}
    	}
    
    }
    
    DataModel model = new FileDataModel(new File("intro.csv"));

    该句表示加载数据文件

    Recommender recommender = new GenericUserBasedRecommender(
    				model, neighborhood, similarity);

    表示创建推荐引擎

    List<RecommendedItem> recommendations = recommender.recommend(1, 1);

    表示对于用户1,推荐1件物品


    3.运行结果

    2013-7-31 21:43:18 org.slf4j.impl.JCLLoggerAdapter info
    信息: Creating FileDataModel for file intro.csv
    2013-7-31 21:43:19 org.slf4j.impl.JCLLoggerAdapter info
    信息: Reading file info...
    2013-7-31 21:43:19 org.slf4j.impl.JCLLoggerAdapter info
    信息: Read lines: 21
    2013-7-31 21:43:19 org.slf4j.impl.JCLLoggerAdapter info
    信息: Processed 5 users
    RecommendedItem[item:104, value:4.257081]


    若把

    List<RecommendedItem> recommendations = recommender.recommend(1, 1);

    改为:

    List<RecommendedItem> recommendations = recommender.recommend(1, 2);

    表示向用户1推荐2件物品

    输出结果如下:

    2013-7-31 21:55:13 org.slf4j.impl.JCLLoggerAdapter info
    信息: Creating FileDataModel for file intro.csv
    2013-7-31 21:55:14 org.slf4j.impl.JCLLoggerAdapter info
    信息: Reading file info...
    2013-7-31 21:55:14 org.slf4j.impl.JCLLoggerAdapter info
    信息: Read lines: 21
    2013-7-31 21:55:14 org.slf4j.impl.JCLLoggerAdapter info
    信息: Processed 5 users
    RecommendedItem[item:104, value:4.257081]
    RecommendedItem[item:106, value:4.0]


    注:

    在把mahout的工程文件导入Eclipse时,可能会出现错误,具体错误忘了记下来了,我的解决方法是:

    把Windows ——> Preferences ——>Maven ——> User Setting 设置为Maven安装的文件位置。如下图所示


  • 相关阅读:
    每日leetcode-数组-387. 字符串中的第一个唯一字符
    每日leetcode-数组-541. 反转字符串 II
    每日leetcode-数组-344. 反转字符串
    每日leetcode-数组-58. 最后一个单词的长度
    每日leetcode-数组-434. 字符串中的单词数
    每日leetcode-数组-14. 最长公共前缀
    每日leetcode-数组-125. 验证回文串
    每日leetcode-数组-520. 检测大写字母
    Weblogic漏洞挖矿病毒解决方法
    C盘空间不足清理
  • 原文地址:https://www.cnblogs.com/aukle/p/3230998.html
Copyright © 2020-2023  润新知