如何将mahout kmeans集群集成到应用程序中?

7fyelxc5  于 2021-06-04  发布在  Hadoop
关注(0)|答案(1)|浏览(495)

我正在尝试为一个简单的应用程序使用mahoutkmeans。我从数据库内容手动创建一系列向量。我只想将这些向量馈送给mahout(0.9),例如kmeansclusterer并使用输出。
我阅读了mahout in action(来自版本0.5的示例)和许多在线论坛来获取背景知识。但是我再也看不到通过hadoop使用mahoutkmeans(或相关集群)而不使用文件名和文件路径的方法了。文档非常粗略,但是mahout还能用这种方式吗?目前有没有使用mahoutkmeans的例子(不是从命令行)。

  1. private List<Cluster> kMeans(List<Vector> allvectors, double closeness, int numclusters, int iterations) {
  2. List<Cluster> clusters = new ArrayList<Cluster>() ;
  3. int clusterId = 0;
  4. for (Vector v : allvectors) {
  5. clusters.add(new Kluster(v, clusterId++, new EuclideanDistanceMeasure()));
  6. }
  7. List<List<Cluster>> finalclusters = KMeansClusterer.clusterPoints(allvectors, clusters, 0.01, numclusters, 10) ;
  8. for(Cluster cluster : finalclusters.get(finalclusters.size() - 1)) {
  9. System.out.println("Fuzzy Cluster id: " + cluster.getId() + " center: " + cluster.getCenter().asFormatString());
  10. }
  11. return clusters ;
  12. }
v1uwarro

v1uwarro1#

首先需要将向量写入seq文件。代码如下:

  1. List<VectorWritable> vectors = new ArrayList<>();
  2. double[] vectorValues = {<your vector values>};
  3. vectors.add(new VectorWritable(new NamedVector(new DenseVector(vectorValues), userName)));
  4. Configuration conf = new Configuration();
  5. FileSystem fs = FileSystem.get(conf);
  6. fs = FileSystem.get(new File(writeFile).toURI(), conf);
  7. writer = new SequenceFile.Writer(fs, conf, new Path(writeFile), Text.class, VectorWritable.class);
  8. try {
  9. int i = 0;
  10. for (VectorWritable vw : vectors) {
  11. writer.append(new Text("mapred_" + i++), vw);
  12. }
  13. } finally {
  14. Closeables.close(writer, false);
  15. }

然后使用下面的行生成簇。您需要向kmeans提供初始集群,因此我使用canopy来生成初始集群。
但是,您将无法理解cluster的输出,因为它是seq文件格式。您需要在mahout-integration.jar中执行clusterdumper类来最终读取和理解集群。

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

相关问题