zoukankan      html  css  js  c++  java
  • 准备Mahout所用的向量ApplesToVectors

    <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>


  • 相关阅读:
    phpcms基础
    读取数据库有的设置选中状态
    用php 生成 excel 表格
    ajax验证用户名是否存在,手机号是不是匹配
    系统登陆简单的密码验证
    分页显视
    时间选择的三级连动 年,月,日
    session控制登入权限
    jQuery, js 验证两次输了密码的一相同
    正则表达式判断手机号是否11位数字
  • 原文地址:https://www.cnblogs.com/gccbuaa/p/7390734.html
Copyright © 2011-2022 走看看