日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

hadoop矩阵乘法源码_使用Hadoop计算共现矩阵

發布時間:2023/12/3 编程问答 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 hadoop矩阵乘法源码_使用Hadoop计算共现矩阵 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

hadoop矩陣乘法源碼

這篇文章繼續我們在MapReduce的數據密集型文本處理一書中實現MapReduce算法的系列。 這次,我們將從文本語料庫創建單詞共現矩陣。 本系列以前的文章是:
  • 使用MapReduce進行數據密集型文本處理
  • 使用MapReduce進行數據密集型文本處理-本地聚合第二部分
  • 共現矩陣可以描述為事件的跟蹤,并且在一定的時間或空間窗口下,似乎還會發生其他事件。 出于本文的目的,我們的“事件”是文本中找到的單個單詞,我們將跟蹤“窗口”中相對于目標單詞的位置出現的其他單詞。 例如,考慮短語“快速的棕色狐貍跳過了懶狗”。 窗口值為2時,單詞“ jumped”的同時出現為[brown,fox,over,the]。 同現矩陣可以應用于需要調查“此”事件何時發生,其他事件似乎同時發生的其他區域。 為了構建我們的文本共現矩陣,我們將使用MapReduce實現數據密集型文本處理的第3章中的“對和條紋”算法。 用來創建我們共現矩陣的正文是威廉·莎士比亞的集體著作。

    實施配對方法很簡單。 對于調用map函數時傳遞的每一行,我們將在空格處分割以創建一個String Array。 下一步將是構造兩個循環。 外循環將遍歷數組中的每個單詞,而內循環將遍歷當前單詞的“鄰居”。 內部循環的迭代次數由捕獲當前單詞鄰居的“窗口”的大小決定。 在內部循環的每個迭代的底部,我們將發出一個WordPair對象(由左側的當前單詞和右側的相鄰單詞組成)作為鍵,并計數1作為值。 這是Pairs實現的代碼:

    public class PairsOccurrenceMapper extends Mapper<LongWritable, Text, WordPair, IntWritable> {private WordPair wordPair = new WordPair();private IntWritable ONE = new IntWritable(1);@Overrideprotected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {int neighbors = context.getConfiguration().getInt('neighbors', 2);String[] tokens = value.toString().split('\\s+');if (tokens.length > 1) {for (int i = 0; i < tokens.length; i++) {wordPair.setWord(tokens[i]);int start = (i - neighbors < 0) ? 0 : i - neighbors;int end = (i + neighbors >= tokens.length) ? tokens.length - 1 : i + neighbors;for (int j = start; j <= end; j++) {if (j == i) continue;wordPair.setNeighbor(tokens[j]);context.write(wordPair, ONE);}}}} }

    Pairs實現的Reducer將簡單地將給定WordPair鍵的所有數字相加:

    public class PairsReducer extends Reducer<WordPair,IntWritable,WordPair,IntWritable> {private IntWritable totalCount = new IntWritable();@Overrideprotected void reduce(WordPair key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {int count = 0;for (IntWritable value : values) {count += value.get();}totalCount.set(count);context.write(key,totalCount);} }


    條紋

    實現共現的條帶化方法同樣簡單。 方法是相同的,但是所有“鄰居”字都是在HashMap中收集的,其中鄰居字為鍵,整數計數為值。 當已經為給定單詞(外部循環的底部)收集了所有值時,將發出單詞和哈希圖。 這是我們的Stripes實現的代碼:

    public class StripesOccurrenceMapper extends Mapper<LongWritable,Text,Text,MapWritable> {private MapWritable occurrenceMap = new MapWritable();private Text word = new Text();@Overrideprotected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {int neighbors = context.getConfiguration().getInt('neighbors', 2);String[] tokens = value.toString().split('\\s+');if (tokens.length > 1) {for (int i = 0; i < tokens.length; i++) {word.set(tokens[i]);occurrenceMap.clear();int start = (i - neighbors < 0) ? 0 : i - neighbors;int end = (i + neighbors >= tokens.length) ? tokens.length - 1 : i + neighbors;for (int j = start; j <= end; j++) {if (j == i) continue;Text neighbor = new Text(tokens[j]);if(occurrenceMap.containsKey(neighbor)){IntWritable count = (IntWritable)occurrenceMap.get(neighbor);count.set(count.get()+1);}else{occurrenceMap.put(neighbor,new IntWritable(1));}}context.write(word,occurrenceMap);}}} }

    由于我們需要迭代一組地圖,然后針對每個映射,遍歷該映射中的所有值,因此使用“ Reducer for Stripes”方法要稍微復雜一些:

    public class StripesReducer extends Reducer<Text, MapWritable, Text, MapWritable> {private MapWritable incrementingMap = new MapWritable();@Overrideprotected void reduce(Text key, Iterable<MapWritable> values, Context context) throws IOException, InterruptedException {incrementingMap.clear();for (MapWritable value : values) {addAll(value);}context.write(key, incrementingMap);}private void addAll(MapWritable mapWritable) {Set<Writable> keys = mapWritable.keySet();for (Writable key : keys) {IntWritable fromCount = (IntWritable) mapWritable.get(key);if (incrementingMap.containsKey(key)) {IntWritable count = (IntWritable) incrementingMap.get(key);count.set(count.get() + fromCount.get());} else {incrementingMap.put(key, fromCount);}}} }


    結論

    查看這兩種方法時,我們可以發現Pairs算法與Stripes算法相比將生成更多的鍵值對。 而且,“對”算法捕獲每個單獨的同現事件,而“條紋”算法捕獲給定事件的所有同現事件。 成對和條紋實現都將受益于使用組合器。 因為兩者都會產生可交換的結果和關聯的結果,所以我們可以簡單地將每個Mapper的Reducer用作合并器。 如前所述,創建共現矩陣不僅適用于文本處理,而且還適用于其他領域,并且代表了有用的MapReduce算法。 謝謝你的時間。

    資源資源

    • Jimmy Lin和Chris Dyer 使用MapReduce進行的數據密集型處理
    • Hadoop: Tom White 的權威指南
    • 來自博客的源代碼和測試
    • Hadoop API
    • MRUnit用于單元測試Apache Hadoop映射減少工作


    參考:來自我們的JCG合作伙伴 Bill Bejeck(來自Random Thoughts On Coding博客)的Hadoop計算共生矩陣 。

    翻譯自: https://www.javacodegeeks.com/2012/11/calculating-a-co-occurrence-matrix-with-hadoop.html

    hadoop矩陣乘法源碼

    總結

    以上是生活随笔為你收集整理的hadoop矩阵乘法源码_使用Hadoop计算共现矩阵的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。