我正在尝试使用Mahout KMeans作为一个简单的应用程序。我从数据库内容中手动创建了一系列向量。我只想将这些向量提供给Mahout (0.9),例如KMeansClusterer并使用输出。
我阅读了Mahout (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 ;
}发布于 2014-03-16 03:20:21
首先,您需要将向量写入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来生成初始集群。
但是,您将无法理解集群的输出,因为它是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);https://stackoverflow.com/questions/22389214
复制相似问题