首页 > 移动平台 > 详细

准备Mahout所用的向量ApplesToVectors

时间:2016-02-25 01:42:03      阅读:184      评论:0      收藏:0      [点我收藏+]
<strong><span style="font-size:18px;">/***
 * @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();
	}
}
</span></strong>

准备Mahout所用的向量ApplesToVectors

原文:http://blog.csdn.net/u012965373/article/details/50734799

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!