• 热门专题

准备Mahout所用的向量ApplesToVectors

作者:  发布日期:2016-02-26 22:02:36
Tag标签:向量  所用  
  • /***
     * @author YangXin
     * @info 准备Mahout所用的向量
     * 将苹果的信息转化为输入的向量
     */
    package unitEight;
    
    import java.util.ArrayList;
    import java.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;
    
    /**
     * 我们可以使用向量的名字或描述为键,此处为NameVector,而向量本身作为值。
     * Mahout的Vector类没有实现Writable接口,以避免他们和Hadoop直接耦合。
     * 但可以用VectorWritable类来封装一个Vector并使之为Writable。
     * 即Mahout中的向量可以使用VectorWritable类写入SequenceFile。
     */
    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.add(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();
    	}
    }
    
About IT165 - 广告服务 - 隐私声明 - 版权申明 - 免责条款 - 网站地图 - 网友投稿 - 联系方式
本站内容来自于互联网,仅供用于网络技术学习,学习中请遵循相关法律法规