Prepare the vector used by the mahout applestovectors

Source: Internet
Author: User

<strong><span style= "FONT-SIZE:18PX;" >/*** * @author Yangxin * @info prepare the vector used for mahout * Convert apple information into input vector */package uniteight;import java.util.arraylist;import ja Va.util.list;import Org.apache.hadoop.conf.configuration;import Org.apache.hadoop.fs.filesystem;import Org.apache.hadoop.fs.path;import Org.apache.hadoop.io.sequencefile;import Org.apache.hadoop.io.text;import Org.apache.mahout.math.densevector;import Org.apache.mahout.math.namedvector;import org.apache.mahout.math.vectorwritable;/** * We can use the name or description of the vector as the key, here is the namevector, and the vector itself as the value.

* The mahout vector class does not implement the writable interface. To avoid their direct coupling with Hadoop.

* But can use the Vectorwritable class to encapsulate a vector and make it a writable.

* That is, vectors in Mahout can be written to Sequencefile using the Vectorwritable class.

*/public class Applestovectors {public static void main (string[] args) throws exception{list<namedvector> apples = New Arraylist<namedvector> (); Namedvector apple;apple = new Namedvector (new Densevector (New double[]{0.11, 510, 1}), Small round green apple); APPLES.A DD (apple), Apple = new Namedvector (new Densevector (new double[]{0.23, 650, 3}), Large oval Red apple, Apples.add (Apple); Apple = new Namedvector (new Densevector (new double[]{0.09, 630, 1}), Small elongated red apple), Apples.add (Apple), Apple = new Namedvector (new Densevector (New double[]{0.25, 590, 3}), "Large round yellow apple"); Apples.add (apple); Apple = new Namedvector (New Densevector (New double[]{0.18, 520, 2}), "Medium Oval green apple"); Configuration conf = new configuration (); FileSystem fs = Filesystem.get (conf); Path PATH = new Path ("E:\\apples.txt"); Sequencefile.writer Writer = new Sequencefile.writer (FS, conf, path, Text.class, Vectorwritable.class); Vectorwritable VEC = new vectorwritable (); for (namedvector Vector:apples) {vec.set (vector); Writer.append (New Text (Vector.getname ()), VEC);} Writer.close (); Sequencefile.reader Reader = new Sequencefile.reader (FS, New Path ("E:\\apples.txt"), conf); Text key = new text (); Vectorwritable value = new Vectorwritable (), while (Reader.next (key, value)) {System.out.println (key.tostring () + "" + Value.get (). asformatstring ());;} Reader.close ();}} </span></strong>


Prepare the vector used by the mahout applestovectors

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.