How to perform k-means clustering in mahout with vector data stored as CSV?
Asked Answered
S

2

6

I have a file containing vectors of data, where each row contains a comma-separated list of values. I am wondering how to perform k-means clustering on this data using mahout. The example provided in the wiki mentions creating sequenceFiles, but otherwise I am not sure if I need to do some type of conversion in order to obtain these sequenceFiles.

Sordello answered 9/1, 2012 at 8:1 Comment(3)
Do you need to use mahout for this or will anything do? There are a lot of clustering api's, tools, sample code etc. that would do this easily. If you have a single file your data points might be quite small, Mahout in theory is meant for large scale problems.Atmolysis
I'm looking at clustering data sets from here: grouplens.org/node/73 The largest data set potentially contains 10,000 by 72,000 data points. That is why I thought mahout might be best, WEKA crashes when I try to load the smaller data setsSordello
Try glaros.dtc.umn.edu/gkhome/software , Weka also has an SDK. k-means is quite straight forward to implement in most languages so I'm sure you can find some code snippets on the googleAtmolysis
W
8

I would recommend manually reading in the entries from the CSV file, creating NamedVectors from them, and then using a sequence file writer to write the vectors in a sequence file. From there on, the KMeansDriver run method should know how to handle these files.

Sequence files encode key-value pairs, so the key would be an ID of the sample (it should be a string), and the value is a VectorWritable wrapper around the vectors.

Here is a simple code sample on how to do this:

    List<NamedVector> vector = new LinkedList<NamedVector>();
    NamedVector v1;
    v1 = new NamedVector(new DenseVector(new double[] {0.1, 0.2, 0.5}), "Item number one");
    vector.add(v1);

    Configuration config = new Configuration();
    FileSystem fs = FileSystem.get(config);

    Path path = new Path("datasamples/data");

    //write a SequenceFile form a Vector
    SequenceFile.Writer writer = new SequenceFile.Writer(fs, config, path, Text.class, VectorWritable.class);
    VectorWritable vec = new VectorWritable();
    for(NamedVector v:vector){
        vec.set(v);
        writer.append(new Text(v.getName()), v);
    }
    writer.close();

Also, I would recommend reading chapter 8 of Mahout in Action. It gives more details on data representation in Mahout.

Wellfounded answered 11/1, 2012 at 12:45 Comment(2)
Do you know how I can get the vector names back from the clustering results? See #14477206Allynallys
There's a small error in your example (thanks for posting it, BTW). Instead of "writer.append(new Text(v.getName()), v);" I think it needs to be "write.append(new Text(v.getName()), vec);". Otherwise you get an exception saying "java.io.IOException: wrong value class: org.apache.mahout.math.NamedVector is not class org.apache.mahout.math.VectorWritable"Avogadro
W
0

maybe you could use Elephant Bird to write vectors in mahout format

https://github.com/kevinweil/elephant-bird#hadoop-sequencefiles-and-pig

Wormy answered 22/1, 2013 at 17:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.