zoukankan      html  css  js  c++  java
  • 利用艺术家的整数ID映射将标签转换为向量

    <strong><span style="font-size:18px;">/***
     * @author YangXin
     * @info Mapper选择艺术家的整数特征ID然后建立单个特征的向量。这些一维的部分
     * 向量会传给Reducer,后者会将这些向量简单地进行联结。生成一个完整的向量。

    */ package unitTwelve; import java.io.IOException; import java.util.HashMap; import java.util.Map; import java.util.regex.Pattern; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.io.DefaultStringifier; import org.apache.hadoop.io.LongWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.Mapper; import org.apache.hadoop.util.GenericsUtil; import org.apache.mahout.math.NamedVector; import org.apache.mahout.math.SequentialAccessSparseVector; import org.apache.mahout.math.VectorWritable; public class VectorMapper extends Mapper<LongWritable, Text, Text, VectorWritable>{ private Pattern splitter; private VectorWritable writer; private Map<String, Integer> dictionary = new HashMap<String, Integer>(); @Override protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException{ String[] fields = splitter.split(value.toString()); if(fields.length < 4){ context.getCounter("Map", "LinesWithErrors").increment(1); return; } String arrtist = fields[1]; String tag = fields[2]; double weight = Double.parseDouble(fields[3]); NamedVector vector = new NamedVector(new SequentialAccessSparseVector(dictionary.size()), tag); vector.set(dictionary.get(value), weight); writer.set(vector); context.write(new Text(tag), writer); } @Override protected void setup(Context context) throws IOException, InterruptedException{ super.setup(context); Configuration conf = context.getConfiguration(); DefaultStringifier<Map<String, Integer>> mapStringifier = new DefaultStringifier<Map<String, Integer>>(conf, GenericsUtil.getClass(dictionary)); dictionary = mapStringifier.fromString(conf.get("dictionary")); splitter = Pattern.compile("<sep>"); writer = new VectorWritable(); } } </span></strong>


  • 相关阅读:
    全面理解javascript的caller,callee,call,apply概念(修改版)
    动态显示更多信息(toggle_visible函数的运用)
    再论call和apply
    RSS News Module的应用
    准备制作一套全新的DNN皮肤(包括个人定制或企业级定制)
    ControlPanel研究系列二:简单Ajax模式的ControlPanel(SimplAjax)
    New_Skin发布了....
    如何定制dnn的FckEditor
    Blog已迁移到dnnsun.com(最新DotNetNuke咨询平台)
    新DNN皮肤的经验及成果分享
  • 原文地址:https://www.cnblogs.com/brucemengbm/p/7055080.html
Copyright © 2011-2022 走看看