`
beneo
  • 浏览: 54346 次
  • 性别: Icon_minigender_1
  • 来自: 希伯來
社区版块
存档分类
最新评论

一个基于Mahout与hadoop的聚类搭建

阅读更多
    mahout是基于hadoop的数据挖掘工具,因为有了hadoop,所以进行海量数据的挖掘工作显得更为简单。但是因为算法需要支持M/R,所以不是所有常用的数据挖掘算法都会支持。这篇文章会告诉你,如何使用hadoop + mahout搭出一个简易的聚类工具。

    第一步:搭建hadoop平台。

我使用的是ubuntu 11.04,如果没有ubuntu的开发环境,就参考我的帖子《Ubuntu 10.10 java 开发环境》

    #1 在ubuntu下面建立一个用户组与用户
beneo@ubuntu:~$ sudo addgroup hadoop
beneo@ubuntu:~$ sudo adduser --ingroup hadoop hduser


   #2 安装ssh-server
beneo@ubuntu:~$ sudo apt-get install ssh
beneo@ubuntu:~$ su - hduser
hduser@ubuntu:~$ ssh-keygen -t rsa -P ""
hduser@ubuntu:~$ cat $HOME/.ssh/id_rsa.pub >> $HOME/.ssh/authorized_keys


   #3 验证ssh通信
hduser@ubuntu:ssh localhost


ssh localhost 后,选择 yes,如果没有问题,就可以安装hadoop了
  
   #4 添加java_home
修改conf/hadoop-env.sh文件,让JAVA_HOME指向正确的地址

   #5 修改下面的配置
conf/core-site.xml:
<configuration>
     <property>
         <name>fs.default.name</name>
         <value>hdfs://localhost:9000</value>
     </property>
</configuration>

conf/hdfs-site.xml:
<configuration>
     <property>
         <name>dfs.replication</name>
         <value>1</value>
     </property>
</configuration>

conf/mapred-site.xml:
<configuration>
     <property>
         <name>mapred.job.tracker</name>
         <value>localhost:9001</value>
     </property>
</configuration>


    #6 Format a new distributed-filesystem:
$ bin/hadoop namenode -format


    #7 Start the hadoop daemons:
$ bin/start-all.sh


    #8 验证启动成功没有
$ jps

数一下有没有6个,没有的话,删除logs下面的文件,然后从#6开始

    #9 别慌,先打开网页,打不开,等!!!
    NameNode - http://localhost:50070/
    JobTracker - http://localhost:50030/


第一步搭建hadoop结束

    第二步,Mahout的配置

    #1 下载Mahout,解压
    #2 .bash_profile里面设置HADOOP_HOME
    #3 mahout/bin/mahout 看看打印结果

    第三步,做一个聚类的demo吧

我的聚类是文本 -> lucene index -> mahout -> clustering dumper
可以选择的是 sequeneceFile -> mahout -> clustering dumper

我直接贴代码吧,用的是groovy,可能写的不好
    #1 text -> lucene index
def assembleDoc = {
    label, content ->
    assert !label.toString().trim().empty
    assert !content.toString().trim().empty

    def doc = new Document()
    doc.add(new Field("label", label, Field.Store.YES, Field.Index.NOT_ANALYZED))
    doc.add(new Field("content", content, Field.Store.NO, Field.Index.ANALYZED, TermVector.YES))
    doc
}

def mockContent = {
    def set = []
    new File("""/home/beneo/text.txt""").newReader().eachLine {
        String line ->
        set << line
    }
    set
}


def mockExpandoSet = {

    def lst = []

    mockContent()?.each {
        content ->
        // 过滤掉所有非中文字符
        def line = content.replaceAll("[^\u4e00-\u9fa5]+", "")
        if (line != null && line.trim().length() > 2) {
            println(content)
            def expando = new Expando()
            expando.label = content
            expando.content = line
            lst << expando
        }
    }
    lst
}

//创建一个dic
def directory = FSDirectory.open(new File("""/home/beneo/index"""), NoLockFactory.getNoLockFactory())
// 用的是 IK分词
def analyzer = new IKAnalyzer()
//创建一个indexWriter,这个wirter就是用来产生出index
def indexWriter = new IndexWriter(directory, analyzer, true, IndexWriter.MaxFieldLength.UNLIMITED)

//从本地获得文本
mockExpandoSet().each {
    expando ->
    indexWriter.addDocument(assembleDoc(expando.label, expando.content))
}

indexWriter.commit()
indexWriter.close()
directory.close()
        

    #2 lucene index -> mahout vector
mahout/bin/mahout lucene.vector -d index/ -i label -o tmp/vector/vector -f content -t tmp/vector/dict -n 2

 
    #3 mahout vector -> mahout canopy
mahout/bin/mahout canopy -i tmp/vector/vector -o tmp/canopy/ -dm org.apache.mahout.common.distance.CosineDistanceMeasure -t1 0.32 -t2 0.08 -ow


    #4 mahout canopy -> mahout kmeans
mahout/bin/mahout kmeans -i tmp/vector/vector -c tmp/canopy/clusters-0/ -dm org.apache.mahout.common.distance.CosineDistanceMeasure -o tmp/kmeans/ -cd 0.001 -x 10 -ow -cl

   
   #5 mahout keamns -> 结果分析
String seqFileDir = "/home/hduser/tmp/kmeans/clusters-2/"
String pointsDir = "/home/hduser/tmp/kmeans/clusteredPoints/"

def conf = new Configuration()

FileSystem fs = new Path(seqFileDir).getFileSystem(conf)

Map<Integer, List<WeightedVectorWritable>> clusterIdToPoints = readPoints(new Path(pointsDir), new Configuration());

for (FileStatus seqFile: fs.globStatus(new Path(seqFileDir, "part-*"))) {
    Path path = seqFile.getPath()

    SequenceFile.Reader reader = new SequenceFile.Reader(fs, path, conf);

    org.apache.hadoop.io.Writable key = reader.getKeyClass().asSubclass(org.apache.hadoop.io.Writable.class).newInstance();
    org.apache.hadoop.io.Writable value = reader.getValueClass().asSubclass(org.apache.hadoop.io.Writable.class).newInstance();

    while (reader.next(key, value)) {
        Cluster cluster = (Cluster) value;
        int id = cluster.getId()
        int np = cluster.getNumPoints()

        List<WeightedVectorWritable> points = clusterIdToPoints.get(cluster.getId());
        if (points != null && points.size() > 4) {
            for (Iterator<WeightedVectorWritable> iterator = points.iterator(); iterator.hasNext();) {
                println(((NamedVector) iterator.next().getVector()).getName())
            }
            println "======================================"
        }
    }
}


private static Map<Integer, List<WeightedVectorWritable>> readPoints(Path pointsPathDir, Configuration conf)
throws IOException {
    Map<Integer, List<WeightedVectorWritable>> result = new TreeMap<Integer, List<WeightedVectorWritable>>();

    FileSystem fs = pointsPathDir.getFileSystem(conf);
    FileStatus[] children = fs.listStatus(pointsPathDir, new PathFilter() {
        @Override
        public boolean accept(Path path) {
            String name = path.getName();
            return !(name.endsWith(".crc") || name.startsWith("_"));
        }
    });

    for (FileStatus file: children) {
        Path path = file.getPath();
        SequenceFile.Reader reader = new SequenceFile.Reader(fs, path, conf);

        IntWritable key = reader.getKeyClass().asSubclass(IntWritable.class).newInstance();
        WeightedVectorWritable value = reader.getValueClass().asSubclass(WeightedVectorWritable.class).newInstance();
        while (reader.next(key, value)) {
            // value is the cluster id as an int, key is the name/id of the
            // vector, but that doesn't matter because we only care about printing
            // it
            // String clusterId = value.toString();
            List<WeightedVectorWritable> pointList = result.get(key.get());
            if (pointList == null) {
                pointList = new ArrayList<WeightedVectorWritable>();
                result.put(key.get(), pointList);
            }
            pointList.add(value);
            value = reader.getValueClass().asSubclass(WeightedVectorWritable.class).newInstance();
        }

    }

    return result;
}


效果我就不展示了
        
分享到:
评论
2 楼 beneo 2012-10-11  
大海lb 写道
楼主,我想问下,就是在运行kmeans的时候那个-c 如何指定,这个目录如何生成的,我对这个参数一直没搞明白,谢谢


用canopy,否则就是经验值
1 楼 大海lb 2012-04-28  
楼主,我想问下,就是在运行kmeans的时候那个-c 如何指定,这个目录如何生成的,我对这个参数一直没搞明白,谢谢

相关推荐

    云环境下基于MapReduce的用户聚类研究与实现

    以淘宝网上海量的购买用户聚类作为应用背景,通过使用Hadoop平台的数据挖掘组件Mahout对用户聚类进行了实例研究,并给出了使用Mahout进行挖掘的一般步骤。结果表明,基于MapReduce的聚类算法在大规模数据集上具有较...

    基于Spark框架的聚类算法研究

    新型框架Spark部署在Hadoop平台上,它的机器学习算法几乎可以完全替代传统的Mahout Map Reduce的编程模式,但由于Spark的内存模型特点,执行速度快。该文研究了Spark中的机器学习中的聚类算法KMeans,先分析了算法思想,...

    mahout0.9 jar包支持hadoop2

    mahout0.9 的jar包,支持hadoop2(此为第一部分)

    基于Java Mahout的电影推荐系统【100011537】

    Apache Mahout 是 Apache Software Foundation(ASF) 旗下的一个开源项目,提供一些可扩展的机器学习领域经典算法的实现,旨在帮助开发人员更加方便快捷地创建智能应用程序。经典算法包括聚类、分类、协同过滤、...

    Mahout in action带书签目录 中文 完整版

    本书涵盖以下内容: Apache Mahout是什么? 现实中推荐系统引擎、聚类、分类概述 配置mahout 读者可能从本书的标题中依然...在目前阶段,这种可伸缩性由java实现,有些部分基于Apache Hadoop这个分布式计算框架实现。

    Mahout_in_Action

    完全版的。Mahout 是一个开源的机器学习库,现在它主要包含的内容是协同过滤(collaborative filtering)、聚类和分类。Mahout用Java实现,并且是scalable的,和Hadoop结合紧密。

    Mahout in action 中文版 高清 完整

    Mahout作为Apache的开源机器学习项目,把推荐系统、分类和聚类等领域的核心算法浓缩到了可扩展的现成的库中。使用Mahout,你可以立即在自己的项目中应用亚马逊、Netflix及其他互联网公司所采用的机器学习技术。, ...

    MAHOUT源码包

    Mahout 是 Apache Software Foundation(ASF) 旗下的一个开源项目,提供一些可扩展的机器学习领域经典算法的实现,旨在帮助开发人员更加方便快捷地创建智能应用程序。Mahout包含许多实现,包括聚类、分类、推荐过滤...

    分布式聚类算法研究与应用

    非常好的分布式数据挖掘资料,讲的很详细!非常不错的!

    mahout-0.11.0.tar.gz

    适合centos7平台,Mahout 是 Apache Software Foundation(ASF) 旗下的一个开源项目,提供一些可扩展的机器学习领域经典算法的实现,旨在帮助开发人员更加方便快捷地创建智能应用程序。Mahout包含许多实现,包括...

    [Mahout.in.Action(2011)].Sean.Owen.文字版.pdf

    Mahout是一个Apache的开源机器学习项目。该算法属于广阔的 “机器学习”,或“集体智慧的伞形结构。这就可以代表很多东西,但此时此刻,我们关心Mahout的主要部分是:协同过滤(CF)/推荐引擎(recommender),聚类...

    mahout安装图文版

    mahout分布式数据挖掘工具,实现了在hadoop分布式环境下的各种数据挖掘算法,比如kmeans,聚类等,本文档是mahout的详细安装步骤。

    Mahout in action

    Mahout 是 Apache Software Foundation(ASF) 旗下的一个开源项目,提供一些可扩展的机器学习领域经典算法的实现,旨在帮助开发人员更加方便快捷地创建智能应用程序。Mahout包含许多实现,包括聚类、分类、推荐过滤...

    聚类、分类、协同过滤、进化编程等经典算法

    Apache Mahout! Mahout is a scalable machine learning library that implements many different approaches to machine learning. The project currently contains implementations of algorithms for ...

    Mahout in Action2(完整版)

    完整版的Mahout in Action 2,包含了分类,推荐,聚类三部分所有内容,PDF清晰版

    大数据挖掘工具

    Mahout 提供一些可扩展的机器学习领域经典算法的实现,旨在帮助开发人员更加方便快捷地...Mahout包含许多实现,包括聚类、分类、推荐过滤、频繁子项挖掘。通过使用 Apache Hadoop 库,Mahout 可以有效地扩展到云中。

    第7章-大数据分析与挖掘技术---大数据基础.pptx

    目前Mahout着力与三个领域——推荐(协同过滤)、聚类、分类算法的实现上,尽管理论上它可以实现机器学习中的所有技术! 第7章-大数据分析与挖掘技术---大数据基础全文共37页,当前为第5页。 Mahout安装与配置 6 ...

    mahout-distribution-0.5-src.tar.gz )

    分布式数据挖掘工具,实现了在hadoop分布式环境下的各种数据挖掘算法,比如kmeans,聚类等

    Mahout in Action

    在Hadoop平台下进行数据挖掘算法的并行化开发,本书包括协同过滤,分类,聚类三大类算法

Global site tag (gtag.js) - Google Analytics