`
beneo
  • 浏览: 55000 次
  • 性别: 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 如何指定,这个目录如何生成的,我对这个参数一直没搞明白,谢谢

相关推荐

    01、机器学习、Mahout与Hadoop的过去,现在与未来

    Mahout的历史可以追溯到2008年,当时它作为一个独立的项目启动,旨在提供一个基于Hadoop的机器学习库。随着Hadoop生态系统的成熟,Mahout逐渐成为了大数据处理领域的重要组成部分。它的早期版本包含了一系列的机器...

    hadoop2.7.3+mahout0.9问题集

    Hadoop是一个开源的分布式计算框架,而Mahout是基于Hadoop的数据挖掘库,专注于机器学习算法。这两者的结合在大数据分析和预测模型构建中具有广泛的应用。 在“hadoop2.7.3+mahout0.9问题集”中,我们可能遇到的...

    hadoop 2.4.1+mahout0.9环境搭建

    Hadoop是一个分布式计算框架,主要用于存储和处理大规模数据集,而Mahout则是一个机器学习库,它提供了多种机器学习算法,如分类、聚类和推荐系统,用于数据分析。在Hadoop之上构建Mahout环境,可以实现高效的大规模...

    Hadoop,Hbase,mahout三者兼容版本的API文档

    HBase是一个基于Hadoop的分布式NoSQL数据库,专为大规模随机读写操作设计。HBase API提供了一套用于操作表格、行、列族和时间戳的接口,使开发人员能够轻松地存储和检索大量结构化和半结构化数据。HBase与Hadoop的...

    mahout0.9 jar包支持hadoop2

    Apache Mahout是一个基于Hadoop的数据挖掘库,它提供了大规模机器学习算法的实现,包括推荐引擎、分类、聚类和关联规则挖掘。Mahout 0.9版本是该库的一个重要里程碑,尤其因为它是第一个全面支持Hadoop 2的版本。在...

    基于Mahout实现协同过滤推荐算法的电影推荐系统.zip

    Apache Mahout是基于Hadoop的数据挖掘库,提供了多种机器学习算法,包括分类、聚类和推荐。在本项目中,Mahout被用作实现协同过滤推荐算法的工具,它支持大规模数据集的处理,并可以与其他大数据处理框架如Hadoop和...

    Hadoop-Mahout:使用 Mahout 在 Hadoop 上进行推荐、集群和分类

    Hadoop-Mahout 是一个基于Apache Hadoop的开源项目,专注于提供大规模的数据挖掘和机器学习算法。这个项目的目标是创建易于使用的、可扩展的机器学习库,使得开发者能够轻松地在Hadoop平台上构建智能应用,实现推荐...

    mahout环境搭建

    3. **Apache Hadoop**:Hadoop 是一个分布式文件系统,为 Mahout 提供了处理大规模数据的基础。在这里,我们选择 Hadoop 1.1.1 版本,同样解压到 `/usr/local/` 并设置环境变量 `HADOOP_INSTALL`。 4. **Apache ...

    王家林Mahout_in_Action

    它用Java编写,并且部分构建在Apache Hadoop分布式计算框架之上,是一个基于Hadoop的可扩展算法库。 Mahout的设计理念是为了解决大数据环境下的机器学习问题,从而在云计算和大数据时代占据一席之地。在实际应用中...

    9.Hadoop入门进阶课程_第9周_Mahout介绍、安装与应用案例.pdf

    ### Hadoop入门进阶课程之Mahout介绍、安装与应用案例 #### Mahout概述 Mahout作为Apache Software Foundation(ASF)旗下的一个开源项目,致力于提供一系列可扩展的机器学习算法实现,以帮助开发者更轻松地构建...

    基于Hadoop与Mahout云数据挖掘推荐研究.pdf

    Mahout是Apache下的一个开源项目,它是在Hadoop平台上运行的机器学习库,主要用于实现分类、聚类、推荐等功能。Mahout的引入使得Hadoop平台不仅能够存储和计算大数据,还能够对数据进行更深层次的学习和分析。通过...

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

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

    Mahout最新基础依赖包.rar

    Apache Mahout 是一个开源项目,它为大数据分析提供了机器学习库。这个库主要关注三个核心领域:推荐系统、分类和聚类。Mahout 的目标是使开发人员能够轻松构建智能应用程序,利用分布式计算平台,如 Apache Hadoop ...

    基于 Mahout 的新闻推荐系统.rar

    Mahout 基于 Hadoop,支持并行计算,可以处理大量数据,非常适合构建新闻推荐系统。 **Mahout 在新闻推荐中的应用** 1. **协同过滤**:Mahout 提供了基于用户和物品的协同过滤算法。在新闻推荐中,可以通过分析...

    mahout的基于用户的推荐Demo

    总结,这个基于用户的推荐Demo展示了如何利用Apache Mahout来创建和实施一个基本的推荐系统。通过理解协同过滤的原理以及Mahout提供的工具,开发者可以构建出更复杂的推荐系统,以满足不同场景下的需求。这个过程...

    基于mahout的协同过滤算法实现

    Apache Mahout是一个用于构建大规模机器学习算法的库,它构建于Hadoop之上,能够处理海量数据。Mahout提供了多种机器学习算法,包括聚类、分类和推荐等,其中协同过滤算法是推荐系统中的重要组成部分。 三、Mahout...

    Mahout_in_Action

    Mahout基于Java开发,与Hadoop紧密结合,能够处理大规模数据集,支持分布式计算环境下的高效算法执行。 #### 二、Mahout简介 Mahout是Apache软件基金会旗下的一个开源项目,专注于提供可扩展的机器学习算法。它...

    Mahout in action 实战中文版 高清 完整

    Apache Mahout是一个基于Hadoop的开源机器学习库,它旨在提供可扩展的、易于使用的机器学习算法。机器学习是一种人工智能分支,通过让计算机从数据中学习规律,从而实现自动化预测和决策。Mahout的目标是简化大数据...

    mahout-0.3.tar.gz

    Apache Mahout是一个基于Apache Hadoop的数据挖掘库,专注于大规模机器学习算法的实现。"mahout-0.3.tar.gz"是Mahout项目的一个早期版本,包含了一系列用于开发和执行机器学习任务的源代码、库文件和其他相关资源。...

Global site tag (gtag.js) - Google Analytics