大家好,最近刚刚开始学习mahout,对聚类后的输出有一个疑问,希望大家指教。
我知道最后输出的一个clusteredPoints里包含了最后的聚类结果,里面记录了每个向量及其对应所分的类,但是我希望可以输出每个向量对应数据的key及其所对应所分的类,不知道有没有什么简单的方法可以实现?
比如说我的数据文件是这样的:
Key class: class org.apache.hadoop.io.LongWritable Value Class: class org.apache.mahout.math.VectorWritable
Key: 0: Value: {0:1.0,1:1.0}
Key: 1: Value: {0:2.0,1:1.0}
Key: 2: Value: {0:1.0,1:2.0}
Key: 3: Value: {0:2.0,1:2.0}
Key: 4: Value: {0:3.0,1:3.0}
Key: 5: Value: {0:8.0,1:8.0}
Key: 6: Value: {0:9.0,1:8.0}
Key: 7: Value: {0:8.0,1:9.0}
Key: 8: Value: {0:9.0,1:9.0}
Count: 9
最后clusteredPoints文件是:
Key class: class org.apache.hadoop.io.IntWritable Value Class: class org.apache.mahout.clustering.classify.WeightedVectorWritable
Key: 0: Value: 1.0: [1.000, 1.000]
Key: 0: Value: 1.0: [2.000, 1.000]
Key: 0: Value: 1.0: [1.000, 2.000]
Key: 0: Value: 1.0: [2.000, 2.000]
Key: 0: Value: 1.0: [3.000, 3.000]
Key: 1: Value: 1.0: [8.000, 8.000]
Key: 1: Value: 1.0: [9.000, 8.000]
Key: 1: Value: 1.0: [8.000, 9.000]
Key: 1: Value: 1.0: [9.000, 9.000]
Count: 9
我最后想输出的形式是:
0 -> 0
1 -> 0
2 -> 0
3 -> 0
4 -> 0
5 -> 1
6 -> 1
7 -> 1
8 -> 1
即表示前5个数据分为类0,后4个数据分为类1.
请问有什么好办法可以简单的实现这个逻辑吗?请大家多多指教,谢谢~
引用来自“Breath_L”的答案
我通过用NamedVector保存向量的id信息实现了我想要的逻辑~
谢谢你的回答:)
请问你具体是怎么实现的呢?