【问题标题】:How can you integrate Mahout KMeans Clustering Into application?如何将 Mahout KMeans 集群集成到应用程序中?
【发布时间】:2014-04-18 19:29:59
【问题描述】:

我正在尝试将 Mahout KMeans 用于一个简单的应用程序。我从数据库内容手动创建了一系列向量。我只是想将这些向量提供给 Mahout (0.9),例如 KMeansClusterer 并使用输出。

我阅读了 Mahout in Action(来自 0.5 版的示例)和许多在线论坛以获取背景信息。但是我看不到没有通过 Hadoop 使用文件名和文件路径的 Mahout KMeans(或相关集群)。文档非常粗略,但是 Mahout 可以再以这种方式使用吗?当前是否有任何使用 Mahout KMeans 的示例(不是从命令行)。

    private List<Cluster> kMeans(List<Vector> allvectors, double closeness, int numclusters, int iterations) {
    List<Cluster> clusters = new ArrayList<Cluster>() ; 

    int clusterId = 0;
    for (Vector v : allvectors) {
        clusters.add(new Kluster(v, clusterId++, new EuclideanDistanceMeasure()));
    }

    List<List<Cluster>> finalclusters = KMeansClusterer.clusterPoints(allvectors, clusters, 0.01, numclusters, 10) ;  


    for(Cluster cluster : finalclusters.get(finalclusters.size() - 1)) {
        System.out.println("Fuzzy Cluster id: " + cluster.getId() + " center: " + cluster.getCenter().asFormatString());
    }

    return clusters ;
}

【问题讨论】:

    标签: hadoop mahout k-means


    【解决方案1】:

    首先,您需要将向量写入 Seq 文件。下面是代码:

    List<VectorWritable> vectors = new ArrayList<>();
    double[] vectorValues = {<your vector values>};
    vectors.add(new VectorWritable(new NamedVector(new DenseVector(vectorValues), userName)));
    
    Configuration conf = new Configuration();
    FileSystem fs = FileSystem.get(conf);
    fs = FileSystem.get(new File(writeFile).toURI(), conf);
    writer = new SequenceFile.Writer(fs, conf, new Path(writeFile), Text.class, VectorWritable.class);
    
    try {
          int i = 0;
          for (VectorWritable vw : vectors) {
            writer.append(new Text("mapred_" + i++), vw);
          }
        } finally {
          Closeables.close(writer, false);
        }
    

    然后使用下面的行来生成集群。您需要向 KMeans 提供初始集群,因此我使用 Canopy 来生成初始集群。

    但是,您将无法理解 cluster 的输出,因为它是 Seq 文件格式。您需要执行 Mahout-Integration.jar 中的 ClusterDumper 类才能最终读取和理解您的集群。

    Configuration conf = new Configuration(); 
    CanopyDriver.run(conf, new Path(inputPath), new Path(canopyOutputPath), new ManhattanDistanceMeasure(), (double) 3.1, (double) 2.1, true, (double) 0.5, true );
    
                        // now run the KMeansDriver job
    KMeansDriver.run(conf, new Path(inputPath), new Path(canopyOutputPath + "/clusters-0-final/"), new Path(kmeansOutput), new EuclideanDistanceMeasure(), 0.001, 10, true, 2d, false);
    

    【讨论】:

      猜你喜欢
      • 2013-08-05
      • 2018-01-23
      • 1970-01-01
      • 2019-09-08
      • 2017-06-01
      • 2019-04-04
      • 2013-07-14
      • 2012-06-15
      • 2014-06-20
      相关资源
      最近更新 更多